85 int to_client_progress;
86 int to_server_progress;
87 } DetectFileHandlerProtocol_t;
88 static DetectFileHandlerProtocol_t al_protocols[] = {
95 .to_client_progress = HTP_RESPONSE_BODY,
96 .to_server_progress = HTP_REQUEST_BODY },
99 .to_client_progress = HTTP2StateDataServer,
100 .to_server_progress = HTTP2StateDataClient },
104 for (
size_t i = 0; i <
ARRAY_SIZE(al_protocols); i++) {
105 int direction = al_protocols[i].direction == 0
107 : al_protocols[i].direction;
111 reg->
GetData, al_protocols[i].al_proto, al_protocols[i].to_client_progress);
118 reg->
GetData, al_protocols[i].al_proto, al_protocols[i].to_server_progress);
131 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
162 #define CASE_CODE_STRING(E, S) case E: return S; break
173 #undef CASE_CODE_STRING
177 #define CASE_CODE(E) case E: return #E
195 Signature *s,
const char *arg,
int sm_type,
int sm_list,
201 if (arg != NULL && strcmp(arg,
"") != 0) {
210 "with a sticky buffer still set. Reset sticky buffer "
211 "with pkt_data before using the modifier.",
225 "found inside the rule without a content context. "
226 "Please use a \"content\" keyword before using the "
234 "be used with the rawbytes rule keyword",
240 "be used with the replace rule keyword",
279 bool reuse_buffer =
false;
292 SCLogError(
"failed to expand rule buffer array");
301 SCLogDebug(
"idx %u list %d set up curbuf %p s->init_data->buffer_index %u",
342 if (sm->
ctx != NULL) {
353 ptrdiff_t
offset = e - table;
367 if (st->
name != NULL) {
368 if (strcasecmp(name,st->
name) == 0)
370 if (st->
alias != NULL && strcasecmp(name,st->
alias) == 0)
400 if (strcmp(
str,
"all") == 0) {
412 char *xsaveptr = NULL;
413 char *key = strtok_r(copy,
",", &xsaveptr);
414 while (key != NULL) {
420 "argument '%s' not found",
423 key = strtok_r(NULL,
",", &xsaveptr);
450 SCLogDebug(
"s:%p new:%p list:%d: %s, s->init_data->list_set %s s->init_data->list %d", s,
new,
484 SCLogDebug(
"reusing buffer %u as it isn't multi-capable", x);
494 SCLogError(
"failed to expand rule buffer array");
518 SCLogDebug(
"appended %s to list %d, rule pos %u (s->init_data->list %d)",
537 if (sm->
prev != NULL)
539 if (sm->
next != NULL)
581 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
593 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
615 SCLogDebug(
"skip x %u s->init_data->list %d (int)s->init_data->buffers[x].id %d", x,
624 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
628 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
645 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
650 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
675 va_start(ap, sm_list);
677 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
679 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
682 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
713 va_start(ap, list_id);
715 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
719 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
731 va_start(ap, list_id);
733 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
734 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
737 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
760 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
768 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
775 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
781 if (sm->
prev != NULL)
783 if (sm->
next != NULL)
786 if (sm == *src_sm_list)
787 *src_sm_list = sm->
next;
788 if (sm == *src_sm_list_tail)
789 *src_sm_list_tail = sm->
prev;
791 if (*dst_sm_list == NULL) {
793 *dst_sm_list_tail = sm;
801 *dst_sm_list_tail = sm;
828 SCLogError(
"Unable to find the sm in any of the "
834 size_t output_size,
bool requires)
837 char *optname = NULL;
838 char *optvalue = NULL;
841 while (isblank(*optstr)) {
846 char *optend = optstr;
848 optend = strchr(optend,
';');
849 if (optend == NULL) {
853 else if (optend > optstr && *(optend -1 ) ==
'\\') {
862 char *optvalptr = strchr(optstr,
':');
864 *(optvalptr++) =
'\0';
867 for (
size_t i = strlen(optvalptr); i > 0; i--) {
868 if (isblank(optvalptr[i - 1])) {
869 optvalptr[i - 1] =
'\0';
875 optvalue = optvalptr;
879 for (
size_t i = strlen(optstr); i > 0; i--) {
880 if (isblank(optstr[i - 1])) {
881 optstr[i - 1] =
'\0';
890 bool requires_only = strcasecmp(optname,
"requires") == 0 || strcasecmp(optname,
"sid") == 0;
891 if ((requires && !requires_only) || (!requires && requires_only)) {
896 st = SigTableGet(optname);
897 if (st == NULL || st->
Setup == NULL) {
898 SCLogError(
"unknown rule keyword '%s'.", optname);
903 if (optvalue == NULL || strlen(optvalue) == 0) {
905 "invalid formatting or malformed option to %s keyword: '%s'", optname, optstr);
909 if (optvalue && strlen(optvalue)) {
910 SCLogError(
"unexpected option to %s keyword: '%s'", optname, optstr);
917 #define URL "https://suricata.io/our-story/deprecation-policy/"
920 "and will be removed soon. See %s",
924 "and will be removed soon. Use '%s' instead. "
933 if (optvalue != NULL && strlen(optvalue) > 0) {
934 size_t ovlen = strlen(optvalue);
935 char *ptr = optvalue;
945 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
964 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
970 SCLogError(
"invalid formatting to %s keyword: "
971 "value must be double quoted \'%s\'",
977 && ovlen && *ptr ==
'"')
979 for (; ovlen > 0; ovlen--) {
980 if (isblank(ptr[ovlen - 1])) {
981 ptr[ovlen - 1] =
'\0';
986 if (ovlen && ptr[ovlen - 1] !=
'"') {
987 SCLogError(
"bad option value formatting (possible missing semicolon) "
988 "for keyword %s: \'%s\'",
996 ptr[ovlen - 1] =
'\0';
1001 "for keyword %s: \'%s\'",
1008 "quotes on %s keyword that doesn't support them: \'%s\'", optname, optstr);
1018 if (setup_ret < 0) {
1022 if (setup_ret == -2) {
1035 if (strlen(optend) > 0) {
1036 strlcpy(output, optend, output_size);
1051 Signature *s,
const char *addrstr,
char flag)
1053 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
1057 if (strcasecmp(addrstr,
"any") == 0)
1065 if (strcasecmp(addrstr,
"any") == 0)
1092 static int SigParseProto(
Signature *s,
const char *protostr)
1107 "in a signature. Either detection for this protocol "
1108 "is not yet supported OR detection has been disabled for "
1109 "protocol through the yaml option "
1110 "app-layer.protocols.%s.detection-enabled",
1111 protostr, protostr);
1140 Signature *s,
const char *portstr,
char flag)
1146 SCLogDebug(
"Port group \"%s\" to be parsed", portstr);
1149 if (strcasecmp(portstr,
"any") == 0)
1153 }
else if (flag == 1) {
1154 if (strcasecmp(portstr,
"any") == 0)
1169 static int SigParseActionRejectValidate(
const char *action)
1171 #ifdef HAVE_LIBNET11
1172 #if defined HAVE_LIBCAP_NG && !defined HAVE_LIBNET_CAPABILITIES
1175 "incompatible with POSIX based capabilities with privs dropping. "
1176 "For rejects to work, run as root/super user.");
1182 "required for action \"%s\" but is not compiled into Suricata",
1200 static int SigParseAction(
Signature *s,
const char *action)
1202 if (strcasecmp(action,
"alert") == 0) {
1204 }
else if (strcasecmp(action,
"drop") == 0) {
1206 }
else if (strcasecmp(action,
"pass") == 0) {
1208 }
else if (strcasecmp(action,
"reject") == 0 ||
1209 strcasecmp(action,
"rejectsrc") == 0)
1211 if (!(SigParseActionRejectValidate(action)))
1214 }
else if (strcasecmp(action,
"rejectdst") == 0) {
1215 if (!(SigParseActionRejectValidate(action)))
1218 }
else if (strcasecmp(action,
"rejectboth") == 0) {
1219 if (!(SigParseActionRejectValidate(action)))
1222 }
else if (strcasecmp(action,
"config") == 0) {
1225 SCLogError(
"An invalid action \"%s\" was given", action);
1242 static inline int SigParseToken(
char **input,
char *output,
1243 const size_t output_size)
1245 size_t len = *input == NULL ? 0 : strlen(*input);
1251 while (
len && isblank(**input)) {
1256 char *endptr = strpbrk(*input,
" \t\n\r");
1257 if (endptr != NULL) {
1260 strlcpy(output, *input, output_size);
1277 static inline int SigParseList(
char **input,
char *output,
1278 const size_t output_size)
1281 size_t len = *input != NULL ? strlen(*input) : 0;
1287 while (
len && isblank(**input)) {
1293 for (i = 0; i <
len; i++) {
1294 char c = (*input)[i];
1297 }
else if (c ==
']') {
1299 }
else if (c ==
' ') {
1310 strlcpy(output, *input, output_size);
1311 *input = *input + i + 1;
1331 SigParseToken(&index, parser->
action,
sizeof(parser->
action));
1337 SigParseList(&index, parser->
src,
sizeof(parser->
src));
1340 SigParseList(&index, parser->
sp,
sizeof(parser->
sp));
1346 SigParseList(&index, parser->
dst,
sizeof(parser->
dst));
1349 SigParseList(&index, parser->
dp,
sizeof(parser->
dp));
1352 if (index == NULL) {
1356 while (isspace(*index) || *index ==
'(') {
1359 for (
size_t i = strlen(index); i > 0; i--) {
1360 if (isspace(index[i - 1]) || index[i - 1] ==
')') {
1361 index[i - 1] =
'\0';
1373 if (SigParseAction(s, parser->
action) < 0)
1376 if (SigParseProto(s, parser->
protocol) < 0)
1379 if (strcmp(parser->
direction,
"<>") == 0) {
1381 }
else if (strcmp(parser->
direction,
"->") != 0) {
1382 SCLogError(
"\"%s\" is not a valid direction modifier, "
1383 "\"->\" and \"<>\" are supported.",
1410 static inline bool CheckAscii(
const char *
str)
1412 for (
size_t i = 0; i < strlen(
str); i++) {
1413 if (
str[i] < 0x20) {
1415 if (
str[i] == 0x0a ||
str[i] == 0x0d ||
str[i] == 0x09) {
1419 }
else if (
str[i] == 0x7f) {
1443 if (!rs_check_utf8(sigstr)) {
1448 if (!CheckAscii(sigstr)) {
1449 SCLogError(
"rule contains invalid (control) characters");
1453 int ret = SigParseBasics(
de_ctx, s, sigstr, parser, addrs_direction, requires);
1460 if (strlen(parser->
opts) > 0) {
1461 size_t buffer_size = strlen(parser->
opts) + 1;
1462 char input[buffer_size];
1463 char output[buffer_size];
1464 memset(input, 0x00, buffer_size);
1465 memcpy(input, parser->
opts, strlen(parser->
opts) + 1);
1471 memset(output, 0x00, buffer_size);
1472 ret = SigParseOptions(
de_ctx, s, input, output, buffer_size, requires);
1474 memcpy(input, output, buffer_size);
1506 memset(b, 0,
sizeof(*b));
1549 static void SigMetadataFree(
Signature *s)
1556 if (s == NULL || s->
metadata == NULL) {
1563 next_mdata = mdata->
next;
1594 next_ref = ref->
next;
1648 while (sm != NULL) {
1657 while (sm != NULL) {
1678 if (s->
sp != NULL) {
1681 if (s->
dp != NULL) {
1719 SCLogError(
"transforms must directly follow stickybuffers");
1746 alproto = AppProtoCommon(s->
alproto, alproto);
1748 SCLogError(
"can't set rule app proto to %s: already set to %s",
1775 if (addr_match4 == NULL) {
1781 addr_match4[idx].
ip =
SCNtohl(da->ip.addr_data32[0]);
1782 addr_match4[idx].
ip2 =
SCNtohl(da->ip2.addr_data32[0]);
1801 if (addr_match6 == NULL) {
1807 addr_match6[idx].
ip[0] =
SCNtohl(da->ip.addr_data32[0]);
1808 addr_match6[idx].
ip[1] =
SCNtohl(da->ip.addr_data32[1]);
1809 addr_match6[idx].
ip[2] =
SCNtohl(da->ip.addr_data32[2]);
1810 addr_match6[idx].
ip[3] =
SCNtohl(da->ip.addr_data32[3]);
1811 addr_match6[idx].
ip2[0] =
SCNtohl(da->ip2.addr_data32[0]);
1812 addr_match6[idx].
ip2[1] =
SCNtohl(da->ip2.addr_data32[1]);
1813 addr_match6[idx].
ip2[2] =
SCNtohl(da->ip2.addr_data32[2]);
1814 addr_match6[idx].
ip2[3] =
SCNtohl(da->ip2.addr_data32[3]);
1827 static void SigBuildAddressMatchArray(
Signature *s)
1844 static int SigMatchListLen(
SigMatch *sm)
1847 for (; sm != NULL; sm = sm->
next)
1858 int len = SigMatchListLen(
head);
1864 FatalError(
"initializing the detection engine failed");
1870 for (; sm != NULL; sm = sm->
next, smd++) {
1892 uint32_t sig_flags = 0;
1897 nlists += (nlists > 0);
1901 SCLogError(
"rule %u setup buffer %s but didn't add matches to it", s->
id,
1906 bool has_frame =
false;
1907 bool has_app =
false;
1908 bool has_pkt =
false;
1909 bool has_pmatch =
false;
1919 struct BufferVsDir {
1922 } bufdir[nlists + 1];
1923 memset(&bufdir, 0, (nlists + 1) *
sizeof(
struct BufferVsDir));
1937 if (b->
head == NULL) {
1942 has_frame |= bt->
frame;
1943 has_app |= (bt->
frame ==
false && bt->
packet ==
false);
1948 "specific matches (like dsize, flags, ttl) with stream / "
1949 "state matching by matching on app layer proto (like using "
1950 "http_* keywords).");
1955 for (; app != NULL; app = app->
next) {
1962 bufdir[b->
id].ts += (app->
dir == 0);
1963 bufdir[b->
id].tc += (app->
dir == 1);
1979 for (
int x = 0; x < nlists; x++) {
1980 if (bufdir[x].
ts == 0 && bufdir[x].tc == 0)
1982 ts_excl += (bufdir[x].ts > 0 && bufdir[x].tc == 0);
1983 tc_excl += (bufdir[x].ts == 0 && bufdir[x].tc > 0);
1984 dir_amb += (bufdir[x].ts > 0 && bufdir[x].tc > 0);
1989 if (ts_excl && tc_excl) {
1990 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
1992 }
else if (ts_excl) {
1993 SCLogDebug(
"%u: implied rule direction is toserver", s->
id);
1995 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
1998 }
else if (tc_excl) {
1999 SCLogDebug(
"%u: implied rule direction is toclient", s->
id);
2001 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2004 }
else if (dir_amb) {
2005 SCLogDebug(
"%u: rule direction cannot be deduced from keywords", s->
id);
2011 "tcp-stream or flow:only_stream. Invalidating signature.");
2016 SCLogError(
"You seem to have mixed keywords "
2017 "that require inspection in both directions. Atm we only "
2018 "support keywords in one direction within a rule.");
2022 if (has_pmatch && has_frame) {
2023 SCLogError(
"can't mix pure content and frame inspection");
2026 if (has_app && has_frame) {
2027 SCLogError(
"can't mix app-layer buffer and frame inspection");
2030 if (has_pkt && has_frame) {
2031 SCLogError(
"can't mix pkt buffer and frame inspection");
2039 if (s->
proto.
proto[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) {
2076 "support file matching",
2081 SCLogError(
"protocol HTTP2 doesn't support file name matching");
2101 memset(&parser, 0x00,
sizeof(parser));
2119 int ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
true);
2126 }
else if (ret < 0) {
2132 SCLogError(
"Signature missing required value \"sid\".");
2137 ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
false);
2143 }
else if (ret == -2) {
2146 }
else if (ret < 0) {
2151 if (sig->
prio == -1)
2158 int override_needed = 0;
2162 override_needed = 1;
2164 override_needed = 1;
2166 for (s = 0; s <
sizeof(sig->
proto.
proto); s++) {
2168 override_needed = 0;
2177 if (override_needed)
2187 for ( ; sm != NULL; sm = sm->
next) {
2203 SCLogDebug(
"sig %"PRIu32
" SIG_FLAG_APPLAYER: %s, SIG_FLAG_PACKET: %s",
2207 SigBuildAddressMatchArray(sig);
2219 if (SigValidate(
de_ctx, sig) == 0) {
2249 static bool SigHasSameSourceAndDestination(
const Signature *s)
2301 if (SigHasSameSourceAndDestination(sig)) {
2302 SCLogInfo(
"Rule with ID %u is bidirectional, but source and destination are the same, "
2303 "treating the rule as unidirectional", sig->
id);
2308 if (sig->
next == NULL) {
2333 static void DetectParseDupSigFreeFunc(
void *data)
2349 static uint32_t DetectParseDupSigHashFunc(
HashListTable *ht,
void *data, uint16_t datalen)
2368 static char DetectParseDupSigCompareFunc(
void *data1, uint16_t len1,
void *data2,
2374 if (sw1 == NULL || sw2 == NULL ||
2375 sw1->
s == NULL || sw2->
s == NULL)
2379 if (sw1->
s->
id == sw2->
s->
id && sw1->
s->
gid == sw2->
s->
gid)
return 1;
2395 DetectParseDupSigHashFunc,
2396 DetectParseDupSigCompareFunc,
2397 DetectParseDupSigFreeFunc);
2458 if (sw_dup == NULL) {
2471 (
void *)&sw_tmp, 0);
2483 if (sw->
s->
rev <= sw_dup->
s->
rev) {
2492 if (sw_dup->
s_prev == NULL) {
2500 sw_temp.
s = sw_dup->
s->
next;
2504 if (sw_temp.
s != NULL) {
2506 (
void *)&sw_temp, 0);
2526 sw_temp.
s = sw_dup->
s->
next;
2534 if (sw_temp.
s != NULL) {
2536 (
void *)&sw_temp, 0);
2551 (
void *)&sw_tmp, 0);
2552 if (sw_old->
s != sw_dup->
s) {
2595 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
2599 SCLogError(
"Duplicate signature \"%s\"", sigstr);
2601 }
else if (dup_sig == 2) {
2603 " so the older sig replaced by this new signature \"%s\"",
2608 if (sig->
next != NULL) {
2625 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
2629 if (sig != NULL && sig->
next != NULL) {
2642 int start_offset,
int options)
2644 *match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
2646 return pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)
str, strlen(
str), options, start_offset,
2647 *match, parse_regex->
context);
2654 pcre2_code_free(r->
regex);
2657 pcre2_match_context_free(r->
context);
2672 g_detect_parse_regex_list = NULL;
2681 FatalError(
"failed to alloc memory for pcre free list");
2684 r->
next = g_detect_parse_regex_list;
2685 g_detect_parse_regex_list = r;
2693 detect_parse->
regex =
2694 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2695 if (detect_parse->
regex == NULL) {
2696 PCRE2_UCHAR errbuffer[256];
2697 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2698 SCLogError(
"pcre compile of \"%s\" failed at "
2700 parse_str, en, errbuffer);
2703 detect_parse->
context = pcre2_match_context_create(NULL);
2704 if (detect_parse->
context == NULL) {
2705 SCLogError(
"pcre2 could not create match context");
2706 pcre2_code_free(detect_parse->
regex);
2707 detect_parse->
regex = NULL;
2722 if (detect_parse == NULL) {
2726 detect_parse->
regex =
2727 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
2728 if (detect_parse->
regex == NULL) {
2729 PCRE2_UCHAR errbuffer[256];
2730 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
2731 SCLogError(
"pcre2 compile of \"%s\" failed at "
2733 parse_str, (
int)eo, errbuffer);
2738 detect_parse->
next = g_detect_parse_regex_list;
2739 g_detect_parse_regex_list = detect_parse;
2740 return detect_parse;
2744 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
2746 int r = pcre2_substring_copy_bynumber(match_data, number, buffer, bufflen);
2747 if (r == PCRE2_ERROR_UNSET) {
2756 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
2758 int r = pcre2_substring_get_bynumber(match_data, number, bufferptr, bufflen);
2759 if (r == PCRE2_ERROR_UNSET) {
2783 static int SigParseTest01 (
void)
2792 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
2802 static int SigParseTest02 (
void)
2816 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;)");
2844 static int SigParseTest03 (
void)
2853 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any <- !1.2.3.4 any (msg:\"SigParseTest03\"; sid:1;)");
2856 printf(
"expected NULL got sig ptr %p: ",sig);
2865 static int SigParseTest04 (
void)
2874 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024: -> !1.2.3.4 1024: (msg:\"SigParseTest04\"; sid:1;)");
2885 static int SigParseTest05 (
void)
2894 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024:65536 -> !1.2.3.4 any (msg:\"SigParseTest05\"; sid:1;)");
2898 printf(
"signature didn't fail to parse as we expected: ");
2908 static int SigParseTest06 (
void)
2917 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;)");
2921 printf(
"signature failed to parse: ");
2935 static int SigParseTest07(
void)
2957 static int SigParseTest08(
void)
2980 static int SigParseTest09(
void)
3031 static int SigParseTest10(
void)
3063 static int SigParseTest11(
void)
3074 "drop tcp any any -> any 80 (msg:\"Snort_Inline is blocking the http link\"; sid:1;) ");
3076 printf(
"sig 1 didn't parse: ");
3081 "the http link\"; sid:2;) ");
3083 printf(
"sig 2 didn't parse: ");
3097 static int SigParseTest12(
void)
3109 printf(
"sig 1 should have given an error: ");
3123 static int SigParseTest13(
void)
3135 printf(
"sig 1 invalidated: failure");
3140 printf(
"sig doesn't have stream flag set\n");
3145 printf(
"sig has packet flag set\n");
3160 static int SigParseTest14(
void)
3172 printf(
"sig 1 invalidated: failure");
3177 printf(
"sig doesn't have packet flag set\n");
3182 printf(
"sig has stream flag set\n");
3197 static int SigParseTest15(
void)
3209 printf(
"sig 1 invalidated: failure");
3214 printf(
"sig doesn't have packet flag set\n");
3219 printf(
"sig doesn't have stream flag set\n");
3234 static int SigParseTest16(
void)
3246 printf(
"sig 1 invalidated: failure");
3251 printf(
"sig doesn't have packet flag set\n");
3256 printf(
"sig doesn't have stream flag set\n");
3271 static int SigParseTest17(
void)
3283 printf(
"sig 1 invalidated: failure");
3288 printf(
"sig doesn't have packet flag set\n");
3293 printf(
"sig doesn't have stream flag set\n");
3306 static int SigParseTest18 (
void)
3314 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:99999999999999999999;)") != NULL)
3325 static int SigParseTest19 (
void)
3333 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; gid:99999999999999999999;)") != NULL)
3344 static int SigParseTest20 (
void)
3352 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; rev:99999999999999999999;)") != NULL)
3363 static int SigParseTest21 (
void)
3382 static int SigParseTest22 (
void)
3390 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)
3403 static int SigParseTest23(
void)
3418 static int SigParseBidirecTest06 (
void)
3438 static int SigParseBidirecTest07 (
void)
3458 static int SigParseBidirecTest08 (
void)
3478 static int SigParseBidirecTest09 (
void)
3498 static int SigParseBidirecTest10 (
void)
3518 static int SigParseBidirecTest11 (
void)
3538 static int SigParseBidirecTest12 (
void)
3558 static int SigParseBidirecTest13 (
void)
3577 static int SigParseBidirecTest14 (
void)
3598 static int SigTestBidirec01 (
void)
3610 if (sig->
next != NULL)
3629 static int SigTestBidirec02 (
void)
3648 if (sig->
next == NULL)
3653 if (copy->
next != NULL)
3674 static int SigTestBidirec03 (
void)
3686 const char *sigs[3];
3687 sigs[0] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 1\"; sid:1;)";
3688 sigs[1] =
"alert tcp any any <> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)";
3689 sigs[2] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 3\"; sid:3;)";
3696 if (sig->
next == NULL)
3707 uint8_t rawpkt1_ether[] = {
3708 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3709 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3710 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3711 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3712 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3713 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3714 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3715 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3716 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3717 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3718 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3719 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3720 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3721 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3722 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3723 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3724 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3725 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3726 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3727 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3728 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3729 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3730 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3731 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3732 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3733 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3734 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3735 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3736 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3737 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3738 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3739 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3740 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3741 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3742 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3743 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3744 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3745 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3746 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3747 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3748 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3749 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3750 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3751 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3752 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3753 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3754 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3755 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3756 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3757 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3758 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3759 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3760 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3761 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3762 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3772 uint32_t sids[3] = {1, 2, 3};
3773 uint32_t results[3] = {1, 1, 1};
3789 static int SigTestBidirec04 (
void)
3804 sig =
DetectEngineAppendSig(
de_ctx,
"alert tcp 192.168.1.1 any <> any any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)");
3809 if (sig->
next == NULL)
3821 if (sig->
next == NULL)
3832 uint8_t rawpkt1_ether[] = {
3833 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
3834 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
3835 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
3836 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
3837 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
3838 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
3839 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
3840 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
3841 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
3842 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
3843 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
3844 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
3845 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
3846 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
3847 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
3848 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
3849 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
3850 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
3851 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
3852 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
3853 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
3854 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
3855 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
3856 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
3857 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
3858 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
3859 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
3860 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
3861 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
3862 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
3863 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
3864 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
3865 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
3866 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
3867 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3868 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3869 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
3870 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
3871 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
3872 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
3873 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
3874 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
3875 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
3876 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
3877 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
3878 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
3879 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
3880 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
3881 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
3882 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
3883 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
3884 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
3885 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
3886 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
3887 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
3896 memset(&th_v, 0,
sizeof(th_v));
3936 static int SigParseTestNegation01 (
void)
3950 static int SigParseTestNegation02 (
void)
3961 s =
SigInit(
de_ctx,
"alert tcp any !any -> any any (msg:\"SigTest41-02 src ip is !any \"; classtype:misc-activity; sid:410002; rev:1;)");
3976 static int SigParseTestNegation03 (
void)
3987 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;)");
4002 static int SigParseTestNegation04 (
void)
4013 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;)");
4028 static int SigParseTestNegation05 (
void)
4039 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;)");
4054 static int SigParseTestNegation06 (
void)
4065 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;)");
4081 static int SigParseTestNegation07 (
void)
4087 de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.0/24] any (sid:410006;)");
4096 static int SigParseTestNegation08 (
void)
4107 s =
SigInit(
de_ctx,
"alert tcp any any -> [192.168.0.0/16,!192.168.0.0/24] any (sid:410006; rev:1;)");
4122 static int SigParseTestMpm01 (
void)
4131 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; sid:1;)");
4133 printf(
"sig failed to init: ");
4138 printf(
"sig doesn't have content list: ");
4153 static int SigParseTestMpm02 (
void)
4162 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; content:\"abcdef\"; sid:1;)");
4164 printf(
"sig failed to init: ");
4169 printf(
"sig doesn't have content list: ");
4184 static int SigParseTestAppLayerTLS01(
void)
4195 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS01 \"; sid:410006; rev:1;)");
4197 printf(
"parsing sig failed: ");
4202 printf(
"alproto not set: ");
4219 static int SigParseTestAppLayerTLS02(
void)
4230 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS02 \"; tls.version:1.0; sid:410006; rev:1;)");
4232 printf(
"parsing sig failed: ");
4237 printf(
"alproto not set: ");
4253 static int SigParseTestAppLayerTLS03(
void)
4264 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS03 \"; tls.version:2.5; sid:410006; rev:1;)");
4277 static int SigParseTestUnbalancedQuotes01(
void)
4287 "alert http any any -> any any (msg:\"SigParseTestUnbalancedQuotes01\"; "
4288 "pcre:\"/\\/[a-z]+\\.php\\?[a-z]+?=\\d{7}&[a-z]+?=\\d{7,8}$/U\" "
4289 "flowbits:set,et.exploitkitlanding; classtype:trojan-activity; sid:2017078; rev:5;)");
4295 static int SigParseTestContentGtDsize01(
void)
4302 "alert http any any -> any any ("
4303 "dsize:21; content:\"0123456789001234567890|00 00|\"; "
4310 static int SigParseTestContentGtDsize02(
void)
4317 "alert http any any -> any any ("
4318 "dsize:21; content:\"0123456789|00 00|\"; offset:10; "
4335 static int SigParseBidirWithSameSrcAndDest01(
void)
4352 "alert tcp [1.2.3.4, 5.6.7.8] [80, 81] <> [5.6.7.8, 1.2.3.4] [81, 80] (sid:3;)");
4361 static int SigParseBidirWithSameSrcAndDest02(
void)
4369 de_ctx,
"alert tcp 1.2.3.4 any <> [1.2.3.4, 5.6.7.8, ::1] any (sid:1;)");
4376 de_ctx,
"alert tcp [1.2.3.4, ::1] [80, 81, 82] <> [1.2.3.4, ::1] [80, 81] (sid:2;)");
4383 "alert tcp [1.2.3.4, ::1, ABCD:AAAA::1] [80] <> [1.2.3.4, ::1] [80, 81] (sid:3;)");
4390 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;)");
4397 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;)");
4406 static int SigParseTestActionReject(
void)
4412 de_ctx,
"reject tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4413 #ifdef HAVE_LIBNET11
4424 static int SigParseTestActionDrop(
void)
4430 de_ctx,
"drop tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
4470 UtRegisterTest(
"SigParseTest21 -- address with space", SigParseTest21);
4471 UtRegisterTest(
"SigParseTest22 -- address with space", SigParseTest22);
4472 UtRegisterTest(
"SigParseTest23 -- carriage return", SigParseTest23);
4487 UtRegisterTest(
"SigParseTestNegation01", SigParseTestNegation01);
4488 UtRegisterTest(
"SigParseTestNegation02", SigParseTestNegation02);
4489 UtRegisterTest(
"SigParseTestNegation03", SigParseTestNegation03);
4490 UtRegisterTest(
"SigParseTestNegation04", SigParseTestNegation04);
4491 UtRegisterTest(
"SigParseTestNegation05", SigParseTestNegation05);
4492 UtRegisterTest(
"SigParseTestNegation06", SigParseTestNegation06);
4493 UtRegisterTest(
"SigParseTestNegation07", SigParseTestNegation07);
4494 UtRegisterTest(
"SigParseTestNegation08", SigParseTestNegation08);
4497 UtRegisterTest(
"SigParseTestAppLayerTLS01", SigParseTestAppLayerTLS01);
4498 UtRegisterTest(
"SigParseTestAppLayerTLS02", SigParseTestAppLayerTLS02);
4499 UtRegisterTest(
"SigParseTestAppLayerTLS03", SigParseTestAppLayerTLS03);
4500 UtRegisterTest(
"SigParseTestUnbalancedQuotes01", SigParseTestUnbalancedQuotes01);
4503 SigParseTestContentGtDsize01);
4505 SigParseTestContentGtDsize02);
4508 SigParseBidirWithSameSrcAndDest01);
4510 SigParseBidirWithSameSrcAndDest02);
4511 UtRegisterTest(
"SigParseTestActionReject", SigParseTestActionReject);
4512 UtRegisterTest(
"SigParseTestActionDrop", SigParseTestActionDrop);