advanced hops on xml data extraction (task not completed)

This commit is contained in:
psy 2013-09-12 11:59:44 -07:00
parent ef1d3346ee
commit f9cb99647c
3 changed files with 48 additions and 67 deletions

73
main.py
View File

@ -41,12 +41,17 @@ class bc(object):
self.url = "" self.url = ""
self.old_url = "" self.old_url = ""
self.ip = "" self.ip = ""
self.longitude ="" self.longitude = ""
self.latitude = "" self.latitude = ""
self.hop_host_name ="" self.hop_host_name = ""
self.city = "" self.city = ""
self.country = "" self.country = ""
self.server_name = "" self.server_name = ""
self.hop_count = 1 # number of hops
if os.path.exists('data.xml'): # removing xml data to has a new map each time that bc is launched
os.remove('data.xml')
open('data.xml', 'w') # starting a new xml data container in write mode
def set_options(self, options): def set_options(self, options):
""" """
@ -231,6 +236,7 @@ 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"
@ -276,6 +282,8 @@ 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()
@ -286,10 +294,9 @@ class bc(object):
asn = self.geoasn.org_by_addr(ip) asn = self.geoasn.org_by_addr(ip)
except: except:
asn = 'nothing' asn = 'nothing'
#print record #print record
try: try:
self.hop_host_name = socket.gethostbyaddr(ip)[0] self.hop_host_name = socket.gethostbyaddr(ip)[0]
except: except:
self.hop_host_name = 'No hostname' self.hop_host_name = 'No hostname'
try: try:
@ -304,25 +311,29 @@ 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:", count, "->", ip, "->", city, "->", country, "->", self.hop_host_name, asn
count+=1 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, "->", country, "->", self.hop_host_name, asn print "Trace:", count, "->", ip, "->", longitude + ":" + latitude, "->", country, "->", self.hop_host_name, asn
self.country = country self.country = country
self.server_name = self.hop_host_name self.server_name = self.hop_host_name
count+=1 count+=1
except: except:
print "Trace:", count, "->", "Not allowed" print "Trace:", count, "->", "Not allowed"
count+=1 count+=1
# write xml data to file
xml_results = xml_reporting(self)
xml_results.print_xml_results('data.xml')
if self.options.debug == True: if self.options.debug == True:
logfile.close() logfile.close()
self.old_url = url self.old_url = url
self.hop_count = 0 # to start a new map
print "\n"
def getGEO(self): def getGEO(self):
""" """
@ -401,6 +412,12 @@ class bc(object):
url = self.try_running(self.getURL, "\nInternal error getting urls from browser's database.") url = self.try_running(self.getURL, "\nInternal error getting urls from browser's database.")
# set geoip database # set geoip database
geo = self.try_running(self.getGEO, "\nInternal error setting geoIP database.") geo = self.try_running(self.getGEO, "\nInternal error setting geoIP database.")
# 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):
pass
else:
traces = self.try_running(self.traces, "\nInternal error tracerouting.")
# start web mode (on a different thread) # start web mode (on a different thread)
try: try:
t = threading.Thread(target=BorderCheckWebserver, args=(self, )) t = threading.Thread(target=BorderCheckWebserver, args=(self, ))
@ -414,29 +431,6 @@ class bc(object):
webbrowser.open('http://127.0.0.1:8080', new=1) webbrowser.open('http://127.0.0.1:8080', new=1)
except: except:
print "Error: Browser is not responding correctly.\n" print "Error: Browser is not responding correctly.\n"
# 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.")
# export data to XML
xml_results = xml_reporting(self)
xml_results.print_xml_results('data.xml')
print '='*45 + "\n" print '='*45 + "\n"
print "Status: Waiting for new urls ...\n" print "Status: Waiting for new urls ...\n"
@ -447,26 +441,9 @@ class bc(object):
match_ip = url.strip('http://').strip(':8080') match_ip = url.strip('http://').strip(':8080')
if url != self.old_url: 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): 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 pass
else: 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.") traces = self.try_running(self.traces, "\nInternal error tracerouting.")
xml_results = xml_reporting(self)
xml_results.print_xml_results('data.xml')
if __name__ == "__main__": if __name__ == "__main__":
app = bc() app = bc()

View File

@ -48,7 +48,7 @@ output = """
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
<script src="js/leaflet/leaflet.js"></script> <script src="js/leaflet/leaflet.js"></script>
<!--<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>--> <!--<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>-->
<meta http-equiv="refresh" content="5"> <meta http-equiv="refresh" content="3">
</head> </head>
<body> <body>
<center> <center>

View File

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