-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
492 lines (413 loc) · 12.5 KB
/
main.cpp
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
#include <pcap.h>
#include <unistd.h>
#include <netinet/ip.h>
#ifdef __APPLE__
#include <netinet/if_ether.h>
#else
#include <netinet/ether.h>
#endif
#define __FAVOR_BSD
#include <netinet/tcp.h>
#include <arpa/inet.h>
#define ETHER_TYPE_IP (0x0800)
#define ETHER_TYPE_8021Q (0x8100)
#define MAX_ENTRY_COUNT 50000
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <unordered_map>
#include <iterator>
#include <list>
#include <algorithm>
#include <iostream>
#ifdef SECURE
#include <antidebug.h>
#endif
//#define DEBUG
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif
#define PPP_HDRLEN 4
using namespace std;
unordered_map<string, string> bytesMapping;
list<string> bytesLRU;
void addpair (time_t *ts, const string & key, string & value,
unordered_map<string, string> & data,
list<string> & lru);
void dumppair (const unordered_map<string, string> & data);
bool process_http (time_t *ts, const string & id, string & bytes);
bool KMP_getContentLength (const char *heystack, size_t hlen, size_t & result);
char *strnstr (const char *s, const char *find, size_t slen);
void help ();
void loop (pcap_t *handle);
bool badchar = true;
int main(int argc, char **argv)
{
pcap_t *handle;
struct bpf_program bpf;
char errbuf[PCAP_ERRBUF_SIZE], *device = NULL, *filter = NULL;
bpf_u_int32 net, mask;
int arg, snaplen = 0;
while ((arg = getopt (argc, argv, "ni:hf:s:")) != -1)
{
switch (arg)
{
case 'i':
device = optarg;
break;
case 'f':
filter = optarg;
break;
case 's':
snaplen = atoi (optarg);
break;
case 'n':
badchar = false;
break;
case 'h':
help ();
break;
default:
break;
}
}
if (argc < 2 && ! device)
help ();
bytesMapping.reserve (MAX_ENTRY_COUNT);
// alive
if (device)
{
if (pcap_lookupnet(device, &net, &mask, errbuf) == -1)
{
fprintf(stderr, "Can't get netmask for device %s: %s\n", device, errbuf);
exit (1);
}
if (! (handle = pcap_open_live (device, BUFSIZ, 1, 0, errbuf)))
{
fprintf(stderr, "pcap_open_live: %s\n", errbuf);
exit (1);
}
if (filter != NULL && -1 == pcap_compile (handle, &bpf, filter, 0, mask))
{
fprintf(stderr, "pcap_compile: %s\n", errbuf);
exit (1);
}
if (filter && -1 == pcap_setfilter (handle, &bpf))
{
fprintf(stderr, "pcap_setfilter: %s\n", errbuf);
exit (1);
}
if (-1 == pcap_set_snaplen (handle, snaplen))
{
fprintf(stderr, "pcap_set_snaplen: %s\n", errbuf);
exit (1);
}
loop(handle);
pcap_close(handle);
}
// offline
else
{
for (int i = optind; i < argc; ++ i)
{
if (! (handle = pcap_open_offline(argv[i], errbuf)))
{
fprintf(stderr, "Couldn't open pcap file %s: %s\n", argv[i], errbuf);
continue;
}
loop(handle);
pcap_close(handle); //close the pcap file
} // argv
}
dumppair (bytesMapping);
return 0; //done
} // main()
void dumppair (const unordered_map<string, string> & data)
{
for (unordered_map<string, string>::const_iterator iter = data.begin();
iter != data.end(); ++ iter)
{
cerr << "ID: " << iter->first << endl;
cerr << "Content: " << iter->second << endl << endl;
}
}
void addpair (time_t *ts, const string & key, string & value, unordered_map<string, string> & data, list<string> & lru)
{
unordered_map<string, string>::iterator iter = data.find (key);
if (iter == data.end())
{
if (process_http (ts, key, value))
return;
if (data.size() == MAX_ENTRY_COUNT)
{
data.erase (lru.front());
lru.pop_front();
}
data[key] = value;
lru.push_back (key);
}
else
{
string combined = (*iter).second + value;
if (process_http (ts, key, combined))
{
data.erase (key);
return;
}
data[key] = combined;
}
}
bool lower_test (const char & l, const char & r) {
return (std::tolower(l) == std::tolower(r));
}
void dump_packet (time_t *ts, const string & id, const string & bytes)
{
bool resp = false;
// no length check is necessary here, everything is valid
if (strnstr (bytes.c_str(), "HTTP/", 5) == bytes.c_str())
resp = true;
if (resp)
cout << "-- Response Packet size: " << bytes.size() << ", Time: " << *ts << ", Tuple: " << id << endl;
else
cout << "-- Packet size: " << bytes.size() << ", Time: " << *ts << ", Tuple: " << id << endl;
for (string::const_iterator iter = bytes.begin();
iter != bytes.end();
++ iter)
{
if (badchar
&& (! isprint (*iter) && *iter != '\n' && *iter != '\r'))
cout << ".";
else
cout << *iter;
}
cout << endl;
if (resp)
cout << "-- End Response Packet --" << endl << endl;
else
cout << "-- End Packet --" << endl << endl;
}
bool process_http (time_t *ts, const string & id, string & bytes)
{
// content-length
size_t length;
//
static const char *heading[] = { "GET ", "POST ", "HTTP/" };
// std::find sucks
size_t minlen = std::min<size_t> (bytes.size(), 5);
bool supported = false;
for (int i = 0; i < sizeof (heading) / sizeof (char*); ++ i)
if (strnstr (bytes.c_str(), heading[i], minlen) == bytes.c_str())
{
supported = true;
break;
}
if (! supported)
return true;
// Incomplete, either GET or POST
size_t payloadPos = bytes.find ("\r\n\r\n");
if (payloadPos == bytes.npos)
{
return false;
}
// No Content-Length header, ignore payloads even if supplied
if (! KMP_getContentLength (bytes.c_str(), payloadPos, length))
{
// Ending here
if (payloadPos < bytes.size() - 1)
{
D(
cerr << "Packe is complete (GET)");
dump_packet (ts, id, bytes);
return true;
}
else
{
// pos of payload + length("\r\n\r\n")
size_t length = payloadPos + 4;
D(
size_t actual = bytes.size();
cerr << " --- Exception --- " << id << endl
<< "HTTP Pipelining detected! Consumed " << length << " bytes. "
<< "remaining: " << actual - length << " bytes" << endl
<< "The multiple bytes are: " << bytes << endl << endl;);
dump_packet (ts, id, bytes.substr(0, length));
bytes.erase (0, length);
return process_http (ts, id, bytes);
}
}
else
{
size_t actual = bytes.size() - payloadPos - 4;
if (length == actual)
{
D(
cerr << "Packet is complete (POST)" << endl;);
dump_packet (ts, id, bytes);
return true;
}
else if (actual > length)
{
D(cerr << " --- Exception --- " << id << endl
<< "HTTP Pipelining detected! Consumed " << length << " bytes. "
<< "remaining: " << actual - length << " bytes" << endl
<< "The multiple bytes are: " << bytes << endl << endl;);
dump_packet (ts, id, bytes.substr(0, length));
bytes.erase (0, payloadPos + 4 + length);
return process_http (ts, id, bytes);
}
else
{
D(cerr << " --- Exception --- Unfinished POST request: " << id << endl
<< "Expecting: " << length << ", actual: " << actual << endl
<< "Current we have: " << bytes << endl
<< endl << "--- FINI Exception ---" << endl << endl;);
}
}
return false;
}
bool KMP_getContentLength (const char *heystack, size_t hlen, size_t & result)
{
static const char *needle = "content-length:";
static int nlen = strlen (needle);
result = 0;
for (int i = 0; i < hlen; ++ i)
{
for (int j = 0; j < nlen; ++ j)
{
if (tolower (heystack[i ++]) != tolower (needle [j]))
{
-- i; break;
}
if (j == nlen - 1)
{
for (; i < hlen; ++ i)
{
if (isdigit (heystack[i]))
result = result * 10 + heystack[i] - '0';
else if (heystack[i] == '\n')
break;
}
return true;
}
}
}
return false;
}
void help ()
{
fprintf(stderr, "myflow [input pcaps] ...\n"
"-or-\n"
"myflow -i eth0 ...\n\n"
"arguments: \n"
" -f bpf filter\n"
" -s snaplen\n"
" -n do not escape non-printable chars\n"
" -h show this dialog\n\n");
exit(1);
}
char *strnstr (const char *s, const char *find, size_t slen)
{
char c, sc;
size_t len;
if ((c = *find++) != '\0') {
len = strlen(find);
do {
do {
if (slen-- < 1 || (sc = *s++) == '\0')
return (NULL);
} while (sc != c);
if (len > slen)
return (NULL);
} while (strncmp(s, find, len) != 0);
s--;
}
return ((char *)s);
}
void loop (pcap_t *handle)
{
struct pcap_pkthdr header;
const u_char *packet;
int dlt = pcap_datalink (handle);
while (NULL != (packet = pcap_next(handle, &header)))
{
bpf_u_int32 caplen = header.caplen, offset = 0;
switch (dlt)
{
case DLT_EN10MB:
case DLT_IEEE802:
{
// ether header
if (caplen <= sizeof (struct ether_header))
{
cerr << "eth" << endl;
continue;
}
struct ether_header *eth_header = (struct ether_header *) packet;
offset += sizeof (ether_header);
if (ntohs(eth_header->ether_type) != ETHERTYPE_IP)
{
cerr << "not a ether frame" << endl;
continue;
}
}
break;
case DLT_PPP:
if (caplen < PPP_HDRLEN)
{
D(
cerr << "Incomplete PPP frame" << endl;);
continue;
}
offset += PPP_HDRLEN;
break;
case DLT_RAW:
break;
default:
D(
cerr << "Unsupported datalink type: " << dlt << endl;);
continue;
}
// ip header
if (offset >= caplen)
{
cerr << "ip" << endl;
continue;
}
struct ip *ip_header = (struct ip *) (packet + offset);
offset += ip_header->ip_hl * 4;
// tcp header
if (offset >= caplen)
{
cerr << "tcp" << endl;
continue;
}
struct tcphdr *tcp_header = (struct tcphdr *) (packet + offset);
offset += tcp_header->th_off * 4;
#ifndef __APPLE__
if (tcp_header->syn || tcp_header->rst)
{
// cerr << "SYN / RST packet" << endl;
continue;
}
#endif
// http data
if (offset >= caplen)
{
// cerr << "no more tcp, but caplen was " << caplen << endl;
continue;
}
char *srcip = strdup (inet_ntoa (ip_header->ip_src)),
*dstip = strdup (inet_ntoa (ip_header->ip_dst)),
tupleID [255];
snprintf (tupleID, 255, "%s:%d:%s:%d",
srcip, ntohs (tcp_header->th_sport),
dstip, ntohs (tcp_header->th_dport));
string value ((char*) (packet + offset), caplen - offset);
string key (tupleID);
addpair (&header.ts.tv_sec, key, value, bytesMapping, bytesLRU);
delete srcip; delete dstip;
} //end internal loop for reading packets (all in one file)
}