diff --git a/rblwatch/rblwatch.py b/rblwatch/rblwatch.py index 0324a32..03a3065 100644 --- a/rblwatch/rblwatch.py +++ b/rblwatch/rblwatch.py @@ -98,6 +98,7 @@ def __init__(self, host, dnslist, listed, resolver): self.resolver = resolver def run(self): + try: host_record = self.resolver.query(self.host, "A") if len(host_record) > 0: @@ -107,6 +108,11 @@ def run(self): if len(text_record) > 0: self.listed[self.dnslist]['TEXT'] = "\n".join(text_record[0].strings) self.listed[self.dnslist]['ERROR'] = False + + if 'query refused' in self.listed[self.dnslist]['TEXT'].lower(): + self.listed[self.dnslist]['LISTED'] = False + self.listed[self.dnslist]['ERROR'] = True + except NXDOMAIN: self.listed[self.dnslist]['ERROR'] = True self.listed[self.dnslist]['ERRORTYPE'] = NXDOMAIN @@ -123,27 +129,62 @@ def run(self): self.listed[self.dnslist]['ERROR'] = True self.listed[self.dnslist]['ERRORTYPE'] = NoAnswer + class RBLSearch(object): - def __init__(self, lookup_host): + def __init__(self, lookup_host, my_rbl_list=None): self.lookup_host = lookup_host self._listed = None self.resolver = Resolver() self.resolver.timeout = 0.2 self.resolver.lifetime = 1.0 + self.my_rbl_list = my_rbl_list def search(self): if self._listed is not None: pass else: - ip = IP(self.lookup_host) - host = ip.reverseName() - if ip.version() == 4: - host = re.sub('.in-addr.arpa.', '', host) - elif ip.version() == 6: - host = re.sub('.ip6.arpa.', '', host) + + try: + ip = IP(self.lookup_host) + except ValueError: + ip = None + self._listed = {'SEARCH_HOST': self.lookup_host} + + if ip: + + host = ip.reverseName() + if ip.version() == 4: + host = re.sub('.in-addr.arpa.', '', host) + elif ip.version() == 6: + host = re.sub('.ip6.arpa.', '', host) + else: + + try: + socket.gethostbyname(self.lookup_host) + host = self.lookup_host + except socket.gaierror: + self._listed['SEARCH_HOST'] = { + 'ERROR': True, + 'ERRORTYPE': socket.gaierror + } + except socket.herror: + self._listed['SEARCH_HOST'] = { + 'ERROR': True, + 'ERRORTYPE': socket.herror + } + + if 'ERROR' in self._listed['SEARCH_HOST']: + return self._listed + threads = [] - for LIST in RBLS: + + lists_to_check = RBLS + + if self.my_rbl_list: + lists_to_check = self.my_rbl_list + + for LIST in lists_to_check: self._listed[LIST] = {'LISTED': False} query = Lookup("%s.%s" % (host, LIST), LIST, self._listed, self.resolver) threads.append(query) @@ -159,7 +200,12 @@ def print_results(self): print("--- DNSBL Report for %s ---" % listed['SEARCH_HOST']) for key in listed: if key == 'SEARCH_HOST': - continue + if not 'ERROR' in listed['SEARCH_HOST']: + continue + else: + print "Error querying for %s" % self.lookup_host + break + if not listed[key].get('ERROR'): if listed[key]['LISTED']: print("Results for %s: %s" % (key, listed[key]['LISTED']))