83 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
114 #define CASE_CODE_STRING(E, S) case E: return S; break
125 #undef CASE_CODE_STRING
129 #define CASE_CODE(E) case E: return #E
147 Signature *s,
const char *arg,
int sm_type,
int sm_list,
153 if (arg != NULL && strcmp(arg,
"") != 0) {
162 "with a sticky buffer still set. Reset sticky buffer "
163 "with pkt_data before using the modifier.",
177 "found inside the rule without a content context. "
178 "Please use a \"content\" keyword before using the "
186 "be used with the rawbytes rule keyword",
192 "be used with the replace rule keyword",
231 bool reuse_buffer =
false;
244 SCLogError(
"failed to expand rule buffer array");
253 SCLogDebug(
"idx %u list %d set up curbuf %p s->init_data->buffer_index %u",
294 if (sm->
ctx != NULL) {
305 ptrdiff_t
offset = e - table;
319 if (st->
name != NULL) {
351 if (strcmp(
str,
"all") == 0) {
363 char *xsaveptr = NULL;
364 char *key = strtok_r(copy,
",", &xsaveptr);
365 while (key != NULL) {
371 "argument '%s' not found",
374 key = strtok_r(NULL,
",", &xsaveptr);
401 SCLogDebug(
"s:%p new:%p list:%d: %s, s->init_data->list_set %s s->init_data->list %d", s,
new,
435 SCLogDebug(
"reusing buffer %u as it isn't multi-capable", x);
445 SCLogError(
"failed to expand rule buffer array");
475 SCLogDebug(
"appended %s to list %d, rule pos %u (s->init_data->list %d)",
494 if (sm->
prev != NULL)
496 if (sm->
next != NULL)
538 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
550 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
572 SCLogDebug(
"skip x %u s->init_data->list %d (int)s->init_data->buffers[x].id %d", x,
581 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
585 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
602 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
607 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
632 va_start(ap, sm_list);
634 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int))
636 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
639 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
670 va_start(ap, list_id);
672 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)
688 va_start(ap, list_id);
690 for (sm_type = va_arg(ap,
int); sm_type != -1; sm_type = va_arg(ap,
int)) {
691 sm_new = SigMatchGetLastSMByType(sm_list, sm_type);
694 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
717 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
725 if (sm_last == NULL || sm_new->
idx > sm_last->
idx)
732 static void SigMatchTransferSigMatchAcrossLists(
SigMatch *sm,
738 if (sm->
prev != NULL)
740 if (sm->
next != NULL)
743 if (sm == *src_sm_list)
744 *src_sm_list = sm->
next;
745 if (sm == *src_sm_list_tail)
746 *src_sm_list_tail = sm->
prev;
748 if (*dst_sm_list == NULL) {
750 *dst_sm_list_tail = sm;
758 *dst_sm_list_tail = sm;
785 SCLogError(
"Unable to find the sm in any of the "
799 static int DetectSetupDirection(
Signature *s,
char **
str,
bool only_dir)
802 if (strncmp(*
str,
"to_client", strlen(
"to_client")) == 0) {
803 *
str += strlen(
"to_client");
805 while (**
str && isblank(**
str)) {
811 SCLogError(
"unknown option: only accepts to_server or to_client");
821 while (**
str && isblank(**
str)) {
833 }
else if (strncmp(*
str,
"to_server", strlen(
"to_server")) == 0) {
834 *
str += strlen(
"to_server");
836 while (**
str && isblank(**
str)) {
842 SCLogError(
"unknown option: only accepts to_server or to_client");
852 while (**
str && isblank(**
str)) {
864 }
else if (only_dir) {
865 SCLogError(
"unknown option: only accepts to_server or to_client");
872 size_t output_size,
bool requires)
875 char *optname = NULL;
876 char *optvalue = NULL;
879 while (isblank(*optstr)) {
884 char *optend = optstr;
886 optend = strchr(optend,
';');
887 if (optend == NULL) {
891 else if (optend > optstr && *(optend -1 ) ==
'\\') {
900 char *optvalptr = strchr(optstr,
':');
902 *(optvalptr++) =
'\0';
905 for (
size_t i = strlen(optvalptr); i > 0; i--) {
906 if (isblank(optvalptr[i - 1])) {
907 optvalptr[i - 1] =
'\0';
913 optvalue = optvalptr;
917 for (
size_t i = strlen(optstr); i > 0; i--) {
918 if (isblank(optstr[i - 1])) {
919 optstr[i - 1] =
'\0';
928 bool requires_only = strcasecmp(optname,
"requires") == 0 || strcasecmp(optname,
"sid") == 0;
929 if ((requires && !requires_only) || (!requires && requires_only)) {
934 st = SigTableGet(optname);
935 if (st == NULL || st->
Setup == NULL) {
936 SCLogError(
"unknown rule keyword '%s'.", optname);
941 if (optvalue == NULL || strlen(optvalue) == 0) {
943 "invalid formatting or malformed option to %s keyword: '%s'", optname, optstr);
947 if (optvalue && strlen(optvalue)) {
948 SCLogError(
"unexpected option to %s keyword: '%s'", optname, optstr);
958 #define URL "https://suricata.io/our-story/deprecation-policy/"
961 "and will be removed soon. See %s",
965 "and will be removed soon. Use '%s' instead. "
974 if (optvalue != NULL && strlen(optvalue) > 0) {
975 size_t ovlen = strlen(optvalue);
976 char *ptr = optvalue;
986 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
992 SCLogWarning(
"keyword \'%s\' has not been tested for firewall rules", optname);
1009 SCLogError(
"invalid formatting or malformed option to %s keyword: \'%s\'", optname,
1015 SCLogError(
"invalid formatting to %s keyword: "
1016 "value must be double quoted \'%s\'",
1022 && ovlen && *ptr ==
'"')
1024 for (; ovlen > 0; ovlen--) {
1025 if (isblank(ptr[ovlen - 1])) {
1026 ptr[ovlen - 1] =
'\0';
1031 if (ovlen && ptr[ovlen - 1] !=
'"') {
1032 SCLogError(
"bad option value formatting (possible missing semicolon) "
1033 "for keyword %s: \'%s\'",
1041 ptr[ovlen - 1] =
'\0';
1046 "for keyword %s: \'%s\'",
1053 "quotes on %s keyword that doesn't support them: \'%s\'", optname, optstr);
1071 if (setup_ret < 0) {
1075 if (setup_ret == -2) {
1087 if (strlen(optend) > 0) {
1088 strlcpy(output, optend, output_size);
1103 Signature *s,
const char *addrstr,
char flag)
1105 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
1109 if (strcasecmp(addrstr,
"any") == 0)
1117 if (strcasecmp(addrstr,
"any") == 0)
1132 static bool IsBuiltIn(
const char *n)
1134 if (strcmp(n,
"request_started") == 0 || strcmp(n,
"response_started") == 0) {
1137 if (strcmp(n,
"request_complete") == 0 || strcmp(n,
"response_complete") == 0) {
1155 if (strcmp(alproto_name,
"http") == 0)
1156 alproto_name =
"http1";
1157 SCLogDebug(
"alproto %u/%s", a, alproto_name);
1159 const int max_progress_ts =
1161 const int max_progress_tc =
1164 char ts_tx_started[64];
1165 snprintf(ts_tx_started,
sizeof(ts_tx_started),
"%s:request_started:generic", alproto_name);
1168 SCLogDebug(
"- hook %s:%s list %s (%u)", alproto_name,
"request_name", ts_tx_started,
1169 (uint32_t)strlen(ts_tx_started));
1171 char tc_tx_started[64];
1172 snprintf(tc_tx_started,
sizeof(tc_tx_started),
"%s:response_started:generic", alproto_name);
1175 SCLogDebug(
"- hook %s:%s list %s (%u)", alproto_name,
"response_name", tc_tx_started,
1176 (uint32_t)strlen(tc_tx_started));
1178 char ts_tx_complete[64];
1179 snprintf(ts_tx_complete,
sizeof(ts_tx_complete),
"%s:request_complete:generic",
1183 SCLogDebug(
"- hook %s:%s list %s (%u)", alproto_name,
"request_name", ts_tx_complete,
1184 (uint32_t)strlen(ts_tx_complete));
1186 char tc_tx_complete[64];
1187 snprintf(tc_tx_complete,
sizeof(tc_tx_complete),
"%s:response_complete:generic",
1191 SCLogDebug(
"- hook %s:%s list %s (%u)", alproto_name,
"response_name", tc_tx_complete,
1192 (uint32_t)strlen(tc_tx_complete));
1194 for (
int p = 0; p <= max_progress_ts; p++) {
1196 IPPROTO_TCP , a, p, STREAM_TOSERVER);
1197 if (
name != NULL && !IsBuiltIn(
name)) {
1199 snprintf(list_name,
sizeof(list_name),
"%s:%s:generic", alproto_name,
name);
1200 SCLogDebug(
"- hook %s:%s list %s (%u)", alproto_name,
name, list_name,
1201 (uint32_t)strlen(list_name));
1207 for (
int p = 0; p <= max_progress_tc; p++) {
1209 IPPROTO_TCP , a, p, STREAM_TOCLIENT);
1210 if (
name != NULL && !IsBuiltIn(
name)) {
1212 snprintf(list_name,
sizeof(list_name),
"%s:%s:generic", alproto_name,
name);
1213 SCLogDebug(
"- hook %s:%s list %s (%u)", alproto_name,
name, list_name,
1214 (uint32_t)strlen(list_name));
1240 if (strcmp(
str,
"flow_start") == 0) {
1242 }
else if (strcmp(
str,
"pre_flow") == 0) {
1244 }
else if (strcmp(
str,
"pre_stream") == 0) {
1246 }
else if (strcmp(
str,
"all") == 0) {
1259 return "flow_start";
1263 return "pre_stream";
1275 .t.pkt.ph = HookPktFromString(hook_str),
1283 static int SigParseProtoHookPkt(
Signature *s,
const char *proto_hook,
const char *p,
const char *h)
1296 SCLogDebug(
"protocol:%s hook:%s: type:%s parsed hook:%s", p, h,
1306 .t.app.alproto = alproto,
1307 .t.app.app_progress = progress,
1315 static int SigParseProtoHookApp(
Signature *s,
const char *proto_hook,
const char *p,
const char *h)
1317 if (strcmp(h,
"request_started") == 0) {
1321 }
else if (strcmp(h,
"response_started") == 0) {
1325 }
else if (strcmp(h,
"request_complete") == 0) {
1329 }
else if (strcmp(h,
"response_complete") == 0) {
1335 IPPROTO_TCP , s->
alproto, h, STREAM_TOSERVER);
1336 if (progress_ts >= 0) {
1341 IPPROTO_TCP , s->
alproto, h, STREAM_TOCLIENT);
1342 if (progress_tc < 0) {
1350 char generic_hook_name[64];
1351 snprintf(generic_hook_name,
sizeof(generic_hook_name),
"%s:generic", proto_hook);
1354 SCLogError(
"no list registered as %s for hook %s", generic_hook_name, proto_hook);
1359 SCLogDebug(
"protocol:%s hook:%s: type:%s alproto:%u hook:%d", p, h,
1369 printf(
"=========Supported Rule Protocols=========\n");
1386 static int SigParseProto(
Signature *s,
const char *protostr)
1389 if (strlen(protostr) > 32)
1394 const char *p =
proto;
1395 const char *h = NULL;
1397 bool has_hook = strchr(
proto,
':') != NULL;
1399 char *xsaveptr = NULL;
1400 p = strtok_r(
proto,
":", &xsaveptr);
1401 h = strtok_r(NULL,
":", &xsaveptr);
1419 if (SigParseProtoHookApp(s, protostr, p, h) < 0) {
1420 SCLogError(
"protocol \"%s\" does not support hook \"%s\"", p, h);
1427 "in a signature. Either detection for this protocol "
1428 "is not yet supported OR detection has been disabled for "
1429 "protocol through the yaml option "
1430 "app-layer.protocols.%s.detection-enabled",
1434 }
else if (h != NULL) {
1435 SCLogDebug(
"non-app-layer rule with %s:%s", p, h);
1437 if (SigParseProtoHookPkt(s, protostr, p, h) < 0) {
1438 SCLogError(
"protocol \"%s\" does not support hook \"%s\"", p, h);
1467 Signature *s,
const char *portstr,
char flag)
1473 SCLogDebug(
"Port group \"%s\" to be parsed", portstr);
1476 if (strcasecmp(portstr,
"any") == 0)
1480 }
else if (flag == 1) {
1481 if (strcasecmp(portstr,
"any") == 0)
1496 static int SigParseActionRejectValidate(
const char *action)
1498 #ifdef HAVE_LIBNET11
1499 #if defined HAVE_LIBCAP_NG && !defined HAVE_LIBNET_CAPABILITIES
1502 "incompatible with POSIX based capabilities with privs dropping. "
1503 "For rejects to work, run as root/super user.");
1509 "required for action \"%s\" but is not compiled into Suricata",
1519 static uint8_t ActionStringToFlags(
const char *action)
1521 if (strcasecmp(action,
"alert") == 0) {
1523 }
else if (strcasecmp(action,
"drop") == 0) {
1525 }
else if (strcasecmp(action,
"pass") == 0) {
1527 }
else if (strcasecmp(action,
"reject") == 0 ||
1528 strcasecmp(action,
"rejectsrc") == 0)
1530 if (!(SigParseActionRejectValidate(action)))
1533 }
else if (strcasecmp(action,
"rejectdst") == 0) {
1534 if (!(SigParseActionRejectValidate(action)))
1537 }
else if (strcasecmp(action,
"rejectboth") == 0) {
1538 if (!(SigParseActionRejectValidate(action)))
1541 }
else if (strcasecmp(action,
"config") == 0) {
1543 }
else if (strcasecmp(action,
"accept") == 0) {
1546 SCLogError(
"An invalid action \"%s\" was given", action);
1562 static int SigParseAction(
Signature *s,
const char *action_in)
1565 strlcpy(action, action_in,
sizeof(action));
1566 const char *a = action;
1567 const char *o = NULL;
1569 bool has_scope = strchr(action,
':') != NULL;
1571 char *xsaveptr = NULL;
1572 a = strtok_r(action,
":", &xsaveptr);
1573 o = strtok_r(NULL,
":", &xsaveptr);
1577 SCLogError(
"invalid protocol specification '%s'", action_in);
1581 uint8_t
flags = ActionStringToFlags(a);
1587 uint8_t scope_flags = 0;
1589 if (strcmp(o,
"packet") == 0) {
1591 }
else if (strcmp(o,
"flow") == 0) {
1594 SCLogError(
"invalid action scope '%s' in action '%s': only 'packet' and 'flow' "
1601 if (strcmp(o,
"packet") == 0) {
1603 }
else if (strcmp(o,
"hook") == 0) {
1605 }
else if (strcmp(o,
"tx") == 0) {
1607 }
else if (strcmp(o,
"flow") == 0) {
1611 "invalid action scope '%s' in action '%s': only 'packet', 'flow', 'tx' and "
1618 if (strcmp(o,
"packet") == 0) {
1621 SCLogError(
"invalid action scope '%s' in action '%s': only 'packet' allowed", o,
1627 SCLogError(
"invalid action scope '%s' in action '%s': scope only supported for actions "
1628 "'drop', 'pass' and 'reject'",
1636 SCLogError(
"firewall rules require setting an explicit action scope");
1641 SCLogError(
"'accept' action only supported for firewall rules");
1646 SCLogError(
"'pass' action not supported for firewall rules");
1665 static inline int SigParseToken(
char **input,
char *output,
1666 const size_t output_size)
1668 size_t len = *input == NULL ? 0 : strlen(*input);
1674 while (
len && isblank(**input)) {
1679 char *endptr = strpbrk(*input,
" \t\n\r");
1680 if (endptr != NULL) {
1683 strlcpy(output, *input, output_size);
1700 static inline int SigParseList(
char **input,
char *output,
1701 const size_t output_size)
1704 size_t len = *input != NULL ? strlen(*input) : 0;
1710 while (
len && isblank(**input)) {
1716 for (i = 0; i <
len; i++) {
1717 char c = (*input)[i];
1720 }
else if (c ==
']') {
1722 }
else if (c ==
' ') {
1733 strlcpy(output, *input, output_size);
1734 *input = *input + i + 1;
1754 SigParseToken(&index, parser->
action,
sizeof(parser->
action));
1760 SigParseList(&index, parser->
src,
sizeof(parser->
src));
1763 SigParseList(&index, parser->
sp,
sizeof(parser->
sp));
1769 SigParseList(&index, parser->
dst,
sizeof(parser->
dst));
1772 SigParseList(&index, parser->
dp,
sizeof(parser->
dp));
1775 if (index == NULL) {
1779 while (isspace(*index) || *index ==
'(') {
1782 for (
size_t i = strlen(index); i > 0; i--) {
1783 if (isspace(index[i - 1]) || index[i - 1] ==
')') {
1784 index[i - 1] =
'\0';
1796 if (SigParseAction(s, parser->
action) < 0)
1799 if (SigParseProto(s, parser->
protocol) < 0)
1802 if (strcmp(parser->
direction,
"<>") == 0) {
1804 }
else if (strcmp(parser->
direction,
"=>") == 0) {
1806 SCLogError(
"transactional bidirectional rules not supported for firewall rules");
1811 }
else if (strcmp(parser->
direction,
"->") != 0) {
1812 SCLogError(
"\"%s\" is not a valid direction modifier, "
1813 "\"->\" and \"<>\" are supported.",
1840 static inline bool CheckAscii(
const char *
str)
1842 for (
size_t i = 0; i < strlen(
str); i++) {
1843 if (
str[i] < 0x20) {
1845 if (
str[i] == 0x0a ||
str[i] == 0x0d ||
str[i] == 0x09) {
1849 }
else if (
str[i] == 0x7f) {
1873 if (!SCCheckUtf8(sigstr)) {
1878 if (!CheckAscii(sigstr)) {
1879 SCLogError(
"rule contains invalid (control) characters");
1883 int ret = SigParseBasics(
de_ctx, s, sigstr, parser, addrs_direction, requires);
1890 if (strlen(parser->
opts) > 0) {
1891 size_t buffer_size = strlen(parser->
opts) + 1;
1892 char input[buffer_size];
1893 char output[buffer_size];
1894 memset(input, 0x00, buffer_size);
1895 memcpy(input, parser->
opts, strlen(parser->
opts) + 1);
1901 memset(output, 0x00, buffer_size);
1902 ret = SigParseOptions(
de_ctx, s, input, output, buffer_size, requires);
1904 memcpy(input, output, buffer_size);
1936 memset(b, 0,
sizeof(*b));
1984 static void SigMetadataFree(
Signature *s)
1991 if (s == NULL || s->
metadata == NULL) {
1998 next_mdata = mdata->
next;
2029 next_ref = ref->
next;
2083 while (sm != NULL) {
2092 while (sm != NULL) {
2113 if (s->
sp != NULL) {
2116 if (s->
dp != NULL) {
2162 if (s->
alproto == *alprotos) {
2205 }
else if (i == 1) {
2237 if (!AppProtoIsValid(alproto)) {
2261 alproto = AppProtoCommon(s->
alproto, alproto);
2263 SCLogError(
"can't set rule app proto to %s: already set to %s",
2290 if (addr_match4 == NULL) {
2296 addr_match4[idx].
ip =
SCNtohl(da->ip.addr_data32[0]);
2297 addr_match4[idx].
ip2 =
SCNtohl(da->ip2.addr_data32[0]);
2316 if (addr_match6 == NULL) {
2322 addr_match6[idx].
ip[0] =
SCNtohl(da->ip.addr_data32[0]);
2323 addr_match6[idx].
ip[1] =
SCNtohl(da->ip.addr_data32[1]);
2324 addr_match6[idx].
ip[2] =
SCNtohl(da->ip.addr_data32[2]);
2325 addr_match6[idx].
ip[3] =
SCNtohl(da->ip.addr_data32[3]);
2326 addr_match6[idx].
ip2[0] =
SCNtohl(da->ip2.addr_data32[0]);
2327 addr_match6[idx].
ip2[1] =
SCNtohl(da->ip2.addr_data32[1]);
2328 addr_match6[idx].
ip2[2] =
SCNtohl(da->ip2.addr_data32[2]);
2329 addr_match6[idx].
ip2[3] =
SCNtohl(da->ip2.addr_data32[3]);
2342 static void SigBuildAddressMatchArray(
Signature *s)
2359 static int SigMatchListLen(
SigMatch *sm)
2362 for (; sm != NULL; sm = sm->
next)
2373 int len = SigMatchListLen(
head);
2379 FatalError(
"initializing the detection engine failed");
2385 for (; sm != NULL; sm = sm->
next, smd++) {
2425 SCLogDebug(
"s %u: no mpm; prefilter? de_ctx->prefilter_setting %u "
2426 "s->init_data->has_possible_prefilter %s",
2440 prefilter_list =
MIN(prefilter_list, sm->
type);
2450 if (sm->
type == prefilter_list) {
2467 static bool DetectRuleValidateTable(
const Signature *s)
2476 if (kw_tables_supported != 0 && (kw_tables_supported & table_as_flag) == 0) {
2477 SCLogError(
"rule %u uses hook \"%s\", but keyword \"%s\" doesn't support this hook",
2488 SCLogError(
"rule %u is loaded as a firewall rule, but does not specify an "
2496 static void DetectRuleSetTable(
Signature *s)
2529 if (!DetectFirewallRuleValidate(
de_ctx, s))
2535 static int SigValidateCheckBuffers(
2538 bool has_frame =
false;
2539 bool has_app =
false;
2540 bool has_pkt =
false;
2541 bool has_pmatch =
false;
2547 nlists += (nlists > 0);
2551 SCLogError(
"rule %u setup buffer %s but didn't add matches to it", s->
id,
2565 struct BufferVsDir {
2568 } bufdir[nlists + 1];
2569 memset(&bufdir, 0, (nlists + 1) *
sizeof(
struct BufferVsDir));
2583 if (b->
head == NULL) {
2588 has_frame |= bt->
frame;
2594 "specific matches (like dsize, flags, ttl) with stream / "
2595 "state matching by matching on app layer proto (like using "
2596 "http_* keywords).");
2601 for (; app != NULL; app = app->
next) {
2616 bufdir[b->
id].ts += (app->
dir == 0);
2617 bufdir[b->
id].tc += (app->
dir == 1);
2631 SCLogError(
"engine progress value doesn't match hook");
2650 if (has_pmatch && has_frame) {
2651 SCLogError(
"can't mix pure content and frame inspection");
2654 if (has_app && has_frame) {
2655 SCLogError(
"can't mix app-layer buffer and frame inspection");
2658 if (has_pkt && has_frame) {
2659 SCLogError(
"can't mix pkt buffer and frame inspection");
2663 for (
int x = 0; x < nlists; x++) {
2664 if (bufdir[x].
ts == 0 && bufdir[x].tc == 0)
2666 (*ts_excl) += (bufdir[x].ts > 0 && bufdir[x].tc == 0);
2667 (*tc_excl) += (bufdir[x].ts == 0 && bufdir[x].tc > 0);
2668 (*dir_amb) += (bufdir[x].ts > 0 && bufdir[x].tc > 0);
2677 static int SigValidatePacketStream(
const Signature *s)
2681 "tcp-stream or flow:only_stream. Invalidating signature.");
2687 static int SigConsolidateDirection(
2688 Signature *s,
const int ts_excl,
const int tc_excl,
const int dir_amb)
2691 if (!ts_excl || !tc_excl) {
2692 SCLogError(
"rule %u should use both directions, but does not", s->
id);
2696 SCLogError(
"rule %u means to use both directions, cannot have keywords ambiguous about "
2701 }
else if (ts_excl && tc_excl) {
2703 "rule %u mixes keywords with conflicting directions, a transactional rule with => "
2707 }
else if (ts_excl) {
2708 SCLogDebug(
"%u: implied rule direction is toserver", s->
id);
2710 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2713 }
else if (tc_excl) {
2714 SCLogDebug(
"%u: implied rule direction is toclient", s->
id);
2716 SCLogError(
"rule %u mixes keywords with conflicting directions", s->
id);
2719 }
else if (dir_amb) {
2720 SCLogDebug(
"%u: rule direction cannot be deduced from keywords", s->
id);
2725 static void SigConsolidateTcpBuffer(
Signature *s)
2731 if (s->
proto.
proto[IPPROTO_TCP / 8] & (1 << (IPPROTO_TCP % 8))) {
2757 static bool SigInspectsFiles(
const Signature *s)
2768 static int SigValidateFileHandling(
const Signature *s)
2770 if (!SigInspectsFiles(s)) {
2777 "support file matching",
2794 SCLogError(
"No protocol support file matching");
2799 SCLogError(
"protocol HTTP2 doesn't support file name matching");
2815 static int SigValidateConsolidate(
2820 if (SigValidateFirewall(
de_ctx, s) == 0)
2823 if (SigValidatePacketStream(s) == 0) {
2831 if (SigValidateCheckBuffers(
de_ctx, s, &ts_excl, &tc_excl, &dir_amb) == 0) {
2835 if (SigConsolidateDirection(s, ts_excl, tc_excl, dir_amb) == 0) {
2839 SigConsolidateTcpBuffer(s);
2842 DetectRuleSetTable(s);
2844 int r = SigValidateFileHandling(s);
2848 if (SigInspectsFiles(s)) {
2853 if (DetectRuleValidateTable(s) ==
false) {
2876 memset(&parser, 0x00,
sizeof(parser));
2881 if (firewall_rule) {
2898 int ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
true);
2905 }
else if (ret < 0) {
2911 SCLogError(
"Signature missing required value \"sid\".");
2916 ret = SigParse(
de_ctx, sig, sigstr, dir, &parser,
false);
2922 }
else if (ret == -2) {
2925 }
else if (ret < 0) {
2930 if (sig->
prio == -1)
2937 int override_needed = 0;
2941 override_needed = 1;
2943 override_needed = 1;
2945 for (s = 0; s <
sizeof(sig->
proto.
proto); s++) {
2947 override_needed = 0;
2956 if (override_needed)
2966 for ( ; sm != NULL; sm = sm->
next) {
2990 SCLogDebug(
"sig %"PRIu32
" SIG_FLAG_APPLAYER: %s, SIG_FLAG_PACKET: %s",
2994 SigBuildAddressMatchArray(sig);
3005 SigSetupPrefilter(
de_ctx, sig);
3008 if (SigValidateConsolidate(
de_ctx, sig, &parser, dir) == 0) {
3027 static bool SigHasSameSourceAndDestination(
const Signature *s)
3069 if (SigHasSameSourceAndDestination(sig)) {
3070 SCLogInfo(
"Rule with ID %u is bidirectional, but source and destination are the same, "
3071 "treating the rule as unidirectional", sig->
id);
3076 if (sig->
next == NULL) {
3106 return SigInitDo(
de_ctx, sigstr,
false);
3111 return SigInitDo(
de_ctx, sigstr,
true);
3120 static void DetectParseDupSigFreeFunc(
void *data)
3136 static uint32_t DetectParseDupSigHashFunc(
HashListTable *ht,
void *data, uint16_t datalen)
3155 static char DetectParseDupSigCompareFunc(
void *data1, uint16_t len1,
void *data2,
3161 if (sw1 == NULL || sw2 == NULL ||
3162 sw1->
s == NULL || sw2->
s == NULL)
3166 if (sw1->
s->
id == sw2->
s->
id && sw1->
s->
gid == sw2->
s->
gid)
return 1;
3182 DetectParseDupSigHashFunc,
3183 DetectParseDupSigCompareFunc,
3184 DetectParseDupSigFreeFunc);
3245 if (sw_dup == NULL) {
3258 (
void *)&sw_tmp, 0);
3270 if (sw->
s->
rev <= sw_dup->
s->
rev) {
3279 if (sw_dup->
s_prev == NULL) {
3287 sw_temp.
s = sw_dup->
s->
next;
3291 if (sw_temp.
s != NULL) {
3293 (
void *)&sw_temp, 0);
3313 sw_temp.
s = sw_dup->
s->
next;
3321 if (sw_temp.
s != NULL) {
3323 (
void *)&sw_temp, 0);
3338 (
void *)&sw_tmp, 0);
3339 if (sw_old->
s != sw_dup->
s) {
3382 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
3386 SCLogError(
"Duplicate signature \"%s\"", sigstr);
3388 }
else if (dup_sig == 2) {
3390 " so the older sig replaced by this new signature \"%s\"",
3395 if (sig->
next != NULL) {
3412 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
3416 if (sig != NULL && sig->
next != NULL) {
3454 int dup_sig = DetectEngineSignatureIsDuplicate(
de_ctx, sig);
3458 SCLogError(
"Duplicate signature \"%s\"", sigstr);
3460 }
else if (dup_sig == 2) {
3462 " so the older sig replaced by this new signature \"%s\"",
3467 if (sig->
next != NULL) {
3484 return (dup_sig == 0 || dup_sig == 2) ? sig : NULL;
3488 if (sig != NULL && sig->
next != NULL) {
3501 int start_offset,
int options)
3503 *match = pcre2_match_data_create_from_pattern(parse_regex->
regex, NULL);
3505 return pcre2_match(parse_regex->
regex, (PCRE2_SPTR8)
str, strlen(
str), options, start_offset,
3506 *match, parse_regex->
context);
3513 pcre2_code_free(r->
regex);
3516 pcre2_match_context_free(r->
context);
3531 g_detect_parse_regex_list = NULL;
3540 FatalError(
"failed to alloc memory for pcre free list");
3543 r->
next = g_detect_parse_regex_list;
3544 g_detect_parse_regex_list = r;
3552 detect_parse->
regex =
3553 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
3554 if (detect_parse->
regex == NULL) {
3555 PCRE2_UCHAR errbuffer[256];
3556 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
3557 SCLogError(
"pcre compile of \"%s\" failed at "
3559 parse_str, en, errbuffer);
3562 detect_parse->
context = pcre2_match_context_create(NULL);
3563 if (detect_parse->
context == NULL) {
3564 SCLogError(
"pcre2 could not create match context");
3565 pcre2_code_free(detect_parse->
regex);
3566 detect_parse->
regex = NULL;
3581 if (detect_parse == NULL) {
3585 detect_parse->
regex =
3586 pcre2_compile((PCRE2_SPTR8)parse_str, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
3587 if (detect_parse->
regex == NULL) {
3588 PCRE2_UCHAR errbuffer[256];
3589 pcre2_get_error_message(en, errbuffer,
sizeof(errbuffer));
3590 SCLogError(
"pcre2 compile of \"%s\" failed at "
3592 parse_str, (
int)eo, errbuffer);
3597 detect_parse->
next = g_detect_parse_regex_list;
3598 g_detect_parse_regex_list = detect_parse;
3599 return detect_parse;
3603 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
3605 int r = pcre2_substring_copy_bynumber(match_data, number, buffer, bufflen);
3606 if (r == PCRE2_ERROR_UNSET) {
3615 pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
3617 int r = pcre2_substring_get_bynumber(match_data, number, bufferptr, bufflen);
3618 if (r == PCRE2_ERROR_UNSET) {
3641 static int SigParseTest01 (
void)
3650 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
3660 static int SigParseTest02 (
void)
3675 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;)");
3703 static int SigParseTest03 (
void)
3712 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 any <- !1.2.3.4 any (msg:\"SigParseTest03\"; sid:1;)");
3715 printf(
"expected NULL got sig ptr %p: ",sig);
3724 static int SigParseTest04 (
void)
3733 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024: -> !1.2.3.4 1024: (msg:\"SigParseTest04\"; sid:1;)");
3744 static int SigParseTest05 (
void)
3753 sig =
SigInit(
de_ctx,
"alert tcp 1.2.3.4 1024:65536 -> !1.2.3.4 any (msg:\"SigParseTest05\"; sid:1;)");
3757 printf(
"signature didn't fail to parse as we expected: ");
3767 static int SigParseTest06 (
void)
3776 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;)");
3780 printf(
"signature failed to parse: ");
3794 static int SigParseTest07(
void)
3816 static int SigParseTest08(
void)
3839 static int SigParseTest09(
void)
3890 static int SigParseTest10(
void)
3922 static int SigParseTest11(
void)
3933 "drop tcp any any -> any 80 (msg:\"Snort_Inline is blocking the http link\"; sid:1;) ");
3935 printf(
"sig 1 didn't parse: ");
3940 "the http link\"; sid:2;) ");
3942 printf(
"sig 2 didn't parse: ");
3956 static int SigParseTest12(
void)
3968 printf(
"sig 1 should have given an error: ");
3982 static int SigParseTest13(
void)
3994 printf(
"sig 1 invalidated: failure");
3999 printf(
"sig doesn't have stream flag set\n");
4004 printf(
"sig has packet flag set\n");
4019 static int SigParseTest14(
void)
4031 printf(
"sig 1 invalidated: failure");
4036 printf(
"sig doesn't have packet flag set\n");
4041 printf(
"sig has stream flag set\n");
4056 static int SigParseTest15(
void)
4068 printf(
"sig 1 invalidated: failure");
4073 printf(
"sig doesn't have packet flag set\n");
4078 printf(
"sig doesn't have stream flag set\n");
4093 static int SigParseTest16(
void)
4105 printf(
"sig 1 invalidated: failure");
4110 printf(
"sig doesn't have packet flag set\n");
4115 printf(
"sig doesn't have stream flag set\n");
4130 static int SigParseTest17(
void)
4142 printf(
"sig 1 invalidated: failure");
4147 printf(
"sig doesn't have packet flag set\n");
4152 printf(
"sig doesn't have stream flag set\n");
4165 static int SigParseTest18 (
void)
4173 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:99999999999999999999;)") != NULL)
4184 static int SigParseTest19 (
void)
4192 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; gid:99999999999999999999;)") != NULL)
4203 static int SigParseTest20 (
void)
4211 if (
DetectEngineAppendSig(
de_ctx,
"alert tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1; rev:99999999999999999999;)") != NULL)
4222 static int SigParseTest21 (
void)
4241 static int SigParseTest22 (
void)
4249 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)
4262 static int SigParseTest23(
void)
4277 static int SigParseBidirecTest06 (
void)
4297 static int SigParseBidirecTest07 (
void)
4317 static int SigParseBidirecTest08 (
void)
4337 static int SigParseBidirecTest09 (
void)
4357 static int SigParseBidirecTest10 (
void)
4377 static int SigParseBidirecTest11 (
void)
4397 static int SigParseBidirecTest12 (
void)
4417 static int SigParseBidirecTest13 (
void)
4436 static int SigParseBidirecTest14 (
void)
4457 static int SigTestBidirec01 (
void)
4469 if (sig->
next != NULL)
4488 static int SigTestBidirec02 (
void)
4507 if (sig->
next == NULL)
4512 if (copy->
next != NULL)
4533 static int SigTestBidirec03 (
void)
4545 const char *sigs[3];
4546 sigs[0] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 1\"; sid:1;)";
4547 sigs[1] =
"alert tcp any any <> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)";
4548 sigs[2] =
"alert tcp any any -> 192.168.1.1 any (msg:\"SigTestBidirec03 sid 3\"; sid:3;)";
4555 if (sig->
next == NULL)
4566 uint8_t rawpkt1_ether[] = {
4567 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
4568 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
4569 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
4570 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
4571 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
4572 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
4573 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
4574 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
4575 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
4576 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
4577 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
4578 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
4579 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
4580 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
4581 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
4582 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
4583 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
4584 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
4585 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
4586 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
4587 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
4588 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
4589 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
4590 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
4591 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
4592 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
4593 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
4594 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
4595 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
4596 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
4597 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
4598 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
4599 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
4600 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
4601 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
4602 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
4603 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
4604 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
4605 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
4606 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
4607 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
4608 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
4609 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
4610 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
4611 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
4612 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
4613 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
4614 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
4615 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
4616 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
4617 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
4618 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
4619 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
4620 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
4621 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
4631 uint32_t sids[3] = {1, 2, 3};
4632 uint32_t results[3] = {1, 1, 1};
4648 static int SigTestBidirec04 (
void)
4663 sig =
DetectEngineAppendSig(
de_ctx,
"alert tcp 192.168.1.1 any <> any any (msg:\"SigTestBidirec03 sid 2 bidirectional\"; sid:2;)");
4668 if (sig->
next == NULL)
4680 if (sig->
next == NULL)
4691 uint8_t rawpkt1_ether[] = {
4692 0x00,0x50,0x56,0xea,0x00,0xbd,0x00,0x0c,
4693 0x29,0x40,0xc8,0xb5,0x08,0x00,0x45,0x00,
4694 0x01,0xa8,0xb9,0xbb,0x40,0x00,0x40,0x06,
4695 0xe0,0xbf,0xc0,0xa8,0x1c,0x83,0xc0,0xa8,
4696 0x01,0x01,0xb9,0x0a,0x00,0x50,0x6f,0xa2,
4697 0x92,0xed,0x7b,0xc1,0xd3,0x4d,0x50,0x18,
4698 0x16,0xd0,0xa0,0x6f,0x00,0x00,0x47,0x45,
4699 0x54,0x20,0x2f,0x20,0x48,0x54,0x54,0x50,
4700 0x2f,0x31,0x2e,0x31,0x0d,0x0a,0x48,0x6f,
4701 0x73,0x74,0x3a,0x20,0x31,0x39,0x32,0x2e,
4702 0x31,0x36,0x38,0x2e,0x31,0x2e,0x31,0x0d,
4703 0x0a,0x55,0x73,0x65,0x72,0x2d,0x41,0x67,
4704 0x65,0x6e,0x74,0x3a,0x20,0x4d,0x6f,0x7a,
4705 0x69,0x6c,0x6c,0x61,0x2f,0x35,0x2e,0x30,
4706 0x20,0x28,0x58,0x31,0x31,0x3b,0x20,0x55,
4707 0x3b,0x20,0x4c,0x69,0x6e,0x75,0x78,0x20,
4708 0x78,0x38,0x36,0x5f,0x36,0x34,0x3b,0x20,
4709 0x65,0x6e,0x2d,0x55,0x53,0x3b,0x20,0x72,
4710 0x76,0x3a,0x31,0x2e,0x39,0x2e,0x30,0x2e,
4711 0x31,0x34,0x29,0x20,0x47,0x65,0x63,0x6b,
4712 0x6f,0x2f,0x32,0x30,0x30,0x39,0x30,0x39,
4713 0x30,0x32,0x31,0x37,0x20,0x55,0x62,0x75,
4714 0x6e,0x74,0x75,0x2f,0x39,0x2e,0x30,0x34,
4715 0x20,0x28,0x6a,0x61,0x75,0x6e,0x74,0x79,
4716 0x29,0x20,0x46,0x69,0x72,0x65,0x66,0x6f,
4717 0x78,0x2f,0x33,0x2e,0x30,0x2e,0x31,0x34,
4718 0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74,
4719 0x3a,0x20,0x74,0x65,0x78,0x74,0x2f,0x68,
4720 0x74,0x6d,0x6c,0x2c,0x61,0x70,0x70,0x6c,
4721 0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x2f,
4722 0x78,0x68,0x74,0x6d,0x6c,0x2b,0x78,0x6d,
4723 0x6c,0x2c,0x61,0x70,0x70,0x6c,0x69,0x63,
4724 0x61,0x74,0x69,0x6f,0x6e,0x2f,0x78,0x6d,
4725 0x6c,0x3b,0x71,0x3d,0x30,0x2e,0x39,0x2c,
4726 0x2a,0x2f,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
4727 0x38,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
4728 0x74,0x2d,0x4c,0x61,0x6e,0x67,0x75,0x61,
4729 0x67,0x65,0x3a,0x20,0x65,0x6e,0x2d,0x75,
4730 0x73,0x2c,0x65,0x6e,0x3b,0x71,0x3d,0x30,
4731 0x2e,0x35,0x0d,0x0a,0x41,0x63,0x63,0x65,
4732 0x70,0x74,0x2d,0x45,0x6e,0x63,0x6f,0x64,
4733 0x69,0x6e,0x67,0x3a,0x20,0x67,0x7a,0x69,
4734 0x70,0x2c,0x64,0x65,0x66,0x6c,0x61,0x74,
4735 0x65,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,
4736 0x74,0x2d,0x43,0x68,0x61,0x72,0x73,0x65,
4737 0x74,0x3a,0x20,0x49,0x53,0x4f,0x2d,0x38,
4738 0x38,0x35,0x39,0x2d,0x31,0x2c,0x75,0x74,
4739 0x66,0x2d,0x38,0x3b,0x71,0x3d,0x30,0x2e,
4740 0x37,0x2c,0x2a,0x3b,0x71,0x3d,0x30,0x2e,
4741 0x37,0x0d,0x0a,0x4b,0x65,0x65,0x70,0x2d,
4742 0x41,0x6c,0x69,0x76,0x65,0x3a,0x20,0x33,
4743 0x30,0x30,0x0d,0x0a,0x43,0x6f,0x6e,0x6e,
4744 0x65,0x63,0x74,0x69,0x6f,0x6e,0x3a,0x20,
4745 0x6b,0x65,0x65,0x70,0x2d,0x61,0x6c,0x69,
4746 0x76,0x65,0x0d,0x0a,0x0d,0x0a };
4755 memset(&th_v, 0,
sizeof(th_v));
4795 static int SigParseTestNegation01 (
void)
4809 static int SigParseTestNegation02 (
void)
4820 s =
SigInit(
de_ctx,
"alert tcp any !any -> any any (msg:\"SigTest41-02 src ip is !any \"; classtype:misc-activity; sid:410002; rev:1;)");
4835 static int SigParseTestNegation03 (
void)
4846 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;)");
4861 static int SigParseTestNegation04 (
void)
4872 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;)");
4887 static int SigParseTestNegation05 (
void)
4898 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;)");
4913 static int SigParseTestNegation06 (
void)
4924 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;)");
4940 static int SigParseTestNegation07 (
void)
4946 de_ctx,
"alert tcp any any -> [192.168.0.2,!192.168.0.0/24] any (sid:410006;)");
4955 static int SigParseTestNegation08 (
void)
4967 "alert tcp any any -> [192.168.0.0/16,!192.168.0.0/24] any (sid:410006; rev:1;)");
4982 static int SigParseTestMpm01 (
void)
4991 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; sid:1;)");
4993 printf(
"sig failed to init: ");
4998 printf(
"sig doesn't have content list: ");
5013 static int SigParseTestMpm02 (
void)
5022 sig =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"mpm test\"; content:\"abcd\"; content:\"abcdef\"; sid:1;)");
5024 printf(
"sig failed to init: ");
5029 printf(
"sig doesn't have content list: ");
5044 static int SigParseTestAppLayerTLS01(
void)
5055 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS01 \"; sid:410006; rev:1;)");
5057 printf(
"parsing sig failed: ");
5062 printf(
"alproto not set: ");
5079 static int SigParseTestAppLayerTLS02(
void)
5090 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS02 \"; tls.version:1.0; sid:410006; rev:1;)");
5092 printf(
"parsing sig failed: ");
5097 printf(
"alproto not set: ");
5113 static int SigParseTestAppLayerTLS03(
void)
5124 s =
SigInit(
de_ctx,
"alert tls any any -> any any (msg:\"SigParseTestAppLayerTLS03 \"; tls.version:2.5; sid:410006; rev:1;)");
5137 static int SigParseTestUnbalancedQuotes01(
void)
5143 "alert http any any -> any any (msg:\"SigParseTestUnbalancedQuotes01\"; "
5144 "pcre:\"/\\/[a-z]+\\.php\\?[a-z]+?=\\d{7}&[a-z]+?=\\d{7,8}$/U\" "
5145 "flowbits:set,et.exploitkitlanding; classtype:trojan-activity; sid:2017078; rev:5;)");
5151 static int SigParseTestContentGtDsize01(
void)
5158 "dsize:21; content:\"0123456789001234567890|00 00|\"; "
5165 static int SigParseTestContentGtDsize02(
void)
5172 "dsize:21; content:\"0123456789|00 00|\"; offset:10; "
5189 static int SigParseBidirWithSameSrcAndDest01(
void)
5206 "alert tcp [1.2.3.4, 5.6.7.8] [80, 81] <> [5.6.7.8, 1.2.3.4] [81, 80] (sid:3;)");
5215 static int SigParseBidirWithSameSrcAndDest02(
void)
5223 de_ctx,
"alert tcp 1.2.3.4 any <> [1.2.3.4, 5.6.7.8, ::1] any (sid:1;)");
5230 de_ctx,
"alert tcp [1.2.3.4, ::1] [80, 81, 82] <> [1.2.3.4, ::1] [80, 81] (sid:2;)");
5237 "alert tcp [1.2.3.4, ::1, ABCD:AAAA::1] [80] <> [1.2.3.4, ::1] [80, 81] (sid:3;)");
5244 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;)");
5251 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;)");
5260 static int SigParseTestActionReject(
void)
5266 de_ctx,
"reject tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
5267 #ifdef HAVE_LIBNET11
5278 static int SigParseTestActionDrop(
void)
5284 de_ctx,
"drop tcp 1.2.3.4 any -> !1.2.3.4 any (msg:\"SigParseTest01\"; sid:1;)");
5292 static int SigSetMultiAppProto(
void)
5367 static int DetectSetupDirection01(
void)
5372 char *
str = (
char *)
"to_client";
5373 FAIL_IF(DetectSetupDirection(s, &
str,
true) < 0);
5378 static int DetectSetupDirection02(
void)
5382 char *
str = (
char *)
"to_server";
5383 FAIL_IF(DetectSetupDirection(s, &
str,
true) < 0);
5385 str = (
char *)
"to_client";
5386 FAIL_IF(DetectSetupDirection(s, &
str,
true) >= 0);
5392 static int DetectSetupDirection03(
void)
5396 char *
str = (
char *)
"to_client , something";
5397 FAIL_IF(DetectSetupDirection(s, &
str,
false) < 0);
5399 str = (
char *)
"to_client,something";
5400 FAIL_IF(DetectSetupDirection(s, &
str,
false) < 0);
5406 static int DetectSetupDirection04(
void)
5411 char *
str = (
char *)
"to_client_toto";
5412 FAIL_IF(DetectSetupDirection(s, &
str,
true) >= 0);
5414 str = (
char *)
"to_client_toto";
5415 FAIL_IF(DetectSetupDirection(s, &
str,
false) < 0);
5417 str = (
char *)
"to_client,something";
5419 FAIL_IF(DetectSetupDirection(s, &
str,
true) >= 0);
5456 UtRegisterTest(
"SigParseTest21 -- address with space", SigParseTest21);
5457 UtRegisterTest(
"SigParseTest22 -- address with space", SigParseTest22);
5458 UtRegisterTest(
"SigParseTest23 -- carriage return", SigParseTest23);
5473 UtRegisterTest(
"SigParseTestNegation01", SigParseTestNegation01);
5474 UtRegisterTest(
"SigParseTestNegation02", SigParseTestNegation02);
5475 UtRegisterTest(
"SigParseTestNegation03", SigParseTestNegation03);
5476 UtRegisterTest(
"SigParseTestNegation04", SigParseTestNegation04);
5477 UtRegisterTest(
"SigParseTestNegation05", SigParseTestNegation05);
5478 UtRegisterTest(
"SigParseTestNegation06", SigParseTestNegation06);
5479 UtRegisterTest(
"SigParseTestNegation07", SigParseTestNegation07);
5480 UtRegisterTest(
"SigParseTestNegation08", SigParseTestNegation08);
5483 UtRegisterTest(
"SigParseTestAppLayerTLS01", SigParseTestAppLayerTLS01);
5484 UtRegisterTest(
"SigParseTestAppLayerTLS02", SigParseTestAppLayerTLS02);
5485 UtRegisterTest(
"SigParseTestAppLayerTLS03", SigParseTestAppLayerTLS03);
5486 UtRegisterTest(
"SigParseTestUnbalancedQuotes01", SigParseTestUnbalancedQuotes01);
5489 SigParseTestContentGtDsize01);
5491 SigParseTestContentGtDsize02);
5494 SigParseBidirWithSameSrcAndDest01);
5496 SigParseBidirWithSameSrcAndDest02);
5497 UtRegisterTest(
"SigParseTestActionReject", SigParseTestActionReject);
5498 UtRegisterTest(
"SigParseTestActionDrop", SigParseTestActionDrop);
5502 UtRegisterTest(
"DetectSetupDirection01", DetectSetupDirection01);
5503 UtRegisterTest(
"DetectSetupDirection02", DetectSetupDirection02);
5504 UtRegisterTest(
"DetectSetupDirection03", DetectSetupDirection03);
5505 UtRegisterTest(
"DetectSetupDirection04", DetectSetupDirection04);