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):
|
|
|
|
@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] == "@":
|
2019-12-12 14:13:17 +00:00
|
|
|
hostname = args.pop(i)
|
|
|
|
if hostname in default_lists:
|
|
|
|
lists.insert(0, default_lists[hostname])
|
|
|
|
else:
|
|
|
|
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:
|
2019-12-12 14:13:17 +00:00
|
|
|
failed = ["%s (%s)" % item for item in failed]
|
2019-12-12 13:14:14 +00:00
|
|
|
event["stderr"].write("%s failed for lists: %s" %
|
|
|
|
(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)
|
|
|
|
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:
|
2019-12-12 14:13:17 +00:00
|
|
|
return dns.resolver.query(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
|