90 #define ALPROTO_WITHFILES_MAX 16
104 .to_client_progress = HTTP2StateDataServer,
105 .to_server_progress = HTTP2StateDataClient },
110 AppProto alproto,
int direction,
int to_client_progress,
int to_server_progress)
158 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
189 #define CASE_CODE_STRING(E, S) case E: return S; break
200 #undef CASE_CODE_STRING
204 #define CASE_CODE(E) case E: return #E
222 Signature *s,
const char *arg,
int sm_type,
int sm_list,
228 if (arg != NULL && strcmp(arg,
"") != 0) {
237 "with a sticky buffer still set. Reset sticky buffer "
238 "with pkt_data before using the modifier.",
252 "found inside the rule without a content context. "
253 "Please use a \"content\" keyword before using the "
261 "be used with the rawbytes rule keyword",
267 "be used with the replace rule keyword",
306 bool reuse_buffer =
false;
319 SCLogError(
"failed to expand rule buffer array");
328 SCLogDebug(
"idx %u list %d set up curbuf %p s->init_data->buffer_index %u",
369 if (sm->
ctx != NULL) {
380 ptrdiff_t
offset = e - table;
394 if (st->
name != NULL) {
427 if (strcmp(
str,
"all") == 0) {
439 char *xsaveptr = NULL;
440 char *key = strtok_r(copy,
",", &xsaveptr);
441 while (key != NULL) {
447 "argument '%s' not found",
450 key = strtok_r(NULL,
",", &xsaveptr);
477 SCLogDebug(
"s:%p new:%p list:%d: %s, s->init_data->list_set %s s->init_data->list %d", s,
new,
511 SCLogDebug(
"reusing buffer %u as it isn't multi-capable", x);
521 SCLogError(
"failed to expand rule buffer array");
545 SCLogDebug(
"appended %s to list %d, rule pos %u (s->init_data->list %d)",
564 if (sm->
prev != NULL)
566 if (sm->
next != NULL)
608 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
620 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
642 SCLogDebug(
"skip x %u s->init_data->list %d (int)s->init_data->buffers[x].id %d", x,
651 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
655 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
672 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
677 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
702 va_start(ap, sm_list);
704 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
706 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
709 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
740 va_start(ap, list_id);
742 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
746 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
758 va_start(ap, list_id);
760 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
761 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
764 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
787 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
795 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
802 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
808 if (sm->
prev != NULL)
810 if (sm->
next != NULL)
813 if (sm == *src_sm_list)
814 *src_sm_list = sm->
next;
815 if (sm == *src_sm_list_tail)
816 *src_sm_list_tail = sm->
prev;
818 if (*dst_sm_list == NULL) {
820 *dst_sm_list_tail = sm;
828 *dst_sm_list_tail = sm;
855 SCLogError(
"Unable to find the sm in any of the "
861 size_t output_size,
bool requires)
864 char *optname = NULL;
865 char *optvalue = NULL;
868 while (isblank(*optstr)) {
873 char *optend = optstr;
875 optend = strchr(optend,
';');
876 if (optend == NULL) {
880 else if (optend > optstr && *(optend -1 ) ==
'\\') {
889 char *optvalptr = strchr(optstr,
':');
891 *(optvalptr++) =
'\0';
894 for (
size_t i = strlen(optvalptr); i > 0; i--) {
895 if (isblank(optvalptr[i - 1])) {
896 optvalptr[i - 1] =
'\0';
902 optvalue = optvalptr;
906 for (
size_t i = strlen(optstr); i > 0; i--) {
907 if (isblank(optstr[i - 1])) {
908 optstr[i - 1] =
'\0';
917 bool requires_only = strcasecmp(optname,
"requires") == 0 || strcasecmp(optname,
"sid") == 0;
918 if ((requires && !requires_only) || (!requires && requires_only)) {
923 st = SigTableGet(optname);
924 if (st == NULL || st->
Setup == NULL) {
925 SCLogError(
"unknown rule keyword '%s'.", optname);
930 if (optvalue == NULL || strlen(optvalue) == 0) {
932 "invalid formatting or malformed option to %s keyword: '%s'", optname, optstr);
936 if (optvalue && strlen(optvalue)) {
937 SCLogError(
"unexpected option to %s keyword: '%s'", optname, optstr);
944 #define URL "https://suricata.io/our-story/deprecation-policy/"
947 "and will be removed soon. See %s",
951 "and will be removed soon. Use '%s' instead. "
960 if (optvalue != NULL && strlen(optvalue) > 0) {
961 size_t ovlen = strlen(optvalue);
962 char *ptr = optvalue;
972 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
991 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
997 SCLogError(
"invalid formatting to %s keyword: "
998 "value must be double quoted \'%s\'",
1004 && ovlen && *ptr ==
'"')
1006 for (; ovlen > 0; ovlen--) {
1007 if (isblank(ptr[ovlen - 1])) {
1008 ptr[ovlen - 1] =
'\0';
1013 if (ovlen && ptr[ovlen - 1] !=
'"') {
1014 SCLogError(
"bad option value formatting (possible missing semicolon) "
1015 "for keyword %s: \'%s\'",
1023 ptr[ovlen - 1] =
'\0';
1028 "for keyword %s: \'%s\'",
1035 "quotes on %s keyword that doesn't support them: \'%s\'", optname, optstr);
1045 if (setup_ret < 0) {
1049 if (setup_ret == -2) {
1062 if (strlen(optend) > 0) {
1063 strlcpy(output, optend, output_size);
1078 Signature *s,
const char *addrstr,
char flag)
1080 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
1084 if (strcasecmp(addrstr,
"any") == 0)
1092 if (strcasecmp(addrstr,
"any") == 0)
1119 static int SigParseProto(
Signature *s,
const char *protostr)
1134 "in a signature. Either detection for this protocol "
1135 "is not yet supported OR detection has been disabled for "
1136 "protocol through the yaml option "
1137 "app-layer.protocols.%s.detection-enabled",
1138 protostr, protostr);
1167 Signature *s,
const char *portstr,
char flag)
1173 SCLogDebug(
"Port group \"%s\" to be parsed", portstr);
1176 if (strcasecmp(portstr,
"any") == 0)
1180 }
else if (flag == 1) {
1181 if (strcasecmp(portstr,
"any") == 0)
1196 static int SigParseActionRejectValidate(
const char *action)
1198 #ifdef HAVE_LIBNET11
1199 #if defined HAVE_LIBCAP_NG && !defined HAVE_LIBNET_CAPABILITIES
1202 "incompatible with POSIX based capabilities with privs dropping. "
1203 "For rejects to work, run as root/super user.");
1209 "required for action \"%s\" but is not compiled into Suricata",
1227 static int SigParseAction(
Signature *s,
const char *action)
1229 if (strcasecmp(action,
"alert") == 0) {
1231 }
else if (strcasecmp(action,
"drop") == 0) {
1233 }
else if (strcasecmp(action,
"pass") == 0) {
1235 }
else if (strcasecmp(action,
"reject") == 0 ||
1236 strcasecmp(action,
"rejectsrc") == 0)
1238 if (!(SigParseActionRejectValidate(action)))
1241 }
else if (strcasecmp(action,
"rejectdst") == 0) {
1242 if (!(SigParseActionRejectValidate(action)))
1245 }
else if (strcasecmp(action,
"rejectboth") == 0) {
1246 if (!(SigParseActionRejectValidate(action)))
1249 }
else if (strcasecmp(action,
"config") == 0) {
1252 SCLogError(
"An invalid action \"%s\" was given", action);
1269 static inline int SigParseToken(
char **input,
char *output,
1270 const size_t output_size)
1272 size_t len = *input == NULL ? 0 : strlen(*input);
1278 while (
len && isblank(**input)) {
1283 char *endptr = strpbrk(*input,
" \t\n\r");
1284 if (endptr != NULL) {
1287 strlcpy(output, *input, output_size);
1304 static inline int SigParseList(
char **input,
char *output,
1305 const size_t output_size)
1308 size_t len = *input != NULL ? strlen(*input) : 0;
1314 while (
len && isblank(**input)) {
1320 for (i = 0; i <
len; i++) {
1321 char c = (*input)[i];
1324 }
else if (c ==
']') {
1326 }
else if (c ==
' ') {
1337 strlcpy(output, *input, output_size);
1338 *input = *input + i + 1;
1358 SigParseToken(&index, parser->
action,
sizeof(parser->
action));
1364 SigParseList(&index, parser->
src,
sizeof(parser->
src));
1367 SigParseList(&index, parser->
sp,
sizeof(parser->
sp));
1373 SigParseList(&index, parser->
dst,
sizeof(parser->
dst));
1376 SigParseList(&index, parser->
dp,
sizeof(parser->
dp));
1379 if (index == NULL) {
1383 while (isspace(*index) || *index ==
'(') {
1386 for (
size_t i = strlen(index); i > 0; i--) {
1387 if (isspace(index[i - 1]) || index[i - 1] ==
')') {
1388 index[i - 1] =
'\0';
1400 if (SigParseAction(s, parser->
action) < 0)
1403 if (SigParseProto(s, parser->
protocol) < 0)
1406 if (strcmp(parser->
direction,
"<>") == 0) {
1408 }
else if (strcmp(parser->
direction,
"->") != 0) {
1409 SCLogError(
"\"%s\" is not a valid direction modifier, "
1410 "\"->\" and \"<>\" are supported.",
1437 static inline bool CheckAscii(
const char *
str)
1439 for (
size_t i = 0; i < strlen(
str); i++) {
1440 if (
str[i] < 0x20) {
1442 if (
str[i] == 0x0a ||
str[i] == 0x0d ||
str[i] == 0x09) {
1446 }
else if (
str[i] == 0x7f) {
1470 if (!rs_check_utf8(sigstr)) {
1475 if (!CheckAscii(sigstr)) {
1476 SCLogError(
"rule contains invalid (control) characters");
1480 int ret = SigParseBasics(
de_ctx, s, sigstr, parser, addrs_direction, requires);
1487 if (strlen(parser->
opts) > 0) {
1488 size_t buffer_size = strlen(parser->
opts) + 1;
1489 char input[buffer_size];
1490 char output[buffer_size];
1491 memset(input, 0x00, buffer_size);
1492 memcpy(input, parser->
opts, strlen(parser->
opts) + 1);
1498 memset(output, 0x00, buffer_size);
1499 ret = SigParseOptions(
de_ctx, s, input, output, buffer_size, requires);
1501 memcpy(input, output, buffer_size);
1533 memset(b, 0,
sizeof(*b));
1576 static void SigMetadataFree(
Signature *s)
1583 if (s == NULL || s->
metadata == NULL) {
1590 next_mdata = mdata->
next;
1621 next_ref = ref->
next;
1675 while (sm != NULL) {
1684 while (sm != NULL) {
1705 if (s->
sp != NULL) {
1708 if (s->
dp != NULL) {
1746 SCLogError(
"transforms must directly follow stickybuffers");
1766 if (!AppProtoIsValid(alproto)) {
1772 alproto = AppProtoCommon(s->
alproto, alproto);
1774 SCLogError(
"can't set rule app proto to %s: already set to %s",
1801 if (addr_match4 == NULL) {
1807 addr_match4[idx].
ip =
SCNtohl(da->ip.addr_data32[0]);
1808 addr_match4[idx].
ip2 =
SCNtohl(da->ip2.addr_data32[0]);
1827 if (addr_match6 == NULL) {
1833 addr_match6[idx].
ip[0] =
SCNtohl(da->ip.addr_data32[0]);
1834 addr_match6[idx].
ip[1] =
SCNtohl(da->ip.addr_data32[1]);
1835 addr_match6[idx].
ip[2] =
SCNtohl(da->ip.addr_data32[2]);
1836 addr_match6[idx].
ip[3] =
SCNtohl(da->ip.addr_data32[3]);
1837 addr_match6[idx].
ip2[0] =
SCNtohl(da->ip2.addr_data32[0]);
1838 addr_match6[idx].
ip2[1] =
SCNtohl(da->ip2.addr_data32[1]);
1839 addr_match6[idx].
ip2[2] =
SCNtohl(da->ip2.addr_data32[2]);
1840 addr_match6[idx].
ip2[3] =
SCNtohl(da->ip2.addr_data32[3]);
1853 static void SigBuildAddressMatchArray(
Signature *s)
1870 static int SigMatchListLen(
SigMatch *sm)
1873 for (; sm != NULL; sm = sm->
next)
1884 int len = SigMatchListLen(
head);
1890 FatalError(
"initializing the detection engine failed");
1896 for (; sm != NULL; sm = sm->
next, smd++) {
1918 uint32_t sig_flags = 0;
1923 nlists += (nlists > 0);
1927 SCLogError(
"rule %u setup buffer %s but didn't add matches to it", s->
id,
1932 bool has_frame =
false;
1933 bool has_app =
false;
1934 bool has_pkt =
false;
1935 bool has_pmatch =
false;
1945 struct BufferVsDir {
1948 } bufdir[nlists + 1];
1949 memset(&bufdir, 0, (nlists + 1) *
sizeof(
struct BufferVsDir));
1963 if (b->
head == NULL) {
1968 has_frame |= bt->
frame;
1969 has_app |= (bt->
frame ==
false && bt->
packet ==
false);
1974 "specific matches (like dsize, flags, ttl) with stream / "
1975 "state matching by matching on app layer proto (like using "
1976 "http_* keywords).");
1981 for (; app != NULL; app = app->
next) {
1988 bufdir[b->
id].ts += (app->
dir == 0);
1989 bufdir[b->
id].tc += (app->
dir == 1);
2008 for (
int x = 0; x < nlists; x++) {
2009 if (bufdir[x].
ts == 0 && bufdir[x].tc == 0)
2011 ts_excl += (bufdir[x].ts > 0 && bufdir[x].tc == 0);
2012 tc_excl += (bufdir[x].ts == 0 && bufdir[x].tc > 0);
2013 dir_amb += (bufdir[x].ts > 0 && bufdir[x].tc > 0);
2018 if (ts_excl && tc_excl) {
2019 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2021 }
else if (ts_excl) {
2022 SCLogDebug(
"%u: implied rule direction is toserver", s->
id);
2024 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2027 }
else if (tc_excl) {
2028 SCLogDebug(
"%u: implied rule direction is toclient", s->
id);
2030 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2033 }
else if (dir_amb) {
2034 SCLogDebug(
"%u: rule direction cannot be deduced from keywords", s->
id);
2040 "tcp-stream or flow:only_stream. Invalidating signature.");
2045 SCLogError(
"You seem to have mixed keywords "
2046 "that require inspection in both directions. Atm we only "
2047 "support keywords in one direction within a rule.");
2051 if (has_pmatch && has_frame) {
2052 SCLogError(
"can't mix pure content and frame inspection");
2055 if (has_app && has_frame) {
2056 SCLogError(
"can't mix app-layer buffer and frame inspection");
2059 if (has_pkt && has_frame) {
2060 SCLogError(
"can't mix pkt buffer and frame inspection");
2068 if (s->
proto.
proto[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) {
2105 "support file matching",
2110 SCLogError(
"protocol HTTP2 doesn't support file name matching");
2130 memset(&parser, 0x00,
sizeof(parser));
2148 int ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
true);
2155 }
else if (ret < 0) {
2161 SCLogError(
"Signature missing required value \"sid\".");
2166 ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
false);
2172 }
else if (ret == -2) {
2175 }
else if (ret < 0) {
2180 if (sig->
prio == -1)
2187 int override_needed = 0;
2191 override_needed = 1;
2193 override_needed = 1;
2195 for (s = 0; s <
sizeof(sig->
proto.
proto); s++) {
2197 override_needed = 0;
2206 if (override_needed)
2216 for ( ; sm != NULL; sm = sm->
next) {
2232 SCLogDebug(
"sig %"PRIu32
" SIG_FLAG_APPLAYER: %s, SIG_FLAG_PACKET: %s",
2236 SigBuildAddressMatchArray(sig);
2248 if (SigValidate(
de_ctx, sig) == 0) {
2278 static bool SigHasSameSourceAndDestination(
const Signature *s)
2330 if (SigHasSameSourceAndDestination(sig)) {
2331 SCLogInfo(
"Rule with ID %u is bidirectional, but source and destination are the same, "
2332 "treating the rule as unidirectional", sig->
id);
2337 if (sig->
next == NULL) {
2362 static void DetectParseDupSigFreeFunc(
void *data)
2378 static uint32_t DetectParseDupSigHashFunc(
HashListTable *ht,
void *data, uint16_t datalen)
2397 static char DetectParseDupSigCompareFunc(
void *data1, uint16_t len1,
void *data2,
2403 if (sw1 == NULL || sw2 == NULL ||
2404 sw1->
s == NULL || sw2->
s == NULL)
2408 if (sw1->
s->
id == sw2->
s->
id && sw1->
s->
gid == sw2->
s->
gid)
return 1;
2424 DetectParseDupSigHashFunc,
2425 DetectParseDupSigCompareFunc,
2426 DetectParseDupSigFreeFunc);
2487 if (sw_dup == NULL) {
2500 (
void *)&sw_tmp, 0);
2512 if (sw->
s->
rev <= sw_dup->
s->
rev) {
2521 if (sw_dup->
s_prev == NULL) {
2529 sw_temp.
s = sw_dup->
s->
next;
2533 if (sw_temp.
s != NULL) {
2535 (
void *)&sw_temp, 0);
2555 sw_temp.
s = sw_dup->
s->
next;
2563 if (sw_temp.
s != NULL) {
2565 (
void *)&sw_temp, 0);
2580 (
void *)&sw_tmp, 0);
2581 if (sw_old->
s != sw_dup->
s) {
2624 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
2628 SCLogError(
"Duplicate signature \"%s\"", sigstr);
2630 }
else if (dup_sig == 2) {
2632 " so the older sig replaced by this new signature \"%s\"",
2637 if (sig->
next != NULL) {
2654 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
2658 if (sig != NULL && sig->
next != NULL) {
2671 int start_offset,
int options)
2673 *match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
2675 return pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)
str, strlen(
str), options, start_offset,
2676 *match, parse_regex->
context);
2683 pcre2_code_free(r->
regex);
2686 pcre2_match_context_free(r->
context);
2701 g_detect_parse_regex_list = NULL;
2710 FatalError(
"failed to alloc memory for pcre free list");
2713 r->
next = g_detect_parse_regex_list;
2714 g_detect_parse_regex_list = r;
2722 detect_parse->
regex =
2723 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2724 if (detect_parse->
regex == NULL) {
2725 PCRE2_UCHAR errbuffer[256];
2726 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2727 SCLogError(
"pcre compile of \"%s\" failed at "
2729 parse_str, en, errbuffer);
2732 detect_parse->
context = pcre2_match_context_create(NULL);
2733 if (detect_parse->
context == NULL) {
2734 SCLogError(
"pcre2 could not create match context");
2735 pcre2_code_free(detect_parse->
regex);
2736 detect_parse->
regex = NULL;
2751 if (detect_parse == NULL) {
2755 detect_parse->
regex =
2756 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2757 if (detect_parse->
regex == NULL) {
2758 PCRE2_UCHAR errbuffer[256];
2759 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2760 SCLogError(
"pcre2 compile of \"%s\" failed at "
2762 parse_str, (
int)eo, errbuffer);
2767 detect_parse->
next = g_detect_parse_regex_list;
2768 g_detect_parse_regex_list = detect_parse;
2769 return detect_parse;
2773 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
2775 int r = pcre2_substring_copy_bynumber(match_data, number, buffer, bufflen);
2776 if (r == PCRE2_ERROR_UNSET) {
2785 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
2787 int r = pcre2_substring_get_bynumber(match_data, number, bufferptr, bufflen);
2788 if (r == PCRE2_ERROR_UNSET) {
2812 static int SigParseTest01 (
void)
2821 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
2831 static int SigParseTest02 (
void)
2845 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;)");
2873 static int SigParseTest03 (
void)
2882 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any <- !1.2.3.4 any (msg:\"SigParseTest03\"; sid:1;)");
2885 printf(
"expected NULL got sig ptr %p: ",sig);
2894 static int SigParseTest04 (
void)
2903 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024: -> !1.2.3.4 1024: (msg:\"SigParseTest04\"; sid:1;)");
2914 static int SigParseTest05 (
void)
2923 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024:65536 -> !1.2.3.4 any (msg:\"SigParseTest05\"; sid:1;)");
2927 printf(
"signature didn't fail to parse as we expected: ");
2937 static int SigParseTest06 (
void)
2946 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;)");
2950 printf(
"signature failed to parse: ");
2964 static int SigParseTest07(
void)
2986 static int SigParseTest08(
void)
3009 static int SigParseTest09(
void)
3060 static int SigParseTest10(
void)
3092 static int SigParseTest11(
void)
3103 "drop tcp any any -> any 80 (msg:\"Snort_Inline is blocking the http link\"; sid:1;) ");
3105 printf(
"sig 1 didn't parse: ");
3110 "the http link\"; sid:2;) ");
3112 printf(
"sig 2 didn't parse: ");
3126 static int SigParseTest12(
void)
3138 printf(
"sig 1 should have given an error: ");
3152 static int SigParseTest13(
void)
3164 printf(
"sig 1 invalidated: failure");
3169 printf(
"sig doesn't have stream flag set\n");
3174 printf(
"sig has packet flag set\n");
3189 static int SigParseTest14(
void)
3201 printf(
"sig 1 invalidated: failure");
3206 printf(
"sig doesn't have packet flag set\n");
3211 printf(
"sig has stream flag set\n");
3226 static int SigParseTest15(
void)
3238 printf(
"sig 1 invalidated: failure");
3243 printf(
"sig doesn't have packet flag set\n");
3248 printf(
"sig doesn't have stream flag set\n");
3263 static int SigParseTest16(
void)
3275 printf(
"sig 1 invalidated: failure");
3280 printf(
"sig doesn't have packet flag set\n");
3285 printf(
"sig doesn't have stream flag set\n");
3300 static int SigParseTest17(
void)
3312 printf(
"sig 1 invalidated: failure");
3317 printf(
"sig doesn't have packet flag set\n");
3322 printf(
"sig doesn't have stream flag set\n");
3335 static int SigParseTest18 (
void)
3343 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:99999999999999999999;)") != NULL)
3354 static int SigParseTest19 (
void)
3362 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; gid:99999999999999999999;)") != NULL)
3373 static int SigParseTest20 (
void)
3381 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; rev:99999999999999999999;)") != NULL)
3392 static int SigParseTest21 (
void)
3411 static int SigParseTest22 (
void)
3419 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)
3432 static int SigParseTest23(
void)
3447 static int SigParseBidirecTest06 (
void)
3467 static int SigParseBidirecTest07 (
void)
3487 static int SigParseBidirecTest08 (
void)
3507 static int SigParseBidirecTest09 (
void)
3527 static int SigParseBidirecTest10 (
void)
3547 static int SigParseBidirecTest11 (
void)
3567 static int SigParseBidirecTest12 (
void)
3587 static int SigParseBidirecTest13 (
void)
3606 static int SigParseBidirecTest14 (
void)
3627 static int SigTestBidirec01 (
void)
3639 if (sig->
next != NULL)
3658 static int SigTestBidirec02 (
void)
3677 if (sig->
next == NULL)
3682 if (copy->
next != NULL)
3703 static int SigTestBidirec03 (
void)
3715 const char *sigs[3];
3716 sigs[0] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 1\"; sid:1;)";
3717 sigs[1] =
"alert tcp any any <> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)";
3718 sigs[2] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 3\"; sid:3;)";
3725 if (sig->
next == NULL)
3736 uint8_t rawpkt1_ether[] = {
3737 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3738 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3739 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3740 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3741 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3742 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3743 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3744 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3745 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3746 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3747 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3748 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3749 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3750 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3751 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3752 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3753 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3754 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3755 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3756 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3757 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3758 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3759 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3760 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3761 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3762 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3763 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3764 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3765 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3766 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3767 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3768 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3769 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3770 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3771 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3772 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3773 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3774 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3775 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3776 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3777 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3778 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3779 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3780 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3781 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3782 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3783 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3784 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3785 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3786 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3787 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3788 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3789 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3790 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3791 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3801 uint32_t sids[3] = {1, 2, 3};
3802 uint32_t results[3] = {1, 1, 1};
3818 static int SigTestBidirec04 (
void)
3833 sig =
DetectEngineAppendSig(
de_ctx,
"alert tcp 192.168.1.1 any <> any any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)");
3838 if (sig->
next == NULL)
3850 if (sig->
next == NULL)
3861 uint8_t rawpkt1_ether[] = {
3862 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3863 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3864 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3865 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3866 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3867 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3868 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3869 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3870 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3871 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3872 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3873 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3874 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3875 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3876 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3877 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3878 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3879 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3880 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3881 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3882 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3883 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3884 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3885 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3886 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3887 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3888 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3889 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3890 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3891 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3892 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3893 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3894 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3895 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3896 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3897 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3898 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3899 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3900 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3901 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3902 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3903 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3904 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3905 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3906 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3907 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3908 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3909 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3910 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3911 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3912 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3913 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3914 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3915 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3916 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3925 memset(&th_v, 0,
sizeof(th_v));
3965 static int SigParseTestNegation01 (
void)
3979 static int SigParseTestNegation02 (
void)
3990 s =
SigInit(
de_ctx,
"alert tcp any !any -> any any (msg:\"SigTest41-02 src ip is !any \"; classtype:misc-activity; sid:410002; rev:1;)");
4005 static int SigParseTestNegation03 (
void)
4016 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;)");
4031 static int SigParseTestNegation04 (
void)
4042 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;)");
4057 static int SigParseTestNegation05 (
void)
4068 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;)");
4083 static int SigParseTestNegation06 (
void)
4094 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;)");
4110 static int SigParseTestNegation07 (
void)
4116 de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.0/24] any (sid:410006;)");
4125 static int SigParseTestNegation08 (
void)
4136 s =
SigInit(
de_ctx,
"alert tcp any any -> [192.168.0.0/16,!192.168.0.0/24] any (sid:410006; rev:1;)");
4151 static int SigParseTestMpm01 (
void)
4160 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; sid:1;)");
4162 printf(
"sig failed to init: ");
4167 printf(
"sig doesn't have content list: ");
4182 static int SigParseTestMpm02 (
void)
4191 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; content:\"abcdef\"; sid:1;)");
4193 printf(
"sig failed to init: ");
4198 printf(
"sig doesn't have content list: ");
4213 static int SigParseTestAppLayerTLS01(
void)
4224 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS01 \"; sid:410006; rev:1;)");
4226 printf(
"parsing sig failed: ");
4231 printf(
"alproto not set: ");
4248 static int SigParseTestAppLayerTLS02(
void)
4259 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS02 \"; tls.version:1.0; sid:410006; rev:1;)");
4261 printf(
"parsing sig failed: ");
4266 printf(
"alproto not set: ");
4282 static int SigParseTestAppLayerTLS03(
void)
4293 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS03 \"; tls.version:2.5; sid:410006; rev:1;)");
4306 static int SigParseTestUnbalancedQuotes01(
void)
4316 "alert http any any -> any any (msg:\"SigParseTestUnbalancedQuotes01\"; "
4317 "pcre:\"/\\/[a-z]+\\.php\\?[a-z]+?=\\d{7}&[a-z]+?=\\d{7,8}$/U\" "
4318 "flowbits:set,et.exploitkitlanding; classtype:trojan-activity; sid:2017078; rev:5;)");
4324 static int SigParseTestContentGtDsize01(
void)
4331 "alert http any any -> any any ("
4332 "dsize:21; content:\"0123456789001234567890|00 00|\"; "
4339 static int SigParseTestContentGtDsize02(
void)
4346 "alert http any any -> any any ("
4347 "dsize:21; content:\"0123456789|00 00|\"; offset:10; "
4364 static int SigParseBidirWithSameSrcAndDest01(
void)
4381 "alert tcp [1.2.3.4, 5.6.7.8] [80, 81] <> [5.6.7.8, 1.2.3.4] [81, 80] (sid:3;)");
4390 static int SigParseBidirWithSameSrcAndDest02(
void)
4398 de_ctx,
"alert tcp 1.2.3.4 any <> [1.2.3.4, 5.6.7.8, ::1] any (sid:1;)");
4405 de_ctx,
"alert tcp [1.2.3.4, ::1] [80, 81, 82] <> [1.2.3.4, ::1] [80, 81] (sid:2;)");
4412 "alert tcp [1.2.3.4, ::1, ABCD:AAAA::1] [80] <> [1.2.3.4, ::1] [80, 81] (sid:3;)");
4419 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;)");
4426 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;)");
4435 static int SigParseTestActionReject(
void)
4441 de_ctx,
"reject tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4442 #ifdef HAVE_LIBNET11
4453 static int SigParseTestActionDrop(
void)
4459 de_ctx,
"drop tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4499 UtRegisterTest(
"SigParseTest21 -- address with space", SigParseTest21);
4500 UtRegisterTest(
"SigParseTest22 -- address with space", SigParseTest22);
4501 UtRegisterTest(
"SigParseTest23 -- carriage return", SigParseTest23);
4516 UtRegisterTest(
"SigParseTestNegation01", SigParseTestNegation01);
4517 UtRegisterTest(
"SigParseTestNegation02", SigParseTestNegation02);
4518 UtRegisterTest(
"SigParseTestNegation03", SigParseTestNegation03);
4519 UtRegisterTest(
"SigParseTestNegation04", SigParseTestNegation04);
4520 UtRegisterTest(
"SigParseTestNegation05", SigParseTestNegation05);
4521 UtRegisterTest(
"SigParseTestNegation06", SigParseTestNegation06);
4522 UtRegisterTest(
"SigParseTestNegation07", SigParseTestNegation07);
4523 UtRegisterTest(
"SigParseTestNegation08", SigParseTestNegation08);
4526 UtRegisterTest(
"SigParseTestAppLayerTLS01", SigParseTestAppLayerTLS01);
4527 UtRegisterTest(
"SigParseTestAppLayerTLS02", SigParseTestAppLayerTLS02);
4528 UtRegisterTest(
"SigParseTestAppLayerTLS03", SigParseTestAppLayerTLS03);
4529 UtRegisterTest(
"SigParseTestUnbalancedQuotes01", SigParseTestUnbalancedQuotes01);
4532 SigParseTestContentGtDsize01);
4534 SigParseTestContentGtDsize02);
4537 SigParseBidirWithSameSrcAndDest01);
4539 SigParseBidirWithSameSrcAndDest02);
4540 UtRegisterTest(
"SigParseTestActionReject", SigParseTestActionReject);
4541 UtRegisterTest(
"SigParseTestActionDrop", SigParseTestActionDrop);