PageRenderTime 59ms CodeModel.GetById 18ms app.highlight 21ms RepoModel.GetById 15ms app.codeStats 0ms

/DNSBL.py

http://adns-python.googlecode.com/
Python | 82 lines | 77 code | 3 blank | 2 comment | 0 complexity | 19a4b444a7ee754248e95c893b9d1bdc MD5 | raw file
 1#!/usr/bin/env python
 2import os, sys, string
 3import ADNS
 4
 5class DNSBL:
 6
 7    """A class for defining various DNS-based blacklists."""
 8
 9    def __init__(self, name, zone, URL='', results=None):
10        """Create a DNS blacklist name, based on the given zone.
11        If presently, URL is a template that produces a link
12        back to information for a given address. results
13        should map returned addresses to list codes."""
14
15        self.name = name
16        self.zone = zone
17        self.URL = URL
18        self.results = {}
19        if results:
20            for result, name in results.items(): self.result(result, name)
21
22    def result(self, result, name):
23        """Add a possible result set."""
24        self.results[result] = name
25
26    def getURL(self, ip):
27        """Return a URL to information on the list of ip on this
28        blacklist."""
29        return self.URL % ip
30    
31
32class DNSBLQueryEngine(ADNS.QueryEngine):
33
34    def __init__(self, s=None, blacklists=None):
35        ADNS.QueryEngine.__init__(self, s)
36        self.blacklists = {}
37        self.dnsbl_results = {}
38        if blacklists:
39            for l in blacklists: self.blacklist(l)
40            
41    def blacklist(self, dnsbl):
42        """Add a DNSBL."""
43        self.blacklists[dnsbl.name] = dnsbl
44        
45    def submit_dnsbl(self, qname):
46        from adns import rr
47        for l, d in self.blacklists.items():
48            self.dnsbl_results[qname] = []
49            self.submit_reverse_any(qname, d.zone, rr.A,
50                                    callback=self.dnsbl_callback,
51                                    extra=l)
52
53    def dnsbl_callback(self, answer, qname, rr, flags, l):
54        if not answer[0]:
55            for addr in answer[3]:
56                self.dnsbl_results[qname].append( (
57                    self.blacklists[l].results.get(addr, "%s-%s"%(l,addr)),
58                    self.blacklists[l].getURL(qname)) )
59
60if __name__ == "__main__":
61    blacklists = [
62        DNSBL('ORDB', 'relays.ordb.org.',
63              'http://ordb.org/lookup?addr=%s',
64              {'127.0.0.2': 'ORDB'}),
65        DNSBL('DEVNULL', 'dev.null.dk.',
66              'http://fabel.dk/relay/test/index.epl?ip=%s&send=Check',
67              { '127.0.0.2': 'DEVNULL' }),
68        ]
69
70    s = DNSBLQueryEngine(blacklists=blacklists)
71    for i in sys.argv[1:]:
72        s.submit_dnsbl(i)
73    s.finish()
74    listed = s.dnsbl_results
75    for k, v in listed.items():
76        hits = []
77        for l, url in v: hits.append(l)
78        if len(listed) > 1:
79            print "%s: %s" % (k, string.join(hits))
80        else:
81            print string.join(hits)
82