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",
1220 static uint8_t ActionStringToFlags(
const char *action)
1222 if (strcasecmp(action,
"alert") == 0) {
1224 }
else if (strcasecmp(action,
"drop") == 0) {
1226 }
else if (strcasecmp(action,
"pass") == 0) {
1228 }
else if (strcasecmp(action,
"reject") == 0 ||
1229 strcasecmp(action,
"rejectsrc") == 0)
1231 if (!(SigParseActionRejectValidate(action)))
1234 }
else if (strcasecmp(action,
"rejectdst") == 0) {
1235 if (!(SigParseActionRejectValidate(action)))
1238 }
else if (strcasecmp(action,
"rejectboth") == 0) {
1239 if (!(SigParseActionRejectValidate(action)))
1242 }
else if (strcasecmp(action,
"config") == 0) {
1245 SCLogError(
"An invalid action \"%s\" was given", action);
1261 static int SigParseAction(
Signature *s,
const char *action)
1263 uint8_t
flags = ActionStringToFlags(action);
1282 static inline int SigParseToken(
char **input,
char *output,
1283 const size_t output_size)
1285 size_t len = *input == NULL ? 0 : strlen(*input);
1291 while (
len && isblank(**input)) {
1296 char *endptr = strpbrk(*input,
" \t\n\r");
1297 if (endptr != NULL) {
1300 strlcpy(output, *input, output_size);
1317 static inline int SigParseList(
char **input,
char *output,
1318 const size_t output_size)
1321 size_t len = *input != NULL ? strlen(*input) : 0;
1327 while (
len && isblank(**input)) {
1333 for (i = 0; i <
len; i++) {
1334 char c = (*input)[i];
1337 }
else if (c ==
']') {
1339 }
else if (c ==
' ') {
1350 strlcpy(output, *input, output_size);
1351 *input = *input + i + 1;
1371 SigParseToken(&index, parser->
action,
sizeof(parser->
action));
1377 SigParseList(&index, parser->
src,
sizeof(parser->
src));
1380 SigParseList(&index, parser->
sp,
sizeof(parser->
sp));
1386 SigParseList(&index, parser->
dst,
sizeof(parser->
dst));
1389 SigParseList(&index, parser->
dp,
sizeof(parser->
dp));
1392 if (index == NULL) {
1396 while (isspace(*index) || *index ==
'(') {
1399 for (
size_t i = strlen(index); i > 0; i--) {
1400 if (isspace(index[i - 1]) || index[i - 1] ==
')') {
1401 index[i - 1] =
'\0';
1413 if (SigParseAction(s, parser->
action) < 0)
1416 if (SigParseProto(s, parser->
protocol) < 0)
1419 if (strcmp(parser->
direction,
"<>") == 0) {
1421 }
else if (strcmp(parser->
direction,
"->") != 0) {
1422 SCLogError(
"\"%s\" is not a valid direction modifier, "
1423 "\"->\" and \"<>\" are supported.",
1450 static inline bool CheckAscii(
const char *
str)
1452 for (
size_t i = 0; i < strlen(
str); i++) {
1453 if (
str[i] < 0x20) {
1455 if (
str[i] == 0x0a ||
str[i] == 0x0d ||
str[i] == 0x09) {
1459 }
else if (
str[i] == 0x7f) {
1483 if (!rs_check_utf8(sigstr)) {
1488 if (!CheckAscii(sigstr)) {
1489 SCLogError(
"rule contains invalid (control) characters");
1493 int ret = SigParseBasics(
de_ctx, s, sigstr, parser, addrs_direction, requires);
1500 if (strlen(parser->
opts) > 0) {
1501 size_t buffer_size = strlen(parser->
opts) + 1;
1502 char input[buffer_size];
1503 char output[buffer_size];
1504 memset(input, 0x00, buffer_size);
1505 memcpy(input, parser->
opts, strlen(parser->
opts) + 1);
1511 memset(output, 0x00, buffer_size);
1512 ret = SigParseOptions(
de_ctx, s, input, output, buffer_size, requires);
1514 memcpy(input, output, buffer_size);
1546 memset(b, 0,
sizeof(*b));
1594 static void SigMetadataFree(
Signature *s)
1601 if (s == NULL || s->
metadata == NULL) {
1608 next_mdata = mdata->
next;
1639 next_ref = ref->
next;
1693 while (sm != NULL) {
1702 while (sm != NULL) {
1723 if (s->
sp != NULL) {
1726 if (s->
dp != NULL) {
1764 SCLogError(
"transforms must directly follow stickybuffers");
1784 if (!AppProtoIsValid(alproto)) {
1790 alproto = AppProtoCommon(s->
alproto, alproto);
1792 SCLogError(
"can't set rule app proto to %s: already set to %s",
1819 if (addr_match4 == NULL) {
1825 addr_match4[idx].
ip =
SCNtohl(da->ip.addr_data32[0]);
1826 addr_match4[idx].
ip2 =
SCNtohl(da->ip2.addr_data32[0]);
1845 if (addr_match6 == NULL) {
1851 addr_match6[idx].
ip[0] =
SCNtohl(da->ip.addr_data32[0]);
1852 addr_match6[idx].
ip[1] =
SCNtohl(da->ip.addr_data32[1]);
1853 addr_match6[idx].
ip[2] =
SCNtohl(da->ip.addr_data32[2]);
1854 addr_match6[idx].
ip[3] =
SCNtohl(da->ip.addr_data32[3]);
1855 addr_match6[idx].
ip2[0] =
SCNtohl(da->ip2.addr_data32[0]);
1856 addr_match6[idx].
ip2[1] =
SCNtohl(da->ip2.addr_data32[1]);
1857 addr_match6[idx].
ip2[2] =
SCNtohl(da->ip2.addr_data32[2]);
1858 addr_match6[idx].
ip2[3] =
SCNtohl(da->ip2.addr_data32[3]);
1871 static void SigBuildAddressMatchArray(
Signature *s)
1888 static int SigMatchListLen(
SigMatch *sm)
1891 for (; sm != NULL; sm = sm->
next)
1902 int len = SigMatchListLen(
head);
1908 FatalError(
"initializing the detection engine failed");
1914 for (; sm != NULL; sm = sm->
next, smd++) {
1954 SCLogDebug(
"s %u: no mpm; prefilter? de_ctx->prefilter_setting %u "
1955 "s->init_data->has_possible_prefilter %s",
1969 prefilter_list =
MIN(prefilter_list, sm->
type);
1979 if (sm->
type == prefilter_list) {
2006 uint32_t sig_flags = 0;
2011 nlists += (nlists > 0);
2015 SCLogError(
"rule %u setup buffer %s but didn't add matches to it", s->
id,
2020 bool has_frame =
false;
2021 bool has_app =
false;
2022 bool has_pkt =
false;
2023 bool has_pmatch =
false;
2033 struct BufferVsDir {
2036 } bufdir[nlists + 1];
2037 memset(&bufdir, 0, (nlists + 1) *
sizeof(
struct BufferVsDir));
2051 if (b->
head == NULL) {
2056 has_frame |= bt->
frame;
2062 "specific matches (like dsize, flags, ttl) with stream / "
2063 "state matching by matching on app layer proto (like using "
2064 "http_* keywords).");
2069 for (; app != NULL; app = app->
next) {
2076 bufdir[b->
id].ts += (app->
dir == 0);
2077 bufdir[b->
id].tc += (app->
dir == 1);
2096 for (
int x = 0; x < nlists; x++) {
2097 if (bufdir[x].
ts == 0 && bufdir[x].tc == 0)
2099 ts_excl += (bufdir[x].ts > 0 && bufdir[x].tc == 0);
2100 tc_excl += (bufdir[x].ts == 0 && bufdir[x].tc > 0);
2101 dir_amb += (bufdir[x].ts > 0 && bufdir[x].tc > 0);
2106 if (ts_excl && tc_excl) {
2107 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2109 }
else if (ts_excl) {
2110 SCLogDebug(
"%u: implied rule direction is toserver", s->
id);
2112 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2115 }
else if (tc_excl) {
2116 SCLogDebug(
"%u: implied rule direction is toclient", s->
id);
2118 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2121 }
else if (dir_amb) {
2122 SCLogDebug(
"%u: rule direction cannot be deduced from keywords", s->
id);
2128 "tcp-stream or flow:only_stream. Invalidating signature.");
2133 SCLogError(
"You seem to have mixed keywords "
2134 "that require inspection in both directions. Atm we only "
2135 "support keywords in one direction within a rule.");
2139 if (has_pmatch && has_frame) {
2140 SCLogError(
"can't mix pure content and frame inspection");
2143 if (has_app && has_frame) {
2144 SCLogError(
"can't mix app-layer buffer and frame inspection");
2147 if (has_pkt && has_frame) {
2148 SCLogError(
"can't mix pkt buffer and frame inspection");
2156 if (s->
proto.
proto[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) {
2193 "support file matching",
2198 SCLogError(
"protocol HTTP2 doesn't support file name matching");
2218 memset(&parser, 0x00,
sizeof(parser));
2236 int ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
true);
2243 }
else if (ret < 0) {
2249 SCLogError(
"Signature missing required value \"sid\".");
2254 ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
false);
2260 }
else if (ret == -2) {
2263 }
else if (ret < 0) {
2268 if (sig->
prio == -1)
2275 int override_needed = 0;
2279 override_needed = 1;
2281 override_needed = 1;
2283 for (s = 0; s <
sizeof(sig->
proto.
proto); s++) {
2285 override_needed = 0;
2294 if (override_needed)
2304 for ( ; sm != NULL; sm = sm->
next) {
2320 SCLogDebug(
"sig %"PRIu32
" SIG_FLAG_APPLAYER: %s, SIG_FLAG_PACKET: %s",
2324 SigBuildAddressMatchArray(sig);
2335 SigSetupPrefilter(
de_ctx, sig);
2338 if (SigValidate(
de_ctx, sig) == 0) {
2368 static bool SigHasSameSourceAndDestination(
const Signature *s)
2420 if (SigHasSameSourceAndDestination(sig)) {
2421 SCLogInfo(
"Rule with ID %u is bidirectional, but source and destination are the same, "
2422 "treating the rule as unidirectional", sig->
id);
2427 if (sig->
next == NULL) {
2452 static void DetectParseDupSigFreeFunc(
void *data)
2468 static uint32_t DetectParseDupSigHashFunc(
HashListTable *ht,
void *data, uint16_t datalen)
2487 static char DetectParseDupSigCompareFunc(
void *data1, uint16_t len1,
void *data2,
2493 if (sw1 == NULL || sw2 == NULL ||
2494 sw1->
s == NULL || sw2->
s == NULL)
2498 if (sw1->
s->
id == sw2->
s->
id && sw1->
s->
gid == sw2->
s->
gid)
return 1;
2514 DetectParseDupSigHashFunc,
2515 DetectParseDupSigCompareFunc,
2516 DetectParseDupSigFreeFunc);
2577 if (sw_dup == NULL) {
2590 (
void *)&sw_tmp, 0);
2602 if (sw->
s->
rev <= sw_dup->
s->
rev) {
2611 if (sw_dup->
s_prev == NULL) {
2619 sw_temp.
s = sw_dup->
s->
next;
2623 if (sw_temp.
s != NULL) {
2625 (
void *)&sw_temp, 0);
2645 sw_temp.
s = sw_dup->
s->
next;
2653 if (sw_temp.
s != NULL) {
2655 (
void *)&sw_temp, 0);
2670 (
void *)&sw_tmp, 0);
2671 if (sw_old->
s != sw_dup->
s) {
2714 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
2718 SCLogError(
"Duplicate signature \"%s\"", sigstr);
2720 }
else if (dup_sig == 2) {
2722 " so the older sig replaced by this new signature \"%s\"",
2727 if (sig->
next != NULL) {
2744 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
2748 if (sig != NULL && sig->
next != NULL) {
2761 int start_offset,
int options)
2763 *match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
2765 return pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)
str, strlen(
str), options, start_offset,
2766 *match, parse_regex->
context);
2773 pcre2_code_free(r->
regex);
2776 pcre2_match_context_free(r->
context);
2791 g_detect_parse_regex_list = NULL;
2800 FatalError(
"failed to alloc memory for pcre free list");
2803 r->
next = g_detect_parse_regex_list;
2804 g_detect_parse_regex_list = r;
2812 detect_parse->
regex =
2813 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2814 if (detect_parse->
regex == NULL) {
2815 PCRE2_UCHAR errbuffer[256];
2816 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2817 SCLogError(
"pcre compile of \"%s\" failed at "
2819 parse_str, en, errbuffer);
2822 detect_parse->
context = pcre2_match_context_create(NULL);
2823 if (detect_parse->
context == NULL) {
2824 SCLogError(
"pcre2 could not create match context");
2825 pcre2_code_free(detect_parse->
regex);
2826 detect_parse->
regex = NULL;
2841 if (detect_parse == NULL) {
2845 detect_parse->
regex =
2846 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2847 if (detect_parse->
regex == NULL) {
2848 PCRE2_UCHAR errbuffer[256];
2849 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2850 SCLogError(
"pcre2 compile of \"%s\" failed at "
2852 parse_str, (
int)eo, errbuffer);
2857 detect_parse->
next = g_detect_parse_regex_list;
2858 g_detect_parse_regex_list = detect_parse;
2859 return detect_parse;
2863 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
2865 int r = pcre2_substring_copy_bynumber(match_data, number, buffer, bufflen);
2866 if (r == PCRE2_ERROR_UNSET) {
2875 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
2877 int r = pcre2_substring_get_bynumber(match_data, number, bufferptr, bufflen);
2878 if (r == PCRE2_ERROR_UNSET) {
2902 static int SigParseTest01 (
void)
2911 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
2921 static int SigParseTest02 (
void)
2935 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;)");
2963 static int SigParseTest03 (
void)
2972 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any <- !1.2.3.4 any (msg:\"SigParseTest03\"; sid:1;)");
2975 printf(
"expected NULL got sig ptr %p: ",sig);
2984 static int SigParseTest04 (
void)
2993 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024: -> !1.2.3.4 1024: (msg:\"SigParseTest04\"; sid:1;)");
3004 static int SigParseTest05 (
void)
3013 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024:65536 -> !1.2.3.4 any (msg:\"SigParseTest05\"; sid:1;)");
3017 printf(
"signature didn't fail to parse as we expected: ");
3027 static int SigParseTest06 (
void)
3036 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;)");
3040 printf(
"signature failed to parse: ");
3054 static int SigParseTest07(
void)
3076 static int SigParseTest08(
void)
3099 static int SigParseTest09(
void)
3150 static int SigParseTest10(
void)
3182 static int SigParseTest11(
void)
3193 "drop tcp any any -> any 80 (msg:\"Snort_Inline is blocking the http link\"; sid:1;) ");
3195 printf(
"sig 1 didn't parse: ");
3200 "the http link\"; sid:2;) ");
3202 printf(
"sig 2 didn't parse: ");
3216 static int SigParseTest12(
void)
3228 printf(
"sig 1 should have given an error: ");
3242 static int SigParseTest13(
void)
3254 printf(
"sig 1 invalidated: failure");
3259 printf(
"sig doesn't have stream flag set\n");
3264 printf(
"sig has packet flag set\n");
3279 static int SigParseTest14(
void)
3291 printf(
"sig 1 invalidated: failure");
3296 printf(
"sig doesn't have packet flag set\n");
3301 printf(
"sig has stream flag set\n");
3316 static int SigParseTest15(
void)
3328 printf(
"sig 1 invalidated: failure");
3333 printf(
"sig doesn't have packet flag set\n");
3338 printf(
"sig doesn't have stream flag set\n");
3353 static int SigParseTest16(
void)
3365 printf(
"sig 1 invalidated: failure");
3370 printf(
"sig doesn't have packet flag set\n");
3375 printf(
"sig doesn't have stream flag set\n");
3390 static int SigParseTest17(
void)
3402 printf(
"sig 1 invalidated: failure");
3407 printf(
"sig doesn't have packet flag set\n");
3412 printf(
"sig doesn't have stream flag set\n");
3425 static int SigParseTest18 (
void)
3433 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:99999999999999999999;)") != NULL)
3444 static int SigParseTest19 (
void)
3452 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; gid:99999999999999999999;)") != NULL)
3463 static int SigParseTest20 (
void)
3471 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; rev:99999999999999999999;)") != NULL)
3482 static int SigParseTest21 (
void)
3501 static int SigParseTest22 (
void)
3509 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)
3522 static int SigParseTest23(
void)
3537 static int SigParseBidirecTest06 (
void)
3557 static int SigParseBidirecTest07 (
void)
3577 static int SigParseBidirecTest08 (
void)
3597 static int SigParseBidirecTest09 (
void)
3617 static int SigParseBidirecTest10 (
void)
3637 static int SigParseBidirecTest11 (
void)
3657 static int SigParseBidirecTest12 (
void)
3677 static int SigParseBidirecTest13 (
void)
3696 static int SigParseBidirecTest14 (
void)
3717 static int SigTestBidirec01 (
void)
3729 if (sig->
next != NULL)
3748 static int SigTestBidirec02 (
void)
3767 if (sig->
next == NULL)
3772 if (copy->
next != NULL)
3793 static int SigTestBidirec03 (
void)
3805 const char *sigs[3];
3806 sigs[0] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 1\"; sid:1;)";
3807 sigs[1] =
"alert tcp any any <> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)";
3808 sigs[2] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 3\"; sid:3;)";
3815 if (sig->
next == NULL)
3826 uint8_t rawpkt1_ether[] = {
3827 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3828 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3829 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3830 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3831 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3832 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3833 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3834 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3835 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3836 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3837 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3838 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3839 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3840 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3841 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3842 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3843 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3844 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3845 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3846 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3847 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3848 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3849 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3850 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3851 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3852 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3853 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3854 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3855 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3856 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3857 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3858 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3859 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3860 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3861 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3862 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3863 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3864 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3865 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3866 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3867 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3868 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3869 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3870 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3871 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3872 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3873 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3874 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3875 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3876 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3877 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3878 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3879 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3880 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3881 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3891 uint32_t sids[3] = {1, 2, 3};
3892 uint32_t results[3] = {1, 1, 1};
3908 static int SigTestBidirec04 (
void)
3923 sig =
DetectEngineAppendSig(
de_ctx,
"alert tcp 192.168.1.1 any <> any any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)");
3928 if (sig->
next == NULL)
3940 if (sig->
next == NULL)
3951 uint8_t rawpkt1_ether[] = {
3952 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3953 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3954 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3955 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3956 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3957 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3958 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3959 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3960 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3961 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3962 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3963 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3964 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3965 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3966 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3967 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3968 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3969 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3970 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3971 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3972 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3973 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3974 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3975 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3976 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3977 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3978 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3979 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3980 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3981 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3982 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3983 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3984 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3985 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3986 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3987 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3988 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3989 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3990 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3991 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3992 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3993 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3994 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3995 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3996 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3997 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3998 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3999 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
4000 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
4001 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
4002 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
4003 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
4004 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
4005 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
4006 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
4015 memset(&th_v, 0,
sizeof(th_v));
4055 static int SigParseTestNegation01 (
void)
4069 static int SigParseTestNegation02 (
void)
4080 s =
SigInit(
de_ctx,
"alert tcp any !any -> any any (msg:\"SigTest41-02 src ip is !any \"; classtype:misc-activity; sid:410002; rev:1;)");
4095 static int SigParseTestNegation03 (
void)
4106 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;)");
4121 static int SigParseTestNegation04 (
void)
4132 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;)");
4147 static int SigParseTestNegation05 (
void)
4158 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;)");
4173 static int SigParseTestNegation06 (
void)
4184 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;)");
4200 static int SigParseTestNegation07 (
void)
4206 de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.0/24] any (sid:410006;)");
4215 static int SigParseTestNegation08 (
void)
4226 s =
SigInit(
de_ctx,
"alert tcp any any -> [192.168.0.0/16,!192.168.0.0/24] any (sid:410006; rev:1;)");
4241 static int SigParseTestMpm01 (
void)
4250 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; sid:1;)");
4252 printf(
"sig failed to init: ");
4257 printf(
"sig doesn't have content list: ");
4272 static int SigParseTestMpm02 (
void)
4281 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; content:\"abcdef\"; sid:1;)");
4283 printf(
"sig failed to init: ");
4288 printf(
"sig doesn't have content list: ");
4303 static int SigParseTestAppLayerTLS01(
void)
4314 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS01 \"; sid:410006; rev:1;)");
4316 printf(
"parsing sig failed: ");
4321 printf(
"alproto not set: ");
4338 static int SigParseTestAppLayerTLS02(
void)
4349 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS02 \"; tls.version:1.0; sid:410006; rev:1;)");
4351 printf(
"parsing sig failed: ");
4356 printf(
"alproto not set: ");
4372 static int SigParseTestAppLayerTLS03(
void)
4383 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS03 \"; tls.version:2.5; sid:410006; rev:1;)");
4396 static int SigParseTestUnbalancedQuotes01(
void)
4406 "alert http any any -> any any (msg:\"SigParseTestUnbalancedQuotes01\"; "
4407 "pcre:\"/\\/[a-z]+\\.php\\?[a-z]+?=\\d{7}&[a-z]+?=\\d{7,8}$/U\" "
4408 "flowbits:set,et.exploitkitlanding; classtype:trojan-activity; sid:2017078; rev:5;)");
4414 static int SigParseTestContentGtDsize01(
void)
4421 "alert http any any -> any any ("
4422 "dsize:21; content:\"0123456789001234567890|00 00|\"; "
4429 static int SigParseTestContentGtDsize02(
void)
4436 "alert http any any -> any any ("
4437 "dsize:21; content:\"0123456789|00 00|\"; offset:10; "
4454 static int SigParseBidirWithSameSrcAndDest01(
void)
4471 "alert tcp [1.2.3.4, 5.6.7.8] [80, 81] <> [5.6.7.8, 1.2.3.4] [81, 80] (sid:3;)");
4480 static int SigParseBidirWithSameSrcAndDest02(
void)
4488 de_ctx,
"alert tcp 1.2.3.4 any <> [1.2.3.4, 5.6.7.8, ::1] any (sid:1;)");
4495 de_ctx,
"alert tcp [1.2.3.4, ::1] [80, 81, 82] <> [1.2.3.4, ::1] [80, 81] (sid:2;)");
4502 "alert tcp [1.2.3.4, ::1, ABCD:AAAA::1] [80] <> [1.2.3.4, ::1] [80, 81] (sid:3;)");
4509 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;)");
4516 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;)");
4525 static int SigParseTestActionReject(
void)
4531 de_ctx,
"reject tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4532 #ifdef HAVE_LIBNET11
4543 static int SigParseTestActionDrop(
void)
4549 de_ctx,
"drop tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4589 UtRegisterTest(
"SigParseTest21 -- address with space", SigParseTest21);
4590 UtRegisterTest(
"SigParseTest22 -- address with space", SigParseTest22);
4591 UtRegisterTest(
"SigParseTest23 -- carriage return", SigParseTest23);
4606 UtRegisterTest(
"SigParseTestNegation01", SigParseTestNegation01);
4607 UtRegisterTest(
"SigParseTestNegation02", SigParseTestNegation02);
4608 UtRegisterTest(
"SigParseTestNegation03", SigParseTestNegation03);
4609 UtRegisterTest(
"SigParseTestNegation04", SigParseTestNegation04);
4610 UtRegisterTest(
"SigParseTestNegation05", SigParseTestNegation05);
4611 UtRegisterTest(
"SigParseTestNegation06", SigParseTestNegation06);
4612 UtRegisterTest(
"SigParseTestNegation07", SigParseTestNegation07);
4613 UtRegisterTest(
"SigParseTestNegation08", SigParseTestNegation08);
4616 UtRegisterTest(
"SigParseTestAppLayerTLS01", SigParseTestAppLayerTLS01);
4617 UtRegisterTest(
"SigParseTestAppLayerTLS02", SigParseTestAppLayerTLS02);
4618 UtRegisterTest(
"SigParseTestAppLayerTLS03", SigParseTestAppLayerTLS03);
4619 UtRegisterTest(
"SigParseTestUnbalancedQuotes01", SigParseTestUnbalancedQuotes01);
4622 SigParseTestContentGtDsize01);
4624 SigParseTestContentGtDsize02);
4627 SigParseBidirWithSameSrcAndDest01);
4629 SigParseBidirWithSameSrcAndDest02);
4630 UtRegisterTest(
"SigParseTestActionReject", SigParseTestActionReject);
4631 UtRegisterTest(
"SigParseTestActionDrop", SigParseTestActionDrop);