-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_Earth.py
71 lines (54 loc) · 1.88 KB
/
google_Earth.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
import dpkt
import socket
import geoip2.database
import argparse
import webbrowser
def ret_KML(ip):
with geoip2.database.Reader("GeoLite2-City.mmdb") as gi:
rec = gi.city(ip)
try:
latitude = rec.location.latitude
longitude = rec.location.longitude
kml = (
f'<Placemark>\n'
f'<name>{ip}</name>\n'
f'<Point>\n'
f'<coordinates>{latitude:f},{longitude:f}</coordinates>\n'
f'</Point>\n'
f'</Placemark>\n'
)
return kml
except Exception as e:
print(e)
return ''
def plot_IPs(pcap):
kml_pts = ''
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)
src_kml = ret_KML(src)
dst_kml = ret_KML(dst)
kml_pts = kml_pts + src_kml + dst_kml
except Exception as e:
print(e)
pass
return kml_pts
#send_kml
if __name__ == "__main__":
parser=argparse.ArgumentParser(description="Print the GeoLocation of the IP adress ")
parser.add_argument("-p" ,required=True, dest="pcap", help="Add the pacap file location")
args= parser.parse_args()
pcap = args.pcap
with open(pcap,"rb") as file:
pcapf=dpkt.pcap.Reader(file)
kmlheader = '<?xml version="1.0" encoding="UTF-8"?>' \
'\n<kml xmlns="http://www.opengis.net/kml/2.2">' \
'\n<Document>\n'
kmlfooter = '</Document>\n</kml>\n'
kmldoc = kmlheader + plot_IPs(pcapf) + kmlfooter
with open("Google_mapped.kml","a+") as files:
files.writelines(kmldoc)
webbrowser.open("https://earth.google.com/web/")