89 #define ALPROTO_WITHFILES_MAX 16
103 .to_client_progress = HTTP2StateDataServer,
104 .to_server_progress = HTTP2StateDataClient },
109 AppProto alproto,
int direction,
int to_client_progress,
int to_server_progress)
157 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
188 #define CASE_CODE_STRING(E, S) case E: return S; break
199 #undef CASE_CODE_STRING
203 #define CASE_CODE(E) case E: return #E
221 Signature *s,
const char *arg,
int sm_type,
int sm_list,
227 if (arg != NULL && strcmp(arg,
"") != 0) {
236 "with a sticky buffer still set. Reset sticky buffer "
237 "with pkt_data before using the modifier.",
251 "found inside the rule without a content context. "
252 "Please use a \"content\" keyword before using the "
260 "be used with the rawbytes rule keyword",
266 "be used with the replace rule keyword",
305 bool reuse_buffer =
false;
318 SCLogError(
"failed to expand rule buffer array");
327 SCLogDebug(
"idx %u list %d set up curbuf %p s->init_data->buffer_index %u",
368 if (sm->
ctx != NULL) {
379 ptrdiff_t
offset = e - table;
393 if (st->
name != NULL) {
426 if (strcmp(
str,
"all") == 0) {
438 char *xsaveptr = NULL;
439 char *key = strtok_r(copy,
",", &xsaveptr);
440 while (key != NULL) {
446 "argument '%s' not found",
449 key = strtok_r(NULL,
",", &xsaveptr);
476 SCLogDebug(
"s:%p new:%p list:%d: %s, s->init_data->list_set %s s->init_data->list %d", s,
new,
510 SCLogDebug(
"reusing buffer %u as it isn't multi-capable", x);
520 SCLogError(
"failed to expand rule buffer array");
544 SCLogDebug(
"appended %s to list %d, rule pos %u (s->init_data->list %d)",
563 if (sm->
prev != NULL)
565 if (sm->
next != NULL)
607 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
619 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
641 SCLogDebug(
"skip x %u s->init_data->list %d (int)s->init_data->buffers[x].id %d", x,
650 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
654 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
671 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
676 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
701 va_start(ap, sm_list);
703 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
705 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
708 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
739 va_start(ap, list_id);
741 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
745 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
757 va_start(ap, list_id);
759 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
760 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
763 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
786 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
794 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
801 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
807 if (sm->
prev != NULL)
809 if (sm->
next != NULL)
812 if (sm == *src_sm_list)
813 *src_sm_list = sm->
next;
814 if (sm == *src_sm_list_tail)
815 *src_sm_list_tail = sm->
prev;
817 if (*dst_sm_list == NULL) {
819 *dst_sm_list_tail = sm;
827 *dst_sm_list_tail = sm;
854 SCLogError(
"Unable to find the sm in any of the "
860 size_t output_size,
bool requires)
863 char *optname = NULL;
864 char *optvalue = NULL;
867 while (isblank(*optstr)) {
872 char *optend = optstr;
874 optend = strchr(optend,
';');
875 if (optend == NULL) {
879 else if (optend > optstr && *(optend -1 ) ==
'\\') {
888 char *optvalptr = strchr(optstr,
':');
890 *(optvalptr++) =
'\0';
893 for (
size_t i = strlen(optvalptr); i > 0; i--) {
894 if (isblank(optvalptr[i - 1])) {
895 optvalptr[i - 1] =
'\0';
901 optvalue = optvalptr;
905 for (
size_t i = strlen(optstr); i > 0; i--) {
906 if (isblank(optstr[i - 1])) {
907 optstr[i - 1] =
'\0';
916 bool requires_only = strcasecmp(optname,
"requires") == 0 || strcasecmp(optname,
"sid") == 0;
917 if ((requires && !requires_only) || (!requires && requires_only)) {
922 st = SigTableGet(optname);
923 if (st == NULL || st->
Setup == NULL) {
924 SCLogError(
"unknown rule keyword '%s'.", optname);
929 if (optvalue == NULL || strlen(optvalue) == 0) {
931 "invalid formatting or malformed option to %s keyword: '%s'", optname, optstr);
935 if (optvalue && strlen(optvalue)) {
936 SCLogError(
"unexpected option to %s keyword: '%s'", optname, optstr);
946 #define URL "https://suricata.io/our-story/deprecation-policy/"
949 "and will be removed soon. See %s",
953 "and will be removed soon. Use '%s' instead. "
962 if (optvalue != NULL && strlen(optvalue) > 0) {
963 size_t ovlen = strlen(optvalue);
964 char *ptr = optvalue;
974 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
993 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
999 SCLogError(
"invalid formatting to %s keyword: "
1000 "value must be double quoted \'%s\'",
1006 && ovlen && *ptr ==
'"')
1008 for (; ovlen > 0; ovlen--) {
1009 if (isblank(ptr[ovlen - 1])) {
1010 ptr[ovlen - 1] =
'\0';
1015 if (ovlen && ptr[ovlen - 1] !=
'"') {
1016 SCLogError(
"bad option value formatting (possible missing semicolon) "
1017 "for keyword %s: \'%s\'",
1025 ptr[ovlen - 1] =
'\0';
1030 "for keyword %s: \'%s\'",
1037 "quotes on %s keyword that doesn't support them: \'%s\'", optname, optstr);
1047 if (setup_ret < 0) {
1051 if (setup_ret == -2) {
1063 if (strlen(optend) > 0) {
1064 strlcpy(output, optend, output_size);
1079 Signature *s,
const char *addrstr,
char flag)
1081 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
1085 if (strcasecmp(addrstr,
"any") == 0)
1093 if (strcasecmp(addrstr,
"any") == 0)
1120 static int SigParseProto(
Signature *s,
const char *protostr)
1135 "in a signature. Either detection for this protocol "
1136 "is not yet supported OR detection has been disabled for "
1137 "protocol through the yaml option "
1138 "app-layer.protocols.%s.detection-enabled",
1139 protostr, protostr);
1168 Signature *s,
const char *portstr,
char flag)
1174 SCLogDebug(
"Port group \"%s\" to be parsed", portstr);
1177 if (strcasecmp(portstr,
"any") == 0)
1181 }
else if (flag == 1) {
1182 if (strcasecmp(portstr,
"any") == 0)
1197 static int SigParseActionRejectValidate(
const char *action)
1199 #ifdef HAVE_LIBNET11
1200 #if defined HAVE_LIBCAP_NG && !defined HAVE_LIBNET_CAPABILITIES
1203 "incompatible with POSIX based capabilities with privs dropping. "
1204 "For rejects to work, run as root/super user.");
1210 "required for action \"%s\" but is not compiled into Suricata",
1228 static int SigParseAction(
Signature *s,
const char *action)
1230 if (strcasecmp(action,
"alert") == 0) {
1232 }
else if (strcasecmp(action,
"drop") == 0) {
1234 }
else if (strcasecmp(action,
"pass") == 0) {
1236 }
else if (strcasecmp(action,
"reject") == 0 ||
1237 strcasecmp(action,
"rejectsrc") == 0)
1239 if (!(SigParseActionRejectValidate(action)))
1242 }
else if (strcasecmp(action,
"rejectdst") == 0) {
1243 if (!(SigParseActionRejectValidate(action)))
1246 }
else if (strcasecmp(action,
"rejectboth") == 0) {
1247 if (!(SigParseActionRejectValidate(action)))
1250 }
else if (strcasecmp(action,
"config") == 0) {
1253 SCLogError(
"An invalid action \"%s\" was given", action);
1270 static inline int SigParseToken(
char **input,
char *output,
1271 const size_t output_size)
1273 size_t len = *input == NULL ? 0 : strlen(*input);
1279 while (
len && isblank(**input)) {
1284 char *endptr = strpbrk(*input,
" \t\n\r");
1285 if (endptr != NULL) {
1288 strlcpy(output, *input, output_size);
1305 static inline int SigParseList(
char **input,
char *output,
1306 const size_t output_size)
1309 size_t len = *input != NULL ? strlen(*input) : 0;
1315 while (
len && isblank(**input)) {
1321 for (i = 0; i <
len; i++) {
1322 char c = (*input)[i];
1325 }
else if (c ==
']') {
1327 }
else if (c ==
' ') {
1338 strlcpy(output, *input, output_size);
1339 *input = *input + i + 1;
1359 SigParseToken(&index, parser->
action,
sizeof(parser->
action));
1365 SigParseList(&index, parser->
src,
sizeof(parser->
src));
1368 SigParseList(&index, parser->
sp,
sizeof(parser->
sp));
1374 SigParseList(&index, parser->
dst,
sizeof(parser->
dst));
1377 SigParseList(&index, parser->
dp,
sizeof(parser->
dp));
1380 if (index == NULL) {
1384 while (isspace(*index) || *index ==
'(') {
1387 for (
size_t i = strlen(index); i > 0; i--) {
1388 if (isspace(index[i - 1]) || index[i - 1] ==
')') {
1389 index[i - 1] =
'\0';
1401 if (SigParseAction(s, parser->
action) < 0)
1404 if (SigParseProto(s, parser->
protocol) < 0)
1407 if (strcmp(parser->
direction,
"<>") == 0) {
1409 }
else if (strcmp(parser->
direction,
"->") != 0) {
1410 SCLogError(
"\"%s\" is not a valid direction modifier, "
1411 "\"->\" and \"<>\" are supported.",
1438 static inline bool CheckAscii(
const char *
str)
1440 for (
size_t i = 0; i < strlen(
str); i++) {
1441 if (
str[i] < 0x20) {
1443 if (
str[i] == 0x0a ||
str[i] == 0x0d ||
str[i] == 0x09) {
1447 }
else if (
str[i] == 0x7f) {
1471 if (!rs_check_utf8(sigstr)) {
1476 if (!CheckAscii(sigstr)) {
1477 SCLogError(
"rule contains invalid (control) characters");
1481 int ret = SigParseBasics(
de_ctx, s, sigstr, parser, addrs_direction, requires);
1488 if (strlen(parser->
opts) > 0) {
1489 size_t buffer_size = strlen(parser->
opts) + 1;
1490 char input[buffer_size];
1491 char output[buffer_size];
1492 memset(input, 0x00, buffer_size);
1493 memcpy(input, parser->
opts, strlen(parser->
opts) + 1);
1499 memset(output, 0x00, buffer_size);
1500 ret = SigParseOptions(
de_ctx, s, input, output, buffer_size, requires);
1502 memcpy(input, output, buffer_size);
1534 memset(b, 0,
sizeof(*b));
1582 static void SigMetadataFree(
Signature *s)
1589 if (s == NULL || s->
metadata == NULL) {
1596 next_mdata = mdata->
next;
1627 next_ref = ref->
next;
1681 while (sm != NULL) {
1690 while (sm != NULL) {
1711 if (s->
sp != NULL) {
1714 if (s->
dp != NULL) {
1752 SCLogError(
"transforms must directly follow stickybuffers");
1772 if (!AppProtoIsValid(alproto)) {
1778 alproto = AppProtoCommon(s->
alproto, alproto);
1780 SCLogError(
"can't set rule app proto to %s: already set to %s",
1807 if (addr_match4 == NULL) {
1813 addr_match4[idx].
ip =
SCNtohl(da->ip.addr_data32[0]);
1814 addr_match4[idx].
ip2 =
SCNtohl(da->ip2.addr_data32[0]);
1833 if (addr_match6 == NULL) {
1839 addr_match6[idx].
ip[0] =
SCNtohl(da->ip.addr_data32[0]);
1840 addr_match6[idx].
ip[1] =
SCNtohl(da->ip.addr_data32[1]);
1841 addr_match6[idx].
ip[2] =
SCNtohl(da->ip.addr_data32[2]);
1842 addr_match6[idx].
ip[3] =
SCNtohl(da->ip.addr_data32[3]);
1843 addr_match6[idx].
ip2[0] =
SCNtohl(da->ip2.addr_data32[0]);
1844 addr_match6[idx].
ip2[1] =
SCNtohl(da->ip2.addr_data32[1]);
1845 addr_match6[idx].
ip2[2] =
SCNtohl(da->ip2.addr_data32[2]);
1846 addr_match6[idx].
ip2[3] =
SCNtohl(da->ip2.addr_data32[3]);
1859 static void SigBuildAddressMatchArray(
Signature *s)
1876 static int SigMatchListLen(
SigMatch *sm)
1879 for (; sm != NULL; sm = sm->
next)
1890 int len = SigMatchListLen(
head);
1896 FatalError(
"initializing the detection engine failed");
1902 for (; sm != NULL; sm = sm->
next, smd++) {
1942 SCLogDebug(
"s %u: no mpm; prefilter? de_ctx->prefilter_setting %u "
1943 "s->init_data->has_possible_prefilter %s",
1957 prefilter_list =
MIN(prefilter_list, sm->
type);
1967 if (sm->
type == prefilter_list) {
1994 uint32_t sig_flags = 0;
1999 nlists += (nlists > 0);
2003 SCLogError(
"rule %u setup buffer %s but didn't add matches to it", s->
id,
2008 bool has_frame =
false;
2009 bool has_app =
false;
2010 bool has_pkt =
false;
2011 bool has_pmatch =
false;
2021 struct BufferVsDir {
2024 } bufdir[nlists + 1];
2025 memset(&bufdir, 0, (nlists + 1) *
sizeof(
struct BufferVsDir));
2039 if (b->
head == NULL) {
2044 has_frame |= bt->
frame;
2050 "specific matches (like dsize, flags, ttl) with stream / "
2051 "state matching by matching on app layer proto (like using "
2052 "http_* keywords).");
2057 for (; app != NULL; app = app->
next) {
2064 bufdir[b->
id].ts += (app->
dir == 0);
2065 bufdir[b->
id].tc += (app->
dir == 1);
2084 for (
int x = 0; x < nlists; x++) {
2085 if (bufdir[x].
ts == 0 && bufdir[x].tc == 0)
2087 ts_excl += (bufdir[x].ts > 0 && bufdir[x].tc == 0);
2088 tc_excl += (bufdir[x].ts == 0 && bufdir[x].tc > 0);
2089 dir_amb += (bufdir[x].ts > 0 && bufdir[x].tc > 0);
2094 if (ts_excl && tc_excl) {
2095 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2097 }
else if (ts_excl) {
2098 SCLogDebug(
"%u: implied rule direction is toserver", s->
id);
2100 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2103 }
else if (tc_excl) {
2104 SCLogDebug(
"%u: implied rule direction is toclient", s->
id);
2106 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2109 }
else if (dir_amb) {
2110 SCLogDebug(
"%u: rule direction cannot be deduced from keywords", s->
id);
2116 "tcp-stream or flow:only_stream. Invalidating signature.");
2121 SCLogError(
"You seem to have mixed keywords "
2122 "that require inspection in both directions. Atm we only "
2123 "support keywords in one direction within a rule.");
2127 if (has_pmatch && has_frame) {
2128 SCLogError(
"can't mix pure content and frame inspection");
2131 if (has_app && has_frame) {
2132 SCLogError(
"can't mix app-layer buffer and frame inspection");
2135 if (has_pkt && has_frame) {
2136 SCLogError(
"can't mix pkt buffer and frame inspection");
2144 if (s->
proto.
proto[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) {
2181 "support file matching",
2186 SCLogError(
"protocol HTTP2 doesn't support file name matching");
2206 memset(&parser, 0x00,
sizeof(parser));
2224 int ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
true);
2231 }
else if (ret < 0) {
2237 SCLogError(
"Signature missing required value \"sid\".");
2242 ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
false);
2248 }
else if (ret == -2) {
2251 }
else if (ret < 0) {
2256 if (sig->
prio == -1)
2263 int override_needed = 0;
2267 override_needed = 1;
2269 override_needed = 1;
2271 for (s = 0; s <
sizeof(sig->
proto.
proto); s++) {
2273 override_needed = 0;
2282 if (override_needed)
2292 for ( ; sm != NULL; sm = sm->
next) {
2308 SCLogDebug(
"sig %"PRIu32
" SIG_FLAG_APPLAYER: %s, SIG_FLAG_PACKET: %s",
2312 SigBuildAddressMatchArray(sig);
2323 SigSetupPrefilter(
de_ctx, sig);
2326 if (SigValidate(
de_ctx, sig) == 0) {
2356 static bool SigHasSameSourceAndDestination(
const Signature *s)
2408 if (SigHasSameSourceAndDestination(sig)) {
2409 SCLogInfo(
"Rule with ID %u is bidirectional, but source and destination are the same, "
2410 "treating the rule as unidirectional", sig->
id);
2415 if (sig->
next == NULL) {
2440 static void DetectParseDupSigFreeFunc(
void *data)
2456 static uint32_t DetectParseDupSigHashFunc(
HashListTable *ht,
void *data, uint16_t datalen)
2475 static char DetectParseDupSigCompareFunc(
void *data1, uint16_t len1,
void *data2,
2481 if (sw1 == NULL || sw2 == NULL ||
2482 sw1->
s == NULL || sw2->
s == NULL)
2486 if (sw1->
s->
id == sw2->
s->
id && sw1->
s->
gid == sw2->
s->
gid)
return 1;
2502 DetectParseDupSigHashFunc,
2503 DetectParseDupSigCompareFunc,
2504 DetectParseDupSigFreeFunc);
2565 if (sw_dup == NULL) {
2578 (
void *)&sw_tmp, 0);
2590 if (sw->
s->
rev <= sw_dup->
s->
rev) {
2599 if (sw_dup->
s_prev == NULL) {
2607 sw_temp.
s = sw_dup->
s->
next;
2611 if (sw_temp.
s != NULL) {
2613 (
void *)&sw_temp, 0);
2633 sw_temp.
s = sw_dup->
s->
next;
2641 if (sw_temp.
s != NULL) {
2643 (
void *)&sw_temp, 0);
2658 (
void *)&sw_tmp, 0);
2659 if (sw_old->
s != sw_dup->
s) {
2702 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
2706 SCLogError(
"Duplicate signature \"%s\"", sigstr);
2708 }
else if (dup_sig == 2) {
2710 " so the older sig replaced by this new signature \"%s\"",
2715 if (sig->
next != NULL) {
2732 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
2736 if (sig != NULL && sig->
next != NULL) {
2749 int start_offset,
int options)
2751 *match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
2753 return pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)
str, strlen(
str), options, start_offset,
2754 *match, parse_regex->
context);
2761 pcre2_code_free(r->
regex);
2764 pcre2_match_context_free(r->
context);
2779 g_detect_parse_regex_list = NULL;
2788 FatalError(
"failed to alloc memory for pcre free list");
2791 r->
next = g_detect_parse_regex_list;
2792 g_detect_parse_regex_list = r;
2800 detect_parse->
regex =
2801 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2802 if (detect_parse->
regex == NULL) {
2803 PCRE2_UCHAR errbuffer[256];
2804 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2805 SCLogError(
"pcre compile of \"%s\" failed at "
2807 parse_str, en, errbuffer);
2810 detect_parse->
context = pcre2_match_context_create(NULL);
2811 if (detect_parse->
context == NULL) {
2812 SCLogError(
"pcre2 could not create match context");
2813 pcre2_code_free(detect_parse->
regex);
2814 detect_parse->
regex = NULL;
2829 if (detect_parse == NULL) {
2833 detect_parse->
regex =
2834 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2835 if (detect_parse->
regex == NULL) {
2836 PCRE2_UCHAR errbuffer[256];
2837 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2838 SCLogError(
"pcre2 compile of \"%s\" failed at "
2840 parse_str, (
int)eo, errbuffer);
2845 detect_parse->
next = g_detect_parse_regex_list;
2846 g_detect_parse_regex_list = detect_parse;
2847 return detect_parse;
2851 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
2853 int r = pcre2_substring_copy_bynumber(match_data, number, buffer, bufflen);
2854 if (r == PCRE2_ERROR_UNSET) {
2863 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
2865 int r = pcre2_substring_get_bynumber(match_data, number, bufferptr, bufflen);
2866 if (r == PCRE2_ERROR_UNSET) {
2890 static int SigParseTest01 (
void)
2899 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
2909 static int SigParseTest02 (
void)
2923 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;)");
2951 static int SigParseTest03 (
void)
2960 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any <- !1.2.3.4 any (msg:\"SigParseTest03\"; sid:1;)");
2963 printf(
"expected NULL got sig ptr %p: ",sig);
2972 static int SigParseTest04 (
void)
2981 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024: -> !1.2.3.4 1024: (msg:\"SigParseTest04\"; sid:1;)");
2992 static int SigParseTest05 (
void)
3001 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024:65536 -> !1.2.3.4 any (msg:\"SigParseTest05\"; sid:1;)");
3005 printf(
"signature didn't fail to parse as we expected: ");
3015 static int SigParseTest06 (
void)
3024 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;)");
3028 printf(
"signature failed to parse: ");
3042 static int SigParseTest07(
void)
3064 static int SigParseTest08(
void)
3087 static int SigParseTest09(
void)
3138 static int SigParseTest10(
void)
3170 static int SigParseTest11(
void)
3181 "drop tcp any any -> any 80 (msg:\"Snort_Inline is blocking the http link\"; sid:1;) ");
3183 printf(
"sig 1 didn't parse: ");
3188 "the http link\"; sid:2;) ");
3190 printf(
"sig 2 didn't parse: ");
3204 static int SigParseTest12(
void)
3216 printf(
"sig 1 should have given an error: ");
3230 static int SigParseTest13(
void)
3242 printf(
"sig 1 invalidated: failure");
3247 printf(
"sig doesn't have stream flag set\n");
3252 printf(
"sig has packet flag set\n");
3267 static int SigParseTest14(
void)
3279 printf(
"sig 1 invalidated: failure");
3284 printf(
"sig doesn't have packet flag set\n");
3289 printf(
"sig has stream flag set\n");
3304 static int SigParseTest15(
void)
3316 printf(
"sig 1 invalidated: failure");
3321 printf(
"sig doesn't have packet flag set\n");
3326 printf(
"sig doesn't have stream flag set\n");
3341 static int SigParseTest16(
void)
3353 printf(
"sig 1 invalidated: failure");
3358 printf(
"sig doesn't have packet flag set\n");
3363 printf(
"sig doesn't have stream flag set\n");
3378 static int SigParseTest17(
void)
3390 printf(
"sig 1 invalidated: failure");
3395 printf(
"sig doesn't have packet flag set\n");
3400 printf(
"sig doesn't have stream flag set\n");
3413 static int SigParseTest18 (
void)
3421 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:99999999999999999999;)") != NULL)
3432 static int SigParseTest19 (
void)
3440 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; gid:99999999999999999999;)") != NULL)
3451 static int SigParseTest20 (
void)
3459 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; rev:99999999999999999999;)") != NULL)
3470 static int SigParseTest21 (
void)
3489 static int SigParseTest22 (
void)
3497 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)
3510 static int SigParseTest23(
void)
3525 static int SigParseBidirecTest06 (
void)
3545 static int SigParseBidirecTest07 (
void)
3565 static int SigParseBidirecTest08 (
void)
3585 static int SigParseBidirecTest09 (
void)
3605 static int SigParseBidirecTest10 (
void)
3625 static int SigParseBidirecTest11 (
void)
3645 static int SigParseBidirecTest12 (
void)
3665 static int SigParseBidirecTest13 (
void)
3684 static int SigParseBidirecTest14 (
void)
3705 static int SigTestBidirec01 (
void)
3717 if (sig->
next != NULL)
3736 static int SigTestBidirec02 (
void)
3755 if (sig->
next == NULL)
3760 if (copy->
next != NULL)
3781 static int SigTestBidirec03 (
void)
3793 const char *sigs[3];
3794 sigs[0] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 1\"; sid:1;)";
3795 sigs[1] =
"alert tcp any any <> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)";
3796 sigs[2] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 3\"; sid:3;)";
3803 if (sig->
next == NULL)
3814 uint8_t rawpkt1_ether[] = {
3815 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3816 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3817 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3818 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3819 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3820 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3821 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3822 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3823 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3824 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3825 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3826 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3827 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3828 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3829 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3830 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3831 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3832 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3833 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3834 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3835 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3836 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3837 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3838 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3839 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3840 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3841 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3842 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3843 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3844 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3845 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3846 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3847 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3848 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3849 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3850 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3851 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3852 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3853 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3854 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3855 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3856 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3857 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3858 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3859 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3860 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3861 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3862 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3863 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3864 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3865 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3866 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3867 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3868 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3869 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3879 uint32_t sids[3] = {1, 2, 3};
3880 uint32_t results[3] = {1, 1, 1};
3896 static int SigTestBidirec04 (
void)
3911 sig =
DetectEngineAppendSig(
de_ctx,
"alert tcp 192.168.1.1 any <> any any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)");
3916 if (sig->
next == NULL)
3928 if (sig->
next == NULL)
3939 uint8_t rawpkt1_ether[] = {
3940 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3941 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3942 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3943 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3944 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3945 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3946 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3947 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3948 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3949 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3950 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3951 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3952 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3953 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3954 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3955 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3956 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3957 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3958 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3959 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3960 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3961 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3962 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3963 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3964 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3965 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3966 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3967 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3968 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3969 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3970 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3971 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3972 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3973 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3974 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3975 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3976 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3977 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3978 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3979 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3980 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3981 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3982 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3983 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3984 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3985 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3986 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3987 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3988 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3989 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3990 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3991 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3992 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3993 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3994 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
4003 memset(&th_v, 0,
sizeof(th_v));
4043 static int SigParseTestNegation01 (
void)
4057 static int SigParseTestNegation02 (
void)
4068 s =
SigInit(
de_ctx,
"alert tcp any !any -> any any (msg:\"SigTest41-02 src ip is !any \"; classtype:misc-activity; sid:410002; rev:1;)");
4083 static int SigParseTestNegation03 (
void)
4094 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;)");
4109 static int SigParseTestNegation04 (
void)
4120 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;)");
4135 static int SigParseTestNegation05 (
void)
4146 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;)");
4161 static int SigParseTestNegation06 (
void)
4172 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;)");
4188 static int SigParseTestNegation07 (
void)
4194 de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.0/24] any (sid:410006;)");
4203 static int SigParseTestNegation08 (
void)
4214 s =
SigInit(
de_ctx,
"alert tcp any any -> [192.168.0.0/16,!192.168.0.0/24] any (sid:410006; rev:1;)");
4229 static int SigParseTestMpm01 (
void)
4238 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; sid:1;)");
4240 printf(
"sig failed to init: ");
4245 printf(
"sig doesn't have content list: ");
4260 static int SigParseTestMpm02 (
void)
4269 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; content:\"abcdef\"; sid:1;)");
4271 printf(
"sig failed to init: ");
4276 printf(
"sig doesn't have content list: ");
4291 static int SigParseTestAppLayerTLS01(
void)
4302 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS01 \"; sid:410006; rev:1;)");
4304 printf(
"parsing sig failed: ");
4309 printf(
"alproto not set: ");
4326 static int SigParseTestAppLayerTLS02(
void)
4337 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS02 \"; tls.version:1.0; sid:410006; rev:1;)");
4339 printf(
"parsing sig failed: ");
4344 printf(
"alproto not set: ");
4360 static int SigParseTestAppLayerTLS03(
void)
4371 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS03 \"; tls.version:2.5; sid:410006; rev:1;)");
4384 static int SigParseTestUnbalancedQuotes01(
void)
4394 "alert http any any -> any any (msg:\"SigParseTestUnbalancedQuotes01\"; "
4395 "pcre:\"/\\/[a-z]+\\.php\\?[a-z]+?=\\d{7}&[a-z]+?=\\d{7,8}$/U\" "
4396 "flowbits:set,et.exploitkitlanding; classtype:trojan-activity; sid:2017078; rev:5;)");
4402 static int SigParseTestContentGtDsize01(
void)
4409 "alert http any any -> any any ("
4410 "dsize:21; content:\"0123456789001234567890|00 00|\"; "
4417 static int SigParseTestContentGtDsize02(
void)
4424 "alert http any any -> any any ("
4425 "dsize:21; content:\"0123456789|00 00|\"; offset:10; "
4442 static int SigParseBidirWithSameSrcAndDest01(
void)
4459 "alert tcp [1.2.3.4, 5.6.7.8] [80, 81] <> [5.6.7.8, 1.2.3.4] [81, 80] (sid:3;)");
4468 static int SigParseBidirWithSameSrcAndDest02(
void)
4476 de_ctx,
"alert tcp 1.2.3.4 any <> [1.2.3.4, 5.6.7.8, ::1] any (sid:1;)");
4483 de_ctx,
"alert tcp [1.2.3.4, ::1] [80, 81, 82] <> [1.2.3.4, ::1] [80, 81] (sid:2;)");
4490 "alert tcp [1.2.3.4, ::1, ABCD:AAAA::1] [80] <> [1.2.3.4, ::1] [80, 81] (sid:3;)");
4497 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;)");
4504 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;)");
4513 static int SigParseTestActionReject(
void)
4519 de_ctx,
"reject tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4520 #ifdef HAVE_LIBNET11
4531 static int SigParseTestActionDrop(
void)
4537 de_ctx,
"drop tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4577 UtRegisterTest(
"SigParseTest21 -- address with space", SigParseTest21);
4578 UtRegisterTest(
"SigParseTest22 -- address with space", SigParseTest22);
4579 UtRegisterTest(
"SigParseTest23 -- carriage return", SigParseTest23);
4594 UtRegisterTest(
"SigParseTestNegation01", SigParseTestNegation01);
4595 UtRegisterTest(
"SigParseTestNegation02", SigParseTestNegation02);
4596 UtRegisterTest(
"SigParseTestNegation03", SigParseTestNegation03);
4597 UtRegisterTest(
"SigParseTestNegation04", SigParseTestNegation04);
4598 UtRegisterTest(
"SigParseTestNegation05", SigParseTestNegation05);
4599 UtRegisterTest(
"SigParseTestNegation06", SigParseTestNegation06);
4600 UtRegisterTest(
"SigParseTestNegation07", SigParseTestNegation07);
4601 UtRegisterTest(
"SigParseTestNegation08", SigParseTestNegation08);
4604 UtRegisterTest(
"SigParseTestAppLayerTLS01", SigParseTestAppLayerTLS01);
4605 UtRegisterTest(
"SigParseTestAppLayerTLS02", SigParseTestAppLayerTLS02);
4606 UtRegisterTest(
"SigParseTestAppLayerTLS03", SigParseTestAppLayerTLS03);
4607 UtRegisterTest(
"SigParseTestUnbalancedQuotes01", SigParseTestUnbalancedQuotes01);
4610 SigParseTestContentGtDsize01);
4612 SigParseTestContentGtDsize02);
4615 SigParseBidirWithSameSrcAndDest01);
4617 SigParseBidirWithSameSrcAndDest02);
4618 UtRegisterTest(
"SigParseTestActionReject", SigParseTestActionReject);
4619 UtRegisterTest(
"SigParseTestActionDrop", SigParseTestActionDrop);