From c711251578001920dee09f1dd1b36516bd15045c Mon Sep 17 00:00:00 2001 From: Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> Date: Sun, 29 Oct 2023 11:26:35 +0100 Subject: [PATCH] IPv6: add support for custom rules (#2120) --- example/protos.txt | 7 +++ .../fuzz_filecfg_protocols/custom_ipv6_1.txt | 1 + .../fuzz_filecfg_protocols/custom_ipv6_2.txt | 1 + .../fuzz_filecfg_protocols/custom_ipv6_3.txt | 1 + .../fuzz_filecfg_protocols/custom_ipv6_4.txt | 1 + .../fuzz_filecfg_protocols/custom_ipv6_5.txt | 1 + src/lib/ndpi_main.c | 52 +++++++++++++++--- .../default/pcap/custom_rules_ipv6.pcapng | Bin 0 -> 4456 bytes .../result/custom_rules_ipv6.pcapng.out | 35 ++++++++++++ 9 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_1.txt create mode 100644 fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_2.txt create mode 100644 fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_3.txt create mode 100644 fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_4.txt create mode 100644 fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_5.txt create mode 100644 tests/cfgs/default/pcap/custom_rules_ipv6.pcapng create mode 100644 tests/cfgs/default/result/custom_rules_ipv6.pcapng.out diff --git a/example/protos.txt b/example/protos.txt index 9c4425413..cb8ec6044 100644 --- a/example/protos.txt +++ b/example/protos.txt @@ -27,6 +27,7 @@ host:"api-global.netflix.com"@Netflix # IP based Subprotocols # Format: # ip:,ip:,.....@ +# ipv6:[],ipv6:[],.....@ # # NOTES @@ -54,6 +55,12 @@ ip:3.3.3.3:443@CustomProtocolA ip:3.3.3.3:444@CustomProtocolB ip:3.3.3.3:446@CustomProtocolC=400 +ipv6:[3ffe:507:0:1:200:86ff:fe05:80da]@CustomProtocolD +ipv6:[247f:855b:5e16:3caf::]/64:100@CustomProtocolE +ipv6:[247f:855b:5e16:3caf::]/64@CustomProtocolF +ipv6:[fe80::76ac:b9ff:fe6c:c124]:12717@CustomProtocolG +ipv6:[fe80::76ac:b9ff:fe6c:c124]:12718@CustomProtocolH + # # Risk Exceptions # diff --git a/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_1.txt b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_1.txt new file mode 100644 index 000000000..45cf1ce34 --- /dev/null +++ b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_1.txt @@ -0,0 +1 @@ +ipv6:[3ffe:507:0:1:200:86ff:fe05:80da]@CustomProtocolD diff --git a/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_2.txt b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_2.txt new file mode 100644 index 000000000..5d685f27b --- /dev/null +++ b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_2.txt @@ -0,0 +1 @@ +ipv6:[247f:855b:5e16:3caf::]/64:100@CustomProtocolE diff --git a/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_3.txt b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_3.txt new file mode 100644 index 000000000..035328245 --- /dev/null +++ b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_3.txt @@ -0,0 +1 @@ +ipv6:[247f:855b:5e16:3caf::]/64@CustomProtocolF diff --git a/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_4.txt b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_4.txt new file mode 100644 index 000000000..a35ef3255 --- /dev/null +++ b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_4.txt @@ -0,0 +1 @@ +ipv6:[fe80::76ac:b9ff:fe6c:c124]:12717@CustomProtocolG diff --git a/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_5.txt b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_5.txt new file mode 100644 index 000000000..2cff95d88 --- /dev/null +++ b/fuzz/corpus/fuzz_filecfg_protocols/custom_ipv6_5.txt @@ -0,0 +1 @@ +ipv6:[fe80::76ac:b9ff:fe6c:c124]:12718@CustomProtocolH diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 8f7cf7435..c8ecb01f6 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -2697,14 +2697,23 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp char *value, u_int16_t protocol_id) { ndpi_patricia_node_t *node; struct in_addr pin; + struct in6_addr pin6; int bits = 32; + int is_ipv6 = 0; char *ptr = strrchr(value, '/'); - u_int16_t port = 0; /* Format ip:8.248.73.247:443 */ - char *double_column; + u_int16_t port = 0; /* Format ip:8.248.73.247 */ + /* Format ipv6:[fe80::76ac:b9ff:fe6c:c124]/64 */ + char *double_column = NULL; if(!ndpi_str->protocols_ptree) return(-1); + if(value[0] == '[') { + is_ipv6 = 1; + bits = 128; + value += 1; + } + if(ptr) { ptr[0] = '\0'; ptr++; @@ -2714,14 +2723,32 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp port = atoi(&double_column[1]); } - if(atoi(ptr) >= 0 && atoi(ptr) <= 32) - bits = atoi(ptr); + if(!is_ipv6) { + if(atoi(ptr) >= 0 && atoi(ptr) <= 32) + bits = atoi(ptr); + } else { + if(atoi(ptr) >= 0 && atoi(ptr) <= 128) + bits = atoi(ptr); + + ptr = strrchr(value, ']'); + if(ptr) + *ptr = '\0'; + } } else { /* Let's check if there is the port defined Example: ip:8.248.73.247:443@AmazonPrime + Example: ipv6:[fe80::76ac:b9ff:fe6c:c124]:36818@CustomProtocolF */ - double_column = strrchr(value, ':'); + if(!is_ipv6) { + double_column = strrchr(value, ':'); + } else { + ptr = strrchr(value, ']'); + if(ptr) { + double_column = strrchr(ptr, ':'); + *ptr = '\0'; + } + } if(double_column) { double_column[0] = '\0'; @@ -2729,10 +2756,17 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp } } - if(inet_pton(AF_INET, value, &pin) != 1) - return(-1); + if(!is_ipv6) { + if(inet_pton(AF_INET, value, &pin) != 1) + return(-1); + node = add_to_ptree(ndpi_str->protocols_ptree, AF_INET, &pin, bits); + } else { + if(inet_pton(AF_INET6, value, &pin6) != 1) + return(-1); + node = add_to_ptree(ndpi_str->protocols_ptree6, AF_INET6, &pin6, bits); + } - if((node = add_to_ptree(ndpi_str->protocols_ptree, AF_INET, &pin, bits)) != NULL) { + if(node != NULL) { int i; struct patricia_uv16_list *item; @@ -4227,6 +4261,8 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str, is_tcp = 1, value = &attr[4]; else if(strncmp(attr, "udp:", 4) == 0) is_udp = 1, value = &attr[4]; + else if(strncmp(attr, "ipv6:", 5) == 0) + is_ip = 1, value = &attr[5]; else if(strncmp(attr, "ip:", 3) == 0) is_ip = 1, value = &attr[3]; else if(strncmp(attr, "host:", 5) == 0) { diff --git a/tests/cfgs/default/pcap/custom_rules_ipv6.pcapng b/tests/cfgs/default/pcap/custom_rules_ipv6.pcapng new file mode 100644 index 0000000000000000000000000000000000000000..8283b1f47f37337c318ba60d024b0c4ec335cd5b GIT binary patch literal 4456 zcmeHKdo+~m8h-~f!ytr+)Ko|r)7)G}%B|eCiV`Ko7-Njf%$S>^TyjWkCD+a_s;#xd zrjn#Yku<7PVn-T*xMpYHV zMF6nT##9e_fT?LgfDW*g#%2jv(TpH$;9jg3k49(s@MvsiID>~35%3mxD{P>U6&{48 zQm9yr6Dyp7rSljxfy_RF!DF)6OluiJw8qkfd;urIpT!kf_!IG5I*rR_%0SXwkTeG* z&jrbIK+0T@G6yuD3!486Q~+DSMkqZ7lmphphs9&?LukCPl~@WMDhq4k%o1QlvN4xN z52G=mifDWpYDSD@?BNn)g@Ho0Ku99s36NndmrEnA1oA)!xU<;8Jy>%*(Hu`Ov$8Z} zGT00riw<=b0JzU^k4+$z^%?*?9Bdt1PN416gcDlg68GV||;8R+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8ji!8%=SsN*Q(+Uk`K{N7VF^Qsx9QQ5^!P9>*|vknRKjvOcnKT%R_e09f?--RK!(9?{Q-0r#hgSaL1$qA(wOUBeTEcsE?p0d8-T;vs@`Y!q{ zvRG7gX3b}-$0KX=-KbA`ZSGba@JzkkVXTxm);;`K`dyzPLzpZ76lcCxIr--~GhME+ zR-vUx<2z@5=gjY%`JFTWKRGj8`a1|fI~oPV{DSIr&>U7W8Ddw2p!8~z8V=%2j3EZ0 zsDY5g%Q#n5dUZ+?UT-)xeT$i9Vg&IVD-xlos3eC16Xwv(3zEP5kOLSfEabed2$4Fy z8@jv6)-aG$J=2HE0JF{B!_3~%%!9{K0myBZ9?m3ru-w5HA4#WC%`Nu$Tj0&{Br8)Q zg+e9bNn`?%WQiwQ5J?MwiVVU_A?@kSI?&&+E~Ez{wkn;s&>9%uHz3*KTa>m^nEn6& literal 0 HcmV?d00001 diff --git a/tests/cfgs/default/result/custom_rules_ipv6.pcapng.out b/tests/cfgs/default/result/custom_rules_ipv6.pcapng.out new file mode 100644 index 000000000..f3a2f23ad --- /dev/null +++ b/tests/cfgs/default/result/custom_rules_ipv6.pcapng.out @@ -0,0 +1,35 @@ +Guessed flow protos: 0 + +DPI Packets (UDP): 5 (1.00 pkts/flow) +Confidence Unknown : 5 (flows) +Num dissector calls: 0 (0.00 diss/flow) +LRU cache ookla: 0/0/0 (insert/search/found) +LRU cache bittorrent: 0/0/0 (insert/search/found) +LRU cache zoom: 0/0/0 (insert/search/found) +LRU cache stun: 0/0/0 (insert/search/found) +LRU cache tls_cert: 0/0/0 (insert/search/found) +LRU cache mining: 0/0/0 (insert/search/found) +LRU cache msteams: 0/0/0 (insert/search/found) +LRU cache stun_zoom: 0/0/0 (insert/search/found) +Automa host: 0/0 (search/found) +Automa domain: 0/0 (search/found) +Automa tls cert: 0/0 (search/found) +Automa risk mask: 0/0 (search/found) +Automa common alpns: 0/0 (search/found) +Patricia risk mask: 0/0 (search/found) +Patricia risk: 0/0 (search/found) +Patricia risk IPv6: 0/0 (search/found) +Patricia protocols: 0/0 (search/found) +Patricia protocols IPv6: 9/5 (search/found) + +CustomProtocolD 2 600 1 +CustomProtocolE 1 1287 1 +CustomProtocolF 1 1287 1 +CustomProtocolG 1 318 1 +CustomProtocolH 1 318 1 + + 1 UDP [247f:855b:5e16:3caf:3f2c:4134:9592:661b]:100 -> [21bc:b273:7f68:88d7:77a8:585:3990:927b]:1991 [proto: 365/CustomProtocolE][IP: 365/CustomProtocolE][ClearText][Confidence: Unknown][DPI packets: 1][1 pkts/1287 bytes -> 0 pkts/0 bytes][Goodput ratio: 95/0][< 1 sec][Risk: ** Unidirectional Traffic **][Risk Score: 10][Risk Info: No client to server traffic][Plen Bins: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0] + 2 UDP [247f:855b:5e16:3caf:3f2c:4134:9592:661b]:36098 -> [21bc:b273:7f68:88d7:77a8:585:3990:927b]:50621 [proto: 366/CustomProtocolF][IP: 366/CustomProtocolF][ClearText][Confidence: Unknown][DPI packets: 1][1 pkts/1287 bytes -> 0 pkts/0 bytes][Goodput ratio: 95/0][< 1 sec][Risk: ** Unidirectional Traffic **][Risk Score: 10][Risk Info: No server to client traffic][Plen Bins: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0] + 3 UDP [3ffe:507::1:200:86ff:fe05:80da]:21554 <-> [3ffe:501:4819::42]:5333 [proto: 364/CustomProtocolD][IP: 364/CustomProtocolD][ClearText][Confidence: Unknown][DPI packets: 1][1 pkts/90 bytes <-> 1 pkts/510 bytes][Goodput ratio: 31/88][0.07 sec][PLAIN TEXT (itojun)][Plen Bins: 50,0,0,0,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + 4 UDP [fe80::76ac:b9ff:fe6c:c124]:12717 -> [ff02::1]:64315 [proto: 367/CustomProtocolG][IP: 367/CustomProtocolG][ClearText][Confidence: Unknown][DPI packets: 1][1 pkts/318 bytes -> 0 pkts/0 bytes][Goodput ratio: 80/0][< 1 sec][PLAIN TEXT (BZ.qca956)][Plen Bins: 0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] + 5 UDP [fe80::76ac:b9ff:fe6c:c124]:12718 -> [ff02::1]:26993 [proto: 368/CustomProtocolH][IP: 368/CustomProtocolH][ClearText][Confidence: Unknown][DPI packets: 1][1 pkts/318 bytes -> 0 pkts/0 bytes][Goodput ratio: 80/0][< 1 sec][PLAIN TEXT (BZ.qca956)][Plen Bins: 0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]