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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
=head1 NAME
URILocalBL - blocklist URIs using local information (ISP names, address lists, and country codes)
=head1 SYNOPSIS
This plugin creates some new rule test types, such as "uri_block_cc",
"uri_block_cidr", and "uri_block_isp". These rules apply to the URIs
found in the HTML portion of a message, i.e. E<lt>a href=...E<gt> markup.
loadplugin Mail::SpamAssassin::Plugin::URILocalBL
Why local blocklisting? There are a few excellent, effective, and
well-maintained DNSBL's out there. But they have several drawbacks:
=over 2
=item * blocklists can cover tens of thousands of entries, and you can't select which ones you use;
=item * verifying that it's correctly configured can be non-trivial;
=item * new blocklisting entries may take a while to be detected and entered, so it's not instantaneous.
=back
Sometimes all you want is a quick, easy, and very surgical blocklisting of
a particular site or a particular ISP. This plugin is defined for that
exact usage case.
=head1 RULE DEFINITIONS AND PRIVILEGED SETTINGS
The format for defining a rule is as follows:
uri_block_cc SYMBOLIC_TEST_NAME cc1 cc2 cc3 cc4 ..
uri_block_cc SYMBOLIC_TEST_NAME !cc1 !cc2 ..
or:
uri_block_cont SYMBOLIC_TEST_NAME co1 co2 co3 co4 ..
uri_block_cont SYMBOLIC_TEST_NAME !co1 !co2 ..
or:
uri_block_cidr SYMBOLIC_TEST_NAME a.a.a.a b.b.b.b/cc
or:
uri_block_isp SYMBOLIC_TEST_NAME "Data Rancid" McCarrier Phishers-r-Us
Example rule for matching a URI in China:
uri_block_cc TEST1 cn
If you specify list of negations, such rule will match ANY country except
the listed ones (Finland, Sweden):
uri_block_cc TEST1 !fi !se
Continents uri_block_cont works exactly the same as uri_block_cc.
This would block the URL http://www.baidu.com/index.htm. Similarly, to
match a Spam-haven netblock:
uri_block_cidr TEST2 65.181.64.0/18
would match a netblock where several phishing sites were recently hosted.
And to block all CIDR blocks registered to an ISP, one might use:
uri_block_isp TEST3 "Data Rancid" ColoCrossing
Quote ISP names containing spaces.
Lastly, if there's a country that you want to block but there's an explicit
host you wish to exempt from that blocklist, you can use:
uri_block_exclude TEST1 www.baidu.com
if you wish to exempt URL's referring to this host. The same syntax is
applicable to CIDR and ISP blocks as well.
=head1 DEPENDENCIES
The Country-Code based filtering can use any Mail::SpamAssassin::GeoDB
supported module like MaxMind::DB::Reader (GeoIP2) or Geo::IP. ISP based
filtering might require a paid subscription database like GeoIPISP.
=cut
package Mail::SpamAssassin::Plugin::URILocalBL;
use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::Constants qw(:ip :sa);
use Mail::SpamAssassin::Util qw(untaint_var idn_to_ascii);
use Mail::SpamAssassin::NetSet;
use Socket;
use strict;
use warnings;
# use bytes;
use re 'taint';
our @ISA = qw(Mail::SpamAssassin::Plugin);
sub dbg { my $msg = shift; Mail::SpamAssassin::Plugin::dbg ("URILocalBL: $msg", @_); }
my $IP_ADDRESS = IP_ADDRESS;
my $RULENAME_RE = RULENAME_RE;
# constructor
sub new {
my $class = shift;
my $mailsaobject = shift;
# some boilerplate...
$class = ref($class) || $class;
my $self = $class->SUPER::new($mailsaobject);
bless ($self, $class);
$self->register_eval_rule("check_uri_local_bl");
$self->set_config($mailsaobject->{conf});
# we need GeoDB country/isp
$self->{main}->{geodb_wanted}->{country} = 1;
$self->{main}->{geodb_wanted}->{isp} = 1;
return $self;
}
sub set_config {
my ($self, $conf) = @_;
my @cmds;
push (@cmds, {
setting => 'uri_block_cc',
type => $Mail::SpamAssassin::Conf::CONF_TYPE_HASH_KEY_VALUE,
is_priv => 1,
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value !~ /^(${RULENAME_RE})\s+(.+?)\s*$/) {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
my $name = $1;
my $args = $2;
my @added;
foreach my $cc (split(/\s+/, uc($args))) {
# this should match all country codes including satellite providers
if ($cc =~ /^((\!)?([a-z][a-z0-9]))$/i) {
if (defined $2) {
$self->{urilocalbl}->{$name}{countries_neg} = 1;
$self->{urilocalbl}->{$name}{countries}{$3} = 0;
} else {
$self->{urilocalbl}->{$name}{countries}{$3} = 1;
}
push @added, $1;
} else {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
}
my %checkneg = map { $_ => 1 } values %{$self->{urilocalbl}->{$name}{countries}};
if (scalar keys %checkneg > 1) {
dbg("config: uri_block_cc $name failed: trying to combine negations and non-negations");
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
dbg("config: uri_block_cc $name added: ".join(' ', @added));
$self->{parser}->add_test($name, 'check_uri_local_bl()',
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->{parser}->{conf}->{priority}->{$name} = -100;
}
});
push (@cmds, {
setting => 'uri_block_cont',
type => $Mail::SpamAssassin::Conf::CONF_TYPE_HASH_KEY_VALUE,
is_priv => 1,
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value !~ /^(${RULENAME_RE})\s+(.+?)\s*$/) {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
my $name = $1;
my $args = $2;
my @added;
foreach my $cc (split(/\s+/, uc($args))) {
# this should match all continent codes
if ($cc =~ /^((\!)?([a-z]{2}))$/i) {
if (defined $2) {
$self->{urilocalbl}->{$name}{continents_neg} = 1;
$self->{urilocalbl}->{$name}{continents}{$3} = 0;
} else {
$self->{urilocalbl}->{$name}{continents}{$3} = 1;
}
push @added, $1;
} else {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
}
my %checkneg = map { $_ => 1 } values %{$self->{urilocalbl}->{$name}{continents}};
if (scalar keys %checkneg > 1) {
dbg("config: uri_block_cont $name failed: trying to combine negations and non-negations");
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
dbg("config: uri_block_cont $name added: ".join(' ', @added));
$self->{parser}->add_test($name, 'check_uri_local_bl()',
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->{parser}->{conf}->{priority}->{$name} = -100;
}
});
push (@cmds, {
setting => 'uri_block_isp',
type => $Mail::SpamAssassin::Conf::CONF_TYPE_HASH_KEY_VALUE,
is_priv => 1,
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value !~ /^(${RULENAME_RE})\s+(.+?)\s*$/) {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
my $name = $1;
my $args = $2;
my @added;
# gather up possibly quoted strings
while ($args =~ /("[^"]*"|(?<!")\S+(?!"))/g) {
my $isp = $1;
$isp =~ s/"//g;
my $ispkey = uc($isp); $ispkey =~ s/\s+//gs;
$self->{urilocalbl}->{$name}{isps}{$ispkey} = $isp;
push @added, "\"$isp\"";
}
if (!defined $self->{urilocalbl}->{$name}{isps}) {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
dbg("config: uri_block_isp $name added: ". join(', ', @added));
$self->{parser}->add_test($name, 'check_uri_local_bl()',
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->{parser}->{conf}->{priority}->{$name} = -100;
}
});
push (@cmds, {
setting => 'uri_block_cidr',
type => $Mail::SpamAssassin::Conf::CONF_TYPE_HASH_KEY_VALUE,
is_priv => 1,
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value !~ /^(${RULENAME_RE})\s+(.+?)\s*$/) {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
my $name = $1;
my $args = $2;
foreach my $addr (split(/\s+/, $args)) {
if ($addr =~ m!^$IP_ADDRESS(?:/\d{1,3})?$!o) {
$self->{urilocalbl}->{$name}{cidr}{$addr} = 1;
} else {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
}
$self->{parser}->add_test($name, 'check_uri_local_bl()',
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->{parser}->{conf}->{priority}->{$name} = -100;
}
});
push (@cmds, {
setting => 'uri_block_exclude',
type => $Mail::SpamAssassin::Conf::CONF_TYPE_HASH_KEY_VALUE,
is_priv => 1,
code => sub {
my ($self, $key, $value, $line) = @_;
if ($value !~ /^(${RULENAME_RE})\s+(.+?)\s*$/) {
return $Mail::SpamAssassin::Conf::INVALID_VALUE;
}
my $name = $1;
my $args = $2;
foreach my $arg (split(/\s+/, $args)) {
$self->{urilocalbl}->{$name}{exclusions}{lc($arg)} = 1;
}
$self->{parser}->add_test($name, 'check_uri_local_bl()',
$Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
$self->{parser}->{conf}->{priority}->{$name} = -100;
}
});
$conf->{parser}->register_commands(\@cmds);
}
sub finish_parsing_end {
my ($self, $opts) = @_;
my $conf = $opts->{conf};
# compile cidrs now
foreach my $rulename (keys %{$conf->{urilocalbl}}) {
my $ruleconf = $conf->{urilocalbl}->{$rulename};
next if defined $ruleconf->{netset};
next if !defined $ruleconf->{cidr};
my $netset = Mail::SpamAssassin::NetSet->new($rulename);
foreach my $addr (keys %{$ruleconf->{cidr}}) {
if ($netset->add_cidr($addr)) {
dbg("config: uri_block_cidr $rulename added: $addr");
} else {
dbg("config: uri_block_cidr $rulename add failed: $addr");
}
}
if ($netset->get_num_nets()) {
$ruleconf->{netset} = $netset;
}
}
}
sub check_uri_local_bl {
my ($self, $pms) = @_;
return 0 if $self->{urilocalbl_disabled};
if (!$self->{main}->{geodb} ||
(!$self->{main}->{geodb}->can('country') &&
!$self->{main}->{geodb}->can('isp'))) {
dbg("plugin disabled, GeoDB country/isp not available");
$self->{urilocalbl_disabled} = 1;
return 0;
}
my $rulename = $pms->get_current_eval_rule_name();
my $ruleconf = $pms->{conf}->{urilocalbl}->{$rulename};
dbg("running $rulename");
my %found_hosts;
foreach my $info (values %{$pms->get_uri_detail_list()}) {
next unless $info->{hosts};
# look for W3 links only
next unless defined $info->{types}->{a} || defined $info->{types}->{parsed};
my %hosts = %{$info->{hosts}}; # evade hash reset by copy
while (my($host, $domain) = each %hosts) {
if (defined $ruleconf->{exclusions}{lc($domain)}) {
dbg("excluded $host, domain $domain matches");
next;
}
elsif ($host =~ IS_IP_ADDRESS) {
if ($self->_check_host($pms, $rulename, $host, [$host])) {
# if hit, rule is done
return 0;
}
} else {
# do host lookups only after all IPs are checked, since they
# don't need resolving..
$found_hosts{$host} = 1;
}
}
}
return 0 unless %found_hosts;
# bail out now if dns not available
return 0 if !$pms->is_dns_available();
my $queries;
foreach my $host (keys %found_hosts) {
$host = idn_to_ascii($host);
dbg("launching A/AAAA lookup for $host");
# launch dns
my $ret = $pms->{async}->bgsend_and_start_lookup($host, 'A', undef,
{ rulename => $rulename, host => $host, type => 'URILocalBL' },
sub { my($ent, $pkt) = @_; $self->_finish_lookup($pms, $ent, $pkt); },
master_deadline => $pms->{master_deadline}
);
$queries++ if defined $ret;
# also IPv6 if database supports
if ($self->{main}->{geodb}->can('country_v6')) {
$ret = $pms->{async}->bgsend_and_start_lookup($host, 'AAAA', undef,
{ rulename => $rulename, host => $host, type => 'URILocalBL' },
sub { my($ent, $pkt) = @_; $self->_finish_lookup($pms, $ent, $pkt); },
master_deadline => $pms->{master_deadline}
);
$queries++ if defined $ret;
}
}
return 0 if !$queries; # no query started
return; # return undef for async status
}
sub _finish_lookup {
my ($self, $pms, $ent, $pkt) = @_;
my $rulename = $ent->{rulename};
my $host = $ent->{host};
# Skip duplicate A / AAAA matches
return if $pms->{urilocalbl_finished}->{$rulename};
if (!$pkt) {
# $pkt will be undef if the DNS query was aborted (e.g. timed out)
dbg("host lookup failed: $rulename $host");
return;
}
$pms->rule_ready($rulename); # mark rule ready for metas
my @answer = $pkt->answer;
my @addrs;
foreach my $rr (@answer) {
if ($rr->type eq 'A' || $rr->type eq 'AAAA') {
push @addrs, $rr->address;
}
}
if (@addrs) {
if ($self->_check_host($pms, $rulename, $host, \@addrs)) {
$pms->{urilocalbl_finished}->{$rulename} = 1;
}
}
}
sub _check_host {
my ($self, $pms, $rulename, $host, $addrs) = @_;
my $ruleconf = $pms->{conf}->{urilocalbl}->{$rulename};
my $geodb = $self->{main}->{geodb};
if ($host ne $addrs->[0]) {
dbg("resolved $host: ".join(', ', @$addrs));
}
foreach my $ip (@$addrs) {
if (defined $ruleconf->{exclusions}{$ip}) {
dbg("excluded $host, IP $ip matches");
return 1;
}
}
if (defined $ruleconf->{countries}) {
my $neg = defined $ruleconf->{countries_neg};
my $testcc = join(' ', sort keys %{$ruleconf->{countries}});
if ($neg) {
dbg("checking $host for any country except: $testcc");
} else {
dbg("checking $host for countries: $testcc");
}
foreach my $ip (@$addrs) {
my $cc = $geodb->get_country($ip);
if ( (!$neg && defined $ruleconf->{countries}{$cc}) ||
($neg && !defined $ruleconf->{countries}{$cc}) ) {
dbg("$host ($ip) country $cc - HIT");
$pms->test_log("Host: $host in country $cc", $rulename);
$pms->got_hit($rulename, "");
return 1;
} else {
dbg("$host ($ip) country $cc - ".($neg ? "excluded" : "no match"));
}
}
}
if (defined $ruleconf->{continents}) {
my $neg = defined $ruleconf->{continents_neg};
my $testcont = join(' ', sort keys %{$ruleconf->{continents}});
if ($neg) {
dbg("checking $host for any continent except: $testcont");
} else {
dbg("checking $host for continents: $testcont");
}
foreach my $ip (@$addrs) {
my $cc = $geodb->get_continent($ip);
if ( (!$neg && defined $ruleconf->{continents}{$cc}) ||
($neg && !defined $ruleconf->{continents}{$cc}) ) {
dbg("$host ($ip) continent $cc - HIT");
$pms->test_log("Host: $host in continent $cc", $rulename);
$pms->got_hit($rulename, "");
return 1;
} else {
dbg("$host ($ip) continent $cc - ".($neg ? "excluded" : "no match"));
}
}
}
if (defined $ruleconf->{isps}) {
if ($geodb->can('isp')) {
my $testisp = join(', ', map {"\"$_\""} sort values %{$ruleconf->{isps}});
dbg("checking $host for isps: $testisp");
foreach my $ip (@$addrs) {
my $isp = $geodb->get_isp($ip);
next unless defined $isp;
my $ispkey = uc($isp); $ispkey =~ s/\s+//gs;
if (defined $ruleconf->{isps}{$ispkey}) {
dbg("$host ($ip) isp \"$isp\" - HIT");
$pms->test_log("Host: $host in isp $isp", $rulename);
$pms->got_hit($rulename, "");
return 1;
} else {
dbg("$host ($ip) isp $isp - no match");
}
}
} else {
dbg("skipping ISP check, GeoDB database not loaded");
}
}
if (defined $ruleconf->{netset}) {
foreach my $ip (@$addrs) {
if ($ruleconf->{netset}->contains_ip($ip)) {
dbg("$host ($ip) matches cidr - HIT");
$pms->test_log("Host: $host in cidr", $rulename);
$pms->got_hit($rulename, "");
return 1;
} else {
dbg("$host ($ip) not matching cidr");
}
}
}
return 0;
}
1;
|