-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate_database.py
56 lines (46 loc) · 1.59 KB
/
validate_database.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
import sys
import random
import ipaddress
import maxminddb
import logging
log = logging.getLogger("validate_db")
extra_keys_str = [
"autonomous_system_country",
"autonomous_system_name",
"autonomous_system_organization"
]
extra_keys_int = [
"autonomous_system_number",
]
def lookup_random_ips(reader, min_ip : int, max_ip : int, lookup_count : int, min_threshold : int, ip_type : str):
lookedup_ips = 0
for i in range(lookup_count):
ip = ipaddress.ip_address(random.randint(min_ip, max_ip))
resp = reader.get(ip)
if not resp or "country" not in resp:
continue
lookedup_ips += 1
for k in extra_keys_str:
if k not in resp:
log.debug(f"{ip} {k} missing from {resp}")
else:
assert isinstance(resp[k], str), f"{k} is not a string"
for k in extra_keys_int:
if k not in resp:
log.debug(f"{ip} {k} missing from {resp}")
else:
assert isinstance(resp[k], int), f"{k} is not an int"
if lookedup_ips > min_threshold:
break
assert lookedup_ips > min_threshold, f"didn't find enough {ip_type} addresses"
def main():
if len(sys.argv) != 2:
print("Usage: validate_database.py db_path.mmdb")
sys.exit(1)
db_file = sys.argv[1]
print(f"[+] validating {db_file}")
with maxminddb.open_database(db_file) as reader:
lookup_random_ips(reader, 2**24, 2**32, 10**5, 100, "ipv4")
lookup_random_ips(reader, 2**32, 2**128, 10**10, 100, "ipv6")
if __name__ == "__main__":
main()