forked from aflnet/aflnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aflnet.c
584 lines (484 loc) · 15.8 KB
/
aflnet.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include "alloc-inl.h"
#include "aflnet.h"
// Protocol-specific functions for extracting requests and responses
region_t* extract_requests_rtsp(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
char terminator[4] = {0x0D, 0x0A, 0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the last four bytes are 0x0D0A0D0A
if ((mem_count > 3) && (memcmp(&mem[mem_count - 3], terminator, 4) == 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
region_t* extract_requests_ftp(unsigned char* buf, unsigned int buf_size, unsigned int* region_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int region_count = 0;
region_t *regions = NULL;
char terminator[2] = {0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
unsigned int cur_start = 0;
unsigned int cur_end = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the last two bytes are 0x0D0A
if ((mem_count > 1) && (memcmp(&mem[mem_count - 1], terminator, 2) == 0)) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
mem_count = 0;
cur_start = cur_end + 1;
cur_end = cur_start;
} else {
mem_count++;
cur_end++;
//Check if the last byte has been reached
if (cur_end == buf_size - 1) {
region_count++;
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
break;
}
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
//in case region_count equals zero, it means that the structure of the buffer is broken
//hence we create one region for the whole buffer
if ((region_count == 0) && (buf_size > 0)) {
regions = (region_t *)ck_realloc(regions, sizeof(region_t));
regions[0].start_byte = 0;
regions[0].end_byte = buf_size - 1;
regions[0].state_sequence = NULL;
regions[0].state_count = 0;
region_count = 1;
}
*region_count_ref = region_count;
return regions;
}
unsigned int* extract_response_codes_rtsp(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
char terminator[2] = {0x0D, 0x0A};
char rtsp[5] = {0x52, 0x54, 0x53, 0x50, 0x2f};
mem=(char *)ck_alloc(mem_size);
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
//Check if the last two bytes are 0x0D0A
if ((mem_count > 0) && (memcmp(&mem[mem_count - 1], terminator, 2) == 0)) {
if ((mem_count >= 5) && (memcmp(mem, rtsp, 5) == 0)) {
//Extract the response code which is the first 3 bytes
char temp[4];
memcpy(temp, &mem[9], 4);
temp[3] = 0x0;
unsigned int message_code = (unsigned int) atoi(temp);
if (message_code == 0) break;
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = message_code;
mem_count = 0;
} else {
mem_count = 0;
}
} else {
mem_count++;
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
*state_count_ref = state_count;
return state_sequence;
}
unsigned int* extract_response_codes_ftp(unsigned char* buf, unsigned int buf_size, unsigned int* state_count_ref)
{
char *mem;
unsigned int byte_count = 0;
unsigned int mem_count = 0;
unsigned int mem_size = 1024;
unsigned int *state_sequence = NULL;
unsigned int state_count = 0;
char terminator[2] = {0x0D, 0x0A};
mem=(char *)ck_alloc(mem_size);
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = 0;
while (byte_count < buf_size) {
memcpy(&mem[mem_count], buf + byte_count++, 1);
if ((mem_count > 0) && (memcmp(&mem[mem_count - 1], terminator, 2) == 0)) {
//Extract the response code which is the first 3 bytes
char temp[4];
memcpy(temp, mem, 4);
temp[3] = 0x0;
unsigned int message_code = (unsigned int) atoi(temp);
if (message_code == 0) break;
state_count++;
state_sequence = (unsigned int *)ck_realloc(state_sequence, state_count * sizeof(unsigned int));
state_sequence[state_count - 1] = message_code;
mem_count = 0;
} else {
mem_count++;
if (mem_count == mem_size) {
//enlarge the mem buffer
mem_size = mem_size * 2;
mem=(char *)ck_realloc(mem, mem_size);
}
}
}
if (mem) ck_free(mem);
*state_count_ref = state_count;
return state_sequence;
}
// kl_messages manipulating functions
klist_t(lms) *construct_kl_messages(u8* fname, region_t *regions, u32 region_count)
{
FILE *fseed = NULL;
fseed = fopen(fname, "rb");
if (fseed == NULL) PFATAL("Cannot open seed file %s", fname);
klist_t(lms) *kl_messages = kl_init(lms);
u32 i;
for (i = 0; i < region_count; i++) {
//Identify region size
u32 len = regions[i].end_byte - regions[i].start_byte + 1;
//Create a new message
message_t *m = (message_t *) ck_alloc(sizeof(message_t));
m->mdata = (char *) ck_alloc(len);
m->msize = len;
if (m->mdata == NULL) PFATAL("Unable to allocate memory region to store new message");
fread(m->mdata, 1, len, fseed);
//Insert the message to the linked list
*kl_pushp(lms, kl_messages) = m;
}
if (fseed != NULL) fclose(fseed);
return kl_messages;
}
void delete_kl_messages(klist_t(lms) *kl_messages)
{
/* Free all messages in the list before destroying the list itself */
message_t *m;
int ret = kl_shift(lms, kl_messages, &m);
while (ret == 0) {
if (m) {
ck_free(m->mdata);
ck_free(m);
}
ret = kl_shift(lms, kl_messages, &m);
}
/* Finally, destroy the list */
kl_destroy(lms, kl_messages);
}
kliter_t(lms) *get_last_message(klist_t(lms) *kl_messages)
{
kliter_t(lms) *it;
it = kl_begin(kl_messages);
while (kl_next(it) != kl_end(kl_messages)) {
it = kl_next(it);
}
return it;
}
u32 save_kl_messages_to_file(klist_t(lms) *kl_messages, u8 *fname, u8 replay_enabled, u32 max_count)
{
u8 *mem = NULL;
u32 len = 0, message_size = 0;
kliter_t(lms) *it;
s32 fd = open(fname, O_WRONLY | O_CREAT, 0600);
if (fd < 0) PFATAL("Unable to create file '%s'", fname);
u32 message_count = 0;
//Iterate through all messages in the linked list
for (it = kl_begin(kl_messages); it != kl_end(kl_messages) && message_count < max_count; it = kl_next(it)) {
message_size = kl_val(it)->msize;
if (replay_enabled) {
mem = (u8 *)ck_realloc(mem, 4 + len + message_size);
//Save packet size first
u32 *psize = (u32*)&mem[len];
*psize = message_size;
//Save packet content
memcpy(&mem[len + 4], kl_val(it)->mdata, message_size);
len = 4 + len + message_size;
} else {
mem = (u8 *)ck_realloc(mem, len + message_size);
//Save packet content
memcpy(&mem[len], kl_val(it)->mdata, message_size);
len = len + message_size;
}
message_count++;
}
//Write everything to file & close the file
ck_write(fd, mem, len, fname);
close(fd);
//Free the temporary buffer
ck_free(mem);
return len;
}
region_t* convert_kl_messages_to_regions(klist_t(lms) *kl_messages, u32* region_count_ref, u32 max_count)
{
region_t *regions = NULL;
kliter_t(lms) *it;
u32 region_count = 1;
s32 cur_start = 0, cur_end = 0;
//Iterate through all messages in the linked list
for (it = kl_begin(kl_messages); it != kl_end(kl_messages) && region_count <= max_count ; it = kl_next(it)) {
regions = (region_t *)ck_realloc(regions, region_count * sizeof(region_t));
cur_end = cur_start + kl_val(it)->msize - 1;
if (cur_end < 0) PFATAL("End_byte cannot be negative");
regions[region_count - 1].start_byte = cur_start;
regions[region_count - 1].end_byte = cur_end;
regions[region_count - 1].state_sequence = NULL;
regions[region_count - 1].state_count = 0;
cur_start = cur_end + 1;
region_count++;
}
*region_count_ref = region_count - 1;
return regions;
}
// Network communication functions
int net_send(int sockfd, struct timeval timeout, char *mem, unsigned int len) {
unsigned int byte_count = 0;
int n;
struct pollfd pfd[1];
pfd[0].fd = sockfd;
pfd[0].events = POLLOUT;
int rv = poll(pfd, 1, 1);
setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
if (rv > 0) {
if (pfd[0].revents & POLLOUT) {
while (byte_count < len) {
usleep(10);
n = send(sockfd, &mem[byte_count], len - byte_count, MSG_NOSIGNAL);
if (n == 0) return byte_count;
if (n == -1) return -1;
byte_count += n;
}
}
}
return byte_count;
}
int net_recv(int sockfd, struct timeval timeout, int poll_w, char **response_buf, unsigned int *len) {
char temp_buf[1000];
int n;
struct pollfd pfd[1];
pfd[0].fd = sockfd;
pfd[0].events = POLLIN;
int rv = poll(pfd, 1, poll_w);
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
if (rv > 0) {
if (pfd[0].revents & POLLIN) {
n = recv(sockfd, temp_buf, sizeof(temp_buf), 0);
if ((n < 0) && (errno != 11)) {
return 1;
}
while (n > 0) {
usleep(10);
*response_buf = (unsigned char *)ck_realloc(*response_buf, *len + n);
memcpy(&(*response_buf)[*len], temp_buf, n);
*len = *len + n;
n = recv(sockfd, temp_buf, sizeof(temp_buf), 0);
if ((n < 0) && (errno != 11)) {
return 1;
}
}
}
} else if (rv < 0) return 1;
return 0;
}
// Utility function
void save_regions_to_file(region_t *regions, unsigned int region_count, unsigned char *fname)
{
int fd;
FILE* fp;
fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (fd < 0) return;
fp = fdopen(fd, "w");
if (!fp) {
close(fd);
return;
}
int i;
for(i=0; i < region_count; i++) {
fprintf(fp, "Region %d - Start: %d, End: %d\n", i, regions[i].start_byte, regions[i].end_byte);
}
fclose(fp);
}
int str_split(char* a_str, const char* a_delim, char **result, int a_count)
{
char *token;
int count = 0;
/* count number of tokens */
/* get the first token */
char* tmp1 = strdup(a_str);
token = strtok(tmp1, a_delim);
/* walk through other tokens */
while (token != NULL)
{
count++;
token = strtok(NULL, a_delim);
}
if (count != a_count)
{
return 1;
}
/* split input string, store tokens into result */
count = 0;
/* get the first token */
token = strtok(a_str, a_delim);
/* walk through other tokens */
while (token != NULL)
{
result[count] = token;
count++;
token = strtok(NULL, a_delim);
}
free(tmp1);
return 0;
}
void str_rtrim(char* a_str)
{
char* ptr = a_str;
int count = 0;
while ((*ptr != '\n') && (*ptr != '\t') && (*ptr != ' ') && (count < strlen(a_str))) {
ptr++;
count++;
}
if (count < strlen(a_str)) {
*ptr = '\0';
}
}
int parse_net_config(u8* net_config, u8* protocol, u8** ip_address, u32* port)
{
char buf[80];
char **tokens;
int tokenCount = 3;
tokens = (char**)malloc(sizeof(char*) * (tokenCount));
if (strlen(net_config) > 80) return 1;
strncpy(buf, net_config, strlen(net_config));
str_rtrim(buf);
if (!str_split(buf, "/", tokens, tokenCount))
{
if (!strcmp(tokens[0], "tcp:")) {
*protocol = PRO_TCP;
} else if (!strcmp(tokens[0], "udp:")) {
*protocol = PRO_UDP;
} else return 1;
//TODO: check the format of this IP address
*ip_address = strdup(tokens[1]);
*port = atoi(tokens[2]);
if (*port == 0) return 1;
}
return 0;
}
u8* state_sequence_to_string(unsigned int *stateSequence, unsigned int stateCount) {
u32 i = 0;
u8 *out = NULL;
char strState[10];
int len = 0;
for (i = 0; i < stateCount; i++) {
//Limit the loop to shorten the output string
if ((i >= 2) && (stateSequence[i] == stateSequence[i - 1]) && (stateSequence[i] == stateSequence[i - 2])) continue;
unsigned int stateID = stateSequence[i];
if (i == stateCount - 1) {
sprintf(strState, "%d", (int) stateID);
} else {
sprintf(strState, "%d-", (int) stateID);
}
out = (u8 *)ck_realloc(out, len + strlen(strState) + 1);
memcpy(&out[len], strState, strlen(strState) + 1);
len=strlen(out);
//As Linux limit the size of the file name
//we set a fixed upper bound here
if (len > 150 && (i + 1 < stateCount)) {
sprintf(strState, "%s", "end-at-");
out = (u8 *)ck_realloc(out, len + strlen(strState) + 1);
memcpy(&out[len], strState, strlen(strState) + 1);
len=strlen(out);
sprintf(strState, "%d", (int) stateSequence[stateCount - 1]);
out = (u8 *)ck_realloc(out, len + strlen(strState) + 1);
memcpy(&out[len], strState, strlen(strState) + 1);
len=strlen(out);
break;
}
}
return out;
}