Skip to content

Commit 0c43597

Browse files
committed
add script to build dns-bl config
1 parent b432b45 commit 0c43597

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/riskdb/dnsbl/build_config.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from sys import argv
2+
from pathlib import Path
3+
from json import loads as json_loads
4+
5+
# see: https://github.com/O-X-L/dnsbl-server
6+
7+
# todo: separate response-codes for different report-categories
8+
FILE_DNSBL = '/tmp/riskdb-dnsbl.yml'
9+
10+
INCLUDE_NET_REPUTATION = ['bad', 'warn']
11+
INCLUDE_IP_REPORTS = 1000
12+
13+
14+
def _to_yaml_list(d: list) -> str:
15+
return '\n'.join(f" - '{e}'" for e in d)
16+
17+
18+
def main():
19+
if not DB_IP4.is_file() or not DB_IP4.is_file() or not DB_NET4.is_file() or not DB_NET6.is_file():
20+
raise FileNotFoundError('At least one DB-File is missing!')
21+
22+
print('LOADING DATABASES..')
23+
data = {}
24+
for k, file in {'ip4': DB_IP4, 'ip6': DB_IP6, 'net4': DB_NET4, 'net6': DB_NET6}.items():
25+
with open(file, 'r', encoding='utf-8') as f:
26+
data[k] = json_loads(f.read())
27+
28+
print('BUILDING DNS-BL..')
29+
dns_bl = {'nets': [], 'ips': []}
30+
31+
for ipp in ['net4', 'net6']:
32+
print(data.keys(), ipp, ipp in data)
33+
for net, net_info in data[ipp].items():
34+
if net_info['reputation'] in INCLUDE_NET_REPUTATION:
35+
dns_bl['nets'].append(net)
36+
37+
data.pop('net4')
38+
data.pop('net6')
39+
40+
for ipp in ['ip4', 'ip6']:
41+
for ip, ip_info in data[ipp].items():
42+
if ip_info['reports']['sum'] > INCLUDE_IP_REPORTS:
43+
dns_bl['ips'].append(ip)
44+
45+
del data
46+
47+
print('WRITING DNS-BL..')
48+
with open(FILE_DNSBL, 'w', encoding='utf-8') as f:
49+
f.write(f"""
50+
---
51+
52+
nets:
53+
- response: 127.0.0.2
54+
content:
55+
{_to_yaml_list(dns_bl['nets'])}
56+
57+
ips:
58+
- response: 127.0.0.2
59+
content:
60+
{_to_yaml_list(dns_bl['ips'])}
61+
62+
""")
63+
64+
print('DONE:', FILE_DNSBL)
65+
66+
67+
if __name__ == '__main__':
68+
if len(argv) == 1:
69+
DB_PATH_BASE = Path(__file__).parent
70+
71+
else:
72+
DB_PATH_BASE = Path(argv[1])
73+
74+
DB_IP4 = DB_PATH_BASE / 'risk_ip4_med.json'
75+
DB_IP6 = DB_PATH_BASE / 'risk_ip6_med.json'
76+
DB_NET4 = DB_PATH_BASE / 'risk_net4_med.json'
77+
DB_NET6 = DB_PATH_BASE / 'risk_net6_med.json'
78+
79+
main()

0 commit comments

Comments
 (0)