86 int to_client_progress;
87 int to_server_progress;
88 } DetectFileHandlerProtocol_t;
89 static DetectFileHandlerProtocol_t al_protocols[] = {
96 .to_client_progress = HTP_RESPONSE_BODY,
97 .to_server_progress = HTP_REQUEST_BODY },
100 .to_client_progress = HTTP2StateDataServer,
101 .to_server_progress = HTTP2StateDataClient },
105 for (
size_t i = 0; i <
ARRAY_SIZE(al_protocols); i++) {
106 int direction = al_protocols[i].direction == 0
108 : al_protocols[i].direction;
112 reg->
GetData, al_protocols[i].al_proto, al_protocols[i].to_client_progress);
119 reg->
GetData, al_protocols[i].al_proto, al_protocols[i].to_server_progress);
132 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
163 #define CASE_CODE_STRING(E, S) case E: return S; break
174 #undef CASE_CODE_STRING
178 #define CASE_CODE(E) case E: return #E
196 Signature *s,
const char *arg,
int sm_type,
int sm_list,
202 if (arg != NULL && strcmp(arg,
"") != 0) {
211 "with a sticky buffer still set. Reset sticky buffer "
212 "with pkt_data before using the modifier.",
226 "found inside the rule without a content context. "
227 "Please use a \"content\" keyword before using the "
235 "be used with the rawbytes rule keyword",
241 "be used with the replace rule keyword",
280 bool reuse_buffer =
false;
293 SCLogError(
"failed to expand rule buffer array");
302 SCLogDebug(
"idx %u list %d set up curbuf %p s->init_data->buffer_index %u",
343 if (sm->
ctx != NULL) {
354 ptrdiff_t
offset = e - table;
368 if (st->
name != NULL) {
369 if (strcasecmp(name,st->
name) == 0)
371 if (st->
alias != NULL && strcasecmp(name,st->
alias) == 0)
401 if (strcmp(
str,
"all") == 0) {
413 char *xsaveptr = NULL;
414 char *key = strtok_r(copy,
",", &xsaveptr);
415 while (key != NULL) {
421 "argument '%s' not found",
424 key = strtok_r(NULL,
",", &xsaveptr);
451 SCLogDebug(
"s:%p new:%p list:%d: %s, s->init_data->list_set %s s->init_data->list %d", s,
new,
485 SCLogDebug(
"reusing buffer %u as it isn't multi-capable", x);
495 SCLogError(
"failed to expand rule buffer array");
519 SCLogDebug(
"appended %s to list %d, rule pos %u (s->init_data->list %d)",
538 if (sm->
prev != NULL)
540 if (sm->
next != NULL)
582 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
594 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
616 SCLogDebug(
"skip x %u s->init_data->list %d (int)s->init_data->buffers[x].id %d", x,
625 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
629 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
646 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
651 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
676 va_start(ap, sm_list);
678 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
680 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
683 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
714 va_start(ap, list_id);
716 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
720 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
732 va_start(ap, list_id);
734 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
735 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
738 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
761 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
769 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
776 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
782 if (sm->
prev != NULL)
784 if (sm->
next != NULL)
787 if (sm == *src_sm_list)
788 *src_sm_list = sm->
next;
789 if (sm == *src_sm_list_tail)
790 *src_sm_list_tail = sm->
prev;
792 if (*dst_sm_list == NULL) {
794 *dst_sm_list_tail = sm;
802 *dst_sm_list_tail = sm;
829 SCLogError(
"Unable to find the sm in any of the "
835 size_t output_size,
bool requires)
838 char *optname = NULL;
839 char *optvalue = NULL;
842 while (isblank(*optstr)) {
847 char *optend = optstr;
849 optend = strchr(optend,
';');
850 if (optend == NULL) {
854 else if (optend > optstr && *(optend -1 ) ==
'\\') {
863 char *optvalptr = strchr(optstr,
':');
865 *(optvalptr++) =
'\0';
868 for (
size_t i = strlen(optvalptr); i > 0; i--) {
869 if (isblank(optvalptr[i - 1])) {
870 optvalptr[i - 1] =
'\0';
876 optvalue = optvalptr;
880 for (
size_t i = strlen(optstr); i > 0; i--) {
881 if (isblank(optstr[i - 1])) {
882 optstr[i - 1] =
'\0';
891 bool requires_only = strcasecmp(optname,
"requires") == 0 || strcasecmp(optname,
"sid") == 0;
892 if ((requires && !requires_only) || (!requires && requires_only)) {
897 st = SigTableGet(optname);
898 if (st == NULL || st->
Setup == NULL) {
899 SCLogError(
"unknown rule keyword '%s'.", optname);
904 if (optvalue == NULL || strlen(optvalue) == 0) {
906 "invalid formatting or malformed option to %s keyword: '%s'", optname, optstr);
910 if (optvalue && strlen(optvalue)) {
911 SCLogError(
"unexpected option to %s keyword: '%s'", optname, optstr);
918 #define URL "https://suricata.io/our-story/deprecation-policy/"
921 "and will be removed soon. See %s",
925 "and will be removed soon. Use '%s' instead. "
934 if (optvalue != NULL && strlen(optvalue) > 0) {
935 size_t ovlen = strlen(optvalue);
936 char *ptr = optvalue;
946 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
965 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
971 SCLogError(
"invalid formatting to %s keyword: "
972 "value must be double quoted \'%s\'",
978 && ovlen && *ptr ==
'"')
980 for (; ovlen > 0; ovlen--) {
981 if (isblank(ptr[ovlen - 1])) {
982 ptr[ovlen - 1] =
'\0';
987 if (ovlen && ptr[ovlen - 1] !=
'"') {
988 SCLogError(
"bad option value formatting (possible missing semicolon) "
989 "for keyword %s: \'%s\'",
997 ptr[ovlen - 1] =
'\0';
1002 "for keyword %s: \'%s\'",
1009 "quotes on %s keyword that doesn't support them: \'%s\'", optname, optstr);
1019 if (setup_ret < 0) {
1023 if (setup_ret == -2) {
1036 if (strlen(optend) > 0) {
1037 strlcpy(output, optend, output_size);
1052 Signature *s,
const char *addrstr,
char flag)
1054 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
1058 if (strcasecmp(addrstr,
"any") == 0)
1066 if (strcasecmp(addrstr,
"any") == 0)
1093 static int SigParseProto(
Signature *s,
const char *protostr)
1108 "in a signature. Either detection for this protocol "
1109 "is not yet supported OR detection has been disabled for "
1110 "protocol through the yaml option "
1111 "app-layer.protocols.%s.detection-enabled",
1112 protostr, protostr);
1141 Signature *s,
const char *portstr,
char flag)
1147 SCLogDebug(
"Port group \"%s\" to be parsed", portstr);
1150 if (strcasecmp(portstr,
"any") == 0)
1154 }
else if (flag == 1) {
1155 if (strcasecmp(portstr,
"any") == 0)
1170 static int SigParseActionRejectValidate(
const char *action)
1172 #ifdef HAVE_LIBNET11
1173 #if defined HAVE_LIBCAP_NG && !defined HAVE_LIBNET_CAPABILITIES
1176 "incompatible with POSIX based capabilities with privs dropping. "
1177 "For rejects to work, run as root/super user.");
1183 "required for action \"%s\" but is not compiled into Suricata",
1201 static int SigParseAction(
Signature *s,
const char *action)
1203 if (strcasecmp(action,
"alert") == 0) {
1205 }
else if (strcasecmp(action,
"drop") == 0) {
1207 }
else if (strcasecmp(action,
"pass") == 0) {
1209 }
else if (strcasecmp(action,
"reject") == 0 ||
1210 strcasecmp(action,
"rejectsrc") == 0)
1212 if (!(SigParseActionRejectValidate(action)))
1215 }
else if (strcasecmp(action,
"rejectdst") == 0) {
1216 if (!(SigParseActionRejectValidate(action)))
1219 }
else if (strcasecmp(action,
"rejectboth") == 0) {
1220 if (!(SigParseActionRejectValidate(action)))
1223 }
else if (strcasecmp(action,
"config") == 0) {
1226 SCLogError(
"An invalid action \"%s\" was given", action);
1243 static inline int SigParseToken(
char **input,
char *output,
1244 const size_t output_size)
1246 size_t len = *input == NULL ? 0 : strlen(*input);
1252 while (
len && isblank(**input)) {
1257 char *endptr = strpbrk(*input,
" \t\n\r");
1258 if (endptr != NULL) {
1261 strlcpy(output, *input, output_size);
1278 static inline int SigParseList(
char **input,
char *output,
1279 const size_t output_size)
1282 size_t len = *input != NULL ? strlen(*input) : 0;
1288 while (
len && isblank(**input)) {
1294 for (i = 0; i <
len; i++) {
1295 char c = (*input)[i];
1298 }
else if (c ==
']') {
1300 }
else if (c ==
' ') {
1311 strlcpy(output, *input, output_size);
1312 *input = *input + i + 1;
1332 SigParseToken(&index, parser->
action,
sizeof(parser->
action));
1338 SigParseList(&index, parser->
src,
sizeof(parser->
src));
1341 SigParseList(&index, parser->
sp,
sizeof(parser->
sp));
1347 SigParseList(&index, parser->
dst,
sizeof(parser->
dst));
1350 SigParseList(&index, parser->
dp,
sizeof(parser->
dp));
1353 if (index == NULL) {
1357 while (isspace(*index) || *index ==
'(') {
1360 for (
size_t i = strlen(index); i > 0; i--) {
1361 if (isspace(index[i - 1]) || index[i - 1] ==
')') {
1362 index[i - 1] =
'\0';
1374 if (SigParseAction(s, parser->
action) < 0)
1377 if (SigParseProto(s, parser->
protocol) < 0)
1380 if (strcmp(parser->
direction,
"<>") == 0) {
1382 }
else if (strcmp(parser->
direction,
"->") != 0) {
1383 SCLogError(
"\"%s\" is not a valid direction modifier, "
1384 "\"->\" and \"<>\" are supported.",
1411 static inline bool CheckAscii(
const char *
str)
1413 for (
size_t i = 0; i < strlen(
str); i++) {
1414 if (
str[i] < 0x20) {
1416 if (
str[i] == 0x0a ||
str[i] == 0x0d ||
str[i] == 0x09) {
1420 }
else if (
str[i] == 0x7f) {
1444 if (!rs_check_utf8(sigstr)) {
1449 if (!CheckAscii(sigstr)) {
1450 SCLogError(
"rule contains invalid (control) characters");
1454 int ret = SigParseBasics(
de_ctx, s, sigstr, parser, addrs_direction, requires);
1461 if (strlen(parser->
opts) > 0) {
1462 size_t buffer_size = strlen(parser->
opts) + 1;
1463 char input[buffer_size];
1464 char output[buffer_size];
1465 memset(input, 0x00, buffer_size);
1466 memcpy(input, parser->
opts, strlen(parser->
opts) + 1);
1472 memset(output, 0x00, buffer_size);
1473 ret = SigParseOptions(
de_ctx, s, input, output, buffer_size, requires);
1475 memcpy(input, output, buffer_size);
1507 memset(b, 0,
sizeof(*b));
1550 static void SigMetadataFree(
Signature *s)
1557 if (s == NULL || s->
metadata == NULL) {
1564 next_mdata = mdata->
next;
1595 next_ref = ref->
next;
1649 while (sm != NULL) {
1658 while (sm != NULL) {
1679 if (s->
sp != NULL) {
1682 if (s->
dp != NULL) {
1720 SCLogError(
"transforms must directly follow stickybuffers");
1747 alproto = AppProtoCommon(s->
alproto, alproto);
1749 SCLogError(
"can't set rule app proto to %s: already set to %s",
1776 if (addr_match4 == NULL) {
1782 addr_match4[idx].
ip =
SCNtohl(da->ip.addr_data32[0]);
1783 addr_match4[idx].
ip2 =
SCNtohl(da->ip2.addr_data32[0]);
1802 if (addr_match6 == NULL) {
1808 addr_match6[idx].
ip[0] =
SCNtohl(da->ip.addr_data32[0]);
1809 addr_match6[idx].
ip[1] =
SCNtohl(da->ip.addr_data32[1]);
1810 addr_match6[idx].
ip[2] =
SCNtohl(da->ip.addr_data32[2]);
1811 addr_match6[idx].
ip[3] =
SCNtohl(da->ip.addr_data32[3]);
1812 addr_match6[idx].
ip2[0] =
SCNtohl(da->ip2.addr_data32[0]);
1813 addr_match6[idx].
ip2[1] =
SCNtohl(da->ip2.addr_data32[1]);
1814 addr_match6[idx].
ip2[2] =
SCNtohl(da->ip2.addr_data32[2]);
1815 addr_match6[idx].
ip2[3] =
SCNtohl(da->ip2.addr_data32[3]);
1828 static void SigBuildAddressMatchArray(
Signature *s)
1845 static int SigMatchListLen(
SigMatch *sm)
1848 for (; sm != NULL; sm = sm->
next)
1859 int len = SigMatchListLen(
head);
1865 FatalError(
"initializing the detection engine failed");
1871 for (; sm != NULL; sm = sm->
next, smd++) {
1893 uint32_t sig_flags = 0;
1898 nlists += (nlists > 0);
1902 SCLogError(
"rule %u setup buffer %s but didn't add matches to it", s->
id,
1907 bool has_frame =
false;
1908 bool has_app =
false;
1909 bool has_pkt =
false;
1910 bool has_pmatch =
false;
1920 struct BufferVsDir {
1923 } bufdir[nlists + 1];
1924 memset(&bufdir, 0, (nlists + 1) *
sizeof(
struct BufferVsDir));
1938 if (b->
head == NULL) {
1943 has_frame |= bt->
frame;
1944 has_app |= (bt->
frame ==
false && bt->
packet ==
false);
1949 "specific matches (like dsize, flags, ttl) with stream / "
1950 "state matching by matching on app layer proto (like using "
1951 "http_* keywords).");
1956 for (; app != NULL; app = app->
next) {
1963 bufdir[b->
id].ts += (app->
dir == 0);
1964 bufdir[b->
id].tc += (app->
dir == 1);
1983 for (
int x = 0; x < nlists; x++) {
1984 if (bufdir[x].
ts == 0 && bufdir[x].tc == 0)
1986 ts_excl += (bufdir[x].ts > 0 && bufdir[x].tc == 0);
1987 tc_excl += (bufdir[x].ts == 0 && bufdir[x].tc > 0);
1988 dir_amb += (bufdir[x].ts > 0 && bufdir[x].tc > 0);
1993 if (ts_excl && tc_excl) {
1994 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
1996 }
else if (ts_excl) {
1997 SCLogDebug(
"%u: implied rule direction is toserver", s->
id);
1999 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2002 }
else if (tc_excl) {
2003 SCLogDebug(
"%u: implied rule direction is toclient", s->
id);
2005 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2008 }
else if (dir_amb) {
2009 SCLogDebug(
"%u: rule direction cannot be deduced from keywords", s->
id);
2015 "tcp-stream or flow:only_stream. Invalidating signature.");
2020 SCLogError(
"You seem to have mixed keywords "
2021 "that require inspection in both directions. Atm we only "
2022 "support keywords in one direction within a rule.");
2026 if (has_pmatch && has_frame) {
2027 SCLogError(
"can't mix pure content and frame inspection");
2030 if (has_app && has_frame) {
2031 SCLogError(
"can't mix app-layer buffer and frame inspection");
2034 if (has_pkt && has_frame) {
2035 SCLogError(
"can't mix pkt buffer and frame inspection");
2043 if (s->
proto.
proto[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) {
2080 "support file matching",
2085 SCLogError(
"protocol HTTP2 doesn't support file name matching");
2105 memset(&parser, 0x00,
sizeof(parser));
2123 int ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
true);
2130 }
else if (ret < 0) {
2136 SCLogError(
"Signature missing required value \"sid\".");
2141 ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
false);
2147 }
else if (ret == -2) {
2150 }
else if (ret < 0) {
2155 if (sig->
prio == -1)
2162 int override_needed = 0;
2166 override_needed = 1;
2168 override_needed = 1;
2170 for (s = 0; s <
sizeof(sig->
proto.
proto); s++) {
2172 override_needed = 0;
2181 if (override_needed)
2191 for ( ; sm != NULL; sm = sm->
next) {
2207 SCLogDebug(
"sig %"PRIu32
" SIG_FLAG_APPLAYER: %s, SIG_FLAG_PACKET: %s",
2211 SigBuildAddressMatchArray(sig);
2223 if (SigValidate(
de_ctx, sig) == 0) {
2253 static bool SigHasSameSourceAndDestination(
const Signature *s)
2305 if (SigHasSameSourceAndDestination(sig)) {
2306 SCLogInfo(
"Rule with ID %u is bidirectional, but source and destination are the same, "
2307 "treating the rule as unidirectional", sig->
id);
2312 if (sig->
next == NULL) {
2337 static void DetectParseDupSigFreeFunc(
void *data)
2353 static uint32_t DetectParseDupSigHashFunc(
HashListTable *ht,
void *data, uint16_t datalen)
2372 static char DetectParseDupSigCompareFunc(
void *data1, uint16_t len1,
void *data2,
2378 if (sw1 == NULL || sw2 == NULL ||
2379 sw1->
s == NULL || sw2->
s == NULL)
2383 if (sw1->
s->
id == sw2->
s->
id && sw1->
s->
gid == sw2->
s->
gid)
return 1;
2399 DetectParseDupSigHashFunc,
2400 DetectParseDupSigCompareFunc,
2401 DetectParseDupSigFreeFunc);
2462 if (sw_dup == NULL) {
2475 (
void *)&sw_tmp, 0);
2487 if (sw->
s->
rev <= sw_dup->
s->
rev) {
2496 if (sw_dup->
s_prev == NULL) {
2504 sw_temp.
s = sw_dup->
s->
next;
2508 if (sw_temp.
s != NULL) {
2510 (
void *)&sw_temp, 0);
2530 sw_temp.
s = sw_dup->
s->
next;
2538 if (sw_temp.
s != NULL) {
2540 (
void *)&sw_temp, 0);
2555 (
void *)&sw_tmp, 0);
2556 if (sw_old->
s != sw_dup->
s) {
2599 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
2603 SCLogError(
"Duplicate signature \"%s\"", sigstr);
2605 }
else if (dup_sig == 2) {
2607 " so the older sig replaced by this new signature \"%s\"",
2612 if (sig->
next != NULL) {
2629 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
2633 if (sig != NULL && sig->
next != NULL) {
2646 int start_offset,
int options)
2648 *match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
2650 return pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)
str, strlen(
str), options, start_offset,
2651 *match, parse_regex->
context);
2658 pcre2_code_free(r->
regex);
2661 pcre2_match_context_free(r->
context);
2676 g_detect_parse_regex_list = NULL;
2685 FatalError(
"failed to alloc memory for pcre free list");
2688 r->
next = g_detect_parse_regex_list;
2689 g_detect_parse_regex_list = r;
2697 detect_parse->
regex =
2698 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2699 if (detect_parse->
regex == NULL) {
2700 PCRE2_UCHAR errbuffer[256];
2701 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2702 SCLogError(
"pcre compile of \"%s\" failed at "
2704 parse_str, en, errbuffer);
2707 detect_parse->
context = pcre2_match_context_create(NULL);
2708 if (detect_parse->
context == NULL) {
2709 SCLogError(
"pcre2 could not create match context");
2710 pcre2_code_free(detect_parse->
regex);
2711 detect_parse->
regex = NULL;
2726 if (detect_parse == NULL) {
2730 detect_parse->
regex =
2731 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2732 if (detect_parse->
regex == NULL) {
2733 PCRE2_UCHAR errbuffer[256];
2734 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2735 SCLogError(
"pcre2 compile of \"%s\" failed at "
2737 parse_str, (
int)eo, errbuffer);
2742 detect_parse->
next = g_detect_parse_regex_list;
2743 g_detect_parse_regex_list = detect_parse;
2744 return detect_parse;
2748 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
2750 int r = pcre2_substring_copy_bynumber(match_data, number, buffer, bufflen);
2751 if (r == PCRE2_ERROR_UNSET) {
2760 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
2762 int r = pcre2_substring_get_bynumber(match_data, number, bufferptr, bufflen);
2763 if (r == PCRE2_ERROR_UNSET) {
2787 static int SigParseTest01 (
void)
2796 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
2806 static int SigParseTest02 (
void)
2820 sig =
SigInit(
de_ctx,
"alert tcp any !21:902 -> any any (msg:\"ET MALWARE Suspicious 220 Banner on Local Port\"; content:\"220\"; offset:0; depth:4; pcre:\"/220[- ]/\"; sid:2003055; rev:4;)");
2848 static int SigParseTest03 (
void)
2857 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any <- !1.2.3.4 any (msg:\"SigParseTest03\"; sid:1;)");
2860 printf(
"expected NULL got sig ptr %p: ",sig);
2869 static int SigParseTest04 (
void)
2878 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024: -> !1.2.3.4 1024: (msg:\"SigParseTest04\"; sid:1;)");
2889 static int SigParseTest05 (
void)
2898 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024:65536 -> !1.2.3.4 any (msg:\"SigParseTest05\"; sid:1;)");
2902 printf(
"signature didn't fail to parse as we expected: ");
2912 static int SigParseTest06 (
void)
2921 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (flow:to_server; content:\"GET\"; nocase; http_method; uricontent:\"/uri/\"; nocase; content:\"Host|3A| abc\"; nocase; sid:1; rev:1;)");
2925 printf(
"signature failed to parse: ");
2939 static int SigParseTest07(
void)
2961 static int SigParseTest08(
void)
2984 static int SigParseTest09(
void)
3035 static int SigParseTest10(
void)
3067 static int SigParseTest11(
void)
3078 "drop tcp any any -> any 80 (msg:\"Snort_Inline is blocking the http link\"; sid:1;) ");
3080 printf(
"sig 1 didn't parse: ");
3085 "the http link\"; sid:2;) ");
3087 printf(
"sig 2 didn't parse: ");
3101 static int SigParseTest12(
void)
3113 printf(
"sig 1 should have given an error: ");
3127 static int SigParseTest13(
void)
3139 printf(
"sig 1 invalidated: failure");
3144 printf(
"sig doesn't have stream flag set\n");
3149 printf(
"sig has packet flag set\n");
3164 static int SigParseTest14(
void)
3176 printf(
"sig 1 invalidated: failure");
3181 printf(
"sig doesn't have packet flag set\n");
3186 printf(
"sig has stream flag set\n");
3201 static int SigParseTest15(
void)
3213 printf(
"sig 1 invalidated: failure");
3218 printf(
"sig doesn't have packet flag set\n");
3223 printf(
"sig doesn't have stream flag set\n");
3238 static int SigParseTest16(
void)
3250 printf(
"sig 1 invalidated: failure");
3255 printf(
"sig doesn't have packet flag set\n");
3260 printf(
"sig doesn't have stream flag set\n");
3275 static int SigParseTest17(
void)
3287 printf(
"sig 1 invalidated: failure");
3292 printf(
"sig doesn't have packet flag set\n");
3297 printf(
"sig doesn't have stream flag set\n");
3310 static int SigParseTest18 (
void)
3318 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:99999999999999999999;)") != NULL)
3329 static int SigParseTest19 (
void)
3337 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; gid:99999999999999999999;)") != NULL)
3348 static int SigParseTest20 (
void)
3356 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; rev:99999999999999999999;)") != NULL)
3367 static int SigParseTest21 (
void)
3386 static int SigParseTest22 (
void)
3394 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp [10.10.10.0/24, !10.10.10.247] any -> [10.10.10.0/24, !10.10.10.247] any (sid:1;)") == NULL)
3407 static int SigParseTest23(
void)
3422 static int SigParseBidirecTest06 (
void)
3442 static int SigParseBidirecTest07 (
void)
3462 static int SigParseBidirecTest08 (
void)
3482 static int SigParseBidirecTest09 (
void)
3502 static int SigParseBidirecTest10 (
void)
3522 static int SigParseBidirecTest11 (
void)
3542 static int SigParseBidirecTest12 (
void)
3562 static int SigParseBidirecTest13 (
void)
3581 static int SigParseBidirecTest14 (
void)
3602 static int SigTestBidirec01 (
void)
3614 if (sig->
next != NULL)
3633 static int SigTestBidirec02 (
void)
3652 if (sig->
next == NULL)
3657 if (copy->
next != NULL)
3678 static int SigTestBidirec03 (
void)
3690 const char *sigs[3];
3691 sigs[0] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 1\"; sid:1;)";
3692 sigs[1] =
"alert tcp any any <> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)";
3693 sigs[2] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 3\"; sid:3;)";
3700 if (sig->
next == NULL)
3711 uint8_t rawpkt1_ether[] = {
3712 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3713 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3714 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3715 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3716 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3717 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3718 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3719 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3720 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3721 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3722 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3723 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3724 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3725 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3726 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3727 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3728 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3729 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3730 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3731 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3732 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3733 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3734 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3735 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3736 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3737 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3738 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3739 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3740 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3741 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3742 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3743 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3744 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3745 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3746 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3747 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3748 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3749 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3750 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3751 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3752 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3753 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3754 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3755 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3756 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3757 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3758 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3759 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3760 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3761 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3762 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3763 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3764 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3765 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3766 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3776 uint32_t sids[3] = {1, 2, 3};
3777 uint32_t results[3] = {1, 1, 1};
3793 static int SigTestBidirec04 (
void)
3808 sig =
DetectEngineAppendSig(
de_ctx,
"alert tcp 192.168.1.1 any <> any any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)");
3813 if (sig->
next == NULL)
3825 if (sig->
next == NULL)
3836 uint8_t rawpkt1_ether[] = {
3837 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3838 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3839 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3840 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3841 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3842 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3843 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3844 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3845 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3846 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3847 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3848 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3849 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3850 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3851 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3852 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3853 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3854 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3855 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3856 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3857 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3858 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3859 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3860 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3861 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3862 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3863 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3864 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3865 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3866 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3867 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3868 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3869 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3870 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3871 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3872 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3873 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3874 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3875 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3876 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3877 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3878 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3879 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3880 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3881 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3882 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3883 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3884 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3885 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3886 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3887 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3888 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3889 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3890 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3891 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3900 memset(&th_v, 0,
sizeof(th_v));
3940 static int SigParseTestNegation01 (
void)
3954 static int SigParseTestNegation02 (
void)
3965 s =
SigInit(
de_ctx,
"alert tcp any !any -> any any (msg:\"SigTest41-02 src ip is !any \"; classtype:misc-activity; sid:410002; rev:1;)");
3980 static int SigParseTestNegation03 (
void)
3991 s =
SigInit(
de_ctx,
"alert tcp any any -> any [80:!80] (msg:\"SigTest41-03 dst port [80:!80] \"; classtype:misc-activity; sid:410003; rev:1;)");
4006 static int SigParseTestNegation04 (
void)
4017 s =
SigInit(
de_ctx,
"alert tcp any any -> any [80,!80] (msg:\"SigTest41-03 dst port [80:!80] \"; classtype:misc-activity; sid:410003; rev:1;)");
4032 static int SigParseTestNegation05 (
void)
4043 s =
SigInit(
de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.2] any (msg:\"SigTest41-04 dst ip [192.168.0.2,!192.168.0.2] \"; classtype:misc-activity; sid:410004; rev:1;)");
4058 static int SigParseTestNegation06 (
void)
4069 s =
SigInit(
de_ctx,
"alert tcp any any -> any [100:1000,!1:20000] (msg:\"SigTest41-05 dst port [100:1000,!1:20000] \"; classtype:misc-activity; sid:410005; rev:1;)");
4085 static int SigParseTestNegation07 (
void)
4091 de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.0/24] any (sid:410006;)");
4100 static int SigParseTestNegation08 (
void)
4111 s =
SigInit(
de_ctx,
"alert tcp any any -> [192.168.0.0/16,!192.168.0.0/24] any (sid:410006; rev:1;)");
4126 static int SigParseTestMpm01 (
void)
4135 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; sid:1;)");
4137 printf(
"sig failed to init: ");
4142 printf(
"sig doesn't have content list: ");
4157 static int SigParseTestMpm02 (
void)
4166 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; content:\"abcdef\"; sid:1;)");
4168 printf(
"sig failed to init: ");
4173 printf(
"sig doesn't have content list: ");
4188 static int SigParseTestAppLayerTLS01(
void)
4199 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS01 \"; sid:410006; rev:1;)");
4201 printf(
"parsing sig failed: ");
4206 printf(
"alproto not set: ");
4223 static int SigParseTestAppLayerTLS02(
void)
4234 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS02 \"; tls.version:1.0; sid:410006; rev:1;)");
4236 printf(
"parsing sig failed: ");
4241 printf(
"alproto not set: ");
4257 static int SigParseTestAppLayerTLS03(
void)
4268 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS03 \"; tls.version:2.5; sid:410006; rev:1;)");
4281 static int SigParseTestUnbalancedQuotes01(
void)
4291 "alert http any any -> any any (msg:\"SigParseTestUnbalancedQuotes01\"; "
4292 "pcre:\"/\\/[a-z]+\\.php\\?[a-z]+?=\\d{7}&[a-z]+?=\\d{7,8}$/U\" "
4293 "flowbits:set,et.exploitkitlanding; classtype:trojan-activity; sid:2017078; rev:5;)");
4299 static int SigParseTestContentGtDsize01(
void)
4306 "alert http any any -> any any ("
4307 "dsize:21; content:\"0123456789001234567890|00 00|\"; "
4314 static int SigParseTestContentGtDsize02(
void)
4321 "alert http any any -> any any ("
4322 "dsize:21; content:\"0123456789|00 00|\"; offset:10; "
4339 static int SigParseBidirWithSameSrcAndDest01(
void)
4356 "alert tcp [1.2.3.4, 5.6.7.8] [80, 81] <> [5.6.7.8, 1.2.3.4] [81, 80] (sid:3;)");
4365 static int SigParseBidirWithSameSrcAndDest02(
void)
4373 de_ctx,
"alert tcp 1.2.3.4 any <> [1.2.3.4, 5.6.7.8, ::1] any (sid:1;)");
4380 de_ctx,
"alert tcp [1.2.3.4, ::1] [80, 81, 82] <> [1.2.3.4, ::1] [80, 81] (sid:2;)");
4387 "alert tcp [1.2.3.4, ::1, ABCD:AAAA::1] [80] <> [1.2.3.4, ::1] [80, 81] (sid:3;)");
4394 de_ctx,
"alert tcp [!1.2.3.4, 1.2.3.0/24] any <> [1.2.3.0/24, !1.2.3.4] any (sid:4;)");
4401 de_ctx,
"alert tcp [1.2.3.4, 1.2.3.0/24] any <> [1.2.3.0/24, !1.2.3.4] any (sid:5;)");
4410 static int SigParseTestActionReject(
void)
4416 de_ctx,
"reject tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4417 #ifdef HAVE_LIBNET11
4428 static int SigParseTestActionDrop(
void)
4434 de_ctx,
"drop tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4474 UtRegisterTest(
"SigParseTest21 -- address with space", SigParseTest21);
4475 UtRegisterTest(
"SigParseTest22 -- address with space", SigParseTest22);
4476 UtRegisterTest(
"SigParseTest23 -- carriage return", SigParseTest23);
4491 UtRegisterTest(
"SigParseTestNegation01", SigParseTestNegation01);
4492 UtRegisterTest(
"SigParseTestNegation02", SigParseTestNegation02);
4493 UtRegisterTest(
"SigParseTestNegation03", SigParseTestNegation03);
4494 UtRegisterTest(
"SigParseTestNegation04", SigParseTestNegation04);
4495 UtRegisterTest(
"SigParseTestNegation05", SigParseTestNegation05);
4496 UtRegisterTest(
"SigParseTestNegation06", SigParseTestNegation06);
4497 UtRegisterTest(
"SigParseTestNegation07", SigParseTestNegation07);
4498 UtRegisterTest(
"SigParseTestNegation08", SigParseTestNegation08);
4501 UtRegisterTest(
"SigParseTestAppLayerTLS01", SigParseTestAppLayerTLS01);
4502 UtRegisterTest(
"SigParseTestAppLayerTLS02", SigParseTestAppLayerTLS02);
4503 UtRegisterTest(
"SigParseTestAppLayerTLS03", SigParseTestAppLayerTLS03);
4504 UtRegisterTest(
"SigParseTestUnbalancedQuotes01", SigParseTestUnbalancedQuotes01);
4507 SigParseTestContentGtDsize01);
4509 SigParseTestContentGtDsize02);
4512 SigParseBidirWithSameSrcAndDest01);
4514 SigParseBidirWithSameSrcAndDest02);
4515 UtRegisterTest(
"SigParseTestActionReject", SigParseTestActionReject);
4516 UtRegisterTest(
"SigParseTestActionDrop", SigParseTestActionDrop);