File: Phishing.pm

package info (click to toggle)
spamassassin 4.0.1-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,184 kB
  • sloc: perl: 86,247; ansic: 5,193; sh: 3,723; javascript: 339; sql: 279; makefile: 209; python: 49
file content (325 lines) | stat: -rw-r--r-- 9,475 bytes parent folder | download
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
#
# Author: Giovanni Bechis <gbechis@apache.org>
# Copyright 2018,2020 Giovanni Bechis
#
# <@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

Mail::SpamAssassin::Plugin::Phishing - check uris against phishing feed

=head1 SYNOPSIS

  loadplugin Mail::SpamAssassin::Plugin::Phishing

  ifplugin Mail::SpamAssassin::Plugin::Phishing
    phishing_openphish_feed /etc/spamassassin/openphish-feed.txt
    phishing_phishtank_feed /etc/spamassassin/phishtank-feed.csv
    phishing_phishstats_feed /etc/mail/spamassassin/phishstats-feed.csv
    body     URI_PHISHING      eval:check_phishing()
    describe URI_PHISHING      Url match phishing in feed
  endif

=head1 DESCRIPTION

This plugin finds uris used in phishing campaigns detected by
OpenPhish, PhishTank or PhishStats feeds.

The Openphish free feed is updated every 6 hours and can be downloaded from
https://openphish.com/feed.txt.

The PhishTank free feed is updated every 1 hours and can be downloaded from
http://data.phishtank.com/data/online-valid.csv.
To avoid download limits a registration is required.

The PhishStats feed is updated every 90 minutes and can be downloaded from
https://phishstats.info/phish_score.csv.

=cut

package Mail::SpamAssassin::Plugin::Phishing;
use strict;
use warnings;
use re 'taint';

my $VERSION = 1.1;

use Errno qw(EBADF);
use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::PerMsgStatus;

our @ISA = qw(Mail::SpamAssassin::Plugin);

sub dbg { my $msg = shift; Mail::SpamAssassin::Plugin::dbg("Phishing: $msg", @_); }

sub new {
    my ($class, $mailsa) = @_;

    $class = ref($class) || $class;
    my $self = $class->SUPER::new($mailsa);
    bless ($self, $class);

    $self->set_config($mailsa->{conf});
    $self->register_eval_rule("check_phishing", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);

    return $self;
}

sub set_config {
    my ($self, $conf) = @_;
    my @cmds;
    push(@cmds, {
        setting => 'phishing_openphish_feed',
        is_admin => 1,
        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
        }
    );

=head1 ADMIN PREFERENCES

The following options can be used in site-wide (C<local.cf>)
configuration files to customize how the module handles phishing uris

=cut

=over 4

=item phishing_openphish_feed

Absolute path of the downloaded OpenPhish datafeed.

=back

=cut
    push(@cmds, {
        setting => 'phishing_phishtank_feed',
        is_admin => 1,
        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
        }
    );

=over 4

=item phishing_phishtank_feed

Absolute path of the downloaded PhishTank datafeed.

=back

=cut
    push(@cmds, {
        setting => 'phishing_uri_noparam',
        is_admin => 1,
        default => 0,
        type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL,
        }
    );

=over 4

=item phishing_uri_noparam ( 0 | 1 ) (default: 0)

If this option is set uri parameters will not be take into consideration
when parsing the phishing uris datafeed.
If this option is enabled and the url without parameters is "generic"
(like https://www.kisa.link/url_redirector.php?url=...) the url will be
skipped.

=back

=cut
    push(@cmds, {
        setting => 'phishing_phishstats_feed',
        is_admin => 1,
        type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
        }
    );

=over 4

=item phishing_phishstats_feed

Absolute path of the downloaded PhishStats datafeed.

=back

=cut
    push(@cmds, {
        setting => 'phishing_phishstats_minscore',
        is_admin => 1,
        default => 6,
        type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
        }
    );

=over 4

=item phishing_phishstats_minscore ( 0 - 10 ) (default: 6)

Minimum score to take into consideration for phishing uris downloaded
from PhishStats datafeed.

=back

=cut
    $conf->{parser}->register_commands(\@cmds);
}

sub finish_parsing_end {
  my ($self, $opts) = @_;
  $self->_read_configfile($self);
}

sub _read_configfile {
  my ($self) = @_;
  my $conf = $self->{main}->{registryboundaries}->{conf};
  my (@phtank_ln, @phstats_ln);
  my $stripped_cluri;

  local *F;
  if ( defined($conf->{phishing_openphish_feed}) && ( -f $conf->{phishing_openphish_feed} ) ) {
    open(F, '<', $conf->{phishing_openphish_feed});
    for ($!=0; <F>; $!=0) {
        chomp;
        #lines that start with pound are comments
        next if(/^\s*\#/);
        $stripped_cluri = $_;
	if ( $conf->{phishing_uri_noparam} eq 1 ) {
          $stripped_cluri =~ s/\?.*//;
	}
        my $phishdomain = $self->{main}->{registryboundaries}->uri_to_domain($_);
        if ( defined $phishdomain ) {
          push @{$self->{PHISHING}->{$stripped_cluri}->{phishdomain}}, $phishdomain;
          push @{$self->{PHISHING}->{$stripped_cluri}->{phishinfo}->{$phishdomain}}, "OpenPhish";
        }
    }

    defined $_ || $!==0  or
      $!==EBADF ? dbg("PHISHING: error reading config file: $!")
                : die "error reading config file: $!";
    close(F) or die "error closing config file: $!";
  }

  if ( defined($conf->{phishing_phishtank_feed}) && (-f $conf->{phishing_phishtank_feed} ) ) {
    open(F, '<', $conf->{phishing_phishtank_feed});
    for ($!=0; <F>; $!=0) {
        #skip first line
        next if ( $. eq 1);
        chomp;
        #lines that start with pound are comments
        next if(/^\s*\#/);

        @phtank_ln = split(/,/, $_);
        $phtank_ln[1] =~ s/\"//g;
        $stripped_cluri = $phtank_ln[1];
	if ( $conf->{phishing_uri_noparam} eq 1 ) {
          $stripped_cluri =~ s/\?.*//;
	}
        my $phishdomain = $self->{main}->{registryboundaries}->uri_to_domain($phtank_ln[1]);
        if ( defined $phishdomain ) {
          push @{$self->{PHISHING}->{$stripped_cluri}->{phishdomain}}, $phishdomain;
          push @{$self->{PHISHING}->{$stripped_cluri}->{phishinfo}->{$phishdomain}}, "PhishTank";
        }
    }

    defined $_ || $!==0  or
      $!==EBADF ? dbg("PHISHING: error reading config file: $!")
                : die "error reading config file: $!";
    close(F) or die "error closing config file: $!";
  }

  if ( defined($conf->{phishing_phishstats_feed}) && (-f $conf->{phishing_phishstats_feed} ) ) {
    open(F, '<', $conf->{phishing_phishstats_feed});
    for ($!=0; <F>; $!=0) {
        #skip first line
        next if ( $. eq 1);
        chomp;
        #lines that start with pound are comments
        next if(/^\s*\#/);

	# CSV: Date,Score,URL,IP
        @phstats_ln = split(/,/, $_);
        next if (not defined $phstats_ln[1] or not defined $phstats_ln[2]);
        $phstats_ln[1] =~ s/\"//g;
        $phstats_ln[2] =~ s/\"//g;
	if ( $conf->{phishing_phishstats_minscore} >= $phstats_ln[1] ) {
	  next;
	}
        $stripped_cluri = $phstats_ln[2];
	if ( $conf->{phishing_uri_noparam} eq 1 ) {
          $stripped_cluri =~ s/\?.*//;
	}
        my $phishdomain = $self->{main}->{registryboundaries}->uri_to_domain($phstats_ln[2]);
        if ( defined $phishdomain ) {
          push @{$self->{PHISHING}->{$stripped_cluri}->{phishdomain}}, $phishdomain;
          push @{$self->{PHISHING}->{$stripped_cluri}->{phishinfo}->{$phishdomain}}, "PhishStats";
        }
    }

    defined $_ || $!==0  or
      $!==EBADF ? dbg("PHISHING: error reading config file: $!")
                : die "error reading config file: $!";
    close(F) or die "error closing config file: $!";
  }

}

sub check_phishing {
  my ($self, $pms) = @_;

  my $feedname;
  my $domain;
  my $stripped_cluri;
  my $dcnt;

  my $uris = $pms->get_uri_detail_list();
  my $rulename = $pms->get_current_eval_rule_name();
  while (my($uri, $info) = each %{$uris}) {
    # we want to skip mailto: uris
    next if ($uri =~ /^mailto:/i);

    # no hosts/domains were found via this uri, so skip
    next unless ($info->{hosts});
    if (($info->{types}->{a}) || ($info->{types}->{parsed})) {
      # check url
      foreach my $cluri (@{$info->{cleaned}}) {
        $stripped_cluri = $cluri;
	if( $self->{main}->{conf}->{phishing_uri_noparam} eq 1 ) {
          $stripped_cluri =~ s/\?.*//;
          $dcnt = $stripped_cluri =~ tr/\///;
	}
	# If uri without parameters are considered, skip too short uris
	# like https://www.google.com/url?sa=t&url=http://badsite.com
        if( ($self->{main}->{conf}->{phishing_uri_noparam} eq 1) && ($dcnt <= 3) ) {
          next;
        }
        if ( exists $self->{PHISHING}->{$stripped_cluri} ) {
          $domain = $self->{main}->{registryboundaries}->uri_to_domain($cluri);
          $feedname = $self->{PHISHING}->{$stripped_cluri}->{phishinfo}->{$domain}[0];
          dbg("HIT! $domain [$stripped_cluri] found in $feedname feed");
          $pms->test_log("$feedname ($domain)", $rulename);
          return 1;
        }
      }
    }
   }
  return 0;
}

1;