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