added AS database, added long+lat variables, added hostname variable, fixed some stuff

This commit is contained in:
rscmbbng 2013-09-10 19:43:43 +02:00
parent f2aa3bfc30
commit 62ff6f2b3f
7 changed files with 111 additions and 20 deletions

97
main.py
View File

@ -41,10 +41,12 @@ class bc(object):
self.url = "" self.url = ""
self.old_url = "" self.old_url = ""
self.ip = "" self.ip = ""
self.longitude =""
self.latitude = ""
self.hop_host_name ="" self.hop_host_name =""
self.city = "" self.city = ""
self.country = "" self.country = ""
self.routes = "" self.server_name = ""
def set_options(self, options): def set_options(self, options):
""" """
@ -219,7 +221,8 @@ class bc(object):
Use LFT to traceroute objetives and pass data to webserver Use LFT to traceroute objetives and pass data to webserver
''' '''
# Set database (GeoLiteCity) # 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 '='*45 + "\n", "Current target:\n" + '='*45 + "\n"
print "URL:", self.url[0], "\n" print "URL:", self.url[0], "\n"
@ -248,12 +251,12 @@ class bc(object):
# using udp # using udp
try: try:
print "Method: udp\n" 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 # using tcp
except: except:
try: try:
print "Method: tcp\n" 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: except:
print "Error: network is not responding correctly. Aborting...\n" print "Error: network is not responding correctly. Aborting...\n"
sys.exit(2) sys.exit(2)
@ -270,31 +273,46 @@ class bc(object):
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):
record = self.geoip.record_by_addr(ip) record = self.geoip.record_by_addr(ip)
try:
asn = self.geoasn.org_by_addr(ip)
except:
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:
longitude = str(record['longitude'])
self.longitude = longitude
latitude = str(record['latitude'])
self.latitude = latitude
except:
self.longitude = '-'
self.latitude = '-'
try: try:
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, "->", city, "->", country, "->", self.hop_host_name
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.routes = "Trace:", count, "->", ip, "->", city, "->", country 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 print "Trace:", count, "->", ip, "->", country, "->", self.hop_host_name, asn
self.country = country self.country = country
self.routes = "Trace:", count, "->", ip, "->", country 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
logfile.close() if self.options.debug == True:
logfile.close()
self.old_url = url self.old_url = url
def getGEO(self): def getGEO(self):
@ -329,7 +347,28 @@ class bc(object):
f_in.close() f_in.close()
os.remove('GeoLiteCity.gz') 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): def run(self, opts=None):
""" """
@ -369,12 +408,26 @@ class bc(object):
# run traceroutes # run traceroutes
match_ip = self.url[0].strip('http://').strip(':8080') 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): 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) # export data to XML
#xml_results.print_xml_results('data.xml') xml_results = xml_reporting(self)
# export data to XML 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"
@ -385,11 +438,25 @@ 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 = xml_reporting(self)
#xml_results.print_xml_results('data.xml') xml_results.print_xml_results('data.xml')
if __name__ == "__main__": if __name__ == "__main__":

BIN
web/.DS_Store vendored Normal file

Binary file not shown.

0
web/__init__.py Normal file → Executable file
View File

BIN
web/__init__.pyc Normal file

Binary file not shown.

0
web/images/WM1.svg Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

22
web/index.py Normal file → Executable file
View File

@ -14,7 +14,11 @@ 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('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() xmlMeta = dom.getElementsByTagName('meta')[0].toxml()
output = """ output = """
@ -41,8 +45,20 @@ output = """
<td>"""+xmlIP+"""</td> <td>"""+xmlIP+"""</td>
</tr> </tr>
<tr> <tr>
<td><b>Packages Route:</b></td> <td><b>Coordinates:</b></td>
<td>"""+xmlRoutes+"""</td> <td>"""+xmlLongitude+""" : """+xmlLatitude+"""</td>
</tr>
<tr>
<td><b>Server name:</b></td>
<td>"""+xmlServerName+"""</td>
</tr>
<tr>
<td><b>Country:</b></td>
<td>"""+xmlCountry+"""</td>
</tr>
<tr>
<td><b>City:</b></td>
<td>"""+xmlCity+"""</td>
</tr> </tr>
<tr> <tr>
<td><b>Metadata:</b></td> <td><b>Metadata:</b></td>

12
xml_exporter.py Normal file → Executable file
View File

@ -18,12 +18,20 @@ class xml_reporting(object):
root = ET.Element("travel") root = ET.Element("travel")
host = ET.SubElement(root, "host") host = ET.SubElement(root, "host")
ip = ET.SubElement(root, "ip") 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") meta = ET.SubElement(root, "meta")
host.text = self.instance.url[0] host.text = self.instance.url[0]
ip.text = self.instance.ip 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" meta.text = "Connect here XML metadata"
tree = ET.ElementTree(root) tree = ET.ElementTree(root)