Last active
December 15, 2015 03:39
-
-
Save sebgoa/5196009 to your computer and use it in GitHub Desktop.
TCP traceroute using Scapy and plot the resulting KML files of hops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scapy.all import * | |
import GeoIP | |
import xml.dom.minidom | |
def createkml(urls): | |
''' This function creates an XML document and adds the necessary | |
KML elements. | |
Mostly inspired by http://code.google.com/apis/kml/articles/csvtokml.html | |
''' | |
kmlDoc = xml.dom.minidom.Document() | |
kmlElement = kmlDoc.createElementNS('http://earth.google.com/kml/2.2','kml') | |
kmlElement.setAttribute('xmlns','http://earth.google.com/kml/2.2') | |
kmlElement = kmlDoc.appendChild(kmlElement) | |
documentElement = kmlDoc.createElement('Document') | |
documentElement = kmlElement.appendChild(documentElement) | |
for url in urls: | |
placemarkElement = kmlDoc.createElement('Placemark') | |
extElement = kmlDoc.createElement('ExtendedData') | |
placemarkElement.appendChild(extElement) | |
descriptionElement = kmlDoc.createElement('Data') | |
descriptionElement.setAttribute('name', 'URL') | |
urlElement = kmlDoc.createElement('value') | |
descriptionElement.appendChild(urlElement) | |
urlText = kmlDoc.createTextNode(url) | |
urlElement.appendChild(urlText) | |
extElement.appendChild(descriptionElement) | |
pointElement = kmlDoc.createElement('Point') | |
placemarkElement.appendChild(pointElement) | |
coorElement = kmlDoc.createElement('coordinates') | |
# This geocodes the address and adds it to a <Point> element. | |
coordinates = geocoord(url) | |
coorElement.appendChild(kmlDoc.createTextNode(coordinates)) | |
pointElement.appendChild(coorElement) | |
documentElement.appendChild(placemarkElement) | |
# This writes the KML Document to a file. | |
fh=open('/Users/runseb/Desktop/traceroute.kml','w') | |
fh.write(kmlDoc.toprettyxml(' ', newl = '\n', encoding = 'utf-8')) | |
fh.close() | |
return | |
def scapytraceroute(host): | |
'''Uses scapy to do a tcp traceroute hopefully goes through firewalls''' | |
hops=[] | |
try: | |
res,unans=traceroute(host) | |
except: | |
print "Could not trace route with scapy !" | |
return hops | |
host_key=res.get_trace().keys()[0] | |
for key in res.get_trace()[host_key].keys(): | |
hops.append(res.get_trace()[host_key][key][0]) | |
return hops | |
def geocoord(ip): | |
gic = GeoIP.open('/Users/runseb/GeoLiteCity-20110501/GeoLiteCity.dat',GeoIP.GEOIP_STANDARD) | |
print ip | |
try: | |
longi = gic.record_by_addr(ip)['longitude'] | |
lat = gic.record_by_addr(ip)['latitude'] | |
except: | |
print "GeoIP failed" | |
return (0,0) | |
return '%s,%s' % (longi,lat) | |
def main(): | |
iplist=scapytraceroute('www.google.com') | |
coords=[] | |
for ip in iplist[1:]: | |
print ip | |
coords.append(geocoord(ip)) | |
print coords | |
createkml(iplist[1:]) | |
if __name__=="__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment