lib/Mail/Decency/Policy/Honeypot.pm

filter: Statementstmt 26.1% 23 / 88 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 36.3% 4 / 11 Podpod 100% 5 / 5 totaltotal 18.3% 32 / 174 CC 30 SCAR 46.6
CC
30
Cov
16%
init
210.0
handle
110.0
client_blacklisted
30.0
CRAP 106.2
« lib/Mail/Decency/ContentFilter/Bogofilter.pm ↑ Summary lib/Mail/Decency/Core/Excludes.pm »
1 package Mail::Decency::Policy::Honeypot;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends qw/
5 Mail::Decency::Policy::Core
6 /;
7 1 with qw/
8 Mail::Decency::Core::Meta::Database
9 Mail::Decency::Core::Meta::Maintenance
10 /;
11  
12 1 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Mail::Decency::Helper::IP qw/ is_local_host /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Mail::Decency::Policy::Honeypot
20  
21 =head1 CONFIG
22  
23 ---
24
25 disable: 0
26
27 # attention: enabling this is a good idea if you want thos obvious
28 # spammy mails for later training with your spam filters.
29 # however, if you forget to handle the content filter side with
30 # the Honeycollect module, the mails might just pass to where they
31 # should not: the recipient.
32 pass_for_collection: 1
33
34
35 # time to live .. for maintenance
36 maintenance_ttl: 14d
37
38 # list of addresses
39 addresses:
40 # all incoming mails to those recipients go directly to the blacklist
41 - some@address.tld
42 - another@domain.tld
43
44 # list of domains used for building the blacklist
45 domains:
46
47 # all incoming mails for this recipient domain
48 - spamlover.tld
49
50 # use the whoe domain, but some real existing users
51 -
52 domain: somedomain.tld
53 exceptions:
54 - realuser
55 - anotheremail
56
57  
58 =head1 DESCRIPTION
59  
60 Honeypot facility. All hosts sending mails to a list of provided recipient emails and/or domains will be rejected and added to a blacklist.
61 Later on, this blacklist will be used for rejected any other mails
62  
63 =head1 DATABASE
64  
65 CREATE TABLE honeypot_client_address (
66 id INTEGER PRIMARY KEY,
67 client_address varchar( 39 ),
68 created INTEGER
69 );
70 CREATE UNIQUE INDEX honeypot_client_address_uk ON honeypot_client_address( client_address );
71 CREATE INDEX honeypot_client_created_idx ON honeypot_client_address( created );
72  
73 =head1 CLASS ATTRIBUTES
74  
75  
76 =head2 addresses : HashRef[Bool]
77  
78 List of addresses used as honeypot targets
79  
80 =cut
81  
82 1 has addresses => ( is => 'rw', isa => 'HashRef[Bool]', predicate => 'has_addresses' );
83  
84 =head2 domains : HashRef[Bool]
85  
86 List of (FULL) domains used as honeypot targets
87  
88 =cut
89  
90 1 has domains => ( is => 'rw', isa => 'HashRef', predicate => 'has_domains' );
91  
92 =head2 reject_message : Str
93  
94 Reject message, if an IP was already on the honeypot blacklist.
95  
96 Default: "Your host ip is blacklisted"
97  
98 =cut
99  
100 1 has reject_message => ( is => 'rw', isa => 'Str', default => 'Your host ip is blacklisted.' );
101  
102 =head2 welcome_message : Str
103  
104 Reject message, which will be thrown if a new IP is welcomed on the blacklist.
105  
106 Default: "The honey has been served."
107  
108 =cut
109  
110 1 has welcome_message => ( is => 'rw', isa => 'Str', default => 'The honey has been served.' );
111  
112 =head2 negative_cache : Bool
113  
114 If enabled: negative answers (not on blacklist) will be stored, too.
115  
116 =cut
117  
118 1 has negative_cache => ( is => 'rw', isa => 'Bool', default => 1 );
119  
120 =head2 pass_for_collection : Bool
121  
122 If enabled: Do not reject honeypot mails, but flag them so that they can be collected via L<Mail::Decency::ContentFilter::HoneyCollector>
123  
124 =cut
125  
126 1 has pass_for_collection => ( is => 'rw', isa => 'Bool', default => 0 );
127  
128 =head2 schema_definition : HashRef[Bool]
129  
130 List of addresses used as honeyport targets
131  
132 =cut
133  
134 0 ▶has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 {
136 0 honeypot => {
137 addresses => {
138 client_address => [ varchar => 39 ],
139 created => 'integer',
140 -unique => [ 'client_address' ],
141 -index => [ 'created' ]
142 },
143 }
144 };
145 1 } );
146  
147  
148 =head1 METHODS
149  
150  
151 =head2 init
152  
153 =cut
154  
155 0 ▶sub init {
Subroutine init: not called CC=14 SCAR=53.5
156 0 my ( $self ) = @_;
157
158 die "Require either addresses or domains to run!\n"
159 0 ▶ unless $self->config->{ addresses } || $self->config->{ domains };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"addresses"} or $self->config->{"domains"} 0 0 0
Condition0%0/3
$self->config->{"addresses"} or $self->config->{"domains"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->config->{"addresses"} or $self->config->{"domains"}
$self->config->{"addresses"} $self->config->{"domains"}
Truth table0%0/3
A or B
A= $self->config->{"addresses"}B= $self->config->{"domains"}
ABresult
1X1
011
000
160
161 # init addresses
162 0 ▶ if ( $self->config->{ addresses } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"addresses"}) 0 0 0
163 0 $self->addresses( { map { ( $_ => 1 ) } @{ $self->config->{ addresses } } } );
164 }
165
166 # init domains
167 0 ▶ if ( $self->config->{ domains } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"domains"}) 0 0 0
168 0 $self->domains( {} );
169
170 0 my $count = 1;
171 0 foreach my $ref( @{ $self->config->{ domains } } ) {
172
173 # having hashref -> using exceptions
174 0 ▶ if ( ref( $ref ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref) { } 0 0 0
175 die "Missing 'domain' in domain $count\n"
176 0 ▶ unless $ref->{ domain };
Branch: 1 branch
BranchTrueFalseTotal
unless $ref->{"domain"} 0 0 0
177 $self->domains->{ $ref->{ domain } } = { map {
178 0 ( $_ => 1 );
179 0 ▶ } @{ $ref->{ exceptions } || [] } };
Condition0%0/2
$ref->{'exceptions'} || []
l!l
00
MC/DC0%0/1
$ref->{'exceptions'} || []
$ref->{'exceptions'}
Truth table0%0/2
A
A= $ref->{'exceptions'}
Aresult
11
00
180 }
181
182 # being scalar -> full domain
183 else {
184 0 $self->domains->{ $ref }++;
185 }
186 0 $count++;
187 }
188 }
189
190 # disable negative cache ?
191 $self->negative_cache( 0 )
192 0 ▶ if defined $self->config->{ negative_cache } && ! $self->config->{ negative_cache };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->config->{"negative_cache"} and not $self->config->{"negative_cache"} 0 0 0
Condition0%0/3
defined $self->config->{"negative_cache"} and not $self->config->{"negative_cache"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->config->{"negative_cache"} and not $self->config->{"negative_cache"}
defined $self->config->{"negative_cache"} not $self->config->{"negative_cache"}
Truth table0%0/3
A and not(B)
A= defined $self->config->{"negative_cache"}B= not $self->config->{"negative_cache"}
ABresult
0X0
100
111
193
194 # enable passing for collecting later on ?
195 $self->pass_for_collection( 1 )
196 0 ▶ if $self->config->{ pass_for_collection };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"pass_for_collection"} 0 0 0
197
198 # set messages
199 0 foreach my $message( qw/ reject_message welcome_message / ) {
200 $self->$message( $self->config->{ $message } )
201 0 ▶ if $self->config->{ $message };
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{$message} 0 0 0
202 }
203
204 }
205  
206  
207 =head2 handle
208  
209 =cut
210  
211 0 ▶sub handle {
Subroutine handle: not called CC=10 SCAR=47.0
Pod documented
212 0 my ( $self, $server, $attrs_ref ) = @_;
213
214 # don bother with loopback addresses! EVEN IF ENABLED BY FORCE!
215 0 ▶ return if is_local_host( $attrs_ref->{ client_address } );
Branch: 1 branch
BranchTrueFalseTotal
if is_local_host($attrs_ref->{"client_address"}) 0 0 0
216
217 # ist sender blacklisted
218 0 ▶ if ( my $cached_ref = $self->client_blacklisted( $attrs_ref->{ client_address } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached_ref = $self->client_blacklisted($attrs_ref->{"client_address"})) 0 0 0
219
220 0 $self->go_final_state( REJECT => $self->welcome_message );
221 }
222
223 # being on domains list ?
224 0 ▶ if ( $self->has_domains && defined( my $ref = $self->domains->{ $attrs_ref->{ recipient_domain } } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_domains and defined(my $ref = $self->domains->{$attrs_ref->{"recipient_domain"}})) 0 0 0
Condition0%0/3
$self->has_domains and defined(my $ref = $self->domains->{$attrs_ref->{"recipient_domain"}})
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_domains and defined(my $ref = $self->domains->{$attrs_ref->{"recipient_domain"}})
$self->has_domains defined(my $ref = $self->domains->{$attrs_ref->{"recipient_domain"}})
Truth table0%0/3
A and B
A= $self->has_domainsB= defined(my $ref = $self->domains->{$attrs_ref->{"recipient_domain"}})
ABresult
0X0
100
111
225
226 # having exceptions
227 0 ▶ if ( !ref( $ref ) || ! defined $ref->{ $attrs_ref->{ recipient_prefix } } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref and defined $$ref{$attrs_ref->{"recipient_prefix"}}) 0 0 0
Condition0%0/3
ref $ref and defined $$ref{$attrs_ref->{"recipient_prefix"}}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ref and defined $$ref{$attrs_ref->{"recipient_prefix"}}
ref $ref defined $$ref{$attrs_ref->{"recipient_prefix"}}
Truth table0%0/3
A and B
A= ref $refB= defined $$ref{$attrs_ref->{"recipient_prefix"}}
ABresult
0X0
100
111
228
229 # set final state (throws finalte state excetpion)
230 0 $self->add_to_blacklist( $attrs_ref );
231
232 # set final state (throws exception)
233 0 ▶ if ( $self->pass_for_collection ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pass_for_collection) { } 0 0 0
234 0 $self->go_final_state( 'OK' );
235 }
236 else {
237 0 $self->go_final_state( REJECT => $self->welcome_message );
238 }
239 }
240 }
241
242 # being on addresses list ?
243 0 ▶ if ( $self->has_addresses && defined $self->addresses->{ $attrs_ref->{ recipient_address } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_addresses and defined $self->addresses->{$attrs_ref->{"recipient_address"}}) 0 0 0
Condition0%0/3
$self->has_addresses and defined $self->addresses->{$attrs_ref->{"recipient_address"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_addresses and defined $self->addresses->{$attrs_ref->{"recipient_address"}}
$self->has_addresses defined $self->addresses->{$attrs_ref->{"recipient_address"}}
Truth table0%0/3
A and B
A= $self->has_addressesB= defined $self->addresses->{$attrs_ref->{"recipient_address"}}
ABresult
0X0
100
111
244 0 $self->add_to_blacklist( $attrs_ref );
245
246 # set final state (throws exception)
247 0 $self->go_final_state( REJECT => $self->welcome_message );
248 }
249
250 # not found
251 0 return ;
252 }
253  
254  
255 =head2 client_blacklisted
256  
257 Check wheter client is blacklisted.. first in cache, then in database
258  
259 =cut
260  
261 0 ▶sub client_blacklisted {
Subroutine client_blacklisted: not called CC=5 SCAR=34.0
Pod documented
262 0 my ( $self, $address ) = @_;
263
264 # try cache
265 0 my $cache_name = 'Honeypot-'. $address;
266 0 my $cached = $self->cache->get( $cache_name );
267 0 ▶ if ( $cached ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) 0 0 0
268 0 return $cached eq 'USEME';
269 }
270
271 # try database
272 0 my $res = $self->database->get( honeypot => addresses => {
273 client_address => $address
274 } );
275 0 ▶ if ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
276
277 # refresh entry ??!
278 0 ▶ if ( $res->{ created } + $self->maintenance_ttl < time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"created"} + $self->maintenance_ttl < time) 0 0 0
279 0 $self->add_to_blacklist( {
280 client_address => $address
281 } );
282 }
283
284 # save to cache
285 0 $self->cache->set( $cache_name => "USEME" );
286 0 return 1;
287 }
288
289 # write negative cache also!!
290 0 ▶ $self->cache->set( $cache_name => "IGNORE" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->negative_cache 0 0 0
291 unless $self->negative_cache;
292
293 0 return 0;
294 }
295  
296 =head2 add_to_blacklist
297  
298 Add some ip to the blacklist
299  
300 =cut
301  
302 0 ▶sub add_to_blacklist {
Subroutine add_to_blacklist: not called CC=1 SCAR=6.9
Pod documented
303 0 my ( $self, $attrs_ref ) = @_;
304 $self->database->set( honeypot => addresses => {
305 client_address => $attrs_ref->{ client_address },
306 }, {
307 0 created => time()
308 } );
309 0 return;
310 }
311  
312  
313 =head2 go_final_state
314  
315 Overwrite parent mehtod, go only in a final reject state if "pass_for_collection" is 0, otherwise go in final accept, but set flag (for Honeycollector in content filters..)
316  
317 =cut
318  
319 0 ▶sub go_final_state {
Subroutine go_final_state: not called CC=2 SCAR=17.9
Pod documented
320 0 my ( $self, $state ) = @_;
321 0 ▶ if ( $self->pass_for_collection ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pass_for_collection) { } 0 0 0
322 0 $self->set_flag( 'honey' );
323 0 $self->next::method( "PREPEND" );
324 }
325 else {
326 0 $self->next::method( $state => $self->welcome_message );
327 }
328 }
329  
330  
331  
332  
333  
334 =head2 maintenance
335  
336 Called by policy server in maintenance mode. Cleans up outdated entries in honeypot database
337  
338 =cut
339  
340 0 ▶sub maintenance {
Subroutine maintenance: not called CC=3 SCAR=24.8
Pod documented
341 0 my ( $self ) = @_;
342 0 my $obsolete_time = time() - $self->maintenance_ttl;
343 0 ▶ while ( my ( $schema, $tables_ref ) = each %{ $self->schema_definition } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->schema_definition;}) 0 0 0
344 0 ▶ while ( my ( $table, $ref ) = each %{ $tables_ref } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$tables_ref;}) 0 0 0
345 0 $self->database->remove( $schema => $table => {
346 last_seen => {
347 '<' => $obsolete_time
348 }
349 } );
350 }
351 }
352 }
353  
354 =head1 AUTHOR
355  
356 Ulrich Kautz <uk@fortrabbit.de>
357  
358 =head1 COPYRIGHT
359  
360 Copyright (c) 2010 the L</AUTHOR> as listed above
361  
362 =head1 LICENCSE
363  
364 This library is free software and may be distributed under the same terms as perl itself.
365  
366 =cut
367  
368  
369  
370  
371 1 1;
« lib/Mail/Decency/ContentFilter/Bogofilter.pm ↑ Summary lib/Mail/Decency/Core/Excludes.pm »