diff --git a/main.py b/main.py index 5e169ae..50e2287 100755 --- a/main.py +++ b/main.py @@ -35,12 +35,13 @@ class bc(object): Init defaults """ self.browser = "" # "F" Firefox / "C" Chrome - self.browser_path = "" - self.browser_history_path = "" - self.browser_version = "" + self.browser_path = "" #the path to the browser application + self.browser_history_path = "" # the path to the browser history file + self.browser_version = "" # the version of the browser self.url = "" self.old_url = "" - self.ip = "" + self.destination_ip = "" + self.hop_ip = "" self.longitude = "" self.latitude = "" self.hop_host_name = "" @@ -48,6 +49,9 @@ class bc(object): self.country = "" self.server_name = "" self.hop_count = 1 # number of hops + self.result_list = [] + self.vardict ={} + self.asn = '' if os.path.exists('data.xml'): # removing xml data to has a new map each time that bc is launched os.remove('data.xml') @@ -183,7 +187,6 @@ class bc(object): print "Version:", self.browser_version, "\n" print "History:", self.browser_history_path, "\n" - #move the subprocesses to debug mode def getURL(self): """ @@ -236,7 +239,6 @@ class bc(object): # Set database (GeoLiteCity) self.geoip = pygeoip.GeoIP('GeoLiteCity.dat') self.geoasn = pygeoip.GeoIP('GeoIPASNum.dat') - self.hop_count = 1 print '='*45 + "\n", "Current target:\n" + '='*45 + "\n" print "URL:", self.url[0], "\n" @@ -244,10 +246,10 @@ class bc(object): url = urlparse(self.getURL()).netloc #changed this for prototyping url = url.replace('www.','') #--> doing a tracert to example.com and www.example.com yields different results. url_ip = socket.gethostbyname(url) - self.ip = url_ip + self.destination_ip = url_ip print "Host:", url, "\n" if url != self.old_url: - count = 1 + self.hop_count = 1 if sys.platform.startswith('linux'): # using udp try: @@ -265,7 +267,7 @@ class bc(object): # using udp try: print "Method: udp\n" - a = subprocess.Popen(['lft', '-S', '-n', '-e', url_ip], stdout=subprocess.PIPE) + a = subprocess.Popen(['lft', '-S', '-n', '-u', url_ip], stdout=subprocess.PIPE) # using tcp except: try: @@ -282,18 +284,19 @@ class bc(object): logfile.write(item) print '='*45 + "\n" + "Packages Route:\n" + '='*45 for line in a.stdout: - self.hop_count = self.hop_count + 1 if self.options.debug == True: logfile.write(line) parts = line.split() + for ip in parts: if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",ip): + self.hop_ip = ip record = self.geoip.record_by_addr(ip) try: - asn = self.geoasn.org_by_addr(ip) + self.asn = self.geoasn.org_by_addr(ip) except: - asn = 'nothing' + self.asn = 'No ASN provided' #print record try: self.hop_host_name = socket.gethostbyaddr(ip)[0] @@ -311,21 +314,25 @@ class bc(object): if record.has_key('country_name') and record['city'] is not '': country = record['country_name'] city = record['city'] - print "Trace:", count, "->", ip, "->", longitude + ":" + latitude, "->", city, "->", country, "->", self.hop_host_name, asn - count+=1 + print "Trace:", self.hop_count, "->", ip, "->", longitude + ":" + latitude, "->", city, "->", country, "->", self.hop_host_name, self.asn + #self.hop_count +=1 self.city = city self.country = country self.server_name = self.hop_host_name elif record.has_key('country_name'): country = record['country_name'] - print "Trace:", count, "->", ip, "->", longitude + ":" + latitude, "->", country, "->", self.hop_host_name, asn + print "Trace:", self.hop_count, "->", ip, "->", longitude + ":" + latitude, "->", country, "->", self.hop_host_name, self.asn self.country = country + self.city = '-' self.server_name = self.hop_host_name - count+=1 + #self.hop_count+=1 + self.vardict = {'destination_ip': self.destination_ip, 'hop_count': self.hop_count,'hop_ip': self.hop_ip, 'server_name': self.server_name, 'country': self.country, 'city': self.city, 'longitude': self.longitude, 'latitude': self.latitude, 'asn' : self.asn} except: - print "Trace:", count, "->", "Not allowed" - count+=1 + print "Trace:", self.hop_count, "->", "Not allowed" + self.hop_count+=1 + # write xml data to file + self.result_list.append(self.vardict) xml_results = xml_reporting(self) xml_results.print_xml_results('data.xml') @@ -349,7 +356,7 @@ class bc(object): try: print "Downloading GeoIP database...\n" if self.options.debug == True: - print "Fetching from:", maxmind + print "Fetching from:", maxmind, '\n' urllib.urlretrieve(maxmind, 'GeoLiteCity.gz') except: @@ -376,8 +383,8 @@ class bc(object): try: print "Downloading GeoIP ASN database...\n" if self.options.debug == True: - print "Fetching from:", maxmind_asn - urllib.urlretrieve(maxmind, + print "Fetching from:", maxmind_asn,'\n' + urllib.urlretrieve(maxmind_asn, 'GeoIPASNum.gz') except: print("[Error] - Something wrong fetching GeoIP maps from the Internet. Aborting..."), "\n" diff --git a/web/index.py b/web/index.py index 03e7ff9..6f8ac88 100755 --- a/web/index.py +++ b/web/index.py @@ -14,7 +14,7 @@ dom = parseString(data) xmlTag = dom.getElementsByTagName('travel')[0].toxml() xmlData= xmlTag.replace('','').replace('','') xmlHost = dom.getElementsByTagName('host')[0].toxml() -xmlIP = dom.getElementsByTagName('ip')[0].toxml() +xmlIP = dom.getElementsByTagName('hop_ip')[0].toxml() xmlLongitude = dom.getElementsByTagName('longitude')[0].toxml() xmlLatitude = dom.getElementsByTagName('latitude')[0].toxml() xmlCity = dom.getElementsByTagName('city')[0].toxml() diff --git a/xml_exporter.py b/xml_exporter.py index 4837afa..3dc7f60 100755 --- a/xml_exporter.py +++ b/xml_exporter.py @@ -17,25 +17,27 @@ class xml_reporting(object): def print_xml_results(self, filename): root = ET.Element("travel") i = 1 - for i in range(self.instance.hop_count): + for i in self.instance.result_list: hop = ET.SubElement(root, "hop") host = ET.SubElement(hop, "host") - ip = ET.SubElement(hop, "ip") + hop_ip = ET.SubElement(hop, "hop_ip") longitude = ET.SubElement(hop, "longitude") latitude = ET.SubElement(hop, "latitude") city = ET.SubElement(hop, "city") country = ET.SubElement(hop, "country") server_name = ET.SubElement(hop, "server_name") + asn = ET.SubElement(hop, "asn") meta = ET.SubElement(hop, "meta") - hop.text = str(i) - host.text = self.instance.url[0] - ip.text = self.instance.ip - 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 + hop.text = str(i['hop_count']) + host.text = i['destination_ip'] + hop_ip.text = i['hop_ip'] + longitude.text = i['longitude'] + latitude.text = i['latitude'] + city.text = i['city'] + country.text = i['country'] + server_name.text = i['server_name'] + asn.text = i['asn'] meta.text = "Connect here XML metadata" tree = ET.ElementTree(root)