| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | # ----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 3 | # example_abuse_check.pl -- demonstrate Email::Abuse::Investigator | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Usage: | ||||||||||||||||||||||||||||
| 6 | # perl example_abuse_check.pl spam.eml | ||||||||||||||||||||||||||||
| 7 | # perl example_abuse_check.pl < spam.eml | ||||||||||||||||||||||||||||
| 8 | # ----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use open qw(:std :encoding(UTF-8)); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use lib '.'; # find Email/Abuse/Investigator.pm nearby | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Email::Abuse::Investigator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # ---- Read the raw email ------------------------------------------------ | ||||||||||||||||||||||||||||
| 21 | 4 | my $raw; | |||||||||||||||||||||||||||
| 22 | 4 | ▶ | if (@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | # Validate path: reject NUL bytes, bare newlines, and directory-traversal | ||||||||||||||||||||||||||||
| 24 | # sequences before opening. This also makes the path taint-safe under -T. | ||||||||||||||||||||||||||||
| 25 | 4 | ▶ | my ($safe_path) = $ARGV[0] =~ /\A([^\x00\r\n]+)\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | or die "Invalid characters in path '$ARGV[0]'\n"; | ||||||||||||||||||||||||||||
| 27 | 4 | ▶ | die "Path traversal attempt in '$safe_path'\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | if $safe_path =~ m{(?:^|/)\.\.(?:/|$)}; | ||||||||||||||||||||||||||||
| 29 | 3 | ▶ | open my $fh, '<:raw', $safe_path or die "Cannot open $safe_path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 1 | local $/; | |||||||||||||||||||||||||||
| 31 | 1 | $raw = <$fh>; | |||||||||||||||||||||||||||
| 32 | 1 | close $fh; | |||||||||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||||||||||
| 34 | 0 | binmode STDIN, ':raw'; | |||||||||||||||||||||||||||
| 35 | 0 | local $/; | |||||||||||||||||||||||||||
| 36 | 0 | $raw = <STDIN>; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 1 | ▶ | die 'No email data supplied' unless $raw && length $raw; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$raw and length $raw
MC/DC0%0/2
$raw and length $raw
$raw
length $raw
Truth table33%1/3
A and B
A= $rawB= length $raw
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # ---- Analyse ----------------------------------------------------------- | ||||||||||||||||||||||||||||
| 41 | 1 | my $analyser = Email::Abuse::Investigator->new( | |||||||||||||||||||||||||||
| 42 | verbose => 1, | ||||||||||||||||||||||||||||
| 43 | timeout => 15, | ||||||||||||||||||||||||||||
| 44 | # List your own outbound relay IPs/CIDRs here so they are skipped | ||||||||||||||||||||||||||||
| 45 | # when walking the Received: chain for the true origin: | ||||||||||||||||||||||||||||
| 46 | # trusted_relays => ['192.0.2.1', '198.51.100.0/24'], | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | 1 | $analyser->parse_email($raw); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # ---- Full analyst report ----------------------------------------------- | ||||||||||||||||||||||||||||
| 51 | 1 | print $analyser->report(); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # ---- Send-ready abuse report text and contacts ------------------------- | ||||||||||||||||||||||||||||
| 54 | 1 | my @contacts = $analyser->abuse_contacts(); | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | if (@contacts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 57 | 1 | print '=' x 72, "\n"; | |||||||||||||||||||||||||||
| 58 | 1 | print " ABUSE REPORT TEXT (send this to each contact below)\n"; | |||||||||||||||||||||||||||
| 59 | 1 | print '=' x 72, "\n\n"; | |||||||||||||||||||||||||||
| 60 | 1 | print $analyser->abuse_report_text(); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 63 | 1 | print '=' x 72, "\n"; | |||||||||||||||||||||||||||
| 64 | 1 | print " SEND TO:\n"; | |||||||||||||||||||||||||||
| 65 | 1 | print '=' x 72, "\n"; | |||||||||||||||||||||||||||
| 66 | 1 | for my $c (@contacts) { | |||||||||||||||||||||||||||
| 67 | 1 | printf " %-45s %s\n", $c->{address}, $c->{role}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 1 | print "\n"; | |||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||
| 71 | 0 | print "\n(No abuse contacts could be determined.)\n"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | exit 0; | |||||||||||||||||||||||||||