/
maxmind.py
487 lines (432 loc) · 16.1 KB
/
maxmind.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#! /usr/bin/env python
# This file is part of IVRE.
# Copyright 2011 - 2018 Pierre LALET <pierre.lalet@cea.fr>
#
# IVRE 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.
#
# IVRE 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 IVRE. If not, see <http://www.gnu.org/licenses/>.
"""This sub-module contains functions to interact with the MaxMind
files.
"""
import codecs
from functools import reduce
import os
import struct
from builtins import object, range
from future.utils import viewitems
from ivre import config, utils
from ivre.db import DBData
class MaxMindFileIter(object):
"""Iterator for MaxMindFile"""
def __init__(self, base):
self.base = base
self.current = []
self.nextval = 0
def __iter__(self):
return self
def __next__(self):
if self.nextval is None:
raise StopIteration()
node_no = 0
for i in range(96 if self.base.ip_version == 4 else 0, 128):
try:
flag = self.current[i]
except IndexError:
flag = self.nextval
self.current.append(self.nextval)
self.nextval = 0
next_node_no = self.base.read_record(node_no, flag)
if next_node_no == 0:
raise Exception('Invalid file format')
if next_node_no >= self.base.node_count:
pos = (next_node_no - self.base.node_count -
self.base.DATA_SECTION_SEPARATOR_SIZE)
curvalinf = int(''.join(str(p) for p in self.current) +
'0' * (128 - len(self.current)), 2)
curvalsup = int(''.join(str(p) for p in self.current) +
'1' * (128 - len(self.current)), 2)
try:
while self.current.pop():
pass
except IndexError:
self.nextval = None
else:
self.current.append(1)
return (curvalinf, curvalsup,
self.base.decode(pos, self.base.data_section_start)[1])
node_no = next_node_no
# We should never reach this point if the file is properly formatted.
raise StopIteration()
class EmptyMaxMindFile(object):
"""Stub to replace MaxMind databases parsers. Used when a file is
missing to emit a warning message and return empty results.
"""
def __init__(self, _):
utils.LOGGER.warning("Cannot find Maxmind database files")
def lookup(self, _):
return {}
class MaxMindFile(object):
"""Parser for MaxMind databases.
Code copied and adapted from
https://github.com/yhirose/maxminddb/blob/master/lib/maxminddb.rb
"""
METADATA_BEGIN_MARKER = b'\xab\xcd\xefMaxMind.com'
DATA_SECTION_SEPARATOR_SIZE = 16
SIZE_BASE_VALUES = [0, 29, 285, 65821]
POINTER_BASE_VALUES = [0, 0, 2048, 526336]
def __init__(self, path):
self.path = path
self._data = None
pos = (self.data.rindex(self.METADATA_BEGIN_MARKER) +
len(self.METADATA_BEGIN_MARKER))
metadata = self.metadata = self.decode(pos, 0)[1]
self.ip_version = metadata['ip_version']
self.node_count = metadata['node_count']
self.node_byte_size = metadata['record_size'] * 2 // 8
self.search_tree_size = self.node_count * self.node_byte_size
self.data_section_start = (self.search_tree_size +
self.DATA_SECTION_SEPARATOR_SIZE)
@property
def data(self):
if self._data is None:
with open(self.path, 'rb') as fdesc:
self._data = fdesc.read()
return self._data
def read_byte(self, pos):
return ord(self.data[pos:pos + 1])
def read_value(self, pos, size):
return reduce(
lambda x, y: (x << 8) + y,
struct.unpack(
'%dB' % size,
self.data[pos:pos + size],
),
0,
)
def decode(self, pos, base_pos):
ctrl = ord(self.data[pos + base_pos:pos + base_pos + 1])
pos += 1
type_ = ctrl >> 5
if type_ == 1:
# pointer
size = ((ctrl >> 3) & 0x3) + 1
val1 = ctrl & 0x7
val2 = self.read_value(pos + base_pos, size)
pointer = ((val1 << (8 * size)) + val2 +
self.POINTER_BASE_VALUES[size])
return pos + size, self.decode(pointer, base_pos)[1]
if type_ == 0:
# extended type
type_ = 7 + self.read_byte(pos + base_pos)
pos += 1
size = ctrl & 0x1f
if size >= 29:
byte_size = size - 29 + 1
val = self.read_value(pos + base_pos, byte_size)
pos += byte_size
size = val + self.SIZE_BASE_VALUES[byte_size]
if type_ == 2:
# utf8
val = self.data[
pos + base_pos:pos + base_pos + size
].decode('utf-8')
pos += size
elif type_ in [3, 15]:
# double
# float
val = struct.unpack(
{3: '>d', 15: '>f'}[type_],
self.data[pos + base_pos:pos + base_pos + size],
)[0]
pos += size
elif type_ == 4:
# bytes
val = self.data[pos + base_pos:pos + base_pos + size]
pos += size
elif type_ in [5, 6, 9, 10]:
# unsigned 16-bit int
# unsigned 32-bit int
# unsigned 64-bit int
# unsigned 128-bit int
val = self.read_value(pos + base_pos, size)
pos += size
elif type_ == 7:
# map
val = {}
for _ in range(size):
pos, k = self.decode(pos, base_pos)
pos, v = self.decode(pos, base_pos)
val[k] = v
elif type_ == 8:
# signed 32-bit int
v1 = struct.unpack(
'>i', self.data[pos + base_pos:pos + base_pos + size]
)[0]
bits = size * 8
val = (v1 & ~(1 << bits)) - (v1 & (1 << bits))
pos += size
elif type_ == 11:
# array
val = []
for _ in range(size):
pos, v = self.decode(pos, base_pos)
val.append(v)
elif type_ == 12:
# data cache container
raise Exception('TODO type == 12 (data cache container)')
elif type_ == 13:
# end marker
val = None
elif type_ == 14:
# boolean
val = bool(size)
else:
# unknown
raise Exception('TODO type == %d (unknown)' % type_)
return pos, val
def read_record(self, node_no, flag):
rec_byte_size = self.node_byte_size // 2
pos = self.node_byte_size * node_no
middle = (self.read_byte(pos + rec_byte_size)
if self.node_byte_size % 2 else 0)
if flag == 0: # left
val = self.read_value(pos, rec_byte_size)
val += ((middle & 0xf0) << 20) if middle else 0
else: # right
val = self.read_value(pos + self.node_byte_size - rec_byte_size,
rec_byte_size)
val += ((middle & 0xf) << 24) if middle else 0
return val
def __repr__(self):
return '<%s from %s>' % (self.__class__.__name__, self.path)
def lookup(self, ip):
node_no = 0
addr = utils.force_ip2int(ip)
for i in range(96 if self.ip_version == 4 else 0, 128):
flag = (addr >> (127 - i)) & 1
next_node_no = self.read_record(node_no, flag)
if next_node_no == 0:
raise Exception('Invalid file format')
if next_node_no >= self.node_count:
pos = (next_node_no - self.node_count -
self.DATA_SECTION_SEPARATOR_SIZE)
return self.decode(pos, self.data_section_start)[1]
node_no = next_node_no
raise Exception('Invalid file format')
def __iter__(self):
return MaxMindFileIter(self)
@staticmethod
def _get_fields(rec, fields):
for field in fields:
val = rec
for subfield in field.split('->'):
try:
val = val[subfield]
except TypeError:
try:
subfield = int(subfield)
except ValueError:
val = None
break
try:
val = val[subfield]
except IndexError:
val = None
break
except KeyError:
val = None
break
yield val
def _get_ranges(self, fields):
gen = iter(self)
try:
start, stop, rec = next(gen)
except StopIteration:
return
rec = tuple(self._get_fields(rec, fields))
for n_start, n_stop, n_rec in gen:
n_rec = tuple(self._get_fields(n_rec, fields))
if n_start <= stop + 1 and n_rec == rec:
stop = n_stop
else:
yield (start, stop) + rec
start, stop, rec = n_start, n_stop, n_rec
yield (start, stop) + rec
def get_ranges(self, fields, cond=None):
for rec in self._get_ranges(fields):
if cond is None or cond(rec):
yield rec
class MaxMindDBData(DBData):
LANG = "en"
AS_KEYS = {
"autonomous_system_number": "as_num",
"autonomous_system_organization": "as_name",
}
@property
def db_asn(self):
try:
return self._db_asn
except AttributeError:
self._db_asn = EmptyMaxMindFile("ASN")
return self._db_asn
@property
def db_city(self):
try:
return self._db_city
except AttributeError:
self._db_city = EmptyMaxMindFile("City")
return self._db_city
@property
def db_country(self):
try:
return self._db_country
except AttributeError:
self._db_country = EmptyMaxMindFile("Country")
return self._db_country
def __init__(self, url):
self.basepath = url.path
self.reload_files()
def reload_files(self):
for fname in os.listdir(self.basepath):
if fname.endswith('.mmdb'):
subdb = MaxMindFile(os.path.join(self.basepath, fname))
name = subdb.metadata['database_type'].lower()
if name.startswith('geolite2-'):
name = name[9:]
setattr(self, "_db_%s" % name, subdb)
def as_byip(self, addr):
return dict(
(self.AS_KEYS.get(key, key), value)
for key, value in viewitems(self.db_asn.lookup(addr))
)
def location_byip(self, addr):
raw = self.db_city.lookup(addr)
result = {}
sub = raw.get('subdivisions')
if sub:
result['region_code'] = tuple(v.get('iso_code') for v in sub)
result['region_name'] = tuple(v.get('names', {}).get(self.LANG)
for v in sub)
sub = raw.get('continent')
if sub:
value = sub.get('code')
if value:
result['continent_code'] = value
value = sub.get('names', {}).get(self.LANG)
if value:
result['continent_name'] = value
sub = raw.get('country')
if sub:
value = sub.get('iso_code')
if value:
result['country_code'] = value
value = sub.get('names', {}).get(self.LANG)
if value:
result['country_name'] = value
sub = raw.get('registered_country')
if sub:
value = sub.get('iso_code')
if value:
result['registered_country_code'] = value
value = sub.get('names', {}).get(self.LANG)
if value:
result['registered_country_name'] = value
value = raw.get('city', {}).get('names', {}).get(self.LANG)
if value:
result['city'] = value
value = raw.get('postal', {}).get('code')
if value:
result['postal_code'] = value
sub = raw.get('location')
if sub:
try:
result['coordinates'] = (sub['latitude'], sub['longitude'])
except KeyError:
pass
value = sub.get('accuracy_radius')
result['coordinates_accuracy_radius'] = value
if result:
return result
return None
def country_byip(self, addr):
result = {}
raw = self.db_country.lookup(addr)
sub = raw.get('country')
if sub:
value = sub.get('iso_code')
if value:
result['country_code'] = value
value = sub.get('names', {}).get(self.LANG)
if value:
result['country_name'] = value
return result
def dump_as_ranges(self, fdesc):
for data in self.db_asn.get_ranges(
["autonomous_system_number"],
cond=lambda line: line[2] is not None,
):
if data[0] > 0xffffffff: # only IPv4
break
fdesc.write('%d,%d,%d\n' % data)
def dump_country_ranges(self, fdesc):
for data in self.db_country.get_ranges(
["country->iso_code"],
cond=lambda line: line[2] is not None,
):
if data[0] > 0xffffffff: # only IPv4
break
fdesc.write('%d,%d,%s\n' % data)
def dump_city_ranges(self, fdesc):
for data in self.db_city.get_ranges(
["country->iso_code", "subdivisions->0->iso_code",
"city->names->%s" % config.GEOIP_LANG, "city->geoname_id"],
cond=lambda line: (line[2] is not None and
(line[3] is not None or
line[4] is not None)),
):
if data[0] > 0xffffffff: # only IPv4
break
fdesc.write('%d,%d,%s,%s,%s,%s\n' % (
data[:4] +
(utils.encode_b64((data[4] or
"").encode('utf-8')).decode('utf-8'),) +
data[5:]
))
def build_dumps(self, force=False):
for attr, func in [
("db_asn", self.dump_as_ranges),
("db_country", self.dump_country_ranges),
("db_city", self.dump_city_ranges),
]:
try:
subdb = getattr(self, attr)
except AttributeError:
continue
if not subdb.path.endswith('.mmdb'):
continue
csv_file = subdb.path[:-4] + 'dump-IPv4.csv'
if not force:
mmdb_mtime = os.path.getmtime(subdb.path)
try:
csv_mtime = os.path.getmtime(csv_file)
except OSError:
pass
else:
if csv_mtime > mmdb_mtime:
utils.LOGGER.info('Skipping %r since %r is newer',
os.path.basename(subdb.path),
os.path.basename(csv_file))
continue
utils.LOGGER.info('Dumping %r to %r', os.path.basename(subdb.path),
os.path.basename(csv_file))
with codecs.open(csv_file, mode="w", encoding='utf-8') as fdesc:
func(fdesc)