-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.py
84 lines (67 loc) · 2.35 KB
/
geo.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
import ipaddress
import geoip2.database
citydb=geoip2.database.Reader('/usr/share/GeoIP/GeoLite2-City.mmdb')
asndb=geoip2.database.Reader('/usr/share/GeoIP/GeoLite2-ASN.mmdb')
customNetworkData={}
import json
import IP2Location, os, IP2Proxy
try:
ip2p = IP2Proxy.IP2Proxy(os.path.join("data", "IP2PROXY-LITE-PX9.BIN"))
except FileNotFoundError as exc:
raise FileNotFoundError("Missing data/IP2PROXY-LITE-PX9.BIN. See: https://www.ip2location.com/development-libraries/ip2location/python")
try:
ip2l = IP2Location.IP2Location(os.path.join("data", "ip2location.bin"))
except FileNotFoundError as exc:
raise FileNotFoundError("Missing data/ip2location.bin. See: https://www.ip2location.com/development-libraries/ip2location/python")
from data.amz import regionInfo as amzRegionInfo
with open('data/amazon_ip-ranges.json') as f:
data = json.load(f)
for prefix in data["prefixes"]:
region=prefix["region"]
rinfo=amzRegionInfo[region]
customNetworkData[ipaddress.ip_network(prefix["ip_prefix"])]=tuple([region]+list(rinfo))
def country(ip):
ip=ipaddress.ip_address(ip)
for r,data in customNetworkData.items():
if ip in r:
return (data[1],data[2],data[3])
try:
response = citydb.city(ip)
except: # Invalid IP verification?
return (False,False,False,)
return (response.country.iso_code,response.location.latitude,response.location.longitude)
import config
CGN=ipaddress.ip_network("100.64.0.0/10")
p1=ipaddress.ip_network("10.0.0.0/8")
p2=ipaddress.ip_network("192.168.0.0/16")
def asn(ip):
ip=ipaddress.ip_address(ip)
if ip in config.homenetwork:
return (-1,"HOME")
if ip in p1:
return (-2,"Private 10.x.x.x")
if ip in p2:
return (-2,"Private 192.168.x.x")
if ip.is_private:
return (-2,"Local Network")
if ip.is_multicast:
return (-3,"Multicast")
if ip.is_link_local:
return (-4,"LinkLocal")
if ip in CGN:
return (-5,"Carrier Grade NAT")
try:
response = asndb.asn(ip)
for r,data in customNetworkData.items():
if ip in r:
return (response.autonomous_system_number,"Amazon "+data[0],)
except:
return (0,"ASN-UNKNOWN: "+str(ip),)
return (response.autonomous_system_number,response.autonomous_system_organization,)
if __name__ == "__main__":
c=country("130.232.246.90")
a=asn("130.232.246.90")
amaze=country("35.180.0.0")
ip2 = ip2p.get_all("35.180.0.0")
ip3 = ip2l.get_all("35.180.0.0")
import code; code.interact(local=locals())