forked from lalkaltest/asnlookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasnlookup.py
172 lines (147 loc) · 7.92 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python
import csv
import sys
import argparse
import requests
import re
import os
from termcolor import colored
from bs4 import BeautifulSoup
requests.packages.urllib3.disable_warnings()
def banner():
print('''
____ ____ _ _ _ ____ ____ _ _ _ _ ___
|__| [__ |\ | | | | | | |_/ | | |__]
| | ___] | \| |___ |__| |__| | \_ |__| |
Author: Yassine Aboukir (@yassineaboukir)\n''')
def parse_args():
# parse the argument
parser = argparse.ArgumentParser(epilog='\tExample: \r\npython ' + sys.argv[0] + " -o twitter")
org = parser.add_argument('-o', '--org', help="Organization to look up", required=True)
nmapscan = parser.add_argument('-n', '--nmapscan', help="Run Nmap", required=False, action="store", nargs='?', const="-p 1-65535 -T4 -A -v")
masscan = parser.add_argument('-m', '--masscan', help="Run Masscan", required=False, action="store", nargs='?', const="-p0-65535 --rate 200")
return parser.parse_args()
org = parse_args().org
nmapscan = parse_args().nmapscan
masscan = parse_args().masscan
def download_db():
useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:64.0) Gecko/20100101 Firefox/64.0'
# Download a local copy of ASN database from maxmind.com
if (os.path.isfile('./GeoLite2-ASN-Blocks-IPv4.csv')) == False:
print(colored("[*] Downloading ASN database ...\n", "red"))
os.system("wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip && unzip GeoLite2-ASN-CSV.zip && rm -f GeoLite2-ASN-CSV.zip && mv GeoLite*/* . && rm -f GeoLite2-ASN-Blocks-IPv6.csv && rm -f COPYRIGHT.txt LICENSE.txt && rm -rf GeoLite*/")
print(colored("\nDone!\n", "red"))
# Extracting and saving database file size locally
try:
response = requests.get("https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip", headers={'User-Agent': useragent}, timeout = 10)
except:
print(colored("[*] Timed out while trying to connect to the database server, please run the tool again.", "red"))
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
try:
response = requests.get("https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip", headers={'User-Agent': useragent}, timeout = 10)
except:
print(colored("[*] Timed out while trying to the database server, please run the tool again.", "red"))
sys.exit(1)
with open("filesize.txt", "r") as filesize:
for line in filesize:
if line == response.headers['Content-Length']:
pass
else:
print(colored("[*] It seems like you have not updated the database.","red"))
choice = raw_input(colored("[?] Do you want to update now? [Y]es [N]o, default: [N] ", "red"))
if choice.upper() == "Y":
os.system("rm -rf GeoLite2*")
print(colored("[*] Downloading a new copy of the database ...\n","red"))
os.system("wget -O GeoLite2-ASN-CSV.zip https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip && unzip GeoLite2-ASN-CSV.zip && rm -f GeoLite2-ASN-CSV.zip && mv GeoLite*/* . && rm -f GeoLite2-ASN-Blocks-IPv6.csv && rm -f COPYRIGHT.txt LICENSE.txt && rm -rf GeoLite*/")
try:
response = requests.get("https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN-CSV.zip", headers={'User-Agent': useragent}, timeout = 10)
except:
print(colored("[*] Timed out while trying to the database server, please run the tool again.", "red"))
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
asn_ipv4 = csv.reader(open('GeoLite2-ASN-Blocks-IPv4.csv', "r"), delimiter=",")
#loop through csv list
for row in asn_ipv4:
#if current rows 2nd value is equal to input, print that row
if organization.upper() in row[2].upper():
return(row[1])
def extract_ip(asn, organization):
path_ipv6 = os.path.dirname(os.path.realpath(__file__)) + "/output/" + organization + "_ipv6.txt"
path_ipv4 = os.path.dirname(os.path.realpath(__file__)) + "/output/" + organization + "_ipv4.txt"
if asn:
if not os.path.exists("output"):
os.makedirs("output")
elif os.path.isfile('./output/' + organization + '.txt') == True:
os.system('cd ./output/ && rm -f ' + organization + '.txt')
else:
pass
ipinfo = "https://ipinfo.io/"
try:
response = requests.get(ipinfo + "AS" + asn)
except:
print(colored("[*] Timed out while trying to the ASN lookup server, please run the tool again.", "red"))
sys.exit(1)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
ipv6 = []
ipv4 = []
for link in soup.find_all('a'):
if asn in link.get('href'):
search_criteria = '/' + "AS" + asn + '/'
ip = re.sub(search_criteria, '', link.get('href'))
if "robtex" not in ip:
if ":" in ip:
ipv6.append(ip)
else: ipv4.append(ip)
else: pass
print(colored("[*] IP addresses owned by {} are the following (IPv4 or IPv6):".format(organization),"red"))
if ipv4:
print(colored("\n[*] IPv4 addresses saved to: ", "red"))
print(colored("{}\n".format(path_ipv4), "yellow"))
with open("./output/" + organization + "_ipv4.txt", "w") as dump:
for i in ipv4:
dump.write(i + "\n")
print(colored(i, "yellow"))
if ipv6:
print(colored("\n[*] IPv6 addresses saved to: ", "red"))
print(colored("{}\n".format(path_ipv6), "yellow"))
with open("./output/" + organization + "_ipv6.txt", "w") as dump:
for i in ipv6:
dump.write(i + "\n")
print(colored(i, "yellow"))
else:
print(colored("[*] Sorry! We couldn't find the organization's ASN and IP addresses.", "red"))
def scanning(n, m, organization):
# Only allow one scanner choice
if n and m:
print(colored("\n[*] Please only select one port scanner: -m --> Masscan or -n --> Nmap.", "red"))
# Run Nmap on the IP addresses if -m argument is set
elif n:
if os.path.isfile("./output/" + organization + '_ipv4.txt') == True:
print(colored("\n[*] Running port scanning using Nmap on IPV4 ...", "red"))
os.system("nmap {} -iL {}".format(n, "./output/" + organization + "_ipv4.txt"))
if os.path.isfile("./output/" + organization + '_ipv6.txt') == True:
print(colored("\n[*] Running port scanning using Nmap on IPV6 ...\n", "red"))
os.system("nmap {} -iL {}".format(n, "./output/" + organization + "_ipv6.txt"))
else: pass
# Run Masscan on the IP addresses if -m argument is set
elif m:
if os.path.isfile("./output/" + organization + '_ipv4.txt') == True:
print(colored("\n[*] Running port scanning using Masscan (Warning: supports only IPV4)...", "red"))
os.system("masscan {} -iL {}".format(m, "./output/" + organization + "_ipv4.txt"))
else: pass
if __name__ == '__main__':
banner()
org = parse_args().org
download_db()
extract_ip(extract_asn(org), org)
scanning(nmapscan, masscan, org)