-
Notifications
You must be signed in to change notification settings - Fork 20
/
whois.py
executable file
·366 lines (287 loc) · 11.1 KB
/
whois.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
IPv4 Whois data collection and analysis tool
Usage:
./whois.py collect <elastic_search_url> <index_name> <doc_name>
[--sleep_min=<n>] [--sleep_max=<n>] [--threads=<n>]
./whois.py stats <elastic_search_url> <index_name>
./whois.py test
./whois.py (-h | --help)
Options:
-h, --help Show this screen and exit.
--sleep_min=<n> Least number of seconds to sleep for [Default: 1]
--sleep_max=<n> Most number of seconds to sleep for [Default: 5]
--threads=<n> Number of threads [Default: 8]
Examples:
./whois.py collect http://127.0.0.1:9200/ netblocks netblock
./whois.py stats http://127.0.0.1:9200/ netblocks
License:
The MIT License (MIT)
Copyright (c) 2014 Mark Litwintschik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import json
from random import randint
import socket
import struct
import sys
from docopt import docopt
import ipcalc
from ipwhois import IPWhois
import gevent
from pyelasticsearch import ElasticSearch
from pyelasticsearch.exceptions import \
ElasticHttpError, ElasticHttpNotFoundError
import requests
def ip2long(ip):
"""
Convert IPv4 address in string format into an integer
:param str ip: ipv4 address
:return: ipv4 address
:rtype: integer
"""
packed_ip = socket.inet_aton(ip)
return struct.unpack("!L", packed_ip)[0]
def get_next_ip(ip_address):
"""
:param str ip_address: ipv4 address
:return: next ipv4 address
:rtype: str
>>> get_next_ip('0.0.0.0')
'0.0.0.1'
>>> get_next_ip('24.24.24.24')
'24.24.24.25'
>>> get_next_ip('24.24.255.255')
'24.25.0.0'
>>> get_next_ip('255.255.255.255') is None
True
"""
assert ip_address.count('.') == 3, \
'Must be an IPv4 address in str representation'
if ip_address == '255.255.255.255':
return None
try:
return socket.inet_ntoa(struct.pack('!L', ip2long(ip_address) + 1))
except Exception, error:
print 'Unable to get next IP for %s' % ip_address
raise error
def get_netrange_end(asn_cidr):
"""
:param str asn_cidr: ASN CIDR
:return: ipv4 address of last IP in netrange
:rtype: str
"""
try:
last_in_netrange = \
ip2long(str(ipcalc.Network(asn_cidr).host_first())) + \
ipcalc.Network(asn_cidr).size() - 2
except ValueError, error:
print 'Issue calculating size of %s network' % asn_cidr
raise error
return socket.inet_ntoa(struct.pack('!L', last_in_netrange))
def get_next_undefined_address(ip):
"""
Get the next non-private IPv4 address if the address sent is private
:param str ip: IPv4 address
:return: ipv4 address of net non-private address
:rtype: str
>>> get_next_undefined_address('0.0.0.0')
'1.0.0.0'
>>> get_next_undefined_address('24.24.24.24')
'24.24.24.24'
>>> get_next_undefined_address('127.0.0.1')
'128.0.0.0'
>>> get_next_undefined_address('255.255.255.256') is None
True
"""
try:
# Should weed out many invalid IP addresses
ipcalc.Network(ip)
except ValueError, error:
return None
defined_networks = (
'0.0.0.0/8',
'10.0.0.0/8',
'127.0.0.0/8',
'169.254.0.0/16',
'192.0.0.0/24',
'192.0.2.0/24',
'192.88.99.0/24',
'192.168.0.0/16',
'198.18.0.0/15',
'198.51.100.0/24',
'203.0.113.0/24',
'224.0.0.0/4',
'240.0.0.0/4',
'255.255.255.255/32',
)
for network_cidr in defined_networks:
if ip in ipcalc.Network(network_cidr):
return get_next_ip(get_netrange_end(network_cidr))
return ip
def break_up_ipv4_address_space(num_threads=8):
"""
>>> break_up_ipv4_address_space() == \
[('0.0.0.0', '31.255.255.255'), ('32.0.0.0', '63.255.255.255'),\
('64.0.0.0', '95.255.255.255'), ('96.0.0.0', '127.255.255.255'),\
('128.0.0.0', '159.255.255.255'), ('160.0.0.0', '191.255.255.255'),\
('192.0.0.0', '223.255.255.255'), ('224.0.0.0', '255.255.255.255')]
True
"""
ranges = []
multiplier = 256 / num_threads
for marker in range(0, num_threads):
starting_class_a = (marker * multiplier)
ending_class_a = ((marker + 1) * multiplier) - 1
ranges.append(('%d.0.0.0' % starting_class_a,
'%d.255.255.255' % ending_class_a))
return ranges
def get_netranges(starting_ip='1.0.0.0',
last_ip='2.0.0.0',
elastic_search_url='http://127.0.0.1:9200/',
index_name='netblocks',
doc_name='netblock', sleep_min=1, sleep_max=5):
connection = ElasticSearch(elastic_search_url)
current_ip = starting_ip
while True:
# See if we've finished the range of work
if ip2long(current_ip) > ip2long(last_ip):
return
current_ip = get_next_undefined_address(current_ip)
if current_ip == None: # No more undefined ip addresses
return
print current_ip
try:
whois_resp = IPWhois(current_ip).lookup_rws()
except Exception as error:
"""
If a message like: 'STDERR: getaddrinfo(whois.apnic.net): Name or
service not known' appears' then print it out and try the next
IP address.
"""
print type(error), error
current_ip = get_next_ip(current_ip)
if current_ip is None:
return # No more undefined ip addresses
gevent.sleep(randint(sleep_min, sleep_max))
continue
if 'asn_cidr' in whois_resp and \
whois_resp['asn_cidr'] is not None and \
whois_resp['asn_cidr'].count('.') == 3:
last_netrange_ip = get_netrange_end(whois_resp['asn_cidr'])
else:
try:
last_netrange_ip = \
whois_resp['nets'][0]['range'].split('-')[-1].strip()
assert last_netrange_ip.count('.') == 3
except:
# No match found for n + 192.0.1.0.
print 'Missing ASN CIDR in whois resp: %s' % whois_resp
current_ip = get_next_ip(current_ip)
if current_ip is None:
return # No more undefined ip addresses
gevent.sleep(randint(sleep_min, sleep_max))
continue
assert last_netrange_ip is not None and \
last_netrange_ip.count('.') == 3, \
'Unable to find last netrange ip for %s: %s' % (current_ip,
whois_resp)
# Save current_ip and whois_resp
entry = {
'netblock_start': current_ip,
'netblock_end': last_netrange_ip,
'block_size': ip2long(last_netrange_ip) - ip2long(current_ip) + 1,
'whois': json.dumps(whois_resp),
}
keys = ('cidr', 'name', 'handle', 'range', 'description',
'country', 'state', 'city', 'address', 'postal_code',
'abuse_emails', 'tech_emails', 'misc_emails', 'created',
'updated')
for _key in keys:
entry[_key] = str(whois_resp['nets'][0][_key]) \
if _key in whois_resp['nets'][0] and \
whois_resp['nets'][0][_key] else None
if _key == 'city' and entry[_key] and ' ' in entry[_key]:
entry[_key] = entry[_key].replace(' ', '_')
try:
connection.index(index_name, doc_name, entry)
except ElasticHttpError, error:
print 'At %s. Unable to save record: %s' % (current_ip, entry)
raise error
current_ip = get_next_ip(last_netrange_ip)
if current_ip is None:
return # No more undefined ip addresses
gevent.sleep(randint(sleep_min, sleep_max))
def stats(elastic_search_url, index_name, doc_name):
fields = ('country', 'city')
url = '%s/%s/_search?fields=aggregations' % (elastic_search_url, index_name)
for field in fields:
data = {
"aggs": {
field: {
"terms": {
"field": field,
"order": {"total_ips": "desc"}
},
"aggs": {
"total_ips": {"sum": {"field": "block_size"}}
}
}
}
}
resp = requests.get(url, data=json.dumps(data))
assert resp.status_code == 200, \
'Did not get HTTP 200 back: %s' % resp.status_code
_stats = json.loads(resp.content)["aggregations"][field]["buckets"]
_stats = {stat['key']: int(stat['total_ips']['value'])
for stat in _stats}
print 'Top 10 netblock locations by %s' % field
for _key in sorted(_stats, key=_stats.get, reverse=True):
print "{:14,d}".format(_stats[_key]), _key.replace('_', ' ')
print
def main(argv):
"""
:param dict argv: command line arguments
"""
opt = docopt(__doc__, argv)
if opt['collect']:
sleep_min = int(opt['--sleep_min']) \
if opt['--sleep_min'] is not None else randint(1, 5)
sleep_max = int(opt['--sleep_max']) \
if opt['--sleep_max'] is not None else randint(1, 5)
num_threads = int(opt['--threads'])
if sleep_min > sleep_max:
sleep_min, sleep_max = sleep_max, sleep_min
threads = [gevent.spawn(get_netranges, starting_id, ending_ip,
opt['<elastic_search_url>'], opt['<index_name>'],
opt['<doc_name>'], sleep_min, sleep_max)
for starting_id, ending_ip in
break_up_ipv4_address_space(num_threads)]
gevent.joinall(threads)
if opt['stats']:
stats(opt['<elastic_search_url>'],
opt['<index_name>'],
opt['<doc_name>'])
if opt['test']:
import doctest
doctest.testmod()
if __name__ == "__main__":
try:
main(sys.argv[1:])
except KeyboardInterrupt:
pass