| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
p0f - A TCP Fingerprinting Identification Plugin |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
Use TCP fingerprint info (remote computer OS, network distance, etc) to |
| 10 |
implement more sophisticated anti-spam policies. |
| 11 |
|
| 12 |
=head1 DESCRIPTION |
| 13 |
|
| 14 |
This p0f module inserts a I<p0f> connection note with information deduced |
| 15 |
from the TCP fingerprint. The note typically includes at least the link, |
| 16 |
detail, distance, uptime, genre. Here's a p0f v2 example: |
| 17 |
|
| 18 |
genre => FreeBSD |
| 19 |
detail => 6.x (1) |
| 20 |
uptime => 1390 |
| 21 |
link => ethernet/modem |
| 22 |
distance => 17 |
| 23 |
|
| 24 |
Which was parsed from this p0f fingerprint: |
| 25 |
|
| 26 |
24.18.227.2:39435 - FreeBSD 6.x (1) (up: 1390 hrs) |
| 27 |
-> 208.75.177.101:25 (distance 17, link: ethernet/modem) |
| 28 |
|
| 29 |
When using p0f v3, the following additional values may also be available in |
| 30 |
the I<p0f> connection note: |
| 31 |
|
| 32 |
=over 4 |
| 33 |
|
| 34 |
magic, status, first_seen, last_seen, total_conn, uptime_min, up_mod_days, last_nat, last_chg, distance, bad_sw, os_match_q, os_name, os_flavor, http_name, http_flavor, link_type, and language. |
| 35 |
|
| 36 |
=back |
| 37 |
|
| 38 |
=head1 MOTIVATION |
| 39 |
|
| 40 |
This p0f plugin provides a way to make sophisticated policies for email |
| 41 |
messages. For example, the vast majority of email connections to my server |
| 42 |
from Windows computers are spam (>99%). But, I have clients with |
| 43 |
Exchange servers so I can't block email from all Windows computers. |
| 44 |
|
| 45 |
Same goes for greylisting. Finance companies (AmEx, BoA, etc) send notices |
| 46 |
that they don't queue and retry. They deliver immediately or never. Enabling |
| 47 |
greylisting means maintaining manual whitelists or losing valid messages. |
| 48 |
|
| 49 |
While I'm not willing to use greylisting for every connection, and I'm not |
| 50 |
willing to block connections from Windows computers, I am willing to greylist |
| 51 |
all email from Windows computers. |
| 52 |
|
| 53 |
=head1 CONFIGURATION |
| 54 |
|
| 55 |
Configuration consists of two steps: starting p0f and configuring this plugin. |
| 56 |
|
| 57 |
=head2 start p0f |
| 58 |
|
| 59 |
Create a startup script for p0f that creates a communication socket when your |
| 60 |
server starts up. |
| 61 |
|
| 62 |
p0f v2 example: |
| 63 |
|
| 64 |
p0f -u qpsmtpd -d -q -Q /tmp/.p0f_socket2 'dst port 25' -o /dev/null |
| 65 |
chown qpsmtpd /tmp/.p0f_socket2 |
| 66 |
|
| 67 |
p0f v3 example: |
| 68 |
|
| 69 |
p0f -u qpsmtpd -d -s /tmp/.p0f_socket3 'dst port 25' |
| 70 |
chown qpsmtpd /tmp/.p0f_socket3 |
| 71 |
|
| 72 |
=head2 configure p0f plugin |
| 73 |
|
| 74 |
add an entry to config/plugins to enable p0f: |
| 75 |
|
| 76 |
ident/p0f /tmp/.p0f_socket3 |
| 77 |
|
| 78 |
It's even possible to run both versions of p0f simultaneously: |
| 79 |
|
| 80 |
ident/p0f:2 /tmp/.p0f_socket2 version 2 |
| 81 |
ident/p0f:3 /tmp/.p0f_socket3 |
| 82 |
|
| 83 |
=head2 local_ip |
| 84 |
|
| 85 |
Use I<local_ip> to override the IP address of your mail server. This is useful |
| 86 |
if your mail server runs on a private IP behind a firewall. My mail server has |
| 87 |
the IP 127.0.0.6, but the world knows my mail server as 208.75.177.101. |
| 88 |
|
| 89 |
Example config/plugins entry with local_ip override: |
| 90 |
|
| 91 |
ident/p0f /tmp/.p0f_socket local_ip 208.75.177.101 |
| 92 |
|
| 93 |
|
| 94 |
=head2 version |
| 95 |
|
| 96 |
The version settings specifies the version of p0f you are running. This plugin supports p0f versions 2 and 3. If version is not defined, version 3 is assumed. |
| 97 |
|
| 98 |
Example entry specifying p0f version 2 |
| 99 |
|
| 100 |
ident/p0f /tmp/.p0f_socket version 2 |
| 101 |
|
| 102 |
=head2 smite_os |
| 103 |
|
| 104 |
Assign -1 karma to senders whose OS match the regex pattern supplied. I only recommend using with this p0f 3, as it's OS database is far more reliable than p0f v2. |
| 105 |
|
| 106 |
Example entry: |
| 107 |
|
| 108 |
ident/p0f /tmp/.p0f_socket smite_os windows |
| 109 |
|
| 110 |
=head1 Environment requirements |
| 111 |
|
| 112 |
p0f v3 requires only the remote IP. |
| 113 |
|
| 114 |
p0f v2 requires four pieces of information to look up the p0f fingerprint: |
| 115 |
local_ip, local_port, remote_ip, and remote_port. TcpServer.pm has been |
| 116 |
has been updated to provide that information when running under djb's |
| 117 |
tcpserver. The async, forkserver, and prefork models will likely require |
| 118 |
some additional changes to make sure these fields are populated. |
| 119 |
|
| 120 |
=head1 ACKNOWLEDGEMENTS |
| 121 |
|
| 122 |
Version 2 code heavily based upon the p0fq.pl included with the p0f distribution. |
| 123 |
|
| 124 |
=head1 AUTHORS |
| 125 |
|
| 126 |
2004 - Robert Spier ( original author ) |
| 127 |
|
| 128 |
2010 - Matt Simerson - added local_ip option |
| 129 |
|
| 130 |
2012 - Matt Simerson - refactored, added v3 support |
| 131 |
|
| 132 |
=cut |
| 133 |
|
| 134 |
use strict; |
| 135 |
use warnings; |
| 136 |
|
| 137 |
use Qpsmtpd::Constants; |
| 138 |
use IO::Socket; |
| 139 |
use Net::IP; |
| 140 |
|
| 141 |
my $QUERY_MAGIC_V2 = 0x0defaced; |
| 142 |
my $QUERY_MAGIC_V3 = 0x50304601; |
| 143 |
my $RESP_MAGIC_V3 = 0x50304602; |
| 144 |
|
| 145 |
my $P0F_STATUS_BADQUERY = 0x00; |
| 146 |
my $P0F_STATUS_OK = 0x10; |
| 147 |
my $P0F_STATUS_NOMATCH = 0x20; |
| 148 |
|
| 149 |
sub register { |
| 150 |
my ($self, $qp, $p0f_socket, %args) = @_; |
| 151 |
|
| 152 |
$p0f_socket =~ /(.*)/; # untaint |
| 153 |
$self->{_args}->{p0f_socket} = $1; |
| 154 |
foreach (keys %args) { |
| 155 |
$self->{_args}->{$_} = $args{$_}; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
sub hook_connect { |
| 160 |
my ($self, $qp) = @_; |
| 161 |
|
| 162 |
my $p0f_version = $self->{_args}{version} || 3; |
| 163 |
if ($p0f_version == 3) { |
| 164 |
my $response = $self->query_p0f_v3() or return DECLINED; |
| 165 |
$self->test_v3_response($response) or return DECLINED; |
| 166 |
$self->store_v3_results($response); |
| 167 |
} |
| 168 |
else { |
| 169 |
my $response = $self->query_p0f_v2() or return DECLINED; |
| 170 |
$self->test_v2_response($response) or return DECLINED; |
| 171 |
$self->store_v2_results($response); |
| 172 |
} |
| 173 |
|
| 174 |
return DECLINED; |
| 175 |
} |
| 176 |
|
| 177 |
sub get_v2_query { |
| 178 |
my $self = shift; |
| 179 |
|
| 180 |
my $local_ip = $self->{_args}{local_ip} || $self->qp->connection->local_ip; |
| 181 |
|
| 182 |
my $src = new Net::IP($self->qp->connection->remote_ip) |
| 183 |
or $self->log(LOGERROR, "skip, " . Net::IP::Error()), return; |
| 184 |
|
| 185 |
my $dst = new Net::IP($local_ip) |
| 186 |
or $self->log(LOGERROR, "skip, " . NET::IP::Error()), return; |
| 187 |
|
| 188 |
return |
| 189 |
pack("L L L N N S S", |
| 190 |
$QUERY_MAGIC_V2, |
| 191 |
1, |
| 192 |
rand ^ 42 ^ time, |
| 193 |
$src->intip(), |
| 194 |
$dst->intip(), |
| 195 |
$self->qp->connection->remote_port, |
| 196 |
$self->qp->connection->local_port); |
| 197 |
} |
| 198 |
|
| 199 |
sub get_v3_query { |
| 200 |
my $self = shift; |
| 201 |
|
| 202 |
my $src_ip = $self->qp->connection->remote_ip or do { |
| 203 |
$self->log(LOGERROR, "skip, unable to determine remote IP"); |
| 204 |
return; |
| 205 |
}; |
| 206 |
|
| 207 |
if ($src_ip =~ /:/) { # IPv6 |
| 208 |
my @bits = split(/\:/, $src_ip); |
| 209 |
return |
| 210 |
pack("L C C C C C C C C C C C C C C C C C", |
| 211 |
$QUERY_MAGIC_V3, 0x06, @bits); |
| 212 |
} |
| 213 |
|
| 214 |
my @octets = split(/\./, $src_ip); |
| 215 |
return pack("L C C16", $QUERY_MAGIC_V3, 0x04, @octets); |
| 216 |
} |
| 217 |
|
| 218 |
sub query_p0f_v3 { |
| 219 |
my $self = shift; |
| 220 |
|
| 221 |
my $p0f_socket = $self->{_args}{p0f_socket} or do { |
| 222 |
$self->log(LOGERROR, "skip, socket not defined in config."); |
| 223 |
return; |
| 224 |
}; |
| 225 |
my $query = $self->get_v3_query() or return; |
| 226 |
|
| 227 |
# Open the connection to p0f |
| 228 |
my $sock; |
| 229 |
eval { |
| 230 |
$sock = IO::Socket::UNIX->new(Peer => $p0f_socket, Type => SOCK_STREAM); |
| 231 |
}; |
| 232 |
if (!$sock) { |
| 233 |
$self->log(LOGERROR, "skip, could not open socket: $@"); |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
$sock->autoflush(1); # paranoid redundancy |
| 238 |
$sock->connected or do { |
| 239 |
$self->log(LOGERROR, "skip, socket not connected: $!"); |
| 240 |
return; |
| 241 |
}; |
| 242 |
|
| 243 |
my $sent = $sock->send($query, 0) or do { |
| 244 |
$self->log(LOGERROR, "skip, send failed: $!"); |
| 245 |
return; |
| 246 |
}; |
| 247 |
|
| 248 |
print $sock $query |
| 249 |
; # yes, this is redundant, but I get no response from p0f otherwise |
| 250 |
|
| 251 |
$self->log(LOGDEBUG, "sent $sent byte request"); |
| 252 |
|
| 253 |
my $response; |
| 254 |
$sock->recv($response, 232); |
| 255 |
my $length = length $response; |
| 256 |
$self->log(LOGDEBUG, "received $length byte response"); |
| 257 |
close $sock; |
| 258 |
return $response; |
| 259 |
} |
| 260 |
|
| 261 |
sub query_p0f_v2 { |
| 262 |
my $self = shift; |
| 263 |
|
| 264 |
my $p0f_socket = $self->{_args}->{p0f_socket}; |
| 265 |
my $query = $self->get_v2_query() or return; |
| 266 |
|
| 267 |
# Open the connection to p0f |
| 268 |
socket(SOCK, PF_UNIX, SOCK_STREAM, 0) |
| 269 |
or $self->log(LOGERROR, "socket: $!"), return; |
| 270 |
connect(SOCK, sockaddr_un($p0f_socket)) |
| 271 |
or $self->log(LOGERROR, "connect: $! ($p0f_socket)"), return; |
| 272 |
defined syswrite SOCK, $query |
| 273 |
or $self->log(LOGERROR, "write: $!"), close SOCK, return; |
| 274 |
|
| 275 |
my $response; |
| 276 |
defined sysread SOCK, $response, 1024 |
| 277 |
or $self->log(LOGERROR, "read: $!"), close SOCK, return; |
| 278 |
close SOCK; |
| 279 |
return $response; |
| 280 |
} |
| 281 |
|
| 282 |
sub test_v2_response { |
| 283 |
my ($self, $response) = @_; |
| 284 |
|
| 285 |
# Extract part of the p0f response |
| 286 |
my ($magic, $id, $type) = unpack("L L C", $response); |
| 287 |
|
| 288 |
# $self->log(LOGERROR, $response); |
| 289 |
if ($magic != $QUERY_MAGIC_V2) { |
| 290 |
$self->log(LOGERROR, "skip, Bad response magic."); |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
if ($type == 1) { |
| 295 |
$self->log(LOGERROR, "skip, p0f did not honor our query"); |
| 296 |
return; |
| 297 |
} |
| 298 |
elsif ($type == 2) { |
| 299 |
$self->log(LOGWARN, "skip, connection not in the cache"); |
| 300 |
return; |
| 301 |
} |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
sub test_v3_response { |
| 306 |
my ($self, $response) = @_; |
| 307 |
|
| 308 |
my ($magic, $status) = unpack("L L", $response); |
| 309 |
|
| 310 |
# check the magic response value (a p0f constant) |
| 311 |
if ($magic != $RESP_MAGIC_V3) { |
| 312 |
$self->log(LOGERROR, "skip, Bad response magic."); |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
# check the response status |
| 317 |
if ($status == $P0F_STATUS_BADQUERY) { |
| 318 |
$self->log(LOGERROR, "skip, bad query"); |
| 319 |
return; |
| 320 |
} |
| 321 |
elsif ($status == $P0F_STATUS_NOMATCH) { |
| 322 |
$self->log(LOGINFO, "skip, no match"); |
| 323 |
return; |
| 324 |
} |
| 325 |
if ($status == $P0F_STATUS_OK) { |
| 326 |
$self->log(LOGDEBUG, "pass, query ok"); |
| 327 |
return 1; |
| 328 |
} |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
sub store_v2_results { |
| 333 |
my ($self, $response) = @_; |
| 334 |
|
| 335 |
my ( |
| 336 |
$magic, $id, $type, $genre, $detail, $dist, $link, |
| 337 |
$tos, $fw, $nat, $real, $score, $mflags, $uptime |
| 338 |
) |
| 339 |
= unpack("L L C Z20 Z40 c Z30 Z30 C C C s S N", $response); |
| 340 |
|
| 341 |
my $p0f = { |
| 342 |
genre => $genre, |
| 343 |
detail => $detail, |
| 344 |
distance => $dist, |
| 345 |
link => $link, |
| 346 |
uptime => $uptime, |
| 347 |
}; |
| 348 |
|
| 349 |
$self->connection->notes('p0f', $p0f); |
| 350 |
$self->log(LOGINFO, $genre . " (" . $detail . ")"); |
| 351 |
$self->log(LOGERROR, "error: $@") if $@; |
| 352 |
return $p0f; |
| 353 |
} |
| 354 |
|
| 355 |
sub store_v3_results { |
| 356 |
my ($self, $response) = @_; |
| 357 |
|
| 358 |
my @labels = qw/ magic status first_seen last_seen total_conn uptime_min |
| 359 |
up_mod_days last_nat last_chg distance bad_sw os_match_q os_name os_flavor |
| 360 |
http_name http_flavor link_type language /; |
| 361 |
my @values = |
| 362 |
unpack("L L L L L L L L L s C C A32 A32 A32 A32 A32 A32 A32", $response); |
| 363 |
|
| 364 |
my %r; |
| 365 |
foreach my $i (0 .. (scalar @labels - 1)) { |
| 366 |
next if !defined $values[$i]; |
| 367 |
next if !defined $values[$i]; |
| 368 |
$r{$labels[$i]} = $values[$i]; |
| 369 |
} |
| 370 |
if ($r{os_name}) { # compat with p0f v2 |
| 371 |
$r{genre} = "$r{os_name} $r{os_flavor}"; |
| 372 |
$r{link} = $r{link_type} if $r{link_type}; |
| 373 |
$r{uptime} = $r{uptime_min} if $r{uptime_min}; |
| 374 |
} |
| 375 |
|
| 376 |
if ($r{genre} && $self->{_args}{smite_os}) { |
| 377 |
my $sos = $self->{_args}{smite_os}; |
| 378 |
$self->adjust_karma(-1) if $r{genre} =~ /$sos/i; |
| 379 |
} |
| 380 |
$self->connection->notes('p0f', \%r); |
| 381 |
$self->log(LOGINFO, "$r{os_name} $r{os_flavor}"); |
| 382 |
$self->log(LOGDEBUG, join(' ', @values)); |
| 383 |
$self->log(LOGERROR, "error: $@") if $@; |
| 384 |
return \%r; |
| 385 |
} |
| 386 |
|