| 1 | |||
| 2 | package SyslogScan::Daemon::SpamDetector::BadAddr; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Plugins; | ||
| 8 | use Plugins::SimpleConfig; | ||
| 9 | use SyslogScan::Daemon::SpamDetector::Plugin; | ||
| 10 | use Tie::Cache::LRU; | ||
| 11 | use Net::Netmask; | ||
| 12 | |||
| 13 | our(@ISA) = qw(SyslogScan::Daemon::SpamDetector::Plugin); | ||
| 14 | |||
| 15 | our $ipcachesize = 10_000; | ||
| 16 | |||
| 17 | my %defaults = ( | ||
| 18 | debug => 0, | ||
| 19 | configfile => '', | ||
| 20 | ipcachesize => sub { | ||
| 21 | my ($pkgself, $key, $value) = @_; | ||
| 22 | if (ref($pkgself)) { | ||
| 23 | $pkgself->{ipcachesize} = $value; | ||
| 24 | if ($pkgself->{ipcache}) { | ||
| 25 | my $t = tied(%{$pkgself->{ipcache}}); | ||
| 26 | $t->max_size($value); | ||
| 27 | } | ||
| 28 | } else { | ||
| 29 | $ipcachesize = $value; | ||
| 30 | } | ||
| 31 | }, | ||
| 32 | ignoreip => '', | ||
| 33 | every => 3, | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub config_prefix { 'badaddr_' } | ||
| 37 | |||
| 38 | sub parse_config_line { simple_config_line(\%defaults, @_); } | ||
| 39 | |||
| 40 | sub new | ||
| 41 | { | ||
| 42 | my $self = simple_new(\%defaults, @_); | ||
| 43 | $self->{ipcache} = {}; | ||
| 44 | die if ref($self->{ipcachesize}); | ||
| 45 | tie %{$self->{ipcache}}, 'Tie::Cache::LRU', $self->{ipcachesize} || $ipcachesize; | ||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub preconfig | ||
| 50 | { | ||
| 51 | my ($self, $ssd_configfile) = @_; | ||
| 52 | |||
| 53 | $self->set_api($ssd_configfile, | ||
| 54 | process_badaddr_match => {}, | ||
| 55 | is_ourip => { first_defined => 1}, | ||
| 56 | ); | ||
| 57 | |||
| 58 | $self->{ourip} = {}; | ||
| 59 | if ($self->{ignoreip} && -s $self->{ignoreip}) { | ||
| 60 | open(IGNORE, "<$self->{ignoreip}") || die "open $self->{ignoreip}: $!"; | ||
| 61 | while(<IGNORE>) { | ||
| 62 | next if /^#/; | ||
| 63 | next if /^$/; | ||
| 64 | chomp; | ||
| 65 | my $block = Net::Netmask->new2($_); | ||
| 66 | unless ($block) { | ||
| 67 | warn "could not parse network block $_ in $self->{ignoreip} line $.: $Net::Netmask::error\n"; | ||
| 68 | next; | ||
| 69 | } | ||
| 70 | $block->storeNetblock($self->{ourip}); | ||
| 71 | } | ||
| 72 | close(IGNORE); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub parse_logs | ||
| 77 | { | ||
| 78 | my ($self, $logfile, $rx) = @_; | ||
| 79 | |||
| 80 | for my $plugin (@{$self->{logs}{$logfile}{$rx}}) { | ||
| 81 | my %info = $plugin->invoke('parse_logs', $logfile, $rx); | ||
| 82 | next unless %info; | ||
| 83 | $self->process_badaddr_match(%info); | ||
| 84 | |||
| 85 | } | ||
| 86 | return (); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub process_badaddr_match | ||
| 90 | { | ||
| 91 | my ($self, %info) = @_; | ||
| 92 | |||
| 93 | if ($self->{myapi}->is_ourip($info{ip})) { | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $filter = $self->{plugins}->invoke_until('filter', | ||
| 98 | sub { defined($_[0]) }, | ||
| 99 | %info, | ||
| 100 | status => 'badaddr'); | ||
| 101 | |||
| 102 | if (defined $filter and ! $filter) { | ||
| 103 | print "BA FILTERED: $info{status} $info{match}\n" if $self->{debug} > 2; | ||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | if ((++$self->{ipcache}{$info{ip}} % $self->{every}) == 0) { | ||
| 108 | print "Reporting as spam $self->{ipcache}{$info{ip}} unknown users from $info{ip}\n" if $self->{debug}; | ||
| 109 | $self->{api}->process_spam_match( | ||
| 110 | %info, | ||
| 111 | status => 'spam', | ||
| 112 | score => "$self->{ipcache}{$info{ip}} unknown users", | ||
| 113 | ); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | print "Not yet time to report $info{ip} for unknown users ($self->{ipcache}{$info{ip}})\n" if $self->{debug}; | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub is_ourip | ||
| 121 | { | ||
| 122 | my ($self, $ip) = @_; | ||
| 123 | if (findNetblock($ip, $self->{ourip})) { | ||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | sub periodic | ||
| 131 | { | ||
| 132 | my ($self) = @_; | ||
| 133 | $self->{plugins}->invoke('periodic'); | ||
| 134 | } | ||
| 135 | |||
| 136 | package SyslogScan::Daemon::SpamDetector::BadAddr::Plugin; | ||
| 137 | |||
| 138 | use Plugins::Plugin; | ||
| 139 | use strict; | ||
| 140 | use warnings; | ||
| 141 | |||
| 142 | our @ISA = qw(SyslogScan::Daemon::SpamDetector::Plugin); | ||
| 143 | |||
| 144 | sub periodic {} | ||
| 145 | sub spam_found {} | ||
| 146 | sub ham_found {} | ||
| 147 | |||
| 148 | 1; | ||
| 149 | |||
| 150 | =head1 NAME | ||
| 151 | |||
| 152 | SyslogScan::Daemon::SpamDetector::BadAddr - notice mail sent to non-existant addressess | ||
| 153 | |||
| 154 | =head1 SYNOPSIS | ||
| 155 | |||
| 156 | plugin SyslogScan::Daemon::SpamDetector as sd_ | ||
| 157 | |||
| 158 | sd_plugin SyslogScan::Daemon::SpamDetector::BadAddr as bad_ | ||
| 159 | debug 0 | ||
| 160 | every 3 | ||
| 161 | ipcachesize 10000 | ||
| 162 | ignoreip /etc/postfix/ourip | ||
| 163 | |||
| 164 | =head1 DESCRIPTION | ||
| 165 | |||
| 166 | Watch the system log files for messages that are addressed to non-existant addresses. | ||
| 167 | |||
| 168 | This plugin requires mailer-specific plugins to help it. | ||
| 169 | |||
| 170 | =head1 CONFIGURATION PARAMETERS | ||
| 171 | |||
| 172 | The following configuration parameters are supported: | ||
| 173 | |||
| 174 | =over 4 | ||
| 175 | |||
| 176 | =item debug | ||
| 177 | |||
| 178 | Debugging on (1) or off (0). | ||
| 179 | |||
| 180 | =item every | ||
| 181 | |||
| 182 | This module is being used in the context of noticing spam. We will consider | ||
| 183 | every Nth such mis-addressed message to be a spam. This parameter says | ||
| 184 | what N is. (Default: 3) | ||
| 185 | |||
| 186 | =item ipcachesize | ||
| 187 | |||
| 188 | Since we wait for N (default 3) bad addresses from a host before we count it | ||
| 189 | as a spam, we must cache the IP addresses and message counts. This parameter | ||
| 190 | sets the cache size. (Default 10,000). | ||
| 191 | |||
| 192 | =item ignoreip | ||
| 193 | |||
| 194 | This parameter provides a filename to look in for a list of IP addresses or | ||
| 195 | blocks that should be ignored. (No default) | ||
| 196 | |||
| 197 | =back | ||
| 198 | |||
| 199 | =head1 SEE ALSO | ||
| 200 | |||
| 201 | L<SyslogScan::Daemon::SpamDetector> | ||
| 202 | |||
| 203 | =head1 THANK THE AUTHOR | ||
| 204 | |||
| 205 | If you need high-speed internet services (T1, T3, OC3 etc), please | ||
| 206 | send me your request-for-quote. I have access to very good pricing: | ||
| 207 | you'll save money and get a great service. | ||
| 208 | |||
| 209 | =head1 LICENSE | ||
| 210 | |||
| 211 | Copyright(C) 2006 David Muir Sharnoff <muir@idiom.com>. | ||
| 212 | This module may be used and distributed on the same terms | ||
| 213 | as Perl itself. | ||
| 214 |