Browse Source

now prints correct xml data for each hop in trace + various minor improvements

pull/3/head
rscmbbng 11 years ago
parent
commit
33231c3761
  1. 49
      main.py
  2. 2
      web/index.py
  3. 22
      xml_exporter.py

49
main.py

@ -35,12 +35,13 @@ class bc(object):
Init defaults Init defaults
""" """
self.browser = "" # "F" Firefox / "C" Chrome self.browser = "" # "F" Firefox / "C" Chrome
self.browser_path = "" self.browser_path = "" #the path to the browser application
self.browser_history_path = "" self.browser_history_path = "" # the path to the browser history file
self.browser_version = "" self.browser_version = "" # the version of the browser
self.url = "" self.url = ""
self.old_url = "" self.old_url = ""
self.ip = "" self.destination_ip = ""
self.hop_ip = ""
self.longitude = "" self.longitude = ""
self.latitude = "" self.latitude = ""
self.hop_host_name = "" self.hop_host_name = ""
@ -48,6 +49,9 @@ class bc(object):
self.country = "" self.country = ""
self.server_name = "" self.server_name = ""
self.hop_count = 1 # number of hops 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 if os.path.exists('data.xml'): # removing xml data to has a new map each time that bc is launched
os.remove('data.xml') os.remove('data.xml')
@ -183,7 +187,6 @@ class bc(object):
print "Version:", self.browser_version, "\n" print "Version:", self.browser_version, "\n"
print "History:", self.browser_history_path, "\n" print "History:", self.browser_history_path, "\n"
#move the subprocesses to debug mode
def getURL(self): def getURL(self):
""" """
@ -236,7 +239,6 @@ class bc(object):
# Set database (GeoLiteCity) # Set database (GeoLiteCity)
self.geoip = pygeoip.GeoIP('GeoLiteCity.dat') self.geoip = pygeoip.GeoIP('GeoLiteCity.dat')
self.geoasn = pygeoip.GeoIP('GeoIPASNum.dat') self.geoasn = pygeoip.GeoIP('GeoIPASNum.dat')
self.hop_count = 1
print '='*45 + "\n", "Current target:\n" + '='*45 + "\n" print '='*45 + "\n", "Current target:\n" + '='*45 + "\n"
print "URL:", self.url[0], "\n" print "URL:", self.url[0], "\n"
@ -244,10 +246,10 @@ class bc(object):
url = urlparse(self.getURL()).netloc #changed this for prototyping 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 = url.replace('www.','') #--> doing a tracert to example.com and www.example.com yields different results.
url_ip = socket.gethostbyname(url) url_ip = socket.gethostbyname(url)
self.ip = url_ip self.destination_ip = url_ip
print "Host:", url, "\n" print "Host:", url, "\n"
if url != self.old_url: if url != self.old_url:
count = 1 self.hop_count = 1
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
# using udp # using udp
try: try:
@ -265,7 +267,7 @@ class bc(object):
# using udp # using udp
try: try:
print "Method: udp\n" 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 # using tcp
except: except:
try: try:
@ -282,18 +284,19 @@ class bc(object):
logfile.write(item) logfile.write(item)
print '='*45 + "\n" + "Packages Route:\n" + '='*45 print '='*45 + "\n" + "Packages Route:\n" + '='*45
for line in a.stdout: for line in a.stdout:
self.hop_count = self.hop_count + 1
if self.options.debug == True: if self.options.debug == True:
logfile.write(line) logfile.write(line)
parts = line.split() parts = line.split()
for ip in parts: for ip in parts:
if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",ip): 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) record = self.geoip.record_by_addr(ip)
try: try:
asn = self.geoasn.org_by_addr(ip) self.asn = self.geoasn.org_by_addr(ip)
except: except:
asn = 'nothing' self.asn = 'No ASN provided'
#print record #print record
try: try:
self.hop_host_name = socket.gethostbyaddr(ip)[0] 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 '': if record.has_key('country_name') and record['city'] is not '':
country = record['country_name'] country = record['country_name']
city = record['city'] city = record['city']
print "Trace:", count, "->", ip, "->", longitude + ":" + latitude, "->", city, "->", country, "->", self.hop_host_name, asn print "Trace:", self.hop_count, "->", ip, "->", longitude + ":" + latitude, "->", city, "->", country, "->", self.hop_host_name, self.asn
count+=1 #self.hop_count +=1
self.city = city self.city = city
self.country = country self.country = country
self.server_name = self.hop_host_name self.server_name = self.hop_host_name
elif record.has_key('country_name'): elif record.has_key('country_name'):
country = record['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.country = country
self.city = '-'
self.server_name = self.hop_host_name 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: except:
print "Trace:", count, "->", "Not allowed" print "Trace:", self.hop_count, "->", "Not allowed"
count+=1 self.hop_count+=1
# write xml data to file # write xml data to file
self.result_list.append(self.vardict)
xml_results = xml_reporting(self) xml_results = xml_reporting(self)
xml_results.print_xml_results('data.xml') xml_results.print_xml_results('data.xml')
@ -349,7 +356,7 @@ class bc(object):
try: try:
print "Downloading GeoIP database...\n" print "Downloading GeoIP database...\n"
if self.options.debug == True: if self.options.debug == True:
print "Fetching from:", maxmind print "Fetching from:", maxmind, '\n'
urllib.urlretrieve(maxmind, urllib.urlretrieve(maxmind,
'GeoLiteCity.gz') 'GeoLiteCity.gz')
except: except:
@ -376,8 +383,8 @@ class bc(object):
try: try:
print "Downloading GeoIP ASN database...\n" print "Downloading GeoIP ASN database...\n"
if self.options.debug == True: if self.options.debug == True:
print "Fetching from:", maxmind_asn print "Fetching from:", maxmind_asn,'\n'
urllib.urlretrieve(maxmind, urllib.urlretrieve(maxmind_asn,
'GeoIPASNum.gz') 'GeoIPASNum.gz')
except: except:
print("[Error] - Something wrong fetching GeoIP maps from the Internet. Aborting..."), "\n" print("[Error] - Something wrong fetching GeoIP maps from the Internet. Aborting..."), "\n"

2
web/index.py

@ -14,7 +14,7 @@ dom = parseString(data)
xmlTag = dom.getElementsByTagName('travel')[0].toxml() xmlTag = dom.getElementsByTagName('travel')[0].toxml()
xmlData= xmlTag.replace('<travel>','').replace('</travel>','') xmlData= xmlTag.replace('<travel>','').replace('</travel>','')
xmlHost = dom.getElementsByTagName('host')[0].toxml() 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() xmlLongitude = dom.getElementsByTagName('longitude')[0].toxml()
xmlLatitude = dom.getElementsByTagName('latitude')[0].toxml() xmlLatitude = dom.getElementsByTagName('latitude')[0].toxml()
xmlCity = dom.getElementsByTagName('city')[0].toxml() xmlCity = dom.getElementsByTagName('city')[0].toxml()

22
xml_exporter.py

@ -17,25 +17,27 @@ class xml_reporting(object):
def print_xml_results(self, filename): def print_xml_results(self, filename):
root = ET.Element("travel") root = ET.Element("travel")
i = 1 i = 1
for i in range(self.instance.hop_count): for i in self.instance.result_list:
hop = ET.SubElement(root, "hop") hop = ET.SubElement(root, "hop")
host = ET.SubElement(hop, "host") host = ET.SubElement(hop, "host")
ip = ET.SubElement(hop, "ip") hop_ip = ET.SubElement(hop, "hop_ip")
longitude = ET.SubElement(hop, "longitude") longitude = ET.SubElement(hop, "longitude")
latitude = ET.SubElement(hop, "latitude") latitude = ET.SubElement(hop, "latitude")
city = ET.SubElement(hop, "city") city = ET.SubElement(hop, "city")
country = ET.SubElement(hop, "country") country = ET.SubElement(hop, "country")
server_name = ET.SubElement(hop, "server_name") server_name = ET.SubElement(hop, "server_name")
asn = ET.SubElement(hop, "asn")
meta = ET.SubElement(hop, "meta") meta = ET.SubElement(hop, "meta")
hop.text = str(i) hop.text = str(i['hop_count'])
host.text = self.instance.url[0] host.text = i['destination_ip']
ip.text = self.instance.ip hop_ip.text = i['hop_ip']
longitude.text = self.instance.longitude longitude.text = i['longitude']
latitude.text = self.instance.latitude latitude.text = i['latitude']
city.text = self.instance.city city.text = i['city']
country.text = self.instance.country country.text = i['country']
server_name.text = self.instance.server_name server_name.text = i['server_name']
asn.text = i['asn']
meta.text = "Connect here XML metadata" meta.text = "Connect here XML metadata"
tree = ET.ElementTree(root) tree = ET.ElementTree(root)

Loading…
Cancel
Save