65 #include <netinet/in.h>
96 for (; i < lhs->
netmask / 32 || i < 1; i++) {
97 if (lhs->
ip[i] < rhs->
ip[i])
99 if (lhs->
ip[i] > rhs->
ip[i])
108 static int IPOnlyCIDRItemCompare(
const void *lhsv,
const void *rhsv)
113 return IPOnlyCIDRItemCompareReal(lhs, rhs);
134 SCLogError(
"Failed to allocate enough memory to sort IP-only CIDR items.");
139 for (
size_t i = 0; i <
len; i++) {
150 for (
size_t i = 0; i + 1 <
len; i++) {
151 tmp[i]->
next = tmp[i + 1];
163 static int InsertRange(
169 uint32_t first = first_in;
170 uint32_t last = last_in;
178 while (dd->
netmask > 0 && (first & (1UL << (32 - dd->
netmask))) == 0 &&
179 first + (1UL << (32 - (dd->
netmask - 1))) - 1 <= last) {
182 dd->
ip[0] = htonl(first);
183 first += 1UL << (32 - dd->
netmask);
185 while (first <= last && first != 0) {
192 while (new->netmask > 0 && (first & (1UL << (32 - new->netmask))) == 0 &&
193 first + (1UL << (32 - (new->netmask - 1))) - 1 <= last) {
196 new->ip[0] = htonl(first);
197 first += 1UL << (32 -
new->netmask);
198 dd = IPOnlyCIDRItemInsert(dd,
new);
222 char *ip = NULL, *ip2 = NULL;
227 while (*
str !=
'\0' && *
str ==
' ')
235 if (strcasecmp(
str,
"any") == 0) {
237 SCLogDebug(
"adding 0.0.0.0/0 and ::/0 as we\'re handling \'any\'");
239 IPOnlyCIDRItemParseSingle(&dd,
"0.0.0.0/0");
242 dd->
next = IPOnlyCIDRItemNew();
243 if (dd->
next == NULL)
246 IPOnlyCIDRItemParseSingle(&dd->
next,
"::/0");
260 if ((strchr(
str,
':')) == NULL) {
266 if ((mask = strchr(ip,
'/')) != NULL) {
268 ip[mask - ip] =
'\0';
270 uint32_t netmask = 0;
273 if ((strchr (mask,
'.')) == NULL) {
276 for (u = 0; u < strlen(mask); u++) {
277 if(!isdigit((
unsigned char)mask[u]))
289 r = inet_pton(AF_INET, mask, &in);
300 r = inet_pton(AF_INET, ip, &in);
304 dd->
ip[0] = in.s_addr & netmask;
306 }
else if ((ip2 = strchr(ip,
'-')) != NULL) {
311 uint32_t first, last;
313 r = inet_pton(AF_INET, ip, &in);
318 r = inet_pton(AF_INET, ip2, &in);
327 SCLogDebug(
"Creating CIDR range for [%s - %s]", ip, ip2);
328 return InsertRange(pdd, dd, first, last);
331 r = inet_pton(AF_INET, ip, &in);
336 dd->
ip[0] = in.s_addr;
341 struct in6_addr in6, mask6;
342 uint32_t ip6addr[4], netmask[4];
346 if ((mask = strchr(ip,
'/')) != NULL) {
350 r = inet_pton(AF_INET6, ip, &in6);
356 (
const char *)mask, 0, 128) < 0) {
360 memcpy(&ip6addr, &in6.s6_addr,
sizeof(ip6addr));
362 memcpy(&netmask, &mask6.s6_addr,
sizeof(netmask));
364 dd->
ip[0] = ip6addr[0] & netmask[0];
365 dd->
ip[1] = ip6addr[1] & netmask[1];
366 dd->
ip[2] = ip6addr[2] & netmask[2];
367 dd->
ip[3] = ip6addr[3] & netmask[3];
369 r = inet_pton(AF_INET6, ip, &in6);
373 memcpy(dd->
ip, &in6.s6_addr,
sizeof(dd->
ip));
403 if (IPOnlyCIDRItemParseSingle(gh, s) == -1) {
404 SCLogError(
"address parsing error \"%s\"", s);
449 SCLogDebug(
"Head is NULL to insert item (%p)",item);
461 while (prev != NULL) {
469 head = IPOnlyCIDRItemInsertReal(
head, prev);
490 if (tmphead == NULL) {
501 SCLogDebug(
"Item(%p) %"PRIu32
" removed", it, i);
521 while (tmphead != NULL) {
523 tmphead = tmphead->
next;
537 while (tmphead != NULL) {
539 SCLogDebug(
"Item %"PRIu32
" has netmask %"PRIu8
" negated:"
540 " %s; IP: %s; signum: %"PRIu32, i, tmphead->
netmask,
541 (tmphead->
negated) ?
"yes":
"no",
542 inet_ntoa(*(
struct in_addr*)&tmphead->
ip[0]),
544 tmphead = tmphead->
next;
564 static void SigNumArrayPrint(
void *tmp)
567 for (uint32_t u = 0; u < sna->
size; u++) {
568 uint8_t bitarray = sna->
array[u];
569 for (uint8_t i = 0; i < 8; i++) {
571 printf(
"%" PRIu32
" ", u * 8 + i);
572 bitarray = bitarray >> 1;
591 FatalError(
"Fatal error encountered in SigNumArrayNew. Exiting...");
595 if (new->array == NULL) {
599 new->size = io_ctx->
max_idx / 8 + 1;
619 FatalError(
"Fatal error encountered in SigNumArrayCopy. Exiting...");
622 new->size = orig->
size;
625 if (new->array == NULL) {
629 memcpy(new->array, orig->
array, orig->
size);
637 static void SigNumArrayFree(
void *tmp)
644 if (sna->
array != NULL)
665 int o_set = 0, n_set = 0, d_set = 0;
667 size_t size = strlen(s);
669 const char *rule_var_address = NULL;
670 char *temp_rule_var_address = NULL;
673 head = subhead = NULL;
675 SCLogDebug(
"s %s negate %s", s, negate ?
"true" :
"false");
677 for (u = 0, x = 0; u < size && x <
sizeof(
address); u++) {
681 if (!o_set && s[u] ==
'!') {
684 }
else if (s[u] ==
'[') {
690 }
else if (s[u] ==
']') {
696 (negate + n_set) % 2)) == NULL)
699 head = IPOnlyCIDRItemInsert(
head, subhead);
703 }
else if (depth == 0 && s[u] ==
',') {
706 }
else if (d_set == 1) {
711 if (rule_var_address == NULL)
714 if ((negate + n_set) % 2) {
715 temp_rule_var_address =
SCMalloc(strlen(rule_var_address) + 3);
716 if (
unlikely(temp_rule_var_address == NULL)) {
720 snprintf(temp_rule_var_address, strlen(rule_var_address) + 3,
721 "[%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) {
777 temp_rule_var_address =
SCMalloc(strlen(rule_var_address) + 3);
778 if (
unlikely(temp_rule_var_address == NULL)) {
781 snprintf(temp_rule_var_address, strlen(rule_var_address) + 3,
782 "[%s]", rule_var_address);
784 temp_rule_var_address =
SCStrdup(rule_var_address);
785 if (
unlikely(temp_rule_var_address == NULL)) {
789 subhead = IPOnlyCIDRListParse2(
de_ctx, temp_rule_var_address,
790 (negate + n_set) % 2);
791 head = IPOnlyCIDRItemInsert(
head, subhead);
795 SCFree(temp_rule_var_address);
797 subhead = IPOnlyCIDRItemNew();
801 if (!((negate + n_set) % 2))
806 if (IPOnlyCIDRItemSetup(&subhead,
address) < 0) {
811 head = IPOnlyCIDRItemInsert(
head, subhead);
843 *gh = IPOnlyCIDRListParse2(
de_ctx,
str, 0);
845 SCLogDebug(
"IPOnlyCIDRListParse2 returned null");
868 Signature *s,
const char *addrstr,
char flag)
870 SCLogDebug(
"Address Group \"%s\" to be parsed now", addrstr);
874 if (strcasecmp(addrstr,
"any") == 0) {
885 if (strcasecmp(addrstr,
"any") == 0) {
904 static const SCRadix4Config iponly_radix4_config = { SigNumArrayFree, SigNumArrayPrint };
905 static const SCRadix6Config iponly_radix6_config = { SigNumArrayFree, SigNumArrayPrint };
922 FatalError(
"Unable to allocate iponly signature tracking area");
969 static inline int IPOnlyMatchCompatSMs(
1003 void *user_data_src = NULL, *user_data_dst = NULL;
1023 src = user_data_src;
1024 dst = user_data_dst;
1026 if (
src == NULL ||
dst == NULL)
1029 for (uint32_t u = 0; u <
src->size; u++) {
1032 uint8_t bitarray =
dst->array[u] &
src->array[u];
1042 for (uint8_t i = 0; i < 8; i++, bitarray = bitarray >> 1) {
1043 if (bitarray & 0x01) {
1060 if (p->
proto == IPPROTO_TCP || p->
proto == IPPROTO_UDP ||
1067 if (dport == NULL) {
1077 if (sport == NULL) {
1084 SCLogDebug(
"port-less protocol and sig needs ports");
1088 if (!IPOnlyMatchCompatSMs(
tv, det_ctx, s, p)) {
1092 SCLogDebug(
"Signum %" PRIu32
" match (sid: %" PRIu32
", msg: %s)", u * 8 + i, s->
id,
1099 SCLogDebug(
"running match functions, sm %p", smd);
1121 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1122 if (
src->negated > 0)
1124 sna->
array[
src->signum / 8] &= ~tmp;
1127 sna->
array[
src->signum / 8] |= tmp;
1155 if (
src->family == AF_INET) {
1165 void *user_data = NULL;
1166 if (
src->netmask == 32)
1171 src->netmask, &user_data);
1172 if (user_data == NULL) {
1179 if (user_data == NULL) {
1184 IPOnlyPrepareUpdateBitarray(
src, sna);
1186 if (
src->netmask == 32)
1188 &iponly_radix4_config, (uint8_t *)&
src->ip[0], sna);
1191 &iponly_radix4_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1194 "src ipv4 radix tree");
1200 IPOnlyPrepareUpdateBitarray(
src, sna);
1202 if (
src->netmask == 32)
1204 &iponly_radix4_config, (uint8_t *)&
src->ip[0], sna);
1207 &iponly_radix4_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1208 if (node4 == NULL) {
1212 " src ipv4 radix tree ip %s netmask %" PRIu8,
1213 tmpstr,
src->netmask);
1222 IPOnlyPrepareUpdateBitarray(
src, sna);
1224 }
else if (
src->family == AF_INET6) {
1227 void *user_data = NULL;
1228 if (
src->netmask == 128)
1233 src->netmask, &user_data);
1234 if (user_data == NULL) {
1238 if (user_data == NULL) {
1241 IPOnlyPrepareUpdateBitarray(
src, sna);
1243 if (
src->netmask == 128)
1245 &iponly_radix6_config, (uint8_t *)&
src->ip[0], sna);
1248 &iponly_radix6_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1255 IPOnlyPrepareUpdateBitarray(
src, sna);
1257 if (
src->netmask == 128)
1259 &iponly_radix6_config, (uint8_t *)&
src->ip[0], sna);
1262 &iponly_radix6_config, (uint8_t *)&
src->ip[0],
src->netmask, sna);
1270 IPOnlyPrepareUpdateBitarray(
src, sna);
1282 if (
dst->family == AF_INET) {
1284 SCLogDebug(
"Item has netmask %"PRIu8
" negated: %s; IP: %s; signum:"
1285 " %"PRIu32
"",
dst->netmask, (
dst->negated)?
"yes":
"no",
1286 inet_ntoa(*(
struct in_addr*)&
dst->ip[0]),
dst->signum);
1288 void *user_data = NULL;
1289 if (
dst->netmask == 32)
1294 dst->netmask, &user_data);
1295 if (user_data == NULL) {
1304 if (user_data == NULL) {
1309 IPOnlyPrepareUpdateBitarray(
dst, sna);
1311 if (
dst->netmask == 32)
1313 &iponly_radix4_config, (uint8_t *)&
dst->ip[0], sna);
1316 &iponly_radix4_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1325 IPOnlyPrepareUpdateBitarray(
dst, sna);
1327 if (
dst->netmask == 32)
1329 &iponly_radix4_config, (uint8_t *)&
dst->ip[0], sna);
1332 &iponly_radix4_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1343 IPOnlyPrepareUpdateBitarray(
dst, sna);
1345 }
else if (
dst->family == AF_INET6) {
1348 void *user_data = NULL;
1349 if (
dst->netmask == 128)
1354 dst->netmask, &user_data);
1355 if (user_data == NULL) {
1361 if (user_data == NULL) {
1364 IPOnlyPrepareUpdateBitarray(
dst, sna);
1366 if (
dst->netmask == 128)
1368 &iponly_radix6_config, (uint8_t *)&
dst->ip[0], sna);
1371 &iponly_radix6_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1378 IPOnlyPrepareUpdateBitarray(
dst, sna);
1380 if (
dst->netmask == 128)
1382 &iponly_radix6_config, (uint8_t *)&
dst->ip[0], sna);
1385 &iponly_radix6_config, (uint8_t *)&
dst->ip[0],
dst->netmask, sna);
1393 IPOnlyPrepareUpdateBitarray(
dst, sna);
1417 SCLogDebug(
"Adding IPs from rule: %" PRIu32
" (%s) as %" PRIu32
" mapped to %" PRIu32
"\n",
1418 s->
id, s->
msg, s->
num, mapped_signum);
1431 if (mapped_signum > io_ctx->
max_idx)
1432 io_ctx->
max_idx = mapped_signum;
1445 static int IPOnlyTestSig01(
void)
1465 static int IPOnlyTestSig02 (
void)
1485 static int IPOnlyTestSig03 (
void)
1497 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;)");
1503 printf(
"got a IPOnly signature (content): ");
1509 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;)");
1515 printf(
"got a IPOnly signature (content): ");
1521 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;)");
1527 printf(
"got a IPOnly signature (uricontent): ");
1533 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;)");
1539 printf(
"got a IPOnly signature (pcre): ");
1545 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;)");
1551 printf(
"got a IPOnly signature (flow): ");
1557 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;)");
1563 printf(
"got a IPOnly signature (dsize): ");
1569 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;)");
1575 printf(
"got a IPOnly signature (flowbits): ");
1581 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;)");
1587 printf(
"got a IPOnly signature (flowvar): ");
1593 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;)");
1599 printf(
"got a IPOnly signature (pktvar): ");
1613 static int IPOnlyTestSig04 (
void)
1619 for (
int size = 0; size < 6; size++) {
1623 new = IPOnlyCIDRItemNew();
1627 head = IPOnlyCIDRItemInsert(
head,
new);
1631 new = IPOnlyCIDRItemNew();
1634 head = IPOnlyCIDRItemInsert(
head,
new);
1638 new = IPOnlyCIDRItemNew();
1641 head = IPOnlyCIDRItemInsert(
head,
new);
1645 new = IPOnlyCIDRItemNew();
1649 head = IPOnlyCIDRItemInsert(
head,
new);
1653 new = IPOnlyCIDRItemNew();
1657 head = IPOnlyCIDRItemInsert(
head,
new);
1660 IPOnlyCIDRListPrint(
head);
1662 IPOnlyCIDRListQSort(&
head);
1681 if (new->netmask != 9) {
1689 if (new->netmask != 10 || new->ip[0] != 1) {
1697 if (new->netmask != 10 || new->ip[0] != 2) {
1705 if (new->netmask != 10 || new->ip[0] != 3) {
1713 if (new->netmask != 11) {
1741 static int IPOnlyTestSig05(
void)
1744 uint8_t *buf = (uint8_t *)
"Hi all!";
1745 uint16_t buflen = strlen((
char *)buf);
1747 uint8_t numpkts = 1;
1748 uint8_t numsigs = 7;
1754 const char *sigs[numsigs];
1755 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1756 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1757 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1758 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1759 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1760 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1761 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;)";
1764 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1765 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1767 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1778 static int IPOnlyTestSig06(
void)
1781 uint8_t *buf = (uint8_t *)
"Hi all!";
1782 uint16_t buflen = strlen((
char *)buf);
1784 uint8_t numpkts = 1;
1785 uint8_t numsigs = 7;
1789 p[0] =
UTHBuildPacketSrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"80.58.0.33",
"195.235.113.3");
1791 const char *sigs[numsigs];
1792 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1793 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1794 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1795 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1796 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1797 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1798 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;)";
1801 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1802 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1804 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1819 static int IPOnlyTestSig07(
void)
1822 uint8_t *buf = (uint8_t *)
"Hi all!";
1823 uint16_t buflen = strlen((
char *)buf);
1825 uint8_t numpkts = 1;
1826 uint8_t numsigs = 7;
1832 char *sigs[numsigs];
1833 sigs[0]=
"alert tcp 192.168.1.5 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
1834 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;)";
1835 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;)";
1836 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;)";
1837 sigs[4]=
"alert tcp any any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1838 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;)";
1839 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;)";
1842 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1843 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1845 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1857 static int IPOnlyTestSig08(
void)
1860 uint8_t *buf = (uint8_t *)
"Hi all!";
1861 uint16_t buflen = strlen((
char *)buf);
1863 uint8_t numpkts = 1;
1864 uint8_t numsigs = 7;
1870 const char *sigs[numsigs];
1871 sigs[0]=
"alert tcp 192.168.1.5 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
1872 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;)";
1873 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;)";
1874 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;)";
1875 sigs[4]=
"alert tcp any any -> !192.168.1.5 any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1876 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;)";
1877 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;)";
1880 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1881 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1883 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1894 static int IPOnlyTestSig09(
void)
1897 uint8_t *buf = (uint8_t *)
"Hi all!";
1898 uint16_t buflen = strlen((
char *)buf);
1900 uint8_t numpkts = 1;
1901 uint8_t numsigs = 7;
1905 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");
1907 const char *sigs[numsigs];
1908 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1909 sigs[1]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1910 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;)";
1911 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;)";
1912 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;)";
1913 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;)";
1914 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;)";
1917 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1918 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1920 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1931 static int IPOnlyTestSig10(
void)
1934 uint8_t *buf = (uint8_t *)
"Hi all!";
1935 uint16_t buflen = strlen((
char *)buf);
1937 uint8_t numpkts = 1;
1938 uint8_t numsigs = 7;
1942 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");
1944 const char *sigs[numsigs];
1945 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1946 sigs[1]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1947 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;)";
1948 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;)";
1949 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;)";
1950 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;)";
1951 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;)";
1954 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1955 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1957 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1972 static int IPOnlyTestSig11(
void)
1975 uint8_t *buf = (uint8_t *)
"Hi all!";
1976 uint16_t buflen = strlen((
char *)buf);
1978 uint8_t numpkts = 2;
1979 uint8_t numsigs = 7;
1983 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");
1986 char *sigs[numsigs];
1987 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;)";
1988 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;)";
1989 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;)";
1990 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;)";
1991 sigs[4]=
"alert tcp any any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1992 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;)";
1993 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;)";
1996 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1997 uint32_t results[2][7] = {{ 1, 1, 1, 1, 1, 1, 1}, { 1, 1, 1, 1, 1, 1, 1}};
1999 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2011 static int IPOnlyTestSig12(
void)
2014 uint8_t *buf = (uint8_t *)
"Hi all!";
2015 uint16_t buflen = strlen((
char *)buf);
2017 uint8_t numpkts = 2;
2018 uint8_t numsigs = 7;
2022 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");
2025 const char *sigs[numsigs];
2026 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;)";
2027 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;)";
2028 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;)";
2029 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;)";
2030 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;)";
2031 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;)";
2032 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;)";
2035 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2036 uint32_t results[2][7] = {{ 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}};
2038 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2045 static int IPOnlyTestSig13(
void)
2052 "alert tcp any any -> any any (msg:\"Test flowbits ip only\"; "
2053 "flowbits:set,myflow1; sid:1; rev:1;)");
2062 static int IPOnlyTestSig14(
void)
2069 "alert tcp any any -> any any (msg:\"Test flowbits ip only\"; "
2070 "flowbits:set,myflow1; flowbits:isset,myflow2; sid:1; rev:1;)");
2079 static int IPOnlyTestSig15(
void)
2082 uint8_t *buf = (uint8_t *)
"Hi all!";
2083 uint16_t buflen = strlen((
char *)buf);
2085 uint8_t numpkts = 1;
2086 uint8_t numsigs = 7;
2091 memset(&f, 0,
sizeof(
Flow));
2102 const char *sigs[numsigs];
2103 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; "
2104 "flowbits:set,one; sid:1;)";
2105 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; "
2106 "flowbits:set,two; sid:2;)";
2107 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; "
2108 "flowbits:set,three; sid:3;)";
2109 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; "
2110 "flowbits:set,four; sid:4;)";
2111 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; "
2112 "flowbits:set,five; sid:5;)";
2113 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; "
2114 "flowbits:set,six; sid:6;)";
2115 sigs[6]=
"alert tcp 192.168.1.0/24 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 7)\"; "
2116 "flowbits:set,seven; content:\"Hi all\"; sid:7;)";
2119 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2120 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
2122 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2133 static int IPOnlyTestSig16(
void)
2136 uint8_t *buf = (uint8_t *)
"Hi all!";
2137 uint16_t buflen = strlen((
char *)buf);
2139 uint8_t numpkts = 1;
2140 uint8_t numsigs = 2;
2146 const char *sigs[numsigs];
2147 sigs[0]=
"alert tcp !100.100.0.1 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2148 sigs[1]=
"alert tcp any any -> !50.0.0.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2151 uint32_t sid[2] = { 1, 2};
2152 uint32_t results[2] = { 1, 1};
2154 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2164 static int IPOnlyTestSig17(
void)
2167 uint8_t *buf = (uint8_t *)
"Hi all!";
2168 uint16_t buflen = strlen((
char *)buf);
2170 uint8_t numpkts = 1;
2171 uint8_t numsigs = 2;
2177 const char *sigs[numsigs];
2178 sigs[0]=
"alert ip 100.100.0.0 80 -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2179 sigs[1]=
"alert ip any any -> 50.0.0.0 123 (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2181 uint32_t sid[2] = { 1, 2};
2182 uint32_t results[2] = { 0, 0};
2184 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2194 static int IPOnlyTestSig18(
void)
2197 uint8_t *buf = (uint8_t *)
"Hi all!";
2198 uint16_t buflen = strlen((
char *)buf);
2200 uint8_t numpkts = 4;
2201 uint8_t numsigs = 4;
2208 p[3] =
UTHBuildPacketSrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"255.255.255.254",
"5.0.0.1");
2210 const char *sigs[numsigs];
2212 sigs[0]=
"alert ip 1.2.3.4-219.6.7.8 any -> any any (sid:1;)";
2213 sigs[1]=
"alert ip 51.2.3.4-253.1.2.3 any -> any any (sid:2;)";
2214 sigs[2]=
"alert ip 0.0.0.0-50.0.0.2 any -> any any (sid:3;)";
2215 sigs[3]=
"alert ip 50.0.0.0-255.255.255.255 any -> any any (sid:4;)";
2217 uint32_t sid[4] = { 1, 2, 3, 4, };
2218 uint32_t results[4][4] = {
2219 { 1, 0, 1, 0, }, { 0, 1, 0, 1}, { 0, 0, 1, 0 }, { 0, 0, 0, 1}};
2221 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2231 static int IPOnlyTestBug5066v1(
void)
2238 de_ctx,
"alert ip [1.2.3.4/24,1.2.3.64/27] any -> any any (sid:1;)");
2249 static int IPOnlyTestBug5066v2(
void)
2254 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.4/24") != 0);
2257 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2267 static int IPOnlyTestBug5066v3(
void)
2272 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.64/26") != 0);
2275 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2285 static int IPOnlyTestBug5066v4(
void)
2290 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"2000::1:1/122") != 0);
2293 PrintInet(AF_INET6, (
const void *)&x->
ip, ip,
sizeof(ip));
2296 FAIL_IF_NOT(strcmp(ip,
"2000:0000:0000:0000:0000:0000:0001:0000") == 0);
2303 static int IPOnlyTestBug5066v5(
void)
2308 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"2000::1:40/122") != 0);
2311 PrintInet(AF_INET6, (
const void *)&x->
ip, ip,
sizeof(ip));
2314 FAIL_IF_NOT(strcmp(ip,
"2000:0000:0000:0000:0000:0000:0001:0040") == 0);
2321 static int IPOnlyTestBug5168v1(
void)
2326 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.64/0.0.0.0") != 0);
2329 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2339 static int IPOnlyTestBug5168v2(
void)
2343 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"0.0.0.5/0.0.0.5") != -1);