bitbot-3.11-fork/modules/dnsbl/__init__.py

57 lines
1.8 KiB
Python
Raw Normal View History

2019-12-12 13:14:14 +00:00
import ipaddress
from src import ModuleManager, utils
import dns.resolver
from . import lists as _lists
2019-12-12 13:14:14 +00:00
class Module(ModuleManager.BaseModule):
_name = "DNSBL"
2019-12-12 13:14:14 +00:00
@utils.hook("received.command.dnsbl")
def dnsbl(self, event):
args = event["args_split"]
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:]
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))
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:
failed = ["%s (%s)" % item for item in failed]
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:
record = self._check_list(list.hostname, address)
if not record == None:
2019-12-12 14:54:58 +00:00
reason = list.process(record) or "unknown"
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:
return dns.resolver.query(list_address, "A")[0].to_text()
2019-12-12 13:14:14 +00:00
except dns.resolver.NXDOMAIN:
return None