In [1]:
from scapy.all import *
In [2]:
# pkts = sniff(filter="tcp and host 8.8.8.8", count=100)
sample_http = 'data/http.cap'
pkts = sniff(offline=sample_http)
In [3]:
pkts
Out[3]:
In [4]:
pkts[3].show()
In [5]:
raw = pkts[3].getlayer(Raw)
In [6]:
load = raw.fields.get('load')
In [7]:
print load
In [8]:
'GET /download' in load # your search term
Out[8]:
In [9]:
import select as s
def trace_route(pkts):
for pkt in pkts:
try:
IP_layer = pkt.getlayer(IP)
proto_layer = pkt.getlayer(TCP)
except Exception:
continue
destination = IP_layer.dst
src = IP_layer.src
dport = proto_layer.dport
sport = proto_layer.sport
while True:
try:
res, unans = traceroute(target=destination, dport=dport, sport=sport, maxttl=20)
traces = res.res
hops = [src]
for trace in traces:
hops.append(trace[1].src)
return hops, sport
except s.error:
continue
In [10]:
tr, sport = trace_route(pkts)
In [11]:
tr
Out[11]:
In [12]:
import pygeoip
In [13]:
def map_ip(hops):
gip = pygeoip.GeoIP('data/GeoLiteCity.dat')
coordinates = []
for hop in hops:
geo_data = gip.record_by_addr(hop)
if geo_data:
lat = geo_data['latitude']
lon = geo_data['longitude']
coordinates.append((lon, lat))
return coordinates
In [14]:
coordinates = map_ip(tr)
In [15]:
coordinates
Out[15]:
In [16]:
import geojson
def create_geojson(coordinates):
geo_list = []
j = 1
for route in coordinates:
data = {}
data["type"] = "Feature"
data["id"] = j
data["properties"] = {"title": "hop %i" % j}
data["geometry"] = {"type": "LineString", "coordinates": route}
j += 1
geo_list.append(data)
d = {"type": "FeatureCollection"}
for item in geo_list:
d.setdefault("features", []).append(item)
return geojson.dumps(d)
In [ ]:
print create_geojson(coordinates)
In [ ]: