22
33from sys import argv
44from pathlib import Path
5+ from yaml import dump as yaml_dump
56from json import loads as json_loads
67
78# see: https://github.com/O-X-L/dnsbl-server
89
9- # todo: separate response-codes for different report-categories
1010FILE_DNSBL = '/tmp/riskdb-dnsbl.yml'
1111
1212INCLUDE_NET_REPUTATION = ['bad' , 'warn' ]
1313INCLUDE_IP_REPORTS = 1000
14+ INCLUDE_CAT_PERCENT = 0.33
15+
16+ # risk-db cat to dns-bl cat
17+ CATEGORY_MAPPING = {
18+ 'bot' : 'bot' ,
19+ 'probe' : 'scanner' ,
20+ 'rate' : 'scanner' ,
21+ 'attack' : 'attack' ,
22+ 'crawler' : 'bot' ,
23+ 'spam' : 'spam' ,
24+ 'malware' : 'attack' ,
25+ }
26+ # dns-bl cat to responses
27+ CATEGORY_TO_RESPONSE = {
28+ 'abuse' : '127.0.0.2' ,
29+ 'scanner' : '127.0.0.3' ,
30+ 'bot' : '127.0.0.4' ,
31+ 'attack' : '127.0.0.5' ,
32+ 'spam' : '127.0.0.6' ,
33+ }
34+
35+
36+ def _get_categories (reports : dict ) -> list [str ]:
37+ cats = []
38+ s = reports ['sum' ]
39+ for c , v in reports .items ():
40+ if c == 'sum' :
41+ continue
42+
43+ if v > INCLUDE_IP_REPORTS :
44+ cats .append (c )
45+
46+ elif v / s > INCLUDE_CAT_PERCENT :
47+ cats .append (c )
48+
49+ cats = list (set (CATEGORY_MAPPING [c ] for c in cats ))
50+ if len (cats ) == 0 :
51+ cats = [CATEGORY_TO_RESPONSE ['abuse' ]]
52+
53+ return cats
1454
1555
1656def _to_yaml_list (d : list ) -> str :
@@ -28,39 +68,49 @@ def main():
2868 data [k ] = json_loads (f .read ())
2969
3070 print ('BUILDING DNS-BL..' )
31- dns_bl = {'nets' : [], 'ips' : []}
71+ dns_bl = {
72+ 'abuse' : {'nets' : [], 'ips' : []},
73+ 'scanner' : {'nets' : [], 'ips' : []},
74+ 'bot' : {'nets' : [], 'ips' : []},
75+ 'attack' : {'nets' : [], 'ips' : []},
76+ 'spam' : {'nets' : [], 'ips' : []},
77+ }
3278
3379 for ipp in ['net4' , 'net6' ]:
3480 for net , net_info in data [ipp ].items ():
3581 if net_info ['reputation' ] in INCLUDE_NET_REPUTATION :
36- dns_bl ['nets' ].append (net )
82+ for c in _get_categories (net_info ['reports' ]):
83+ dns_bl [c ]['nets' ].append (net )
3784
3885 data .pop ('net4' )
3986 data .pop ('net6' )
4087
4188 for ipp in ['ip4' , 'ip6' ]:
4289 for ip , ip_info in data [ipp ].items ():
4390 if ip_info ['reports' ]['sum' ] > INCLUDE_IP_REPORTS :
44- dns_bl ['ips' ].append (ip )
91+ for c in _get_categories (ip_info ['reports' ]):
92+ dns_bl [c ]['ips' ].append (ip )
4593
4694 del data
4795
96+ data = {
97+ 'nets' : [],
98+ 'ips' : [],
99+ }
100+
101+ for category , entries in dns_bl .items ():
102+ data ['nets' ].append ({
103+ 'response' : CATEGORY_TO_RESPONSE [category ],
104+ 'content' : entries ['nets' ],
105+ })
106+ data ['ips' ].append ({
107+ 'response' : CATEGORY_TO_RESPONSE [category ],
108+ 'content' : entries ['ips' ],
109+ })
110+
48111 print ('WRITING DNS-BL..' )
49112 with open (FILE_DNSBL , 'w' , encoding = 'utf-8' ) as f :
50- f .write (f"""
51- ---
52-
53- nets:
54- - response: 127.0.0.2
55- content:
56- { _to_yaml_list (dns_bl ['nets' ])}
57-
58- ips:
59- - response: 127.0.0.2
60- content:
61- { _to_yaml_list (dns_bl ['ips' ])}
62-
63- """ )
113+ f .write (yaml_dump (data ))
64114
65115 print ('DONE:' , FILE_DNSBL )
66116
0 commit comments