2021-03-22 19:24:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
2023-02-05 16:30:58 +00:00
|
|
|
from ipaddress import IPv6Address, ip_address
|
2021-05-29 07:28:05 +00:00
|
|
|
from subprocess import check_output
|
2021-03-22 19:24:14 +00:00
|
|
|
from sys import argv, exit
|
|
|
|
|
2023-06-21 21:23:16 +00:00
|
|
|
BLOCKLISTS = {
|
|
|
|
'0spam.fusionzero.com': set(),
|
|
|
|
'bl.mailspike.org': set(),
|
|
|
|
'bl.spamcop.net': set(),
|
|
|
|
'blackholes.brainerd.net': set(),
|
|
|
|
'dnsbl-1.uceprotect.net': set(),
|
|
|
|
'l2.spews.dnsbl.sorbs.net': set(),
|
|
|
|
'list.dsbl.org': set(),
|
|
|
|
'map.spam-rbl.com': set(),
|
|
|
|
'multihop.dsbl.org': set(),
|
|
|
|
'ns1.unsubscore.com': set(),
|
|
|
|
'opm.blitzed.org': set(),
|
|
|
|
'psbl.surriel.com': set(),
|
|
|
|
'rbl.efnet.org': set(),
|
|
|
|
'rbl.schulte.org': set(),
|
|
|
|
'spamguard.leadmon.net': set(),
|
|
|
|
'ubl.unsubscore.com': set(),
|
|
|
|
'unconfirmed.dsbl.org': set(),
|
|
|
|
'virbl.dnsbl.bit.nl': set(),
|
|
|
|
'zen.spamhaus.org': {
|
|
|
|
# https://www.spamhaus.org/news/article/807/using-our-public-mirrors-check-your-return-codes-now.
|
|
|
|
'127.255.255.252', # Typing Error
|
|
|
|
'127.255.255.254', # public resolver / generic rdns
|
|
|
|
'127.255.255.255', # rate limited
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
def check_list(ip_list, blocklist, warn_ips):
|
2021-03-22 19:24:14 +00:00
|
|
|
dns_name = '{}.{}'.format(
|
|
|
|
'.'.join(ip_list),
|
|
|
|
blocklist,
|
|
|
|
)
|
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
returncode = 0
|
|
|
|
msgs = []
|
|
|
|
|
2021-03-22 19:24:14 +00:00
|
|
|
try:
|
2021-05-29 07:28:05 +00:00
|
|
|
result = check_output([
|
|
|
|
'dig',
|
|
|
|
'+tries=2',
|
|
|
|
'+time=5',
|
|
|
|
'+short',
|
|
|
|
dns_name
|
|
|
|
]).decode().splitlines()
|
2021-03-22 19:24:14 +00:00
|
|
|
for item in result:
|
2023-09-09 12:10:27 +00:00
|
|
|
if line.startswith(';;'):
|
|
|
|
msgs.append('{} - {}'.format(
|
|
|
|
blocklist,
|
|
|
|
item,
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
msgs.append('{} listed in {} as {}'.format(
|
|
|
|
ip,
|
|
|
|
blocklist,
|
|
|
|
item,
|
|
|
|
))
|
|
|
|
if (item in warn_ips or line.startswith(';;')) and returncode < 2:
|
2023-06-21 21:23:16 +00:00
|
|
|
returncode = 1
|
|
|
|
else:
|
|
|
|
returncode = 2
|
2021-03-22 19:24:14 +00:00
|
|
|
except Exception as e:
|
2021-05-29 07:28:05 +00:00
|
|
|
if e.returncode == 9:
|
|
|
|
# no reply from server
|
2021-06-03 12:31:03 +00:00
|
|
|
return [], 0
|
2021-05-29 07:28:05 +00:00
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
return [repr(e)], 3
|
2021-03-22 19:24:14 +00:00
|
|
|
|
2021-03-25 16:42:59 +00:00
|
|
|
return msgs, returncode
|
|
|
|
|
|
|
|
try:
|
|
|
|
ip = ip_address(argv[1])
|
|
|
|
except Exception:
|
|
|
|
print('usage: {} <ip>'.format(argv[0]))
|
|
|
|
exit(3)
|
|
|
|
|
|
|
|
if isinstance(ip, IPv6Address):
|
|
|
|
ip_list = list(ip.exploded.replace(':', ''))
|
2021-03-22 19:24:14 +00:00
|
|
|
else:
|
2021-03-25 16:42:59 +00:00
|
|
|
ip_list = ip.exploded.split('.')
|
|
|
|
|
|
|
|
ip_list.reverse()
|
|
|
|
exitcode = 0
|
|
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=len(BLOCKLISTS)) as executor:
|
|
|
|
futures = set()
|
|
|
|
|
2023-06-21 21:23:16 +00:00
|
|
|
for blocklist, warn_ips in BLOCKLISTS.items():
|
|
|
|
futures.add(executor.submit(check_list, ip_list, blocklist, warn_ips))
|
2021-03-25 16:42:59 +00:00
|
|
|
|
|
|
|
for future in as_completed(futures):
|
|
|
|
msgs, this_exitcode = future.result()
|
|
|
|
|
|
|
|
for msg in msgs:
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
exitcode = max(exitcode, this_exitcode)
|
|
|
|
|
|
|
|
if exitcode == 0:
|
2021-03-22 19:24:14 +00:00
|
|
|
print('OK')
|
2021-03-25 16:42:59 +00:00
|
|
|
|
|
|
|
exit(exitcode)
|