-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpcap-http.py
72 lines (70 loc) · 2.4 KB
/
pcap-http.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
63
64
65
66
67
68
69
70
71
72
import dpkt, pcap
import re
import socket
import time
from struct import unpack
pc = pcap.pcap()
pc.setfilter("port 80")
tracker={}
for ts, pkt in pc:
try:
ethernet=dpkt.ethernet.Ethernet(pkt)
ip=ethernet.data
tcp=ip.data
data=tcp.data
dst=socket.inet_ntoa(ip.dst)
src=socket.inet_ntoa(ip.src)
except socket.error:
continue
ltuple=(src,tcp.sport,dst,tcp.dport)
rtuple=(dst,tcp.dport,src,tcp.sport)
try:
tracker[ltuple]
try:
tracker[ltuple]['out']+=data
except KeyError:
tracker[ltuple]['out']=data
tracker[ltuple]['lastseen']=time.time()
except KeyError:
try:
tracker[rtuple]
tracker[rtuple]['in']+=data
except KeyError:
tracker[rtuple]={
'in' : data,
'complete' : False,
'firstseen' : time.time()
}
tracker[rtuple]['lastseen']=time.time()
for connection in tracker.keys():
if time.time()-tracker[connection]['lastseen']>600:
del tracker[connection]
continue
if tracker[connection]['complete']==True:
if time.time()-tracker[connection]['lastseen']>60:
del tracker[connection]
continue
try:
if tracker[connection]['in'] and tracker[connection]['out']:
data=tracker[connection]['in']
data+=tracker[connection]['out']
uri=re.search("(GET|POST) ([^\r\n]*)",data,re.IGNORECASE)
host=re.search("Host: ([^\r\n]*)",data,re.IGNORECASE)
response=re.search("(HTTP/[\d\.]+) (\d+) ([^\r\n]+)",data,re.IGNORECASE)
if uri and host and response:
print "%-8d [%16s:%-6d => %16s:%-6d ] [ %s %s %-16s ] %6s http://%s%s" % (
len(tracker),
connection[0],
connection[1],
connection[2],
connection[3],
response.group(1),
response.group(2),
response.group(3),
uri.group(1),
host.group(1),
uri.group(2)
)
tracker[connection]['complete']=True
except KeyError:
pass