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) {
919 FatalError(
"Unable to allocate iponly signature tracking area");
976 static inline int IPOnlyMatchCompatSMs(
1010 void *user_data_src = NULL, *user_data_dst = NULL;
1030 src = user_data_src;
1031 dst = user_data_dst;
1033 if (
src == NULL ||
dst == NULL)
1036 for (uint32_t u = 0; u <
src->size; u++) {
1039 uint8_t bitarray =
dst->array[u] &
src->array[u];
1049 for (uint8_t i = 0; i < 8; i++, bitarray = bitarray >> 1) {
1050 if (bitarray & 0x01) {
1067 if (p->
proto == IPPROTO_TCP || p->
proto == IPPROTO_UDP ||
1074 if (dport == NULL) {
1084 if (sport == NULL) {
1091 SCLogDebug(
"port-less protocol and sig needs ports");
1095 if (!IPOnlyMatchCompatSMs(
tv, det_ctx, s, p)) {
1099 SCLogDebug(
"Signum %" PRIu32
" match (sid: %" PRIu32
", msg: %s)", u * 8 + i, s->
id,
1106 SCLogDebug(
"running match functions, sm %p", smd);
1151 if (
src->family == AF_INET) {
1161 void *user_data = NULL;
1162 if (
src->netmask == 32)
1169 src->netmask, &user_data);
1170 if (user_data == NULL) {
1178 if (user_data == NULL) {
1185 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1187 if (
src->negated > 0)
1189 sna->
array[
src->signum / 8] &= ~tmp;
1192 sna->
array[
src->signum / 8] |= tmp;
1194 if (
src->netmask == 32)
1204 "src ipv4 radix tree");
1213 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1215 if (
src->negated > 0)
1217 sna->
array[
src->signum / 8] &= ~tmp;
1220 sna->
array[
src->signum / 8] |= tmp;
1222 if (
src->netmask == 32)
1234 " src ipv4 radix tree ip %s netmask %" PRIu8,
1235 tmpstr,
src->netmask);
1247 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1249 if (
src->negated > 0)
1251 sna->
array[
src->signum / 8] &= ~tmp;
1254 sna->
array[
src->signum / 8] |= tmp;
1256 }
else if (
src->family == AF_INET6) {
1259 void *user_data = NULL;
1260 if (
src->netmask == 128)
1267 src->netmask, &user_data);
1269 if (user_data == NULL) {
1275 if (user_data == NULL) {
1280 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1282 if (
src->negated > 0)
1284 sna->
array[
src->signum / 8] &= ~tmp;
1287 sna->
array[
src->signum / 8] |= tmp;
1289 if (
src->netmask == 128)
1305 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1306 if (
src->negated > 0)
1308 sna->
array[
src->signum / 8] &= ~tmp;
1311 sna->
array[
src->signum / 8] |= tmp;
1313 if (
src->netmask == 128)
1329 uint8_t tmp = (uint8_t)(1 << (
src->signum % 8));
1330 if (
src->negated > 0)
1332 sna->
array[
src->signum / 8] &= ~tmp;
1335 sna->
array[
src->signum / 8] |= tmp;
1347 if (
dst->family == AF_INET) {
1350 SCLogDebug(
"Item has netmask %"PRIu8
" negated: %s; IP: %s; signum:"
1351 " %"PRIu32
"",
dst->netmask, (
dst->negated)?
"yes":
"no",
1352 inet_ntoa(*(
struct in_addr*)&
dst->ip[0]),
dst->signum);
1354 void *user_data = NULL;
1355 if (
dst->netmask == 32)
1365 if (user_data == NULL) {
1375 if (user_data == NULL) {
1382 uint8_t tmp = (uint8_t)(1 << (
dst->signum % 8));
1383 if (
dst->negated > 0)
1385 sna->
array[
dst->signum / 8] &= ~tmp;
1388 sna->
array[
dst->signum / 8] |= tmp;
1390 if (
dst->netmask == 32)
1409 uint8_t tmp = (uint8_t)(1 << (
dst->signum % 8));
1410 if (
dst->negated > 0)
1412 sna->
array[
dst->signum / 8] &= ~tmp;
1415 sna->
array[
dst->signum / 8] |= tmp;
1417 if (
dst->netmask == 32)
1436 uint8_t tmp = (uint8_t)(1 << (
dst->signum % 8));
1437 if (
dst->negated > 0)
1439 sna->
array[
dst->signum / 8] &= ~tmp;
1442 sna->
array[
dst->signum / 8] |= tmp;
1444 }
else if (
dst->family == AF_INET6) {
1447 void *user_data = NULL;
1448 if (
dst->netmask == 128)
1455 dst->netmask, &user_data);
1457 if (user_data == NULL) {
1465 if (user_data == NULL) {
1470 uint8_t tmp = (uint8_t)(1 << (
dst->signum % 8));
1471 if (
dst->negated > 0)
1473 sna->
array[
dst->signum / 8] &= ~tmp;
1476 sna->
array[
dst->signum / 8] |= tmp;
1478 if (
dst->netmask == 128)
1495 uint8_t tmp = (uint8_t)(1 << (
dst->signum % 8));
1496 if (
dst->negated > 0)
1498 sna->
array[
dst->signum / 8] &= ~tmp;
1501 sna->
array[
dst->signum / 8] |= tmp;
1503 if (
dst->netmask == 128)
1520 uint8_t tmp = (uint8_t)(1 << (
dst->signum % 8));
1521 if (
dst->negated > 0)
1523 sna->
array[
dst->signum / 8] &= ~tmp;
1526 sna->
array[
dst->signum / 8] |= tmp;
1564 SCLogDebug(
"Adding IPs from rule: %" PRIu32
" (%s) as %" PRIu32
" mapped to %" PRIu32
"\n",
1565 s->
id, s->
msg, s->
num, mapped_signum);
1578 if (mapped_signum > io_ctx->
max_idx)
1579 io_ctx->
max_idx = mapped_signum;
1592 static int IPOnlyTestSig01(
void)
1612 static int IPOnlyTestSig02 (
void)
1632 static int IPOnlyTestSig03 (
void)
1644 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;)");
1650 printf(
"got a IPOnly signature (content): ");
1656 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;)");
1662 printf(
"got a IPOnly signature (content): ");
1668 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;)");
1674 printf(
"got a IPOnly signature (uricontent): ");
1680 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;)");
1686 printf(
"got a IPOnly signature (pcre): ");
1692 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;)");
1698 printf(
"got a IPOnly signature (flow): ");
1704 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;)");
1710 printf(
"got a IPOnly signature (dsize): ");
1716 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;)");
1722 printf(
"got a IPOnly signature (flowbits): ");
1728 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;)");
1734 printf(
"got a IPOnly signature (flowvar): ");
1740 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;)");
1746 printf(
"got a IPOnly signature (pktvar): ");
1760 static int IPOnlyTestSig04 (
void)
1766 for (
int size = 0; size < 6; size++) {
1770 new = IPOnlyCIDRItemNew();
1774 head = IPOnlyCIDRItemInsert(
head,
new);
1778 new = IPOnlyCIDRItemNew();
1781 head = IPOnlyCIDRItemInsert(
head,
new);
1785 new = IPOnlyCIDRItemNew();
1788 head = IPOnlyCIDRItemInsert(
head,
new);
1792 new = IPOnlyCIDRItemNew();
1796 head = IPOnlyCIDRItemInsert(
head,
new);
1800 new = IPOnlyCIDRItemNew();
1804 head = IPOnlyCIDRItemInsert(
head,
new);
1807 IPOnlyCIDRListPrint(
head);
1809 IPOnlyCIDRListQSort(&
head);
1828 if (new->netmask != 9) {
1836 if (new->netmask != 10 || new->ip[0] != 1) {
1844 if (new->netmask != 10 || new->ip[0] != 2) {
1852 if (new->netmask != 10 || new->ip[0] != 3) {
1860 if (new->netmask != 11) {
1888 static int IPOnlyTestSig05(
void)
1891 uint8_t *buf = (uint8_t *)
"Hi all!";
1892 uint16_t buflen = strlen((
char *)buf);
1894 uint8_t numpkts = 1;
1895 uint8_t numsigs = 7;
1901 const char *sigs[numsigs];
1902 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1903 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1904 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1905 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1906 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1907 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1908 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;)";
1911 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1912 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1914 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1925 static int IPOnlyTestSig06(
void)
1928 uint8_t *buf = (uint8_t *)
"Hi all!";
1929 uint16_t buflen = strlen((
char *)buf);
1931 uint8_t numpkts = 1;
1932 uint8_t numsigs = 7;
1936 p[0] =
UTHBuildPacketSrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"80.58.0.33",
"195.235.113.3");
1938 const char *sigs[numsigs];
1939 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
1940 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
1941 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; sid:3;)";
1942 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; sid:4;)";
1943 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1944 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; sid:6;)";
1945 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;)";
1948 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1949 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
1951 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
1966 static int IPOnlyTestSig07(
void)
1969 uint8_t *buf = (uint8_t *)
"Hi all!";
1970 uint16_t buflen = strlen((
char *)buf);
1972 uint8_t numpkts = 1;
1973 uint8_t numsigs = 7;
1979 char *sigs[numsigs];
1980 sigs[0]=
"alert tcp 192.168.1.5 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
1981 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;)";
1982 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;)";
1983 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;)";
1984 sigs[4]=
"alert tcp any any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
1985 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;)";
1986 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;)";
1989 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
1990 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
1992 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2004 static int IPOnlyTestSig08(
void)
2007 uint8_t *buf = (uint8_t *)
"Hi all!";
2008 uint16_t buflen = strlen((
char *)buf);
2010 uint8_t numpkts = 1;
2011 uint8_t numsigs = 7;
2017 const char *sigs[numsigs];
2018 sigs[0]=
"alert tcp 192.168.1.5 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 1)\"; sid:1;)";
2019 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;)";
2020 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;)";
2021 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;)";
2022 sigs[4]=
"alert tcp any any -> !192.168.1.5 any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
2023 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;)";
2024 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;)";
2027 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2028 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
2030 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2041 static int IPOnlyTestSig09(
void)
2044 uint8_t *buf = (uint8_t *)
"Hi all!";
2045 uint16_t buflen = strlen((
char *)buf);
2047 uint8_t numpkts = 1;
2048 uint8_t numsigs = 7;
2052 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");
2054 const char *sigs[numsigs];
2055 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2056 sigs[1]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2057 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;)";
2058 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;)";
2059 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;)";
2060 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;)";
2061 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;)";
2064 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2065 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
2067 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2078 static int IPOnlyTestSig10(
void)
2081 uint8_t *buf = (uint8_t *)
"Hi all!";
2082 uint16_t buflen = strlen((
char *)buf);
2084 uint8_t numpkts = 1;
2085 uint8_t numsigs = 7;
2089 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");
2091 const char *sigs[numsigs];
2092 sigs[0]=
"alert tcp 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4565 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2093 sigs[1]=
"alert tcp any any -> 3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2094 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;)";
2095 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;)";
2096 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;)";
2097 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;)";
2098 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;)";
2101 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2102 uint32_t results[7] = { 0, 0, 0, 0, 0, 0, 0};
2104 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2119 static int IPOnlyTestSig11(
void)
2122 uint8_t *buf = (uint8_t *)
"Hi all!";
2123 uint16_t buflen = strlen((
char *)buf);
2125 uint8_t numpkts = 2;
2126 uint8_t numsigs = 7;
2130 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");
2133 char *sigs[numsigs];
2134 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;)";
2135 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;)";
2136 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;)";
2137 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;)";
2138 sigs[4]=
"alert tcp any any -> any any (msg:\"Testing src/dst ip (sid 5)\"; sid:5;)";
2139 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;)";
2140 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;)";
2143 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2144 uint32_t results[2][7] = {{ 1, 1, 1, 1, 1, 1, 1}, { 1, 1, 1, 1, 1, 1, 1}};
2146 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2158 static int IPOnlyTestSig12(
void)
2161 uint8_t *buf = (uint8_t *)
"Hi all!";
2162 uint16_t buflen = strlen((
char *)buf);
2164 uint8_t numpkts = 2;
2165 uint8_t numsigs = 7;
2169 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");
2172 const char *sigs[numsigs];
2173 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;)";
2174 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;)";
2175 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;)";
2176 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;)";
2177 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;)";
2178 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;)";
2179 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;)";
2182 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2183 uint32_t results[2][7] = {{ 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}};
2185 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2192 static int IPOnlyTestSig13(
void)
2199 "alert tcp any any -> any any (msg:\"Test flowbits ip only\"; "
2200 "flowbits:set,myflow1; sid:1; rev:1;)");
2209 static int IPOnlyTestSig14(
void)
2216 "alert tcp any any -> any any (msg:\"Test flowbits ip only\"; "
2217 "flowbits:set,myflow1; flowbits:isset,myflow2; sid:1; rev:1;)");
2226 static int IPOnlyTestSig15(
void)
2229 uint8_t *buf = (uint8_t *)
"Hi all!";
2230 uint16_t buflen = strlen((
char *)buf);
2232 uint8_t numpkts = 1;
2233 uint8_t numsigs = 7;
2238 memset(&f, 0,
sizeof(
Flow));
2249 const char *sigs[numsigs];
2250 sigs[0]=
"alert tcp 192.168.1.5 any -> any any (msg:\"Testing src ip (sid 1)\"; "
2251 "flowbits:set,one; sid:1;)";
2252 sigs[1]=
"alert tcp any any -> 192.168.1.1 any (msg:\"Testing dst ip (sid 2)\"; "
2253 "flowbits:set,two; sid:2;)";
2254 sigs[2]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 3)\"; "
2255 "flowbits:set,three; sid:3;)";
2256 sigs[3]=
"alert tcp 192.168.1.5 any -> 192.168.1.1 any (msg:\"Testing src/dst ip (sid 4)\"; "
2257 "flowbits:set,four; sid:4;)";
2258 sigs[4]=
"alert tcp 192.168.1.0/24 any -> any any (msg:\"Testing src/dst ip (sid 5)\"; "
2259 "flowbits:set,five; sid:5;)";
2260 sigs[5]=
"alert tcp any any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 6)\"; "
2261 "flowbits:set,six; sid:6;)";
2262 sigs[6]=
"alert tcp 192.168.1.0/24 any -> 192.168.0.0/16 any (msg:\"Testing src/dst ip (sid 7)\"; "
2263 "flowbits:set,seven; content:\"Hi all\"; sid:7;)";
2266 uint32_t sid[7] = { 1, 2, 3, 4, 5, 6, 7};
2267 uint32_t results[7] = { 1, 1, 1, 1, 1, 1, 1};
2269 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2280 static int IPOnlyTestSig16(
void)
2283 uint8_t *buf = (uint8_t *)
"Hi all!";
2284 uint16_t buflen = strlen((
char *)buf);
2286 uint8_t numpkts = 1;
2287 uint8_t numsigs = 2;
2293 const char *sigs[numsigs];
2294 sigs[0]=
"alert tcp !100.100.0.1 any -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2295 sigs[1]=
"alert tcp any any -> !50.0.0.1 any (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2298 uint32_t sid[2] = { 1, 2};
2299 uint32_t results[2] = { 1, 1};
2301 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2311 static int IPOnlyTestSig17(
void)
2314 uint8_t *buf = (uint8_t *)
"Hi all!";
2315 uint16_t buflen = strlen((
char *)buf);
2317 uint8_t numpkts = 1;
2318 uint8_t numsigs = 2;
2324 const char *sigs[numsigs];
2325 sigs[0]=
"alert ip 100.100.0.0 80 -> any any (msg:\"Testing src ip (sid 1)\"; sid:1;)";
2326 sigs[1]=
"alert ip any any -> 50.0.0.0 123 (msg:\"Testing dst ip (sid 2)\"; sid:2;)";
2328 uint32_t sid[2] = { 1, 2};
2329 uint32_t results[2] = { 0, 0};
2331 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2341 static int IPOnlyTestSig18(
void)
2344 uint8_t *buf = (uint8_t *)
"Hi all!";
2345 uint16_t buflen = strlen((
char *)buf);
2347 uint8_t numpkts = 4;
2348 uint8_t numsigs = 4;
2355 p[3] =
UTHBuildPacketSrcDst((uint8_t *)buf, buflen, IPPROTO_TCP,
"255.255.255.254",
"5.0.0.1");
2357 const char *sigs[numsigs];
2359 sigs[0]=
"alert ip 1.2.3.4-219.6.7.8 any -> any any (sid:1;)";
2360 sigs[1]=
"alert ip 51.2.3.4-253.1.2.3 any -> any any (sid:2;)";
2361 sigs[2]=
"alert ip 0.0.0.0-50.0.0.2 any -> any any (sid:3;)";
2362 sigs[3]=
"alert ip 50.0.0.0-255.255.255.255 any -> any any (sid:4;)";
2364 uint32_t sid[4] = { 1, 2, 3, 4, };
2365 uint32_t results[4][4] = {
2366 { 1, 0, 1, 0, }, { 0, 1, 0, 1}, { 0, 0, 1, 0 }, { 0, 0, 0, 1}};
2368 result =
UTHGenericTest(p, numpkts, sigs, sid, (uint32_t *) results, numsigs);
2378 static int IPOnlyTestBug5066v1(
void)
2385 de_ctx,
"alert ip [1.2.3.4/24,1.2.3.64/27] any -> any any (sid:1;)");
2396 static int IPOnlyTestBug5066v2(
void)
2401 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.4/24") != 0);
2404 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2414 static int IPOnlyTestBug5066v3(
void)
2419 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.64/26") != 0);
2422 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2432 static int IPOnlyTestBug5066v4(
void)
2437 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"2000::1:1/122") != 0);
2440 PrintInet(AF_INET6, (
const void *)&x->
ip, ip,
sizeof(ip));
2443 FAIL_IF_NOT(strcmp(ip,
"2000:0000:0000:0000:0000:0000:0001:0000") == 0);
2450 static int IPOnlyTestBug5066v5(
void)
2455 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"2000::1:40/122") != 0);
2458 PrintInet(AF_INET6, (
const void *)&x->
ip, ip,
sizeof(ip));
2461 FAIL_IF_NOT(strcmp(ip,
"2000:0000:0000:0000:0000:0000:0001:0040") == 0);
2468 static int IPOnlyTestBug5168v1(
void)
2473 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"1.2.3.64/0.0.0.0") != 0);
2476 PrintInet(AF_INET, (
const void *)&x->
ip[0], ip,
sizeof(ip));
2486 static int IPOnlyTestBug5168v2(
void)
2490 FAIL_IF(IPOnlyCIDRItemParseSingle(&x,
"0.0.0.5/0.0.0.5") != -1);