forked from bigio/spamassassin-esp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsp.pm
462 lines (358 loc) · 12.4 KB
/
Esp.pm
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
# <@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>
# Author: Giovanni Bechis <gbechis@apache.org>
=head1 NAME
Esp - checks ESP abused accounts
=head1 SYNOPSIS
loadplugin Mail::SpamAssassin::Plugin::Esp
=head1 DESCRIPTION
This plugin checks emails coming from ESP abused accounts.
=cut
package Mail::SpamAssassin::Plugin::Esp;
use strict;
use warnings;
use Errno qw(EBADF);
use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::PerMsgStatus;
use vars qw(@ISA);
our @ISA = qw(Mail::SpamAssassin::Plugin);
my $VERSION = 1.2.1;
sub dbg { Mail::SpamAssassin::Plugin::dbg ("Esp: @_"); }
sub new {
my $class = shift;
my $mailsaobject = shift;
$class = ref($class) || $class;
my $self = $class->SUPER::new($mailsaobject);
bless ($self, $class);
$self->set_config($mailsaobject->{conf});
$self->register_eval_rule('esp_sendgrid_check_domain', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
$self->register_eval_rule('esp_sendgrid_check_id', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
$self->register_eval_rule('esp_sendgrid_check', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
$self->register_eval_rule('esp_sendinblue_check', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
$self->register_eval_rule('esp_mailup_check', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
$self->register_eval_rule('esp_maildome_check', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
$self->register_eval_rule('esp_mailchimp_check', $Mail::SpamAssassin::Conf::TYPE_HEAD_EVALS);
return $self;
}
=head1 SYNOPSIS
loadplugin Mail::SpamAssassin::Plugin::Esp Esp.pm
ifplugin Mail::SpamAssassin::Plugin::Esp
sendgrid_feed /etc/mail/spamassassin/sendgrid-id-dnsbl.txt,/etc/mail/spamassassin/sendgrid-id-local.txt
sendgrid_domains_feed /etc/mail/spamassassin/sendgrid-envelopefromdomain-dnsbl.txt
header SPBL_SENDGRID eval:esp_sendgrid_check()
describe SPBL_SENDGRID Message from Sendgrid abused account
endif
Usage:
esp_mailchimp_check()
Checks for Mailchimp abused accounts
esp_maildome_check()
Checks for Maildome abused accounts
esp_mailup_check()
Checks for Mailup abused accounts
esp_sendindblue_check()
Checks for Sendinblue abused accounts
esp_sendgrid_check()
Checks for Sendgrid abused accounts (both id and domains)
esp_sendgrid_check_id()
Checks for Sendgrid id abused accounts
esp_sendgrid_check_domain()
Checks for Sendgrid domains abused accounts
=head1 ADMINISTRATOR SETTINGS
=over 4
=item sendgrid_feed [...]
A list of files with all abused Sendgrid accounts.
Files can be separated by a comma.
More info at https://www.invaluement.com/serviceproviderdnsbl/.
Data file can be downloaded from https://www.invaluement.com/spdata/sendgrid-id-dnsbl.txt.
=item sendgrid_domains_feed [...]
A list of files with abused domains managed by Sendgrid.
Files can be separated by a comma.
More info at https://www.invaluement.com/serviceproviderdnsbl/.
Data file can be downloaded from https://www.invaluement.com/spdata/sendgrid-envelopefromdomain-dnsbl.txt.
=item sendinblue_feed [...]
A list of files with abused Sendinblue accounts.
Files can be separated by a comma.
=item mailup_feed [...]
A list of files with abused Mailup accounts.
Files can be separated by a comma.
=item maildome_feed [...]
A list of files with abused Maildome accounts.
Files can be separated by a comma.
=item mailchimp_feed [...]
A list of files with abused Mailchimp accounts.
Files can be separated by a comma.
=back
=head1 TEMPLATE TAGS
=over
The plugin sets some tags when a rule match, those tags can be used to use direct queries against rbl.
If direct queries are used the main rule will be used only to set the tag and the score should be
added to the askdns rule.
ifplugin Mail::SpamAssassin::Plugin::AskDNS
askdns SENDGRID_ID _SENDGRIDID_.rbl.domain.tld A 127.0.0.2
describe SENDGRID_ID Sendgrid account matches rbl
endif
Tags that the plugin could set are:
=back
=over
=item *
SENDGRIDID
=item *
SENDGRIDDOM
=item *
SENDINBLUEID
=item *
MAILUPID
=item *
MAILDOMEID
=item *
MAILCHIMPID
=back
=cut
sub set_config {
my($self, $conf) = @_;
my @cmds = ();
push(@cmds, {
setting => 'sendgrid_feed',
is_admin => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
}
);
push(@cmds, {
setting => 'sendgrid_domains_feed',
is_admin => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
}
);
push(@cmds, {
setting => 'sendinblue_feed',
is_admin => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
}
);
push(@cmds, {
setting => 'mailup_feed',
is_admin => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
}
);
push(@cmds, {
setting => 'maildome_feed',
is_admin => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
}
);
push(@cmds, {
setting => 'mailchimp_feed',
is_admin => 1,
type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
}
);
$conf->{parser}->register_commands(\@cmds);
}
sub finish_parsing_end {
my ($self, $opts) = @_;
$self->_read_configfile('sendgrid_feed', 'SENDGRID');
$self->_read_configfile('sendgrid_domains_feed', 'SENDGRID_DOMAINS');
$self->_read_configfile('sendinblue_feed', 'SENDINBLUE');
$self->_read_configfile('mailup_feed', 'MAILUP');
$self->_read_configfile('maildome_feed', 'MAILDOME');
$self->_read_configfile('mailchimp_feed', 'MAILCHIMP');
}
sub _read_configfile {
my ($self, $feed, $esp) = @_;
my $conf = $self->{main}->{registryboundaries}->{conf};
my $id;
local *F;
return if not defined $conf->{$feed};
my @feed_files = split(/,/, $conf->{$feed});
foreach my $feed_file ( @feed_files ) {
if ( defined($feed_file) && ( -f $feed_file ) ) {
open(F, '<', $feed_file);
for ($!=0; <F>; $!=0) {
chomp;
#lines that start with pound are comments
next if(/^\s*\#/);
$id = $_;
if ( defined $id ) {
push @{$self->{ESP}->{$esp}->{$id}}, $id;
}
}
defined $_ || $!==0 or
$!==EBADF ? dbg("ESP: error reading config file: $!")
: die "error reading config file: $!";
close(F) or die "error closing config file: $!";
}
}
}
sub esp_sendgrid_check_domain {
my ($self, $pms) = @_;
my $sendgrid_id;
my $sendgrid_domain;
# All Sendgrid emails have the X-SG-EID header
my $sg_eid = $pms->get("X-SG-EID", undef);
return if not defined $sg_eid;
my $rulename = $pms->get_current_eval_rule_name();
my $envfrom = $pms->get("EnvelopeFrom:addr", undef);
return if not defined $envfrom;
# Find the domain from the Return-Path
if($envfrom =~ /\@(\w+\.)?([\w\.]+)\>?$/) {
$sendgrid_domain = $2;
# dbg("ENVFROM: $envfrom domain: $sendgrid_domain");
if(defined $sendgrid_domain) {
$pms->set_tag('SENDGRIDDOM', $sendgrid_domain);
if ( exists $self->{ESP}->{SENDGRID_DOMAIN}->{$sendgrid_domain} ) {
dbg("HIT! $sendgrid_domain domain found in Sendgrid Invaluement feed");
$pms->test_log("Sendgrid domain: $sendgrid_domain");
$pms->got_hit($rulename, "", ruletype => 'eval');
return 1;
}
}
}
}
sub esp_sendgrid_check_id {
my ($self, $pms) = @_;
my $sendgrid_id;
my $sendgrid_domain;
# All Sendgrid emails have the X-SG-EID header
my $sg_eid = $pms->get("X-SG-EID", undef);
return if not defined $sg_eid;
my $rulename = $pms->get_current_eval_rule_name();
my $envfrom = $pms->get("EnvelopeFrom:addr", undef);
return if not defined $envfrom;
# Find the customer id from the Return-Path
if($envfrom =~ /bounces\+(\d+)\-/) {
$sendgrid_id = $1;
# dbg("ENVFROM: $envfrom ID: $sendgrid_id");
if(defined $sendgrid_id) {
$pms->set_tag('SENDGRIDID', $sendgrid_id);
if ( exists $self->{ESP}->{SENDGRID}->{$sendgrid_id} ) {
dbg("HIT! $sendgrid_id customer id found in Sendgrid Invaluement feed");
$pms->test_log("Sendgrid id: $sendgrid_id");
$pms->got_hit($rulename, "", ruletype => 'eval');
return 1;
}
}
}
}
sub esp_sendgrid_check {
my ($self, $pms) = @_;
my $ret;
$ret = $self->esp_sendgrid_check_id($pms);
if (!$ret) {
$ret = $self->esp_sendgrid_check_domain($pms);
}
return $ret;
}
sub esp_sendinblue_check {
my ($self, $pms) = @_;
my $sendinblue_id;
my $rulename = $pms->get_current_eval_rule_name();
my $envfrom = $pms->get("EnvelopeFrom:addr", undef);
# All Sendinblue emails have the X-Mailer header set to Sendinblue
my $xmailer = $pms->get("X-Mailer", undef);
if((not defined $xmailer) or ($xmailer !~ /Sendinblue/)) {
return;
}
$sendinblue_id = $pms->get("X-Mailin-Client", undef);
return if not defined $sendinblue_id;
chomp($sendinblue_id);
if(defined $sendinblue_id) {
if ( exists $self->{ESP}->{SENDINBLUE}->{$sendinblue_id} ) {
$pms->set_tag('SENDINBLUEID', $sendinblue_id);
dbg("HIT! $sendinblue_id ID found in Sendinblue feed");
$pms->test_log("Sendinblue id: $sendinblue_id");
$pms->got_hit($rulename, "", ruletype => 'eval');
return 1;
}
}
}
sub esp_mailup_check {
my ($self, $pms) = @_;
my $mailup_id;
my $rulename = $pms->get_current_eval_rule_name();
# All Mailup emails have the X-CSA-Complaints header set to whitelist-complaints@eco.de
my $xcsa = $pms->get("X-CSA-Complaints", undef);
if((not defined $xcsa) or ($xcsa !~ /whitelist-complaints\@eco\.de/)) {
return;
}
# All Mailup emails have the X-Abuse header that must match
$mailup_id = $pms->get("X-Abuse", undef);
return if not defined $mailup_id;
$mailup_id =~ /Please report abuse here: http\:\/\/.*\.musvc([0-9]+)\.net\/p\?c=([0-9]+)/;
$mailup_id = $2;
# if regexp doesn't match it's not Mailup
return if not defined $mailup_id;
chomp($mailup_id);
if(defined $mailup_id) {
if ( exists $self->{ESP}->{MAILUP}->{$mailup_id} ) {
$pms->set_tag('MAILUPID', $mailup_id);
dbg("HIT! $mailup_id customer found in Mailup feed");
$pms->test_log("Mailup id: $mailup_id");
$pms->got_hit($rulename, "", ruletype => 'eval');
return 1;
}
}
}
sub esp_maildome_check {
my ($self, $pms) = @_;
my $maildome_id;
my $rulename = $pms->get_current_eval_rule_name();
# return if X-Mailer is not what we want
my $xmailer = $pms->get("X-Mailer", undef);
if((not defined $xmailer) or ($xmailer !~ /MaildomeMTA/)) {
return;
}
$maildome_id = $pms->get("List-Unsubscribe", undef);
return if not defined $maildome_id;
$maildome_id =~ /subject=https:\/\/.*\/unsubscribe\/([0-9]+)\/([0-9]+)\/.*\/([0-9]+)\/([0-9]+)\>/;
$maildome_id = $2;
# if regexp doesn't match it's not Maildome
return if not defined $maildome_id;
chomp($maildome_id);
if(defined $maildome_id) {
if ( exists $self->{ESP}->{MAILDOME}->{$maildome_id} ) {
$pms->set_tag('MAILDOMEID', $maildome_id);
dbg("HIT! $maildome_id customer found in Maildome feed");
$pms->test_log("Maildome id: $maildome_id");
$pms->got_hit($rulename, "", ruletype => 'eval');
return 1;
}
}
}
sub esp_mailchimp_check {
my ($self, $pms) = @_;
my $mailchimp_id;
my $rulename = $pms->get_current_eval_rule_name();
# return if X-Mailer is not what we want
my $xmailer = $pms->get("X-Mailer", undef);
if((not defined $xmailer) or ($xmailer !~ /MailChimp Mailer/)) {
return;
}
$mailchimp_id = $pms->get("X-MC-User", undef);
return if not defined $mailchimp_id;
chomp($mailchimp_id);
if(defined $mailchimp_id) {
if ( exists $self->{ESP}->{MAILCHIMP}->{$mailchimp_id} ) {
$pms->set_tag('MAILCHIMPID', $mailchimp_id);
dbg("HIT! $mailchimp_id customer found in Mailchimp feed");
$pms->test_log("Mailchimp id: $mailchimp_id");
$pms->got_hit($rulename, "", ruletype => 'eval');
return 1;
}
}
}
1;