/src/ndpi/fuzz/fuzz_ndpi_reader_pl7m_64k.c
Line | Count | Source |
1 | | #include "reader_util.h" |
2 | | #include "ndpi_api.h" |
3 | | #include "fuzz_common_code.h" |
4 | | |
5 | | #include <pcap/pcap.h> |
6 | | |
7 | | #include <errno.h> |
8 | | #include <stdint.h> |
9 | | #include <stdio.h> |
10 | | #include <assert.h> |
11 | | #include <libgen.h> |
12 | | |
13 | | #ifdef ENABLE_PCAP_L7_MUTATOR |
14 | | #include "pl7m.h" |
15 | | #endif |
16 | | |
17 | | struct ndpi_workflow_prefs *prefs = NULL; |
18 | | struct ndpi_workflow *workflow = NULL; |
19 | | struct ndpi_global_context *g_ctx; |
20 | | |
21 | | u_int8_t enable_payload_analyzer = 0; |
22 | | u_int8_t enable_flow_stats = 1; |
23 | | u_int8_t human_readeable_string_len = 5; |
24 | | u_int8_t max_num_udp_dissected_pkts = 0, max_num_tcp_dissected_pkts = 0; /* Disable limits at application layer */; |
25 | | int malloc_size_stats = 0; |
26 | | FILE *fingerprint_fp = NULL; |
27 | | char *addr_dump_path = NULL; |
28 | | int monitoring_enabled = 1; |
29 | | u_int8_t enable_doh_dot_detection = 0; |
30 | | |
31 | | static char *path = NULL; |
32 | | |
33 | | extern void ndpi_report_payload_stats(FILE *out); |
34 | | |
35 | | #ifdef CRYPT_FORCE_NO_AESNI |
36 | | extern int force_no_aesni; |
37 | | #endif |
38 | | |
39 | | #ifdef ENABLE_PCAP_L7_MUTATOR |
40 | | size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, |
41 | 0 | size_t MaxSize, unsigned int Seed) { |
42 | 0 | return pl7m_mutator(Data, Size, MaxSize, Seed); |
43 | 0 | } |
44 | | #endif |
45 | | |
46 | 20 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
47 | 20 | (void)argc; |
48 | | |
49 | 20 | path = dirname(strdup(*argv[0])); /* No errors; no free! */ |
50 | 20 | return 0; |
51 | 20 | } |
52 | | |
53 | 1.84M | static void node_cleanup_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) { |
54 | 1.84M | struct ndpi_flow_info *flow = *(struct ndpi_flow_info **) node; |
55 | | |
56 | 1.84M | (void)depth; |
57 | 1.84M | (void)user_data; |
58 | | |
59 | 1.84M | if((which == ndpi_preorder) || (which == ndpi_leaf)) { /* Avoid walking the same node multiple times */ |
60 | 796k | if((!flow->detection_completed) && flow->ndpi_flow) { |
61 | 729k | flow->detected_protocol = ndpi_detection_giveup(workflow->ndpi_struct, |
62 | 729k | flow->ndpi_flow); |
63 | 729k | } |
64 | | |
65 | 796k | process_ndpi_collected_info(workflow, flow); |
66 | 796k | } |
67 | 1.84M | } |
68 | | |
69 | | static void fn_flow_callback(struct ndpi_workflow *w, struct ndpi_flow_info *f, void *d) |
70 | 67.3k | { |
71 | 67.3k | (void)w; |
72 | 67.3k | (void)f; |
73 | 67.3k | (void)d; |
74 | 67.3k | } |
75 | | |
76 | 171k | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
77 | 171k | pcap_t * pkts; |
78 | 171k | const u_char *pkt; |
79 | 171k | struct pcap_pkthdr *header; |
80 | 171k | int r; |
81 | 171k | char errbuf[PCAP_ERRBUF_SIZE]; |
82 | 171k | u_int i; |
83 | 171k | FILE *fd; |
84 | 171k | char name[256]; |
85 | | |
86 | 171k | if (prefs == NULL) { |
87 | 8 | prefs = calloc(sizeof(struct ndpi_workflow_prefs), 1); /* No failure here */ |
88 | 8 | prefs->decode_tunnels = 1; |
89 | 8 | prefs->num_roots = 16; |
90 | 8 | prefs->max_ndpi_flows = 16 * 1024 * 1024; |
91 | 8 | prefs->quiet_mode = 0; |
92 | | |
93 | | #ifdef ENABLE_MEM_ALLOC_FAILURES |
94 | | fuzz_set_alloc_callbacks(); |
95 | | #endif |
96 | | |
97 | 8 | g_ctx = ndpi_global_init(); |
98 | | |
99 | 8 | workflow = ndpi_workflow_init(prefs, NULL /* pcap handler will be set later */, 0, ndpi_serialization_format_json, g_ctx); |
100 | | |
101 | 8 | ndpi_workflow_set_flow_callback(workflow, fn_flow_callback, NULL); |
102 | | |
103 | 8 | ndpi_set_config(workflow->ndpi_struct, NULL, "log.level", "3"); |
104 | 8 | ndpi_set_config(workflow->ndpi_struct, "all", "log", "1"); |
105 | | |
106 | 8 | sprintf(name, "%s/public_suffix_list.dat", path); |
107 | 8 | assert(ndpi_load_domain_suffixes(workflow->ndpi_struct, name) >= 0); |
108 | 8 | sprintf(name, "%s/lists/", path); |
109 | 8 | assert(ndpi_load_categories_dir(workflow->ndpi_struct, name) >= 0); |
110 | 8 | sprintf(name, "%s/lists/protocols/", path); |
111 | 8 | assert(ndpi_load_protocols_dir(workflow->ndpi_struct, name) >= 0); |
112 | 8 | sprintf(name, "%s/protos.txt", path); |
113 | 8 | assert(ndpi_load_protocols_file(workflow->ndpi_struct, name) >= 0); |
114 | 8 | sprintf(name, "%s/categories.txt", path); |
115 | 8 | assert(ndpi_load_categories_file(workflow->ndpi_struct, name, NULL) >= 0); |
116 | 8 | sprintf(name, "%s/risky_domains.txt", path); |
117 | 8 | assert(ndpi_load_risk_domain_file(workflow->ndpi_struct, name) >= 0); |
118 | 8 | sprintf(name, "%s/ja4_fingerprints.csv", path); |
119 | 8 | assert(ndpi_load_malicious_ja4_file(workflow->ndpi_struct, name) >= 0); |
120 | 8 | sprintf(name, "%s/tcp_fingerprints.csv", path); |
121 | 8 | assert(ndpi_load_tcp_fingerprint_file(workflow->ndpi_struct, name) >= 0); |
122 | 8 | sprintf(name, "%s/sha1_fingerprints.csv", path); |
123 | 8 | assert(ndpi_load_malicious_sha1_file(workflow->ndpi_struct, name) >= 0); |
124 | | |
125 | | #ifdef ENABLE_ONLY_SUBCLASSIFICATION |
126 | | sprintf(name, "%s/config_only_classification.txt", path); |
127 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "filename.config", name) == NDPI_CFG_OK); |
128 | | #else |
129 | | |
130 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "packets_limit_per_flow", "255") == NDPI_CFG_OK); |
131 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "flow.track_payload", "1") == NDPI_CFG_OK); |
132 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "tcp_ack_payload_heuristic", "1") == NDPI_CFG_OK); |
133 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "fully_encrypted_heuristic", "1") == NDPI_CFG_OK); |
134 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "dns", "subclassification", "1") == NDPI_CFG_OK); |
135 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "application_blocks_tracking", "1") == NDPI_CFG_OK); |
136 | 8 | #ifndef ENABLE_CONFIG2 |
137 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "stun", "max_packets_extra_dissection", "40") == NDPI_CFG_OK); |
138 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "zoom", "max_packets_extra_dissection", "255") == NDPI_CFG_OK); |
139 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "rtp", "search_for_stun", "1") == NDPI_CFG_OK); |
140 | 8 | #endif |
141 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "openvpn", "dpi.heuristics", "0x01") == NDPI_CFG_OK); |
142 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "openvpn", "dpi.heuristics.num_messages", "20") == NDPI_CFG_OK); |
143 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "metadata.ja4r_fingerprint", "1") == NDPI_CFG_OK); |
144 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "dpi.heuristics", "0x07") == NDPI_CFG_OK); |
145 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "dpi.heuristics.max_packets_extra_dissection", "40") == NDPI_CFG_OK); |
146 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, "stun", "monitoring", "1") == NDPI_CFG_OK); |
147 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "dpi.address_cache_size", "8192") == NDPI_CFG_OK); |
148 | 8 | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "hostname_dns_check", "1") == NDPI_CFG_OK); |
149 | | |
150 | | #ifdef ENABLE_CONFIG2 |
151 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "flow_risk.all.info", "0") == NDPI_CFG_OK); |
152 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "metadata.tcp_fingerprint_format", "1") == NDPI_CFG_OK); |
153 | | assert(ndpi_set_config(workflow->ndpi_struct, NULL, "metadata.ndpi_fingerprint_format", "1") == NDPI_CFG_OK); |
154 | | assert(ndpi_set_config(workflow->ndpi_struct, "tls", "blocks_analysis", "1") == NDPI_CFG_OK); |
155 | | |
156 | | addr_dump_path = "/tmp/"; |
157 | | #endif |
158 | | |
159 | 8 | #endif /* ENABLE_ONLY_SUBCLASSIFICATION */ |
160 | | |
161 | 8 | ndpi_finalize_initialization(workflow->ndpi_struct); |
162 | | |
163 | | #ifdef ENABLE_FINGERPRINT_FP |
164 | | fingerprint_fp = stdout; |
165 | | #endif |
166 | | |
167 | | #ifdef CRYPT_FORCE_NO_AESNI |
168 | | force_no_aesni = 1; |
169 | | #endif |
170 | | |
171 | | #ifdef ENABLE_PAYLOAD_ANALYZER |
172 | | enable_payload_analyzer = 1; |
173 | | #endif |
174 | 8 | } |
175 | | |
176 | | #ifdef ENABLE_MEM_ALLOC_FAILURES |
177 | | /* Don't fail memory allocations until init phase is done */ |
178 | | fuzz_set_alloc_callbacks_and_seed(Size); |
179 | | #endif |
180 | | |
181 | 171k | fd = buffer_to_file(Data, Size); |
182 | 171k | if (fd == NULL) |
183 | 0 | return 0; |
184 | | |
185 | 171k | pkts = pcap_fopen_offline(fd, errbuf); |
186 | 171k | if (pkts == NULL) { |
187 | 15 | fclose(fd); |
188 | 15 | return 0; |
189 | 15 | } |
190 | 171k | if (ndpi_is_datalink_supported(pcap_datalink(pkts)) == 0) |
191 | 250 | { |
192 | | /* Do not fail if the datalink type is not supported (may happen often during fuzzing). */ |
193 | 250 | pcap_close(pkts); |
194 | 250 | return 0; |
195 | 250 | } |
196 | | |
197 | 171k | workflow->pcap_handle = pkts; |
198 | | /* Init flow tree */ |
199 | 171k | workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *)); |
200 | 171k | if(!workflow->ndpi_flows_root) { |
201 | 2 | pcap_close(pkts); |
202 | 2 | return 0; |
203 | 2 | } |
204 | | |
205 | 171k | header = NULL; |
206 | 171k | r = pcap_next_ex(pkts, &header, &pkt); |
207 | 9.43M | while (r > 0) { |
208 | | /* allocate an exact size buffer to check overflows */ |
209 | 9.26M | uint8_t *packet_checked = malloc(header->caplen); |
210 | | |
211 | 9.26M | if(packet_checked) { |
212 | 9.26M | ndpi_risk flow_risk; |
213 | 9.26M | struct ndpi_flow_info *flow = NULL; /* unused */ |
214 | | |
215 | 9.26M | memcpy(packet_checked, pkt, header->caplen); |
216 | 9.26M | ndpi_workflow_process_packet(workflow, header, packet_checked, &flow_risk, &flow); |
217 | 9.26M | free(packet_checked); |
218 | 9.26M | } |
219 | | |
220 | 9.26M | r = pcap_next_ex(pkts, &header, &pkt); |
221 | 9.26M | } |
222 | 171k | pcap_close(pkts); |
223 | | |
224 | | /* Free flow trees */ |
225 | 2.91M | for(i = 0; i < workflow->prefs.num_roots; i++) { |
226 | 2.74M | ndpi_twalk(workflow->ndpi_flows_root[i], node_cleanup_walker, NULL); |
227 | 2.74M | ndpi_tdestroy(workflow->ndpi_flows_root[i], ndpi_flow_info_freer); |
228 | 2.74M | } |
229 | 171k | ndpi_free(workflow->ndpi_flows_root); |
230 | | /* Free payload analyzer data */ |
231 | 171k | if(enable_payload_analyzer) |
232 | 16.8k | ndpi_report_payload_stats(stdout); |
233 | | |
234 | | #ifdef ENABLE_PAYLOAD_ANALYZER |
235 | | ndpi_update_params(SPLT_PARAM_TYPE, "splt_param.txt"); |
236 | | ndpi_update_params(BD_PARAM_TYPE, "bd_param.txt"); |
237 | | ndpi_update_params(2, ""); /* invalid */ |
238 | | #endif |
239 | | |
240 | 171k | return 0; |
241 | 171k | } |