From 62ff6f2b3f1dfae28d322009f82642b2c6675778 Mon Sep 17 00:00:00 2001 From: rscmbbng Date: Tue, 10 Sep 2013 19:43:43 +0200 Subject: [PATCH] added AS database, added long+lat variables, added hostname variable, fixed some stuff --- main.py | 97 ++++++++++++++++++++++++++++++++++++++------- web/.DS_Store | Bin 0 -> 6148 bytes web/__init__.py | 0 web/__init__.pyc | Bin 0 -> 158 bytes web/images/WM1.svg | 0 web/index.py | 22 ++++++++-- xml_exporter.py | 12 +++++- 7 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 web/.DS_Store mode change 100644 => 100755 web/__init__.py create mode 100644 web/__init__.pyc mode change 100644 => 100755 web/images/WM1.svg mode change 100644 => 100755 web/index.py mode change 100644 => 100755 xml_exporter.py diff --git a/main.py b/main.py index 02c6e72..0d894e2 100755 --- a/main.py +++ b/main.py @@ -41,10 +41,12 @@ class bc(object): self.url = "" self.old_url = "" self.ip = "" + self.longitude ="" + self.latitude = "" self.hop_host_name ="" self.city = "" self.country = "" - self.routes = "" + self.server_name = "" def set_options(self, options): """ @@ -219,7 +221,8 @@ class bc(object): Use LFT to traceroute objetives and pass data to webserver ''' # Set database (GeoLiteCity) - self.geoip= pygeoip.GeoIP('GeoLiteCity.dat') + self.geoip = pygeoip.GeoIP('GeoLiteCity.dat') + self.geoasn = pygeoip.GeoIP('GeoIPASNum.dat') print '='*45 + "\n", "Current target:\n" + '='*45 + "\n" print "URL:", self.url[0], "\n" @@ -248,12 +251,12 @@ class bc(object): # using udp try: print "Method: udp\n" - a = subprocess.Popen(['lft', '-S', '-n', '-u', url_ip], stdout=subprocess.PIPE) + a = subprocess.Popen(['lft', '-S', '-n', '-e', url_ip], stdout=subprocess.PIPE) # using tcp except: try: print "Method: tcp\n" - a = subprocess.Popen(['lft', '-S', '-n', '-E', url_ip], stdout=subprocess.PIPE) + a = subprocess.Popen(['lft', '-S', '-n', '-e', url_ip], stdout=subprocess.PIPE) except: print "Error: network is not responding correctly. Aborting...\n" sys.exit(2) @@ -270,31 +273,46 @@ class bc(object): for ip in parts: if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",ip): record = self.geoip.record_by_addr(ip) + try: + asn = self.geoasn.org_by_addr(ip) + except: + asn = 'nothing' + #print record try: self.hop_host_name = socket.gethostbyaddr(ip)[0] except: self.hop_host_name = 'No hostname' + try: + longitude = str(record['longitude']) + self.longitude = longitude + latitude = str(record['latitude']) + self.latitude = latitude + except: + self.longitude = '-' + self.latitude = '-' try: if record.has_key('country_name') and record['city'] is not '': country = record['country_name'] city = record['city'] - print "Trace:", count, "->", ip, "->", city, "->", country, "->", self.hop_host_name + + print "Trace:", count, "->", ip, "->", city, "->", country, "->", self.hop_host_name, asn count+=1 self.city = city self.country = country - self.routes = "Trace:", count, "->", ip, "->", city, "->", country + self.server_name = self.hop_host_name elif record.has_key('country_name'): country = record['country_name'] - print "Trace:", count, "->", ip, "->", country, "->", self.hop_host_name + print "Trace:", count, "->", ip, "->", country, "->", self.hop_host_name, asn self.country = country - self.routes = "Trace:", count, "->", ip, "->", country + self.server_name = self.hop_host_name count+=1 except: print "Trace:", count, "->", "Not allowed" count+=1 - logfile.close() + if self.options.debug == True: + logfile.close() self.old_url = url def getGEO(self): @@ -329,7 +347,28 @@ class bc(object): f_in.close() os.remove('GeoLiteCity.gz') - print "Database: GeoLiteCity\n" + + maxmind_asn = 'http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz' + # Download, extract and set geoipdatabase + if not os.path.exists('GeoIPASNum.dat'): + import urllib, gzip + geo_db_path = '/' + try: + print "Downloading GeoIP ASN database...\n" + if self.options.debug == True: + print "Fetching from:", maxmind_asn + urllib.urlretrieve(maxmind, + 'GeoIPASNum.gz') + except: + print("[Error] - Something wrong fetching GeoIP maps from the Internet. Aborting..."), "\n" + sys.exit(2) + f_in = gzip.open('GeoIPASNum.gz', 'rb') + f_out = open('GeoIPASNum.dat', 'wb') + f_out.write(f_in.read()) + f_in.close() + + os.remove('GeoIPASNum.gz') + print "Database: GeoIPASNum \n" def run(self, opts=None): """ @@ -369,12 +408,26 @@ class bc(object): # run traceroutes match_ip = self.url[0].strip('http://').strip(':8080') if re.match(r'^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$', match_ip) or re.match(r'^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$', match_ip) or re.match(r'^192.168\.\d{1,3}$', match_ip) or re.match(r'^172.(1[6-9]|2[0-9]|3[0-1]).[0-9]{1,3}.[0-9]{1,3}$', match_ip): + self.ip = 'localhost' + self.longitude = '-' + self.latitude = '-' + self.city = '-' + self.country = '-' + self.server_name = '-' pass else: + self.ip = '' #this is overwritten + self.longitude = '-' + self.latitude = '-' + self.city = '-' + self.country = 'waiting' + self.server_name = 'waiting' + xml_results = xml_reporting(self) + xml_results.print_xml_results('data.xml') traces = self.try_running(self.traces, "\nInternal error tracerouting.") - #xml_results = xml_reporting(self) - #xml_results.print_xml_results('data.xml') - # export data to XML + # export data to XML + xml_results = xml_reporting(self) + xml_results.print_xml_results('data.xml') print '='*45 + "\n" print "Status: Waiting for new urls ...\n" @@ -385,11 +438,25 @@ class bc(object): match_ip = url.strip('http://').strip(':8080') if url != self.old_url: if re.match(r'^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$', match_ip) or re.match(r'^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$', match_ip) or re.match(r'^192.168\.\d{1,3}$', match_ip) or re.match(r'^172.(1[6-9]|2[0-9]|3[0-1]).[0-9]{1,3}.[0-9]{1,3}$', match_ip): + self.ip = 'localhost' + self.longitude = '-' + self.latitude = '-' + self.city = '-' + self.country = '-' + self.server_name = '-' pass else: + self.ip = '' #this is overwritten + self.longitude = '-' + self.latitude = '-' + self.city = '-' + self.country = 'waiting' + self.server_name = 'waiting' + xml_results = xml_reporting(self) + xml_results.print_xml_results('data.xml') traces = self.try_running(self.traces, "\nInternal error tracerouting.") - #xml_results = xml_reporting(self) - #xml_results.print_xml_results('data.xml') + xml_results = xml_reporting(self) + xml_results.print_xml_results('data.xml') if __name__ == "__main__": diff --git a/web/.DS_Store b/web/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..297afbd33b38a750e9511c68435501ba7e7c1a6e GIT binary patch literal 6148 zcmeHK%Sr=55Ukc93-OSn$2ocu{DUP#!K;3NnnWZJ6F~*N?Fae+{>@tbP~3G7o!eqf-#*raP8+nQI{ z;OqKd&eq$-e7&8Mq4J#44_h-v!2WO+7QMXJUfv#Oxx+PXaf630SI&FfirfvBWImgw zy*0_3^6ApHEYnhfR3H^d1yX^ZD8MsYtr|OKnF^!=slcZKx<3@UVjb8x+Sb9so&ZF> zMw>C#%d+N_z&fyTWDiX|mFTGwPYm&NwwK`5fsLc5L*nrvG4sdcMQnDCUyKf^9kWaY zQh}}lyI!5>`G3N{%-|!xo07FuAQkv$3dm@7F`M$E;%xo)d3x3s`W0PGd5s1%#+^$5 iE4q&ySf}?FjmfJ68%G^Q+bx}#7XcF_i&Wqj6nF=apg!*a literal 0 HcmV?d00001 diff --git a/web/__init__.py b/web/__init__.py old mode 100644 new mode 100755 diff --git a/web/__init__.pyc b/web/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..09b2739d88e5ebe487dae22bcf797fcf082d63d5 GIT binary patch literal 158 zcmZSn%*(ZBzHU%50~9aPi&OQTON)w9^Gfuc@{3YZixiwQQj@cx90','').replace('','') xmlHost = dom.getElementsByTagName('host')[0].toxml() xmlIP = dom.getElementsByTagName('ip')[0].toxml() -xmlRoutes = dom.getElementsByTagName('routes')[0].toxml() +xmlLongitude = dom.getElementsByTagName('longitude')[0].toxml() +xmlLatitude = dom.getElementsByTagName('latitude')[0].toxml() +xmlCity = dom.getElementsByTagName('city')[0].toxml() +xmlCountry = dom.getElementsByTagName('country')[0].toxml() +xmlServerName = dom.getElementsByTagName('server_name')[0].toxml() xmlMeta = dom.getElementsByTagName('meta')[0].toxml() output = """ @@ -41,8 +45,20 @@ output = """ """+xmlIP+""" - Packages Route: - """+xmlRoutes+""" + Coordinates: + """+xmlLongitude+""" : """+xmlLatitude+""" + + + Server name: + """+xmlServerName+""" + + + Country: + """+xmlCountry+""" + + + City: + """+xmlCity+""" Metadata: diff --git a/xml_exporter.py b/xml_exporter.py old mode 100644 new mode 100755 index 90da0c6..33b50c7 --- a/xml_exporter.py +++ b/xml_exporter.py @@ -18,12 +18,20 @@ class xml_reporting(object): root = ET.Element("travel") host = ET.SubElement(root, "host") ip = ET.SubElement(root, "ip") - routes = ET.SubElement(root, "routes") + longitude = ET.SubElement(root, "longitude") + latitude = ET.SubElement(root, "latitude") + city = ET.SubElement(root, "city") + country = ET.SubElement(root, "country") + server_name = ET.SubElement(root, "server_name") meta = ET.SubElement(root, "meta") host.text = self.instance.url[0] ip.text = self.instance.ip - routes.text = self.instance.routes + longitude.text = self.instance.longitude + latitude.text = self.instance.latitude + city.text = self.instance.city + country.text = self.instance.country + server_name.text = self.instance.server_name meta.text = "Connect here XML metadata" tree = ET.ElementTree(root)