forked from yoda66/RIRTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_rir_database.py
executable file
·258 lines (235 loc) · 7.91 KB
/
build_rir_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
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
#!/usr/bin/env python3
import argparse
import os
import sys
import csv
import re
import hashlib
import math
import urllib.request
import urllib.error
import urllib.parse
import sqlite3
import socket
from datetime import datetime, timedelta
class RIRDatabase:
def __init__(self):
self.ALLRIRS = [
'arin', 'apnic', 'afrinic', 'lacnic', 'ripencc'
]
dbhome = '{}/.rirdb'.format(os.path.expanduser('~'))
if not os.path.exists(dbhome):
os.mkdir(dbhome)
self.dbname = '{}/rir.db'.format(dbhome)
self.lastfetch = '{}/lastfetchdate'.format(dbhome)
self.dbh = self.SQLconnect()
def SQLconnect(self):
dbh = sqlite3.connect(self.dbname)
dbh.text_factory = str
cur = dbh.cursor()
sql = """\
CREATE TABLE IF NOT EXISTS rir
(
registry TEXT, cc TEXT, type TEXT,
start TEXT, start_binary INT, value TEXT, cidr TEXT,
date TEXT, status TEXT, reg_id TEXT
)
"""
cur.execute(sql)
sql = """\
CREATE TABLE IF NOT EXISTS country_codes
(
cc TEXT,
name TEXT
)
"""
cur.execute(sql)
dbh.commit()
return dbh
def Insert_RIR_Records(self, rir, data):
if not data:
return [0, 0]
cur = self.dbh.cursor()
cur.execute('DELETE FROM rir WHERE registry = ?', [rir, ])
self.dbh.commit()
recs = 0
errs = 0
for line in data.split('\n'):
if not line or \
re.match(r'^#', line) or \
re.match(r'^\d\.\d.+', line) or \
re.match(r'^.+\|summary', line):
continue
reg_id = ''
try:
registry, cc, type, start, \
value, date, status, reg_id = line.split('|')
except:
try:
registry, cc, type, start, \
value, date, status = line.split('|')
except:
errs += 1
continue
cidr = ''
start_binary = 0
if type == 'ipv4':
start_binary = socket.inet_pton(
socket.AF_INET, start
)
cidr = '{}/{:d}'.format(
start, 32 - int(math.log(int(value), 2)))
elif type == 'ipv6':
start_binary = socket.inet_pton(
socket.AF_INET6, start
)
sql = """\
INSERT INTO rir (
registry, cc, type, start, start_binary,
value, cidr, date, status, reg_id
)
VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
"""
try:
params = [
registry, cc, type, start, start_binary,
value, cidr, date, status, reg_id
]
cur.execute(sql, params)
except:
errs += 1
continue
recs += 1
self.dbh.commit()
return [recs, errs]
def fetchMD5(self, urlbase, datafile):
url = '{}/{}.md5'.format(urlbase, datafile)
req = urllib.request.Request(url)
f = urllib.request.urlopen(req)
line = f.read().decode()
m = re.match(r'.*([a-f0-9]{32}).*', line)
if m:
return m.group(1)
return None
def GetRIRData(self, rir, datestr):
if options.http:
proto = "http"
else:
proto = "ftp"
urlbase = '{}://ftp.{}.net/pub/stats/{}'.format(proto, rir, rir)
if re.match(r'^ripencc', rir):
urlbase = '{}://ftp.{}.net/pub/stats/{}'.format(proto, 'ripe', rir)
datafile = 'delegated-{}-extended-{}'.format(rir, datestr)
url = '{}/{}'.format(urlbase, datafile)
req = urllib.request.Request(url)
try:
print('[*] Fetching [{}]'.format(datafile))
f = urllib.request.urlopen(req)
data = f.read()
md5_1 = hashlib.md5(data).hexdigest()
md5_2 = self.fetchMD5(urlbase, datafile)
if md5_1 == md5_2:
return [url, 'ok', data.decode()]
else:
return [url, 'error', 'md5 hash mismatch']
except Exception as e:
return [url, 'error', e]
def RegionalRegistryData(self):
for rir in self.ALLRIRS:
done = False
rdata = self.GetRIRData(rir, 'latest')
if rdata[1] == 'ok':
recs, errs = self.Insert_RIR_Records(rir, rdata[2])
print('[*] Inserted {:d} records for [{}]'.format(recs, rir))
if errs > 0:
raise Exception('[*] Errors on insert {:d}'.format(errs))
done = True
else:
print('[-] ERROR: {}'.format(rdata))
days = 0
today = datetime.utcnow()
while not done and days < 5:
date = today - timedelta(days=-days)
rdata = self.GetRIRData(rir, date.strftime('%Y%m%d'))
if rdata[1] == 'ok':
recs, errs = self.Insert_RIR_Records(rir, rdata[2])
print('[*] Inserted {} records for [{}]'.format(recs, rir))
if errs > 0:
raise Exception(
'[*] Errors on insert {:d}'.format(errs)
)
done = True
else:
print('[-] ERROR: {}'.format(rdata))
days += 1
def UpdateCountryCodes(self):
cur = self.dbh.cursor()
url = 'https://raw.githubusercontent.com/' + \
'datasets/country-list/master/data.csv'
req = urllib.request.Request(url)
recs = 0
f = urllib.request.urlopen(req)
data = f.read().decode().split('\n')
if not data:
raise Exception('no data read from {}'.format(url))
cur.execute('DELETE FROM country_codes')
for line in csv.reader(data):
if not line or (line[0] == 'Name' and line[1] == 'Code'):
continue
sql = "INSERT INTO country_codes (cc, name) VALUES (?, ?)"
cc = line[1]
name = line[0]
cur.execute(sql, [cc, name])
recs += 1
cur.execute("""\
INSERT INTO country_codes VALUES ('EU', 'non-iso3166:Europe')""")
cur.execute("""\
INSERT INTO country_codes VALUES ('AP', 'non-iso3166:Asia-Pacific')""")
recs += 2
self.dbh.commit()
print('[*] %d country codes updated.' % (recs))
def has_run_today(self):
today = datetime.utcnow().strftime('%Y%m%d')
with open(self.lastfetch, 'r') as f:
last = f.read()[:-1]
f.close()
if today == last:
return True
return False
def UpdateLastDate(self):
today = datetime.utcnow().strftime('%Y%m%d')
f = open(self.lastfetch, 'w')
f.write('{}\n'.format(today))
f.close()
def run(self):
if self.has_run_today() and not options.force:
print('[*] Exiting: Data has already been fetched today')
return
self.UpdateCountryCodes()
self.RegionalRegistryData()
self.UpdateLastDate()
if __name__ == '__main__':
VERSION = '20220225_0101'
desc = '''
-------------------------------------------------
{} Version {}
Author: Joff Thyer (c) 2015-2022
Black Hills Information Security
-------------------------------------------------
'''.format(os.path.basename(sys.argv[0]), VERSION)
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=desc
)
parser.add_argument(
'--http', action='store_true',
default=False, help='Retrieve RIR data over HTTP'
)
parser.add_argument(
'--force', action='store_true',
default=False, help='Force DB update'
)
options = parser.parse_args()
print('{}'.format(desc))
rirdb = RIRDatabase()
rirdb.run()