-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_test_packet.c
334 lines (290 loc) · 9.42 KB
/
fuzz_test_packet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "fuzz_test_packet.h"
// a temp value only to record the data of last packet.
u32 last_packet_data_length;
u32 last_received_packet_ack_seq;
u32 last_received_packet_ecr;
bool is_received_packet_has_timestamp;
u32 packet_seq = 1;
u32 packet_ack_seq = 1;
u32 outbound_packet_seq = 1;
u32 outbound_packet_ack_seq = 1;
bool is_validate_outbound_running = false;
bool is_validate_outbound_data_error = false;
pthread_t tid;
struct packet *fuzz_to_packet(bool is_fuzz_options, bool is_fuzz_data) {
int fuzz_data_capacity = 200;
int fuzz_data_size = 0;
u8 fixed_data[] = {// IPv4
0x45, 0x00,
// IP length calculated later
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// ttl, protocol, checksum
0xff, 0x06, 0x00, 0x00,
// 0.0.0.0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 *data = (u8 *) malloc(fuzz_data_capacity * sizeof(u8));
for (int i = 0; i < sizeof(fixed_data) / sizeof(u8); i++) {
push_data(&data, fixed_data[i], &fuzz_data_size, &fuzz_data_capacity);
}
// source port 16 bit
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
// dest port 16 bit
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
// seq
if (config.is_fuzz_without_dependency) {
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
} else {
/**
* NOTE use `htonl` may be better?
*/
push_data(&data, (packet_seq >> 24) & 0xFF, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, (packet_seq >> 16) & 0xFF, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, (packet_seq >> 8) & 0xFF, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, packet_seq & 0xFF, &fuzz_data_size, &fuzz_data_capacity);
}
/**
* TODO: change ack seq as needed
*/
// ack seq
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x01, &fuzz_data_size, &fuzz_data_capacity);
// offset, reserved, control flag, window size
// offset calculated later
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x10, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
// checksum calculated later
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
// urgent pointer (seems not use at all)
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
push_data(&data, read_byte(), &fuzz_data_size, &fuzz_data_capacity);
u8 tcp_header_length = 20; // TCP header with options
u8 options_length = 0;
if (is_fuzz_options) {
// most 10 options
char option_count = read_byte() % 11;
for (int i = 0; i < option_count; i++) {
char tmp = read_byte();
u8 kind;
u8 length;
DEBUG_FUZZP("tmp in fuzz_option : %d\n", tmp);
switch (tmp % 17) {
case 0:
kind = TCPOPT_EOL;
length = 1;
break;
case 1:
kind = TCPOPT_NOP;
length = 1;
break;
case 2:
kind = TCPOPT_MAXSEG;
length = TCPOLEN_MAXSEG;
break;
case 3:
kind = TCPOPT_WINDOW;
length = TCPOLEN_WINDOW;
break;
case 4:
/**
* TCP Sack-Permitted Option:
*
* Kind: 4
*
* +---------+---------+
* | Kind=4 | Length=2|
* +---------+---------+
*/
kind = TCPOPT_SACK_PERMITTED;
length = TCPOLEN_SACK_PERMITTED;
break;
case 5:
/**
* TCP SACK Option:
*
* Kind: 5
*
* Length: Variable
*
* +--------+--------+
* | Kind=5 | Length |
* +--------+--------+--------+--------+
* | Left Edge of 1st Block |
* +--------+--------+--------+--------+
* | Right Edge of 1st Block |
* +--------+--------+--------+--------+
* | |
* / . . . /
* | |
* +--------+--------+--------+--------+
* | Left Edge of nth Block |
* +--------+--------+--------+--------+
* | Right Edge of nth Block |
* +--------+--------+--------+--------+
*/
kind = TCPOPT_SACK;
length = (read_byte() % 4 + 1) * 8;
break;
case 6:
kind = TCPOPT_TIMESTAMP;
length = TCPOLEN_TIMESTAMP;
break;
case 7:
kind = TCPOPT_FASTOPEN;
length = read_byte() % MAX_OPTIONS_LENGTH;
break;
case 8:
kind = TCPOPT_EXP;
length = read_byte() % MAX_OPTIONS_LENGTH;
break;
default:
kind = tmp;
length = read_byte();
/**
* TODO: this is bug #17 which should be fixed later.
*/
if (length == 0) {
length = 2u;
}
break;
}
if (options_length + length > MAX_OPTIONS_LENGTH) {
break;
}
options_length += length;
push_data(&data, kind, &fuzz_data_size, &fuzz_data_capacity);
if (length > 1) {
push_data(&data, length, &fuzz_data_size, &fuzz_data_capacity);
}
for (int j = 0; j < length - 2; j++) {
char tmp = read_byte();
push_data(&data, tmp, &fuzz_data_size, &fuzz_data_capacity);
}
char tcp_option_string[100];
if (DEBUG_FUZZ_LOGGING) {
sprintf(tcp_option_string, "// fuzz_file_index is %d\n", fuzz_file_index);
add_content_to_fuzz_script(tcp_option_string);
}
sprintf(tcp_option_string, "// kind is %d, length is %d\n", kind, length);
add_content_to_fuzz_script(tcp_option_string);
}
}
tcp_header_length += options_length;
u8 padding_length = 4 - options_length % 4;
/**
* NOTE: if padding_length is 4, then we should not pad
*/
if (padding_length < 4) {
for (int i = 0; i < padding_length; i++) {
// padding
push_data(&data, 0x00, &fuzz_data_size, &fuzz_data_capacity);
tcp_header_length++;
}
}
// tcp offset, make reserved same with offset
u8 tcp_off = tcp_header_length / 4;
data[32] = tcp_off;
data[32] <<= 4;
data[32] |= tcp_off;
// dynamic data
if (is_fuzz_data) {
last_packet_data_length = read_int() % MAX_PACKET_DATA_LENGTH;
} else {
last_packet_data_length = 0;
}
for (uint i = 0; i < last_packet_data_length; i++) {
push_data(&data, (packet_seq + i) % 10, &fuzz_data_size, &fuzz_data_capacity);
}
// ip length
u16 ip_length = IP_HEADER_LENGTH + tcp_header_length + last_packet_data_length;
((struct ipv4 *) data)->tot_len = htons(ip_length);
struct packet *packet = packet_new(fuzz_data_size);
/* Populate and parse a packet */
memcpy(packet->buffer, data, fuzz_data_size);
free(data);
char *error = NULL;
enum packet_parse_result_t result =
parse_packet(packet, fuzz_data_size, PACKET_LAYER_3_IP, &error);
checksum_packet(packet);
if (error)
printf("%s\n", error);
if (result != PACKET_OK) {
printf("Packet ERROR!\n");
abort();
}
assert(error == NULL);
packet->direction = DIRECTION_INBOUND;
return packet;
}
static int max_times = -1;
void set_max_times_and_set_running(int t) {
is_validate_outbound_running = true;
max_times = t;
}
void *validate_outbound_packet(void *p) {
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
struct packet *packet = (struct packet *) p;
DEBUG_FUZZP("validate_outbound_packet\n");
int times = 0;
while (is_validate_outbound_running && (max_times < 0 || times++ < max_times)) {
struct packet *live_packet = NULL;
char *error = NULL;
int live_payload_len = 0;
struct socket *socket = NULL;
find_or_create_socket_for_script_packet(state, packet, packet->direction, &socket, &error);
int result = sniff_outbound_live_packet(state, socket, &live_packet, &error);
if (result == STATUS_ERR) {
continue;
}
if (live_packet->tcp) {
socket->last_outbound_tcp_header = *(live_packet->tcp);
/* Rewrite TCP sequence number */
if (tcp_convert_seq_number(socket, live_packet, &error))
goto out;
}
live_payload_len = packet_payload_len(live_packet);
DEBUGP("Sniffed packet with live_payload %d bytes\n", live_payload_len);
// if (DEBUG_FUZZ_LOGGING) {
packet_to_string(live_packet, DUMP_FULL, &error, &error);
printf("outbound_packet: %s\n", error);
// }
u32 seq = ntohl(live_packet->tcp->seq);
u8 *live_payload = packet_payload(live_packet);
for (int i = 0; i < live_payload_len; i++) {
if (live_payload[i] != (seq - 1 + i) % 10) {
is_validate_outbound_data_error = true;
}
}
if (live_packet->tcp->ack_seq != 0) {
last_received_packet_ack_seq = live_packet->tcp->ack_seq;
}
find_tcp_timestamp(live_packet, &error);
if (live_packet->tcp_ts_ecr != NULL) {
is_received_packet_has_timestamp = true;
last_received_packet_ecr = *(live_packet->tcp_ts_ecr);
}
out:
if (live_packet != NULL)
packet_free(live_packet);
}
return NULL;
}
void start_validate_outbound_thread(struct packet *packet) {
is_validate_outbound_running = true;
if (pthread_create(&tid, NULL, validate_outbound_packet, (void *) packet) != 0) {
die_perror("pthread_create");
}
}
void stop_validate_outbound_thread() {
is_validate_outbound_running = false;
pthread_cancel(tid);
}