-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpgen_pcap.cc
627 lines (518 loc) · 13 KB
/
pgen_pcap.cc
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* Copyright 2011, 2012 SRI International
* See LICENSE for other credits and copying information
*/
#include "util.h"
#include "pgen.h"
#include "compression.h"
#include <dirent.h>
#include <signal.h>
#include <unistd.h>
#include <pcap/pcap.h>
#define __FAVOR_BSD 1
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#define NUM_FLOWS 1000
#define NUM_LISTS 1000
#define CONN_DATA_REQUEST 1 /* payload packet sent by client */
#define CONN_DATA_REPLY 2 /* payload packet sent by server */
#define RECV_MTU 64000
// #define PKT_MTU 1500
#define MAX_CHAIN_LEN 4000
#define MSG_INSERTED 1
#define MSG_INVALID 0
#define MSG_SEQ_WRAP -2
#define MSG_DUPLICATE -3
#define CHAIN_TOO_LONG -4
#define MSG_OVERLAP -5
#define CHAIN_HAS_GAPS_OVERLAPS -6
#define CHAIN_EMPTY -7
struct msg {
uint8_t *buf;
uint16_t len;
uint32_t seqno;
msg *next_msg;
};
struct flow {
uint32_t src_ip;
uint32_t dst_ip;
uint16_t sport;
uint16_t dport;
uint8_t flags;
uint8_t proto;
struct timeval change_time;
int sockfd;
flow *next_flow;
msg *msg_buf_chain;
int chain_len;
int msg_len_so_far;
int dir; /* data request or data reply */
uint32_t ack_so_far; /* what's acknowledged by other end so far */
};
static flow *flows[NUM_LISTS];
static pcap_t *descr;
static int dir_flag = 0;
static char *bp_filter;
static char errbuf[PCAP_ERRBUF_SIZE];
static struct bpf_program fp;
static uint32_t netp;
static const char *argv0;
#define RECV_MTU 64000
#define PORT_HTTP 80
static FILE *client_file;
static FILE *server_file;
static void ATTR_NORETURN
usage()
{
fprintf(stderr, "Usage: %s [-d dumpdir] [-r dumpfile] \"bpf filter\"\n",
argv0);
exit(1);
}
static void ATTR_NORETURN
terminate(int)
{
struct pcap_stat ps;
if (pcap_stats(descr, &ps) < 0) {
fputs("err: pcap stats not supported?\n", stderr);
exit(1);
}
printf("packets rcvd: %u, packets dropped: %u, interface drops: %u\n",
ps.ps_recv, ps.ps_drop, ps.ps_ifdrop);
exit(1);
}
static void
free_msg_chain(flow *f)
{
msg *m = f->msg_buf_chain;
while (m && f->chain_len > 0) {
msg *n = m->next_msg;
free(m);
m = n;
}
f->chain_len = 0;
f->msg_len_so_far = 0;
f->msg_buf_chain = 0;
}
static bool
has_chain_gaps(flow *f)
{
msg *m = f->msg_buf_chain;
while (m) {
if (!m->next_msg)
return false;
if (m->seqno + m->len < m->next_msg->seqno) {
fprintf(stderr, "gap seqnos: %u %u %u %d\n",
m->seqno, m->len, m->next_msg->seqno, f->dport);
return true;
}
if (m->seqno + m->len > m->next_msg-> seqno) {
fprintf(stderr, "overlap seqnos: %u %u %u\n",
m->seqno, m->len, m->next_msg->seqno);
return true;
}
m = m->next_msg;
}
return false;
}
static int
write_inflate_msg(flow *f, FILE *file, pentry_header *ph)
{
msg *m = f->msg_buf_chain;
uint8_t *buf;
int pos = 0;
uint8_t *outbuf;
int outlen;
uint8_t *hdr_end;
uint8_t *hdr;
int hdrlen;
if (!f->msg_buf_chain)
return CHAIN_EMPTY;
if (strstr((char*) m->buf, "Transfer-Encoding: chunked"))
// we don't handle this yet....need a loop to unzip chunks individually...
return MSG_INVALID;
hdr_end = (uint8_t*) strstr((char*) m->buf, "\r\n\r\n");
if (!hdr_end) {
fprintf(stderr, "hdr too long?? \n");
return MSG_INVALID;
}
hdr_end += 4;
hdrlen = hdr_end - m->buf;
hdr = (uint8_t *) xmemdup(m->buf, hdrlen);
buf = (uint8_t *) xmalloc(f->msg_len_so_far);
outbuf = (uint8_t *) xmalloc(f->msg_len_so_far * 20);
pos = 0;
if (!m)
return CHAIN_EMPTY;
memcpy(buf, hdr_end, m->len - hdrlen);
pos += m->len - hdrlen;
m = m->next_msg;
while (m) {
memcpy(buf+pos, m->buf, m->len);
pos += m->len;
m = m->next_msg;
}
outlen = decompress(buf, f->msg_len_so_far - hdrlen,
outbuf, f->msg_len_so_far*20);
if (outlen < 0) {
fprintf(stderr, "unzip failed outlen = %d %d %d\n",
outlen, pos, f->msg_len_so_far-hdrlen);
return MSG_INVALID;
}
ph->length = htonl(outlen+hdrlen);
if (fwrite(ph, sizeof(pentry_header), 1, file)!= sizeof(pentry_header))
log_warn("error writing data: %s", strerror(errno));
if (fwrite(hdr, hdrlen, 1, file)!= (unsigned int)hdrlen)
log_warn("error writing data: %s", strerror(errno));
if (fwrite(outbuf, outlen, 1, file)!= (unsigned int)outlen)
log_warn("error writing data: %s", strerror(errno));
free(buf);
free(outbuf);
free(hdr);
return 1;
}
static int
write_msg_chains(flow *f, FILE *file, pentry_header *ph)
{
msg *m = f->msg_buf_chain;
int cnt = 0;
if (has_chain_gaps(f))
return CHAIN_HAS_GAPS_OVERLAPS;
if (!m)
return CHAIN_EMPTY;
if (strstr((char*) m->buf, "200 OK") &&
strstr((char*) m->buf, "Content-Encoding: gzip"))
return write_inflate_msg(f, file, ph);
if (fwrite(ph, sizeof(pentry_header), 1, file) != sizeof(pentry_header))
log_warn("error writing data: %s", strerror(errno));
while (m) {
if (fwrite(m->buf, m->len, 1, file) != m->len)
log_warn("error writing data: %s", strerror(errno));
cnt += m->len;
m = m->next_msg;
}
if (cnt != f->msg_len_so_far)
fprintf(stderr, "something funky in writing message\n");
return 1;
}
static bool
is_valid_http_request(flow *f)
{
if (!f->msg_buf_chain) {
fprintf(stderr, "is_valid_http_request: invalid chain %d\n", f->chain_len);
return false;
}
if (!strncmp((char*) f->msg_buf_chain->buf, "GET", 3) ||
!strncmp((char*) f->msg_buf_chain->buf, "POST", 4)) {
msg *m = f->msg_buf_chain;
while (m->next_msg)
m = m->next_msg;
if (m->buf[m->len-2] == '\r' && m->buf[m->len-1] == '\n') {
return true;
}
}
return false;
}
static int
add_msg_to_flow(flow *f, uint8_t *buf, uint seq, int len)
{
if (len > RECV_MTU)
return MSG_INVALID;
if (f->chain_len >= MAX_CHAIN_LEN)
return CHAIN_TOO_LONG;
if (seq > seq + len)
return MSG_SEQ_WRAP;
msg *p = 0;
msg *m = f->msg_buf_chain;
if (!m) {
m = (msg *)xzalloc(sizeof(msg));
m->buf = (uint8_t *)xmalloc(len);
memcpy(m->buf, buf, len);
m->seqno = seq;
f->chain_len = 1;
f->msg_len_so_far += len;
f->msg_buf_chain = m;
m->len = len;
return MSG_INSERTED;
}
while (m) {
if (m->seqno == seq)
return MSG_DUPLICATE;
if (m->seqno < seq) {
if (m->seqno > seq + len)
return MSG_OVERLAP;
p = m;
m = m->next_msg;
continue;
}
if (m->seqno < seq + len)
return MSG_OVERLAP;
msg *n;
if (!p) {
p = (msg *)xzalloc(sizeof(msg));
p->buf = (uint8_t *)xmalloc(len);
memcpy(p->buf, buf, len);
p->seqno = seq;
p->next_msg = m;
f->chain_len++;
f->msg_len_so_far += len;
f->msg_buf_chain = p;
p->len = len;
return MSG_INSERTED;
}
n = (msg *)xzalloc(sizeof(msg));
n->buf = (uint8_t *)xmemdup(buf, len);
memcpy(n->buf, buf, len);
n->seqno = seq;
n->next_msg = m;
p->next_msg = n;
f->chain_len++;
f->msg_len_so_far += len;
n->len = len;
return MSG_INSERTED;
}
m = (msg *)xzalloc(sizeof(msg));
m->buf = (uint8_t *)xmemdup(buf, len);
m->seqno = seq;
p->next_msg = m;
f->chain_len++;
f->msg_len_so_far += len;
m->len = len;
return MSG_INSERTED;
}
static int
hash_flow(flow *f)
{
return (f->src_ip + f->dst_ip + f->sport + f->dport) % NUM_LISTS;
}
static bool
flow_compare(flow *f1, flow *f2)
{
return (f1->src_ip == f2->src_ip && f1->dst_ip == f2->dst_ip
&& f1->sport == f2->sport && f1->dport == f2->dport);
}
static flow *
add_to_flows(flow *f)
{
int hval = hash_flow(f);
flow *cflow = flows[hval];
if (!cflow) {
cflow = (flow *) xmalloc(sizeof(flow));
memcpy(cflow, f, sizeof(flow));
cflow->next_flow = 0;
flows[hval] = cflow;
return cflow;
}
else {
// add flow to the beginning of the chain
flow *old_flow = (flow *) xmalloc(sizeof(flow));
memcpy(old_flow, cflow, sizeof(flow));
memcpy(cflow, f, sizeof(flow));
cflow->next_flow = old_flow;
}
return cflow;
}
static flow *
has_seen_flow(flow *f)
{
int hval = hash_flow(f);
flow *cflow = flows[hval];
while (cflow) {
if (flow_compare(cflow, f))
return cflow;
cflow = cflow->next_flow;
}
return 0;
}
static flow *
reverse_flow(flow *f)
{
uint32_t tmp_ip;
uint16_t tmp_port;
tmp_ip = f->src_ip;
f->src_ip = f->dst_ip;
f->dst_ip = tmp_ip;
tmp_port = f->sport;
f->sport = f->dport;
f->dport = tmp_port;
return f;
}
static void
write_http_packet(flow *f)
{
pentry_header ph;
ph.length = htonl(f->msg_len_so_far);
ph.port = htons(80);
if (f->dir == CONN_DATA_REQUEST) {
ph.ptype = htons(TYPE_HTTP_REQUEST);
if (is_valid_http_request(f))
write_msg_chains(f, client_file, &ph);
}
else {
ph.ptype = htons(TYPE_HTTP_RESPONSE);
write_msg_chains(f, server_file, &ph);
}
}
static void
write_packet(flow *f)
{
uint16_t tport;
if (f->dir == CONN_DATA_REQUEST)
tport = f->dport;
else
tport = f->sport;
switch(tport) {
case PORT_HTTP:
write_http_packet(f);
}
}
static void
my_callback(uint8_t * /*unused*/,
const struct pcap_pkthdr *pkthdr,
const uint8_t *packet)
{
struct ether_header *eth = (struct ether_header*) (packet) ;
int rval;
if (ntohs(eth->ether_type) == ETHERTYPE_IP) {
struct ip *iph = (struct ip*) (packet + sizeof(struct ether_header));
struct tcphdr *tcph = (struct tcphdr*)
((uint8_t*)iph + sizeof(struct ip));
int len = htons(iph->ip_len) - 4*tcph->th_off - sizeof(struct ip);
uint8_t *payload = (uint8_t*) tcph + 4*tcph->th_off;
flow f;
flow *cflow;
flow *rflow;
memset(&f, 0, sizeof(flow));
f.src_ip = iph->ip_src.s_addr;
f.dst_ip = iph->ip_dst.s_addr;
f.sport = ntohs(tcph->th_sport);
f.dport = ntohs(tcph->th_dport);
f.flags = tcph->th_flags;
f.proto = iph->ip_p;
f.change_time = pkthdr->ts;
if (tcph->th_flags & TH_SYN && !(tcph->th_flags & TH_ACK)) {
f.dir = CONN_DATA_REQUEST;
add_to_flows(&f);
return;
}
else if ((tcph->th_flags & TH_SYN) && (tcph->th_flags & TH_ACK)) {
f.dir = CONN_DATA_REPLY;
add_to_flows(&f);
return;
}
cflow = has_seen_flow(&f);
if (!cflow)
return;
rflow = has_seen_flow(reverse_flow(&f));
if (!rflow)
return;
rflow->ack_so_far = ntohl(tcph->th_ack);
cflow->flags = cflow->flags | tcph->th_flags;
if (len > 0 && ntohl(tcph->th_seq) >= cflow->ack_so_far) {
if (rflow->msg_len_so_far > 0) {
write_packet(rflow);
free_msg_chain(rflow);
}
rval = add_msg_to_flow(cflow, payload, ntohl(tcph->th_seq), len);
if (rval <= 0 && rval !=MSG_DUPLICATE && rval != CHAIN_TOO_LONG) {
fprintf(stderr, "adding msg to flow failed %d %d\n", rval, len);
}
}
if (cflow->flags & TH_RST || cflow->flags & TH_FIN) {
if (rflow->msg_len_so_far > 0) {
write_packet(rflow);
free_msg_chain(rflow);
}
return;
}
}
}
static void
handle_pcap_file(const char *filename)
{
descr = pcap_open_offline(filename, errbuf);
if (!descr) {
fprintf(stderr, "%s: %s\n", filename, errbuf);
exit(1);
}
if (pcap_compile(descr, &fp, bp_filter, 1, netp) == -1) {
fprintf(stderr, "Error calling pcap_compile on \"%s\"\n", bp_filter);
exit(1);
}
/* set the compiled program as the filter */
if (pcap_setfilter(descr, &fp) == -1) {
fprintf(stderr,"Error setting filter\n");
exit(1);
}
/* main pcap loop */
pcap_loop(descr, -1, my_callback, 0);
pcap_close(descr);
}
static void
list_files(const char *dirname)
{
DIR *dip;
struct dirent *dit;
char *fname;
size_t plen = strlen(dirname);
if ((dip = opendir(dirname)) == 0) {
perror("opendir");
return;
}
while ((dit = readdir(dip)) != 0) {
if (!strcmp(dit->d_name, ".") || !strcmp(dit->d_name, ".."))
continue;
size_t dlen = strlen(dit->d_name);
fname = (char *)xmalloc(plen + dlen + 2);
memcpy(fname, dirname, plen);
fname[plen] = '/';
memcpy(fname + plen + 1, dit->d_name, dlen);
fname[plen + dlen + 1] = '\0';
fprintf(stderr, "%s\n", fname);
handle_pcap_file(fname);
free(fname);
}
closedir(dip);
}
int
main(int argc, char **argv)
{
int c;
const char *dumpfile = 0;
argv0 = argv[0];
while ((c = getopt (argc, argv, "r:d:")) != -1) {
switch (c) {
case 'r':
dumpfile = optarg;
break;
case 'd':
dir_flag = 1;
dumpfile = optarg;
break;
default:
usage();
}
}
if (!argv[optind] || !dumpfile)
usage();
bp_filter = xstrdup(argv[optind]);
client_file = fopen("traces/client.out", "w");
if (!client_file) {
perror("traces/client.out");
return 1;
}
server_file = fopen("traces/server.out", "w");
if (!server_file) {
perror("traces/server.out");
return 1;
}
/* catch ^C print stats and exit */
signal(SIGTERM, terminate);
signal(SIGINT, terminate);
signal(SIGHUP, terminate);
if (dir_flag)
list_files(dumpfile);
else
handle_pcap_file(dumpfile);
return 0;
}