64 #include <netinet/in.h>
95 for (; i < lhs->
netmask / 32 || i < 1; i++) {
96 if (lhs->
ip[i] < rhs->
ip[i])
98 if (lhs->
ip[i] > rhs->
ip[i])
107 static int IPOnlyCIDRItemCompare(
const void *lhsv,
const void *rhsv)
112 return IPOnlyCIDRItemCompareReal(lhs, rhs);
133 SCLogError(
"Failed to allocate enough memory to sort IP-only CIDR items.");
138 for (
size_t i = 0; i <
len; i++) {
149 for (
size_t i = 0; i + 1 <
len; i++) {
150 tmp[i]->
next = tmp[i + 1];
162 static int InsertRange(
168 uint32_t first = first_in;
169 uint32_t last = last_in;
177 while (dd->
netmask > 0 && (first & (1UL << (32 - dd->
netmask))) == 0 &&
178 first + (1UL << (32 - (dd->
netmask - 1))) - 1 <= last) {
181 dd->
ip[0] = htonl(first);
182 first += 1UL << (32 - dd->
netmask);
184 while (first <= last && first != 0) {
191 while (new->netmask > 0 && (first & (1UL << (32 - new->netmask))) == 0 &&
192 first + (1UL << (32 - (new->netmask - 1))) - 1 <= last) {
195 new->ip[0] = htonl(first);
196 first += 1UL << (32 -
new->netmask);
197 dd = IPOnlyCIDRItemInsert(dd,
new);
221 char *ip = NULL, *ip2 = NULL;
226 while (*
str !=
'\0' && *
str ==
' ')
234 if (strcasecmp(
str,
"any") == 0) {
236 SCLogDebug(
"adding 0.0.0.0/0 and ::/0 as we\'re handling \'any\'");
238 IPOnlyCIDRItemParseSingle(&dd,
"0.0.0.0/0");
241 dd->
next = IPOnlyCIDRItemNew();
242 if (dd->
next == NULL)
245 IPOnlyCIDRItemParseSingle(&dd->
next,
"::/0");
259 if ((strchr(
str,
':')) == NULL) {
265 if ((mask = strchr(ip,
'/')) != NULL) {
267 ip[mask - ip] =
'\0';
269 uint32_t netmask = 0;
272 if ((strchr (mask,
'.')) == NULL) {
275 for (u = 0; u < strlen(mask); u++) {
276 if(!isdigit((
unsigned char)mask[u]))
288 r = inet_pton(AF_INET, mask, &in);
299 r = inet_pton(AF_INET, ip, &in);
303 dd->
ip[0] = in.s_addr & netmask;
305 }
else if ((ip2 = strchr(ip,
'-')) != NULL) {
310 uint32_t first, last;
312 r = inet_pton(AF_INET, ip, &in);
317 r = inet_pton(AF_INET, ip2, &in);
326 SCLogDebug(
"Creating CIDR range for [%s - %s]", ip, ip2);
327 return InsertRange(pdd, dd, first, last);
330 r = inet_pton(AF_INET, ip, &in);
335 dd->
ip[0] = in.s_addr;
340 struct in6_addr in6, mask6;
341 uint32_t ip6addr[4], netmask[4];
345 if ((mask = strchr(ip,
'/')) != NULL) {
349 r = inet_pton(AF_INET6, ip, &in6);
355 (
const char *)mask, 0, 128) < 0) {
359 memcpy(&ip6addr, &in6.s6_addr,
sizeof(ip6addr));
361 memcpy(&netmask, &mask6.s6_addr,
sizeof(netmask));
363 dd->
ip[0] = ip6addr[0] & netmask[0];
364 dd->
ip[1] = ip6addr[1] & netmask[1];
365 dd->
ip[2] = ip6addr[2] & netmask[2];
366 dd->
ip[3] = ip6addr[3] & netmask[3];
368 r = inet_pton(AF_INET6, ip, &in6);
372 memcpy(dd->
ip, &in6.s6_addr,
sizeof(dd->
ip));
402 if (IPOnlyCIDRItemParseSingle(gh, s) == -1) {
403 SCLogError(
"address parsing error \"%s\"", s);
448 SCLogDebug(
"Head is NULL to insert item (%p)",item);
460 while (prev != NULL) {
468 head = IPOnlyCIDRItemInsertReal(
head, prev);
489 if (tmphead == NULL) {
500 SCLogDebug(
"Item(%p) %"PRIu32
" removed", it, i);
520 while (tmphead != NULL) {
522 tmphead = tmphead->
next;
536 while (tmphead != NULL) {
538 SCLogDebug(
"Item %"PRIu32
" has netmask %"PRIu8
" negated:"
539 " %s; IP: %s; signum: %"PRIu32, i, tmphead->
netmask,
540 (tmphead->
negated) ?
"yes":
"no",
541 inet_ntoa(*(
struct in_addr*)&tmphead->
ip[0]),
543 tmphead = tmphead->
next;
563 static void SigNumArrayPrint(
void *tmp)
566 for (uint32_t u = 0; u < sna->
size; u++) {
567 uint8_t bitarray = sna->
array[u];
568 for (uint8_t i = 0; i < 8; i++) {
570 printf(
"%" PRIu32
" ", u * 8 + i);
571 bitarray = bitarray >> 1;
590 FatalError(
"Fatal error encountered in SigNumArrayNew. Exiting...");
594 if (new->array == NULL) {
598 new->size = io_ctx->
max_idx / 8 + 1;
618 FatalError(
"Fatal error encountered in SigNumArrayCopy. Exiting...");
621 new->size = orig->
size;
624 if (new->array == NULL) {
628 memcpy(new->array, orig->
array, orig->
size);
636 static void SigNumArrayFree(
void *tmp)
643 if (sna->
array != NULL)
664 int o_set = 0, n_set = 0, d_set = 0;
666 size_t size = strlen(s);
668 const char *rule_var_address = NULL;
669 char *temp_rule_var_address = NULL;
672 head = subhead = NULL;
674 SCLogDebug(
"s %s negate %s", s, negate ?
"true" :
"false");
676 for (u = 0, x = 0; u < size && x <
sizeof(
address); u++) {
680 if (!o_set && s[u] ==
'!') {
683 }
else if (s[u] ==
'[') {
689 }
else if (s[u] ==
']') {
695 (negate + n_set) % 2)) == NULL)
698 head = IPOnlyCIDRItemInsert(
head, subhead);
702 }
else if (depth == 0 && s[u] ==
',') {
705 }
else if (d_set == 1) {
710 if (rule_var_address == NULL)
713 if ((negate + n_set) % 2) {
715 const size_t str_size = strlen(rule_var_address) + 3 + 1;
716 temp_rule_var_address =
SCMalloc(str_size);
717 if (
unlikely(temp_rule_var_address == NULL)) {
721 snprintf(temp_rule_var_address, str_size,
"[%s]", rule_var_address);
723 temp_rule_var_address =
SCStrdup(rule_var_address);
724 if (
unlikely(temp_rule_var_address == NULL)) {
729 subhead = IPOnlyCIDRListParse2(
de_ctx, temp_rule_var_address,
730 (negate + n_set) % 2);
731 head = IPOnlyCIDRItemInsert(
head, subhead);
736 SCFree(temp_rule_var_address);
741 subhead = IPOnlyCIDRItemNew();
745 if (!((negate + n_set) % 2))
750 if (IPOnlyCIDRItemSetup(&subhead,
address) < 0) {
755 head = IPOnlyCIDRItemInsert(
head, subhead);
760 }
else if (depth == 0 && s[u] ==
'$') {
762 }
else if (depth == 0 && u == size - 1) {
773 if (rule_var_address == NULL)
776 if ((negate + n_set) % 2) {
778 const size_t str_size = strlen(rule_var_address) + 3 + 1;
779 temp_rule_var_address =
SCMalloc(str_size);
780 if (
unlikely(temp_rule_var_address == NULL)) {
783 snprintf(temp_rule_var_address, str_size,
"[%s]", rule_var_address);
785 temp_rule_var_address =
SCStrdup(rule_var_address);
786 if (
unlikely(temp_rule_var_address == NULL)) {
790 subhead = IPOnlyCIDRListParse2(
de_ctx, temp_rule_var_address,
791 (negate + n_set) % 2);
792 head = IPOnlyCIDRItemInsert(
head, subhead);
796 SCFree(temp_rule_var_address);
798 subhead = IPOnlyCIDRItemNew();
802 if (!((negate + n_set) % 2))
807 if (IPOnlyCIDRItemSetup(&subhead,
address) < 0) {
812 head = IPOnlyCIDRItemInsert(
head, subhead);
844 *gh = IPOnlyCIDRListParse2(
de_ctx,
str, 0);
846 SCLogDebug(
"IPOnlyCIDRListParse2 returned null");
869 Signature *s,
const char *addrstr,
char flag)
871 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
875 if (strcasecmp(addrstr,
"any") == 0) {
886 if (strcasecmp(addrstr,
"any") == 0) {
905 static const SCRadix4Config iponly_radix4_config = { SigNumArrayFree, SigNumArrayPrint };
906 static const SCRadix6Config iponly_radix6_config = { SigNumArrayFree, SigNumArrayPrint };
923 FatalError(
"Unable to allocate iponly signature tracking area");
970 static inline int IPOnlyMatchCompatSMs(
1004 void *user_data_src = NULL, *user_data_dst = NULL;
1024 src = user_data_src;
1025 dst = user_data_dst;
1027 if (
src == NULL ||
dst == NULL)
1030 for (uint32_t u = 0; u <
src->size; u++) {
1033 uint8_t bitarray =
dst->array[u] &
src->array[u];
1043 for (uint8_t i = 0; i < 8; i++, bitarray = bitarray >> 1) {
1044 if (bitarray & 0x01) {
1063 if (
p->
proto == IPPROTO_TCP ||
p->
proto == IPPROTO_UDP ||
1070 if (dport == NULL) {
1080 if (sport == NULL) {
1087 SCLogDebug(
"port-less protocol and sig needs ports");
1091 if (!IPOnlyMatchCompatSMs(
tv, det_ctx, s,
p)) {
1095 SCLogDebug(
"Signum %" PRIu32
" match (sid: %" PRIu32
", msg: %s)", u * 8 + i, s->
id,
1102 SCLogDebug(
"running match functions, sm %p", smd);
1124 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1125 if (
src->negated > 0)
1127 sna->
array[
src->signum / 8] &= ~tmp;
1130 sna->
array[
src->signum / 8] |= tmp;
1158 if (
src->family == AF_INET) {
1168 void *user_data = NULL;
1169 if (
src->netmask == 32)
1174 src->netmask, &user_data);
1175 if (user_data == NULL) {
1182 if (user_data == NULL) {
1187 IPOnlyPrepareUpdateBitarray(
src, sna);
1189 if (
src->netmask == 32)
1191 &iponly_radix4_config, (uint8_t *)&
src->ip[0], sna);
1194 &iponly_radix4_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1197 "src ipv4 radix tree");
1203 IPOnlyPrepareUpdateBitarray(
src, sna);
1205 if (
src->netmask == 32)
1207 &iponly_radix4_config, (uint8_t *)&
src->ip[0], sna);
1210 &iponly_radix4_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1211 if (node4 == NULL) {
1215 " src ipv4 radix tree ip %s netmask %" PRIu8,
1216 tmpstr,
src->netmask);
1225 IPOnlyPrepareUpdateBitarray(
src, sna);
1227 }
else if (
src->family == AF_INET6) {
1230 void *user_data = NULL;
1231 if (
src->netmask == 128)
1236 src->netmask, &user_data);
1237 if (user_data == NULL) {
1241 if (user_data == NULL) {
1244 IPOnlyPrepareUpdateBitarray(
src, sna);
1246 if (
src->netmask == 128)
1248 &iponly_radix6_config, (uint8_t *)&
src->ip[0], sna);
1251 &iponly_radix6_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1258 IPOnlyPrepareUpdateBitarray(
src, sna);
1260 if (
src->netmask == 128)
1262 &iponly_radix6_config, (uint8_t *)&
src->ip[0], sna);
1265 &iponly_radix6_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1273 IPOnlyPrepareUpdateBitarray(
src, sna);
1285 if (
dst->family == AF_INET) {
1287 SCLogDebug(
"Item has netmask %"PRIu8
" negated: %s; IP: %s; signum:"
1288 " %"PRIu32
"",
dst->netmask, (
dst->negated)?
"yes":
"no",
1289 inet_ntoa(*(
struct in_addr*)&
dst->ip[0]),
dst->signum);
1291 void *user_data = NULL;
1292 if (
dst->netmask == 32)
1297 dst->netmask, &user_data);
1298 if (user_data == NULL) {
1307 if (user_data == NULL) {
1312 IPOnlyPrepareUpdateBitarray(
dst, sna);
1314 if (
dst->netmask == 32)
1316 &iponly_radix4_config, (uint8_t *)&
dst->ip[0], sna);
1319 &iponly_radix4_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1328 IPOnlyPrepareUpdateBitarray(
dst, sna);
1330 if (
dst->netmask == 32)
1332 &iponly_radix4_config, (uint8_t *)&
dst->ip[0], sna);
1335 &iponly_radix4_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1346 IPOnlyPrepareUpdateBitarray(
dst, sna);
1348 }
else if (
dst->family == AF_INET6) {
1351 void *user_data = NULL;
1352 if (
dst->netmask == 128)
1357 dst->netmask, &user_data);
1358 if (user_data == NULL) {
1364 if (user_data == NULL) {
1367 IPOnlyPrepareUpdateBitarray(
dst, sna);
1369 if (
dst->netmask == 128)
1371 &iponly_radix6_config, (uint8_t *)&
dst->ip[0], sna);
1374 &iponly_radix6_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1381 IPOnlyPrepareUpdateBitarray(
dst, sna);
1383 if (
dst->netmask == 128)
1385 &iponly_radix6_config, (uint8_t *)&
dst->ip[0], sna);
1388 &iponly_radix6_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1396 IPOnlyPrepareUpdateBitarray(
dst, sna);
1420 SCLogDebug(
"Adding IPs from rule: %" PRIu32
" (%s) as %" PRIu32
" mapped to %" PRIu32
"\n",
1421 s->
id, s->
msg, s->
iid, mapped_signum);
1434 if (mapped_signum > io_ctx->
max_idx)
1435 io_ctx->
max_idx = mapped_signum;
1448 static int IPOnlyTestSig01(
void)
1468 static int IPOnlyTestSig02 (
void)
1488 static int IPOnlyTestSig03 (
void)
1500 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (pcre and content) \"; content:\"php\"; pcre:\"/require(_once)?/i\"; classtype:misc-activity; sid:400001; rev:1;)");
1506 printf(
"got a IPOnly signature (content): ");
1512 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (content) \"; content:\"match something\"; classtype:misc-activity; sid:400001; rev:1;)");
1518 printf(
"got a IPOnly signature (content): ");
1524 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (uricontent) \"; uricontent:\"match something\"; classtype:misc-activity; sid:400001; rev:1;)");
1530 printf(
"got a IPOnly signature (uricontent): ");
1536 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (pcre) \"; pcre:\"/e?idps rule[sz]/i\"; classtype:misc-activity; sid:400001; rev:1;)");
1542 printf(
"got a IPOnly signature (pcre): ");
1548 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (flow) \"; flow:to_server; classtype:misc-activity; sid:400001; rev:1;)");
1554 printf(
"got a IPOnly signature (flow): ");
1560 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (dsize) \"; dsize:100; classtype:misc-activity; sid:400001; rev:1;)");
1566 printf(
"got a IPOnly signature (dsize): ");
1572 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (flowbits) \"; flowbits:unset; classtype:misc-activity; sid:400001; rev:1;)");
1578 printf(
"got a IPOnly signature (flowbits): ");
1584 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (flowvar) \"; pcre:\"/(?<flow_var>.*)/i\"; flowvar:var,\"str\"; classtype:misc-activity; sid:400001; rev:1;)");
1590 printf(
"got a IPOnly signature (flowvar): ");
1596 s =
SigInit(
de_ctx,
"alert tcp any any -> any any (msg:\"SigTest40-03 sig is not IPOnly (pktvar) \"; pcre:\"/(?<pkt_var>.*)/i\"; pktvar:var,\"str\"; classtype:misc-activity; sid:400001; rev:1;)");
1602 printf(
"got a IPOnly signature (pktvar): ");
1616 static int IPOnlyTestSig04 (
void)
1622 for (
int size = 0; size < 6; size++) {
1626 new = IPOnlyCIDRItemNew();
1630 head = IPOnlyCIDRItemInsert(
head,
new);
1634 new = IPOnlyCIDRItemNew();
1637 head = IPOnlyCIDRItemInsert(
head,
new);
1641 new = IPOnlyCIDRItemNew();
1644 head = IPOnlyCIDRItemInsert(
head,
new);
1648 new = IPOnlyCIDRItemNew();
1652 head = IPOnlyCIDRItemInsert(
head,
new);
1656 new = IPOnlyCIDRItemNew();
1660 head = IPOnlyCIDRItemInsert(
head,
new);
1663 IPOnlyCIDRListPrint(
head);
1665 IPOnlyCIDRListQSort(&
head);
1684 if (new->netmask != 9) {
1692 if (new->netmask != 10 || new->ip[0] != 1) {
1700 if (new->netmask != 10 || new->ip[0] != 2) {
1708 if (new->netmask != 10 || new->ip[0] != 3) {
1716 if (new->netmask != 11) {
1744 static int IPOnlyTestSig05(
void)
1747 uint8_t *buf = (uint8_t *)
"Hi all!";
1748 uint16_t buflen = strlen((
char *)buf);
1750 uint8_t numpkts = 1;
1751 uint8_t numsigs = 7;
1757 const char *sigs[numsigs];
1758 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1759 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1760 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1761 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1762 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1763 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1764 sigs[6]=
"alert tcp 192.168.1.0/24 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 7)\"; content:\"Hi all\";sid:7;)";
1767 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1768 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1770 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1781 static int IPOnlyTestSig06(
void)
1784 uint8_t *buf = (uint8_t *)
"Hi all!";
1785 uint16_t buflen = strlen((
char *)buf);
1787 uint8_t numpkts = 1;
1788 uint8_t numsigs = 7;
1794 const char *sigs[numsigs];
1795 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1796 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1797 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1798 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1799 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1800 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1801 sigs[6]=
"alert tcp 192.168.1.0/24 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 7)\"; content:\"Hi all\";sid:7;)";
1804 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1805 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1807 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1822 static int IPOnlyTestSig07(
void)
1825 uint8_t *buf = (uint8_t *)
"Hi all!";
1826 uint16_t buflen = strlen((
char *)buf);
1828 uint8_t numpkts = 1;
1829 uint8_t numsigs = 7;
1835 char *sigs[numsigs];
1836 sigs[0]=
"alert tcp 192.168.1.5 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
1837 sigs[1]=
"alert tcp [192.168.1.2,192.168.1.5,192.168.1.4] any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 2)\"; sid:2;)";
1838 sigs[2]=
"alert tcp [192.168.1.0/24,!192.168.1.1] any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1839 sigs[3]=
"alert tcp [192.0.0.0/8,!192.168.0.0/16,192.168.1.0/24,!192.168.1.1] any -> [192.168.1.0/24,!192.168.1.5] any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1840 sigs[4]=
"alert tcp any any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1841 sigs[5]=
"alert tcp any any -> [192.168.0.0/16,!192.168.1.0/24,192.168.1.1] any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1842 sigs[6]=
"alert tcp [78.129.202.0/24,192.168.1.5,78.129.205.64,78.129.214.103,78.129.223.19,78.129.233.17,78.137.168.33,78.140.132.11,78.140.133.15,78.140.138.105,78.140.139.105,78.140.141.107,78.140.141.114,78.140.143.103,78.140.143.13,78.140.145.144,78.140.170.164,78.140.23.18,78.143.16.7,78.143.46.124,78.157.129.71] any -> 192.168.1.1 any (msg:\"ET RBN Known Russian Business Network IP TCP - BLOCKING (246)\"; sid:7;)";
1845 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1846 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1848 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1860 static int IPOnlyTestSig08(
void)
1863 uint8_t *buf = (uint8_t *)
"Hi all!";
1864 uint16_t buflen = strlen((
char *)buf);
1866 uint8_t numpkts = 1;
1867 uint8_t numsigs = 7;
1873 const char *sigs[numsigs];
1874 sigs[0]=
"alert tcp 192.168.1.5 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
1875 sigs[1]=
"alert tcp [192.168.1.2,192.168.1.5,192.168.1.4] any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 2)\"; sid:2;)";
1876 sigs[2]=
"alert tcp [192.168.1.0/24,!192.168.1.1] any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1877 sigs[3]=
"alert tcp [192.0.0.0/8,!192.168.0.0/16,192.168.1.0/24,!192.168.1.1] any -> [192.168.1.0/24,!192.168.1.5] any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1878 sigs[4]=
"alert tcp any any -> !192.168.1.5 any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1879 sigs[5]=
"alert tcp any any -> [192.168.0.0/16,!192.168.1.0/24,192.168.1.1] any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1880 sigs[6]=
"alert tcp [78.129.202.0/24,192.168.1.5,78.129.205.64,78.129.214.103,78.129.223.19,78.129.233.17,78.137.168.33,78.140.132.11,78.140.133.15,78.140.138.105,78.140.139.105,78.140.141.107,78.140.141.114,78.140.143.103,78.140.143.13,78.140.145.144,78.140.170.164,78.140.23.18,78.143.16.7,78.143.46.124,78.157.129.71] any -> 192.168.1.1 any (msg:\"ET RBN Known Russian Business Network IP TCP - BLOCKING (246)\"; sid:7;)";
1883 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1884 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1886 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1897 static int IPOnlyTestSig09(
void)
1900 uint8_t *buf = (uint8_t *)
"Hi all!";
1901 uint16_t buflen = strlen((
char *)buf);
1903 uint8_t numpkts = 1;
1904 uint8_t numsigs = 7;
1908 p[0] =
UTHBuildPacketIPV6SrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565",
"3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562");
1910 const char *sigs[numsigs];
1911 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1912 sigs[1]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1913 sigs[2]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1914 sigs[3]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:0/96 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1915 sigs[4]=
"alert tcp 3FFE:FFFF:7654:FEDA:0:0:0:0/64 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1916 sigs[5]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:0:0:0:0/64 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1917 sigs[6]=
"alert tcp 3FFE:FFFF:7654:FEDA:0:0:0:0/64 any -> 3FFE:FFFF:7654:FEDA:0:0:0:0/64 any (msg:\"Testing src/dst ip (sid 7)\"; content:\"Hi all\";sid:7;)";
1920 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1921 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1923 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1934 static int IPOnlyTestSig10(
void)
1937 uint8_t *buf = (uint8_t *)
"Hi all!";
1938 uint16_t buflen = strlen((
char *)buf);
1940 uint8_t numpkts = 1;
1941 uint8_t numsigs = 7;
1945 p[0] =
UTHBuildPacketIPV6SrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562",
"3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565");
1947 const char *sigs[numsigs];
1948 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1949 sigs[1]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1950 sigs[2]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1951 sigs[3]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> !3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562/96 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1952 sigs[4]=
"alert tcp !3FFE:FFFF:7654:FEDA:0:0:0:0/64 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1953 sigs[5]=
"alert tcp any any -> !3FFE:FFFF:7654:FEDA:0:0:0:0/64 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1954 sigs[6]=
"alert tcp 3FFE:FFFF:7654:FEDA:0:0:0:0/64 any -> 3FFE:FFFF:7654:FEDB:0:0:0:0/64 any (msg:\"Testing src/dst ip (sid 7)\"; content:\"Hi all\";sid:7;)";
1957 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1958 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1960 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1975 static int IPOnlyTestSig11(
void)
1978 uint8_t *buf = (uint8_t *)
"Hi all!";
1979 uint16_t buflen = strlen((
char *)buf);
1981 uint8_t numpkts = 2;
1982 uint8_t numsigs = 7;
1986 p[0] =
UTHBuildPacketIPV6SrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565",
"3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562");
1989 char *sigs[numsigs];
1990 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565,192.168.1.1 any -> 3FFE:FFFF:7654:FEDA:0:0:0:0/64,192.168.1.5 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
1991 sigs[1]=
"alert tcp [192.168.1.1,3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565,192.168.1.4,192.168.1.5,!192.168.1.0/24] any -> [3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.0/24] any (msg:\"Testing src/dst ip (sid 2)\"; sid:2;)";
1992 sigs[2]=
"alert tcp [3FFE:FFFF:7654:FEDA:0:0:0:0/64,!3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.1] any -> [3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.5] any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1993 sigs[3]=
"alert tcp [3FFE:FFFF:0:0:0:0:0:0/32,!3FFE:FFFF:7654:FEDA:0:0:0:0/64,3FFE:FFFF:7654:FEDA:0:0:0:0/64,!3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.1] any -> [3FFE:FFFF:7654:FEDA:0:0:0:0/64,192.168.1.0/24,!3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565] any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1994 sigs[4]=
"alert tcp any any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1995 sigs[5]=
"alert tcp any any -> [3FFE:FFFF:7654:FEDA:0:0:0:0/64,!3FFE:FFFF:7654:FEDA:0:0:0:0/64,3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.5] any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1996 sigs[6]=
"alert tcp [78.129.202.0/24,3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565,192.168.1.1,78.129.205.64,78.129.214.103,78.129.223.19,78.129.233.17,78.137.168.33,78.140.132.11,78.140.133.15,78.140.138.105,78.140.139.105,78.140.141.107,78.140.141.114,78.140.143.103,78.140.143.13,78.140.145.144,78.140.170.164,78.140.23.18,78.143.16.7,78.143.46.124,78.157.129.71] any -> [3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.0.0.0/8] any (msg:\"ET RBN Known Russian Business Network IP TCP - BLOCKING (246)\"; sid:7;)";
1999 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2000 uint32_t results[2][7] = {{ 1, 1, 1, 1, 1, 1, 1}, { 1, 1, 1, 1, 1, 1, 1}};
2002 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2014 static int IPOnlyTestSig12(
void)
2017 uint8_t *buf = (uint8_t *)
"Hi all!";
2018 uint16_t buflen = strlen((
char *)buf);
2020 uint8_t numpkts = 2;
2021 uint8_t numsigs = 7;
2025 p[0] =
UTHBuildPacketIPV6SrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"3FBE:FFFF:7654:FEDA:1245:BA98:3210:4562",
"3FBE:FFFF:7654:FEDA:1245:BA98:3210:4565");
2028 const char *sigs[numsigs];
2029 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565,192.168.1.1 any -> 3FFE:FFFF:7654:FEDA:0:0:0:0/64,192.168.1.5 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
2030 sigs[1]=
"alert tcp [192.168.1.1,3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565,192.168.1.4,192.168.1.5,!192.168.1.0/24] any -> [3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.0/24] any (msg:\"Testing src/dst ip (sid 2)\"; sid:2;)";
2031 sigs[2]=
"alert tcp [3FFE:FFFF:7654:FEDA:0:0:0:0/64,!3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.1] any -> [3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.5] any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
2032 sigs[3]=
"alert tcp [3FFE:FFFF:0:0:0:0:0:0/32,!3FFE:FFFF:7654:FEDA:0:0:0:0/64,3FFE:FFFF:7654:FEDA:0:0:0:0/64,!3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.1] any -> [3FFE:FFFF:7654:FEDA:0:0:0:0/64,192.168.1.0/24,!3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565] any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
2033 sigs[4]=
"alert tcp any any -> [!3FBE:FFFF:7654:FEDA:1245:BA98:3210:4565,!80.198.1.5] any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
2034 sigs[5]=
"alert tcp any any -> [3FFE:FFFF:7654:FEDA:0:0:0:0/64,!3FFE:FFFF:7654:FEDA:0:0:0:0/64,3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.168.1.5] any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
2035 sigs[6]=
"alert tcp [78.129.202.0/24,3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565,192.168.1.1,78.129.205.64,78.129.214.103,78.129.223.19,78.129.233.17,78.137.168.33,78.140.132.11,78.140.133.15,78.140.138.105,78.140.139.105,78.140.141.107,78.140.141.114,78.140.143.103,78.140.143.13,78.140.145.144,78.140.170.164,78.140.23.18,78.143.16.7,78.143.46.124,78.157.129.71] any -> [3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562,192.0.0.0/8] any (msg:\"ET RBN Known Russian Business Network IP TCP - BLOCKING (246)\"; sid:7;)";
2038 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2039 uint32_t results[2][7] = {{ 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}};
2041 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2048 static int IPOnlyTestSig13(
void)
2055 "alert tcp any any -> any any (msg:\"Test flowbits ip only\"; "
2056 "flowbits:set,myflow1; sid:1; rev:1;)");
2065 static int IPOnlyTestSig14(
void)
2072 "alert tcp any any -> any any (msg:\"Test flowbits ip only\"; "
2073 "flowbits:set,myflow1; flowbits:isset,myflow2; sid:1; rev:1;)");
2082 static int IPOnlyTestSig15(
void)
2085 uint8_t *buf = (uint8_t *)
"Hi all!";
2086 uint16_t buflen = strlen((
char *)buf);
2088 uint8_t numpkts = 1;
2089 uint8_t numsigs = 7;
2094 memset(&
f, 0,
sizeof(
Flow));
2105 const char *sigs[numsigs];
2106 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; "
2107 "flowbits:set,one; sid:1;)";
2108 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; "
2109 "flowbits:set,two; sid:2;)";
2110 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; "
2111 "flowbits:set,three; sid:3;)";
2112 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; "
2113 "flowbits:set,four; sid:4;)";
2114 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; "
2115 "flowbits:set,five; sid:5;)";
2116 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; "
2117 "flowbits:set,six; sid:6;)";
2118 sigs[6]=
"alert tcp 192.168.1.0/24 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 7)\"; "
2119 "flowbits:set,seven; content:\"Hi all\"; sid:7;)";
2122 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2123 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
2125 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2136 static int IPOnlyTestSig16(
void)
2139 uint8_t *buf = (uint8_t *)
"Hi all!";
2140 uint16_t buflen = strlen((
char *)buf);
2142 uint8_t numpkts = 1;
2143 uint8_t numsigs = 2;
2149 const char *sigs[numsigs];
2150 sigs[0]=
"alert tcp !100.100.0.1 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2151 sigs[1]=
"alert tcp any any -> !50.0.0.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2154 uint32_t sid[2] = { 1, 2};
2155 uint32_t results[2] = { 1, 1};
2157 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2167 static int IPOnlyTestSig17(
void)
2170 uint8_t *buf = (uint8_t *)
"Hi all!";
2171 uint16_t buflen = strlen((
char *)buf);
2173 uint8_t numpkts = 1;
2174 uint8_t numsigs = 2;
2180 const char *sigs[numsigs];
2181 sigs[0]=
"alert ip 100.100.0.0 80 -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2182 sigs[1]=
"alert ip any any -> 50.0.0.0 123 (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2184 uint32_t sid[2] = { 1, 2};
2185 uint32_t results[2] = { 0, 0};
2187 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2197 static int IPOnlyTestSig18(
void)
2200 uint8_t *buf = (uint8_t *)
"Hi all!";
2201 uint16_t buflen = strlen((
char *)buf);
2203 uint8_t numpkts = 4;
2204 uint8_t numsigs = 4;
2213 const char *sigs[numsigs];
2215 sigs[0]=
"alert ip 1.2.3.4-219.6.7.8 any -> any any (sid:1;)";
2216 sigs[1]=
"alert ip 51.2.3.4-253.1.2.3 any -> any any (sid:2;)";
2217 sigs[2]=
"alert ip 0.0.0.0-50.0.0.2 any -> any any (sid:3;)";
2218 sigs[3]=
"alert ip 50.0.0.0-255.255.255.255 any -> any any (sid:4;)";
2220 uint32_t sid[4] = { 1, 2, 3, 4, };
2221 uint32_t results[4][4] = {
2222 { 1, 0, 1, 0, }, { 0, 1, 0, 1}, { 0, 0, 1, 0 }, { 0, 0, 0, 1}};
2224 result =
UTHGenericTest(
p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2234 static int IPOnlyTestBug5066v1(
void)
2241 de_ctx,
"alert ip [1.2.3.4/24,1.2.3.64/27] any -> any any (sid:1;)");
2252 static int IPOnlyTestBug5066v2(
void)
2257 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.4/24") != 0);
2260 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2270 static int IPOnlyTestBug5066v3(
void)
2275 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.64/26") != 0);
2278 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2288 static int IPOnlyTestBug5066v4(
void)
2293 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"2000::1:1/122") != 0);
2296 PrintInet(AF_INET6, (
const void *)&x->
ip, ip,
sizeof(ip));
2299 FAIL_IF_NOT(strcmp(ip,
"2000:0000:0000:0000:0000:0000:0001:0000") == 0);
2306 static int IPOnlyTestBug5066v5(
void)
2311 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"2000::1:40/122") != 0);
2314 PrintInet(AF_INET6, (
const void *)&x->
ip, ip,
sizeof(ip));
2317 FAIL_IF_NOT(strcmp(ip,
"2000:0000:0000:0000:0000:0000:0001:0040") == 0);
2324 static int IPOnlyTestBug5168v1(
void)
2329 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.64/0.0.0.0") != 0);
2332 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2342 static int IPOnlyTestBug5168v2(
void)
2346 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"0.0.0.5/0.0.0.5") != -1);