-
Notifications
You must be signed in to change notification settings - Fork 43
/
asnlookup.py
100 lines (88 loc) · 4.26 KB
/
asnlookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
import csv, sys, argparse, requests, re, os
from termcolor import colored
from bs4 import BeautifulSoup
def banner():
print('''
____ ____ _ _ _ ____ ____ _ _ _ _ ___
|__| [__ |\ | | | | | | |_/ | | |__]
| | ___] | \| |___ |__| |__| | \_ |__| |
Author: Yassine Aboukir (@yassineaboukir)\n''')
def parse_args():
# parse the arguments
parser = argparse.ArgumentParser(epilog='\tExample: \r\npython ' + sys.argv[0] + " -o twitter")
org = parser.add_argument('-o', '--org', help="Organization to look up", required=False)
return parser.parse_args()
def download_db():
# Download a local copy of ASN database from maxmind.com
if os.path.isfile('./GeoIPASNum2.csv') == False:
print(colored("Downloading ASN database ...\n", "red"))
os.system("wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip && unzip GeoIPASNum2.zip && rm -f GeoIPASNum2.zip")
print(colored("\nDone!\n", "red"))
# Extracting and saving database file size locally
try:
response = requests.get("http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip")
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
with open("filesize.txt", "w") as filesize:
filesize.write(response.headers['Content-Length'])
else:
# Checking if there is a new database change and download a new copy if applicable
response = requests.get("http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip")
with open("filesize.txt", "r") as filesize:
for line in filesize:
if line == response.headers['Content-Length']:
pass
else:
print(colored("[i] It seems like you have not updated the database.","red"))
choice = input(colored("[?] Do you want to update now? [Y]es [N]o, default: [N]", "red"))
if choice.upper() == "Y":
print(colored("Downloading a new copy of the database ...\n","red"))
os.system("wget -O GeoIPASNum2.zip http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip && unzip GeoIPASNum2.zip && rm -f GeoIPASNum2.zip")
try:
response = requests.get("http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip")
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
print("\nDone!\n")
with open("filesize.txt", "w") as filesize:
filesize.write(response.headers['Content-Length'])
else: pass
def extract_asn(organization):
#read csv, and split on "," the line
csv_file = csv.reader(open('GeoIPASNum2.csv', "r"), delimiter=",")
#loop through csv list
for row in csv_file:
#if current rows 2nd value is equal to input, print that row
if organization in row[2] or organization.title() in row[2]:
asn = row[2].split(' ', 1)[0]
return(asn)
def extract_ip(asn, organization):
if asn is not None:
ipinfo = "https://ipinfo.io/"
try:
response = requests.get(ipinfo + asn)
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
ip_addresses = []
for link in soup.find_all('a'):
if asn in link.get('href'):
lookfor = '/'+asn+'/'
ip = re.sub(lookfor, '', link.get('href'))
ip_addresses.append(ip)
print(colored("IP addresses owned by {} are the following:\n".format(organization),"red"))
for i in ip_addresses:
print(colored(i, "yellow"))
print("\n")
else:
print(colored("Sorry! We couldn't find the organization's ASN and IP addresses.", "red"))
if __name__ == '__main__':
requests.packages.urllib3.disable_warnings()
banner()
org = parse_args().org
download_db()
extract_ip(extract_asn(org), org)