-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessPcap.py
63 lines (53 loc) · 1.49 KB
/
processPcap.py
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
from scapy.all import *
import argparse
import re
FIN = 0x01
SYN = 0x02
RST = 0x04
PSH = 0x08
ACK = 0x10
URG = 0x20
ECE = 0x40
CWR = 0x80
parser = argparse.ArgumentParser(description='Generates a monitor_flujos-alike csv output')
parser.add_argument('-i', '--input', type=str, help='Input pcap file', required=True)
parser.add_argument('-o', '--output', type=str, help='Output file', required=True)
args = parser.parse_args()
print(args)
exit
trace = rdpcap(args.input)
SYNs = {}
SYNACKs = {}
res = {}
for packet in trace:
if IP in packet and TCP in packet:
srcIP = packet['IP'].src
sport = packet['TCP'].sport
dstIP = packet['IP'].dst
dport = packet['TCP'].dport
if sport > dport:
srcIP, dstIP = dstIP, srcIP
sport, dport = dport, sport
qtuple = (srcIP, dstIP, sport, dport)
sqtuple = str(qtuple)
F = packet.sprintf('%TCP.flags%')
if F == 'S':
#print('SYN')
SYNs[sqtuple] = packet.time
#print(qtuple)
elif F == 'SA':
#print('SYNACK')
SYNACKs[sqtuple] = packet.time
#print(qtuple)
for k, pSYN in SYNs.iteritems():
if k in SYNACKs:
res[k] = (SYNACKs[k] - pSYN)
else:
print('SYNACK not found for %s' % (k))
print(res)
print(min(res.values()), max(res.values()))
f = open(args.output, 'w')
for k, v in res.iteritems():
k = re.sub('[!\'()]', '', k)
k = '%s, %.12f' % (k, v)
f.write(k+"\n")