-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (54 loc) · 1.75 KB
/
main.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
import dpkt
import socket
import pygeoip
gi = pygeoip.GeoIP('GeoLiteCity.dat')
def main():
f = open('wire.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
kmlheader = '<?xml version="1.0" encoding="UTF-8"?> \n<kml xmlns="http://www.opengis.net/kml/2.2">\n<Document>\n'\
'<Style id="transBluePoly">' \
'<LineStyle>' \
'<width>1.5</width>' \
'<color>501400E6</color>' \
'</LineStyle>' \
'</Style>'
kmlfooter = '</Document>\n</kml>\n'
kmldoc=kmlheader+plotIPs(pcap)+kmlfooter
print(kmldoc)
def plotIPs(pcap):
kmlPts = ''
for (ts, buf) in pcap:
try:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
src = socket.inet_ntoa(ip.src)
dst = socket.inet_ntoa(ip.dst)
KML = retKML(dst, src)
kmlPts = kmlPts + KML
except:
pass
return kmlPts
def retKML(dstip, srcip):
dst = gi.record_by_name(dstip)
src = gi.record_by_name('103.4.222.252')
try:
dstlongitude = dst['longitude']
dstlatitude = dst['latitude']
srclongitude = src['longitude']
srclatitude = src['latitude']
kml = (
'<Placemark>\n'
'<name>%s</name>\n'
'<extrude>1</extrude>\n'
'<tessellate>1</tessellate>\n'
'<styleUrl>#transBluePoly</styleUrl>\n'
'<LineString>\n'
'<coordinates>%6f,%6f\n%6f,%6f</coordinates>\n'
'</LineString>\n'
'</Placemark>\n'
)%(dstip, dstlongitude, dstlatitude, srclongitude, srclatitude)
return kml
except:
return ''
if __name__ == '__main__':
main()