| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # audit::smtp Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Audit::Smtp; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | hostname => [ qw(hostname) ], | ||
| 20 | port => [ qw(integer) ], | ||
| 21 | domainname => [ qw(domainname) ], | ||
| 22 | _smtp => [ qw(INTERNAL) ], | ||
| 23 | }, | ||
| 24 | attributes_default => { | ||
| 25 | port => 25, | ||
| 26 | }, | ||
| 27 | commands => { | ||
| 28 | connect => [ qw(hostname|OPTIONAL port|OPTIONAL domainname|OPTIONAL) ], | ||
| 29 | banner => [ ], | ||
| 30 | quit => [ ], | ||
| 31 | open_auth_login => [ ], | ||
| 32 | open_relay => [ ], | ||
| 33 | all => [ ], | ||
| 34 | }, | ||
| 35 | require_modules => { | ||
| 36 | 'Net::SMTP' => [], | ||
| 37 | 'Net::Cmd' => [ qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING) ], | ||
| 38 | }, | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub brik_use_properties { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | return { | ||
| 46 | attributes_default => { | ||
| 47 | hostname => defined($self->global) && $self->global->hostname || 'hostname', | ||
| 48 | }, | ||
| 49 | }; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub connect { | ||
| 53 | my $self = shift; | ||
| 54 | my ($hostname, $port, $domainname) = @_; | ||
| 55 | |||
| 56 | $hostname ||= $self->hostname; | ||
| 57 | $port ||= $self->port; | ||
| 58 | $domainname ||= $self->domainname; | ||
| 59 | $self->brik_help_run_undef_arg('connect', $hostname) or return; | ||
| 60 | $self->brik_help_run_undef_arg('connect', $port) or return; | ||
| 61 | $self->brik_help_run_undef_arg('connect', $domainname) or return; | ||
| 62 | |||
| 63 | my $timeout = defined($self->global) && $self->global->ctimeout || 3; | ||
| 64 | |||
| 65 | my $smtp = Net::SMTP->new( | ||
| 66 | $hostname, | ||
| 67 | Port => $port, | ||
| 68 | Hello => $domainname, | ||
| 69 | Timeout => $timeout, | ||
| 70 | Debug => $self->log->level, | ||
| 71 | ) or return $self->log->error("connect: $!"); | ||
| 72 | |||
| 73 | $self->_smtp($smtp); | ||
| 74 | |||
| 75 | return $smtp; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub quit { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my $smtp = $self->_smtp; | ||
| 82 | $self->brik_help_run_undef_arg('connect', $smtp) or return; | ||
| 83 | |||
| 84 | $self->_smtp(undef); | ||
| 85 | |||
| 86 | return $smtp->quit; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub banner { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | my $smtp = $self->_smtp; | ||
| 93 | $self->brik_help_run_undef_arg('connect', $smtp) or return; | ||
| 94 | |||
| 95 | chomp(my $banner = $smtp->banner); | ||
| 96 | |||
| 97 | # XXX: move to identify::smtp | ||
| 98 | #if ($banner =~ /rblsmtpd/i) { | ||
| 99 | #$log->debug("smtpRbl=1"); | ||
| 100 | #$result->rbl(1); | ||
| 101 | #} | ||
| 102 | #else { | ||
| 103 | #$log->debug("smtpRbl=0"); | ||
| 104 | #$result->rbl(0); | ||
| 105 | #} | ||
| 106 | |||
| 107 | return $banner; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub open_auth_login { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | my $smtp = $self->_smtp; | ||
| 114 | $self->brik_help_run_undef_arg('connect', $smtp) or return; | ||
| 115 | |||
| 116 | my $smtp_feature_auth_login = 0; | ||
| 117 | my $smtp_open_auth_login = 0; | ||
| 118 | |||
| 119 | my $msg = $smtp->message; | ||
| 120 | if ($msg =~ /AUTH LOGIN/i) { | ||
| 121 | $smtp_feature_auth_login = 1; | ||
| 122 | |||
| 123 | my $ok = $smtp->command("AUTH LOGIN")->response; | ||
| 124 | if ($ok == Net::Cmd::CMD_MORE()) { | ||
| 125 | $ok = $smtp->command("YWRtaW4=")->response; # Send login 'admin' | ||
| 126 | if ($ok == Net::Cmd::CMD_MORE()) { | ||
| 127 | $ok = $smtp->command("YWRtaW4=")->response; # Send password 'admin' | ||
| 128 | if ($ok == Net::Cmd::CMD_OK()) { | ||
| 129 | $smtp_open_auth_login = 1; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | $self->log->info("AUTH LOGIN not supported by target"); | ||
| 136 | } | ||
| 137 | |||
| 138 | return { | ||
| 139 | smtp_feature_auth_login => $smtp_feature_auth_login, | ||
| 140 | smtp_open_auth_login => $smtp_open_auth_login, | ||
| 141 | }; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub open_relay { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | my $smtp = $self->_smtp; | ||
| 148 | $self->brik_help_run_undef_arg('connect', $smtp) or return; | ||
| 149 | |||
| 150 | my $smtp_open_relay = 0; | ||
| 151 | my $smtp_to_reject = 0; | ||
| 152 | my $smtp_to_error = 0; | ||
| 153 | my $smtp_from_reject = 0; | ||
| 154 | my $smtp_from_error = 0; | ||
| 155 | |||
| 156 | my $ok = $smtp->mail('audit@example.com'); | ||
| 157 | if ($ok) { | ||
| 158 | $ok = $smtp->to('audit@example.com'); | ||
| 159 | if ($ok) { | ||
| 160 | $smtp_open_relay = 1; | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | my $status = $smtp->status; | ||
| 164 | if ($status == Net::Cmd::CMD_REJECT()) { | ||
| 165 | $smtp_to_reject = 1; | ||
| 166 | } | ||
| 167 | elsif ($status == Net::Cmd::CMD_ERROR()) { | ||
| 168 | $smtp_to_error = 1; | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | chomp(my $msg = $smtp->message); | ||
| 172 | $self->log->debug("open_relay: MSG[$msg]"); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | my $status = $smtp->status; | ||
| 178 | if ($status == Net::Cmd::CMD_REJECT()) { | ||
| 179 | $smtp_from_reject = 1; | ||
| 180 | } | ||
| 181 | elsif ($status == Net::Cmd::CMD_ERROR()) { | ||
| 182 | $smtp_from_error = 1; | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | chomp(my $msg = $smtp->message); | ||
| 186 | $self->log->debug("open_relay: MSG[$msg]"); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | return { | ||
| 191 | smtp_open_relay => $smtp_open_relay, | ||
| 192 | smtp_to_reject => $smtp_to_reject, | ||
| 193 | smtp_to_error => $smtp_to_error, | ||
| 194 | smtp_from_reject => $smtp_from_reject, | ||
| 195 | smtp_from_error => $smtp_from_error, | ||
| 196 | }; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub all { | ||
| 200 | my $self = shift; | ||
| 201 | |||
| 202 | my $hash = {}; | ||
| 203 | |||
| 204 | my $check_001 = $self->open_auth_login; | ||
| 205 | for (keys %$check_001) { $hash->{$_} = $check_001->{$_} } | ||
| 206 | |||
| 207 | my $check_002 = $self->open_relay; | ||
| 208 | for (keys %$check_002) { $hash->{$_} = $check_002->{$_} } | ||
| 209 | |||
| 210 | return $hash; | ||
| 211 | } | ||
| 212 | |||
| 213 | 1; | ||
| 214 | |||
| 215 | __END__ | ||