Edgewall Software

source: plugins/1.0/spam-filter/tracspamfilter/filters/ip_blacklist.py

Last change on this file was 15475, checked in by Ryan J Ollos, 7 years ago

1.0.10dev: Remove blacklist server that is blacklisting good users

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2015 Edgewall Software
4# Copyright (C) 2006 Matthew Good <trac@matt-good.net>
5# Copyright (C) 2015 Dirk Stöcker <trac@dstoecker.de>
6# All rights reserved.
7#
8# This software is licensed as described in the file COPYING, which
9# you should have received as part of this distribution. The terms
10# are also available at http://trac.edgewall.com/license.html.
11#
12# This software consists of voluntary contributions made by many
13# individuals. For the exact contribution history, see the revision
14# history and logs, available at http://projects.edgewall.com/trac/.
15#
16# Author: Matthew Good <trac@matt-good.net>
17
18from dns.ipv6 import inet_aton as ipv6_inet_aton
19from dns.name import from_text
20from dns.resolver import NXDOMAIN, NoAnswer, NoNameservers, Timeout, query
21
22from trac.config import ListOption, IntOption
23from trac.core import Component, implements
24
25from tracspamfilter.api import IFilterStrategy, N_
26
27
28class IPBlacklistFilterStrategy(Component):
29 """Spam filter based on IP blacklistings.
30
31 Requires the dnspython module from http://www.dnspython.org/.
32 """
33 implements(IFilterStrategy)
34
35 karma_points = IntOption('spam-filter', 'ip_blacklist_karma', '5',
36 """By how many points blacklisting by a single server impacts the
37 overall karma of a submission.""", doc_domain='tracspamfilter')
38
39 servers_default = 'all.s5h.net, dnsbl.dronebl.org, rbl.blockedservers.com'
40 servers = ListOption('spam-filter', 'ip_blacklist_servers',
41 servers_default, doc="Servers used for IPv4 blacklisting.",
42 doc_domain='tracspamfilter')
43
44 servers6_default = 'all.s5h.net, dnsbl.dronebl.org, ' \
45 'bl.ipv6.spameatingmonkey.net'
46 servers6 = ListOption('spam-filter', 'ip6_blacklist_servers',
47 servers6_default, doc="Servers used for IPv6 blacklisting.",
48 doc_domain='tracspamfilter')
49
50 # IFilterStrategy implementation
51
52 def is_external(self):
53 return True
54
55 def test(self, req, author, content, ip):
56 if self.karma_points == 0:
57 return
58
59 serverlist, prefix = self._getdata(ip)
60 if not serverlist:
61 self.log.warning("No IP blacklist servers configured")
62 return
63
64 self.log.debug('Checking for IP blacklisting on "%s"', ip)
65
66 points = 0
67 servers = []
68
69 for server in serverlist:
70 self.log.debug("Checking blacklist %s for %s [%s]", server, ip,
71 prefix)
72 try:
73 res = query(from_text(prefix +
74 server.encode('utf-8')))[0].to_text()
75 points -= abs(self.karma_points)
76 if res == '127.0.0.1':
77 servers.append(server)
78 else:
79 # strip the common part of responses
80 if res.startswith('127.0.0.'):
81 res = res[8:]
82 elif res.startswith('127.'):
83 res = res[4:]
84 servers.append('%s [%s]' % (server, res))
85 except NXDOMAIN: # not blacklisted on this server
86 continue
87 except (Timeout, NoAnswer, NoNameservers), e:
88 self.log.warning('Error checking IP blacklist server "%s" '
89 'for IP "%s": %s', server, ip, e)
90
91 if points != 0:
92 return (points, N_("IP %s blacklisted by %s"), ip,
93 ', '.join(servers))
94
95 def train(self, req, author, content, ip, spam=True):
96 return 0
97
98 # Internal methods
99
100 def _getdata(self, ip):
101 if ip.find(".") < 0:
102 encoded = reversed(list(ipv6_inet_aton(ip).encode('hex_codec')))
103 return self.servers6, '.'.join(encoded) + '.'
104 return self.servers, '.'.join(reversed(ip.split('.'))) + '.'
Note: See TracBrowser for help on using the repository browser.