/
IpGeoLocationLib.py
268 lines (199 loc) · 8.87 KB
/
IpGeoLocationLib.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
# encoding: UTF-8
"""
IPGeoLocation - Retrieve IP Geolocation information
Powered by http://ip-api.com
Copyright (C) 2015 @maldevel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = 'maldevel'
import json, ipaddress
from urllib import request
from geolocation.IpGeoLocation import IpGeoLocation
import socket
import os.path
import random
from time import sleep
from utilities import MyExceptions
class IpGeoLocationLib:
"""Retrieve IP Geolocation information from http://ip-api.com"""
def __init__(self):
self.URL = 'http://ip-api.com/json/{}'
self.Proxy = request.ProxyHandler({})
self.RandomUA = False
self.UserAgentFile = None
self.UserAgents = None
self.TargetsFile = None
self.Targets = None
self.Verbose = False
self.NoPrint = False
def GetInfo(self, target, userAgent, targetsFile=None, rUserAgent=False, userAgentFile=None, proxy=False, noprint=False, verbose=False):
"""Retrieve information"""
self.UserAgent = userAgent
self.RandomUA = rUserAgent
self.Verbose = verbose
self.NoPrint = noprint
try:
if userAgentFile and os.path.isfile(userAgentFile) and os.access(userAgentFile, os.R_OK):
self.UserAgentFile = userAgentFile
if self.Verbose:
self.__print('Loading User-Agent strings from file..')
self.__loadUserAgents()
if targetsFile and os.path.isfile(targetsFile) and os.access(targetsFile, os.R_OK):
self.TargetsFile = targetsFile
if proxy:
self.Proxy = request.ProxyHandler({'http':proxy.scheme + '://' + proxy.netloc})
opener = request.build_opener(self.Proxy)
request.install_opener(opener)
if self.Verbose:
self.__print('Proxy ({}) has been configured.'.format(proxy.scheme + '://' + proxy.netloc))
if self.TargetsFile:
results = self.__retrieveGeolocations()
return results
else:
if self.Verbose:
self.__print('Retrieving target Geolocation..')
result = self.__retrieveGeolocation(target)
return result
except MyExceptions.UserAgentFileEmptyError:
self.__printError("User-Agent strings file is empty!")
except MyExceptions.InvalidTargetError:
self.__printError('Please provide a valid Domain or IP address.')
except MyExceptions.TargetsFileEmptyError:
self.__printError('Targets file is empty!.')
except MyExceptions.UserAgentFileNotSpecifiedError:
self.__printError('User-Agent strings file has not been provided!.')
except MyExceptions.TargetsFileNotSpecifiedError:
self.__printError('Targets file has not been provided!.')
except:
self.__printError("An unexpected error occurred")
return False
def __retrieveGeolocations (self):
"""Retrieve IP Geolocation for each target in the list"""
IpGeoLocObjs = []
if self.Verbose:
self.__print('Loading targets from file..')
self.__loadTargets()
if self.Verbose:
self.__print('Retrieving targets Geolocations..')
for target in self.Targets:
IpGeoLocObjs.append(self.__retrieveGeolocation(target))
if len(self.Targets)>=150:
sleep(.500) #1/2 sec - ip-api will automatically ban any IP address doing over 150 requests per minute
return IpGeoLocObjs
def __retrieveGeolocation(self, target):
"""Retrieve IP Geolocation for single target"""
if not target:
query = 'My IP'
target=''
elif self.__isValidIPAddress(target):
query = target
else:
ip = self.__hostnameToIP(target)#domain?
if not ip:
raise MyExceptions.InvalidTargetError()
query = target
target = ip
if self.RandomUA and self.UserAgentFile:
self.__pickRandomUserAgent()
if self.Verbose:
self.__print('User-Agent string used: {}.'.format(self.UserAgent))
req = request.Request(self.URL.format(target), data=None, headers={
'User-Agent':self.UserAgent
})
response = request.urlopen(req)
if response.code == 200:
encoding = response.headers.get_content_charset()
if self.Verbose:
self.__print('Geolocation information has been retrieved.')
ipGeoLocObj = IpGeoLocation(query, json.loads(response.read().decode(encoding)))
if not self.NoPrint:
self.__printIPGeoLocation(ipGeoLocObj)
return ipGeoLocObj
return False
def __loadUserAgents(self):
"""Load user-agent strings from file"""
if not self.UserAgentFile:
raise MyExceptions.UserAgentFileNotSpecifiedError()
self.UserAgents = [line.strip() for line in open(self.UserAgentFile, 'r') if line.strip()]
if self.Verbose:
self.__print('User-Agent strings loaded.')
if len(self.UserAgents) == 0:
raise MyExceptions.UserAgentFileEmptyError()
def __loadTargets(self):
"""Load targets from file"""
if not self.TargetsFile:
raise MyExceptions.TargetsFileNotSpecifiedError()
self.Targets = [line.strip() for line in open(self.TargetsFile, 'r') if line.strip()]
if self.Verbose:
self.__print('Targets loaded.')
if len(self.Targets) == 0:
raise MyExceptions.TargetsFileEmptyError()
def __pickRandomUserAgent(self):
"""Pick randomly a user-agent string from list"""
if not self.UserAgents or len(self.UserAgents) == 0:
raise MyExceptions.UserAgentFileEmptyError()
self.UserAgent = random.choice(self.UserAgents)
def __hostnameToIP(self, hostname):
"""Resolve hostname to IP address"""
try:
return socket.gethostbyname(hostname)
except:
return False
def __isValidIPAddress(self, ip):
"""Check if ip is a valid IPv4/IPv6 address"""
try:
ipaddress.ip_address(ip)
return True
except:
return False
def __print(self, message, newLine=False):
if newLine:
print('{}\n'.format(message))
else:
print('{}'.format(message))
def __printError(self, message):
print('Error: {}\n'.format(message))
def __printIPGeoLocation(self, ipGeoLocation):
print("""
Target: {}
IP: {}
ASN: {}
City: {}
Country: {}
Country Code: {}
ISP: {}
Latitude: {}
Longtitude: {}
Organization: {}
Region Code: {}
Region Name: {}
Timezone: {}
Zip Code: {}
Google Maps: {}
""".format(ipGeoLocation.Query,
ipGeoLocation.IP,
ipGeoLocation.ASN,
ipGeoLocation.City,
ipGeoLocation.Country,
ipGeoLocation.CountryCode,
ipGeoLocation.ISP,
ipGeoLocation.Latitude,
ipGeoLocation.Longtitude,
ipGeoLocation.Organization,
ipGeoLocation.Region,
ipGeoLocation.RegionName,
ipGeoLocation.Timezone,
ipGeoLocation.Zip,
ipGeoLocation.GoogleMapsLink
)#.encode('cp737', errors='replace').decode('cp737')
)