2019-12-12 13:14:14 +00:00
|
|
|
import ipaddress
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
import dns.resolver
|
2019-12-12 14:13:17 +00:00
|
|
|
from . import lists as _lists
|
2019-12-12 13:14:14 +00:00
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-12-13 15:12:21 +00:00
|
|
|
_name = "DNSBL"
|
|
|
|
|
2019-12-12 13:14:14 +00:00
|
|
|
@utils.hook("received.command.dnsbl")
|
|
|
|
def dnsbl(self, event):
|
|
|
|
args = event["args_split"]
|
|
|
|
|
2019-12-12 14:13:17 +00:00
|
|
|
default_lists = _lists.default_lists()
|
2019-12-12 13:14:14 +00:00
|
|
|
lists = []
|
|
|
|
for i, arg in reversed(list(enumerate(args))):
|
|
|
|
if arg[0] == "@":
|
2020-08-07 16:42:28 +00:00
|
|
|
hostname = args.pop(i)[1:]
|
2019-12-12 14:13:17 +00:00
|
|
|
if hostname in default_lists:
|
|
|
|
lists.insert(0, default_lists[hostname])
|
|
|
|
else:
|
2020-08-07 16:37:56 +00:00
|
|
|
lists.insert(0, _lists.DNSBL(hostname))
|
2019-12-12 14:13:17 +00:00
|
|
|
|
|
|
|
lists = lists or list(default_lists.values())
|
2019-12-12 13:14:14 +00:00
|
|
|
|
|
|
|
address = args[0]
|
|
|
|
failed = self._check_lists(lists, address)
|
|
|
|
if failed:
|
2019-12-12 14:13:17 +00:00
|
|
|
failed = ["%s (%s)" % item for item in failed]
|
2019-12-13 15:12:21 +00:00
|
|
|
event["stderr"].write("%s matched for lists: %s" %
|
2019-12-12 13:14:14 +00:00
|
|
|
(address, ", ".join(failed)))
|
|
|
|
else:
|
|
|
|
event["stdout"].write("%s not found in blacklists" % address)
|
|
|
|
|
|
|
|
def _check_lists(self, lists, address):
|
|
|
|
address_obj = ipaddress.ip_address(address)
|
|
|
|
|
|
|
|
if address_obj.version == 6:
|
|
|
|
address = reversed(address_obj.exploded.replace(":", ""))
|
|
|
|
else:
|
|
|
|
address = reversed(address.split("."))
|
|
|
|
address = ".".join(address)
|
|
|
|
|
|
|
|
failed = []
|
|
|
|
for list in lists:
|
2019-12-12 14:13:17 +00:00
|
|
|
record = self._check_list(list.hostname, address)
|
2020-09-14 13:32:59 +00:00
|
|
|
if record is not None:
|
|
|
|
a_record, txt_record = record
|
|
|
|
reason = list.process(a_record, txt_record) or "unknown"
|
2019-12-12 14:54:58 +00:00
|
|
|
failed.append((list.hostname, reason))
|
2019-12-12 13:14:14 +00:00
|
|
|
return failed
|
|
|
|
|
|
|
|
def _check_list(self, list, address):
|
|
|
|
list_address = "%s.%s" % (address, list)
|
|
|
|
try:
|
2020-09-14 13:52:54 +00:00
|
|
|
a_record = dns.resolver.resolve(list_address, "A")[0].to_text()
|
2019-12-12 13:14:14 +00:00
|
|
|
except dns.resolver.NXDOMAIN:
|
2019-12-12 14:13:17 +00:00
|
|
|
return None
|
2020-09-14 13:32:59 +00:00
|
|
|
|
|
|
|
try:
|
2020-09-14 13:52:54 +00:00
|
|
|
txt_record = dns.resolver.resolve(list_address, "TXT")[0].to_text()
|
2020-09-14 13:32:59 +00:00
|
|
|
except:
|
|
|
|
txt_record = None
|
|
|
|
|
|
|
|
return (a_record, txt_record)
|