]> git.somenet.org - pub/jan/netsec2.git/blob - exercise2/task2/readflows.py
Stop hiding latex/build-errors. Also readded unused info and added easteregg image.
[pub/jan/netsec2.git] / exercise2 / task2 / readflows.py
1 #!/usr/bin/env python
2
3 # disable IPv6 error message
4 import logging
5 logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
6 from scapy.all import *
7 logging.getLogger("scapy.runtime").setLevel(logging.WARN)
8 from pprint import pprint
9 import os
10
11 # disable payload parsing (saves two seconds runtime^^)
12 IP.payload_guess = []
13
14 flowset = {}
15
16 fcount = 0
17 # V discards the wirelen... ( http://stackoverflow.com/questions/21752576/whole-packet-length-scapy )
18 #for p in PcapReader('ex22.pcap'):
19 for pkt, (sec, usec, wirelen) in RawPcapReader('ex22.pcap'):
20     p = Ether(pkt)
21     if IP in p:
22         src = p[IP].src
23         dst = p[IP].dst
24         #lenip = p[Ether].len
25         lenip = p[IP].len
26         if wirelen != 42:
27             continue
28         fcount = fcount + 1
29 #        print ("Src: %s (ether), Dest: %s (ip)" % (src, dst))
30
31         if (src,dst) in flowset:
32             i,o,l = flowset[(src,dst)]
33             if wirelen in l:
34                 l[wirelen] = l[wirelen]+1
35             else:
36                 l[wirelen] = 1
37             flowset[(src,dst)] = (i+1,o,l)
38         elif (dst,src) in flowset:
39             i,o,l = flowset[(dst,src)]
40             if wirelen in l:
41                 l[wirelen] = l[wirelen]+1
42             else:
43                 l[wirelen] = 1
44             flowset[(dst,src)] = (i,o+1,l)
45         else:
46             flowset[(src,dst)] = (1,0,{wirelen: 1})
47
48 print("fcount: %d" % fcount)
49
50 largeflows = {}
51
52 filecount = 0
53 for flow,counts in flowset.items():
54     if counts[0] > 200 or counts[1] > 200:
55         largeflows[flow] = (counts[0],counts[1])
56         # extract each flow
57         if counts[0] > 200:
58             filecount = filecount + 1
59             filename = "large_flow_%02d.pcap" % filecount
60             os.system('tshark -r ex22.pcap -w '+filename+' -F pcap "ip.src == '+flow[0]+' and ip.dst == '+flow[1]+' and frame.len == 42"')
61         if counts[1] > 200:
62             filecount = filecount + 1
63             filename = "large_flow_%02d.pcap" % filecount
64             os.system('tshark -r ex22.pcap -w '+filename+' -F pcap "ip.src == '+flow[1]+' and ip.dst == '+flow[0]+' and frame.len == 42"')
65
66 pprint(largeflows)