| 1 | # | ||
| 2 | # Courier::Filter::Module::Parts class | ||
| 3 | # | ||
| 4 | # (C) 2003-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: Parts.pm 210 2008-03-21 19:30:31Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Module::Parts - Message (MIME multipart and ZIP archive) | ||
| 12 | parts filter module for the Courier::Filter framework | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Courier::Filter::Module::Parts; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use base 'Courier::Filter::Module'; | ||
| 22 | |||
| 23 | use MIME::Parser 5.4; | ||
| 24 | use IO::InnerFile 2.110; | ||
| 25 | # Require either MIME::Parser 5.413 or lower, or IO::InnerFile 2.110+ | ||
| 26 | # (where IO::InnerFile::seek() properly returns TRUE when appropriate). | ||
| 27 | use Digest::MD5; | ||
| 28 | use File::Spec; | ||
| 29 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 30 | |||
| 31 | use constant TRUE => (0 == 0); | ||
| 32 | use constant FALSE => not TRUE; | ||
| 33 | |||
| 34 | use constant default_response => 'Prohibited message part detected.'; | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Courier::Filter::Module::Parts; | ||
| 39 | |||
| 40 | my $module = Courier::Filter::Module::Parts->new( | ||
| 41 | max_message_size | ||
| 42 | => $max_message_size, | ||
| 43 | max_part_size => $max_part_size, | ||
| 44 | views => ['raw', 'zip'], | ||
| 45 | signatures => [ | ||
| 46 | { | ||
| 47 | # One or more of the following options: | ||
| 48 | mime_type => 'text/html' || qr/html/i, | ||
| 49 | file_name => 'file_name.ext' || qr/\.(com|exe)$/i, | ||
| 50 | size => 106496, | ||
| 51 | digest_md5 => 'b09e26c292759d654633d3c8ed00d18d', | ||
| 52 | encrypted => 0, | ||
| 53 | |||
| 54 | # Optionally any of the following: | ||
| 55 | views => ['raw', 'zip'], | ||
| 56 | response => $response_text | ||
| 57 | }, | ||
| 58 | ... | ||
| 59 | ], | ||
| 60 | |||
| 61 | logger => $logger, | ||
| 62 | inverse => 0, | ||
| 63 | trusting => 0, | ||
| 64 | testing => 0, | ||
| 65 | debugging => 0 | ||
| 66 | ); | ||
| 67 | |||
| 68 | my $filter = Courier::Filter->new( | ||
| 69 | ... | ||
| 70 | modules => [ $module ], | ||
| 71 | ... | ||
| 72 | ); | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | This class is a filter module class for use with Courier::Filter. It matches a | ||
| 77 | message if one of the message's parts (MIME parts, or files in a ZIP archive) | ||
| 78 | matches one of the configured signatures. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | # Implementation: | ||
| 83 | ############################################################################### | ||
| 84 | |||
| 85 | =head2 Constructor | ||
| 86 | |||
| 87 | The following constructor is provided: | ||
| 88 | |||
| 89 | =over | ||
| 90 | |||
| 91 | =item B<new(%options)>: returns I<Courier::Filter::Module::Parts> | ||
| 92 | |||
| 93 | Creates a new B<Parts> filter module. | ||
| 94 | |||
| 95 | %options is a list of key/value pairs representing any of the following | ||
| 96 | options: | ||
| 97 | |||
| 98 | =over | ||
| 99 | |||
| 100 | =item B<views> | ||
| 101 | |||
| 102 | An arrayref containing the global default set of I<views> the filter module | ||
| 103 | should apply to message parts when matching the configured signatures against | ||
| 104 | them. A view is the way how a MIME part's (MIME-decoded) data is interpreted. | ||
| 105 | Defaults to B<['raw']>. | ||
| 106 | |||
| 107 | The following views are supported: | ||
| 108 | |||
| 109 | =over | ||
| 110 | |||
| 111 | =item B<raw> | ||
| 112 | |||
| 113 | The MIME part is MIME-decoded but not otherwise transformed. The raw MIME part | ||
| 114 | is then matched against the configured signatures. | ||
| 115 | |||
| 116 | =item B<zip> | ||
| 117 | |||
| 118 | If the MIME part has a file name ending in C<.zip>, it is considered a ZIP | ||
| 119 | archive, and all unencrypted files in the archive are matched as individual | ||
| 120 | message parts against the configured signatures. The zip view requires the | ||
| 121 | B<Archive::Zip> Perl module to be installed. | ||
| 122 | |||
| 123 | =back | ||
| 124 | |||
| 125 | =item B<max_message_size> | ||
| 126 | |||
| 127 | =item B<max_size> (DEPRECATED) | ||
| 128 | |||
| 129 | An integer value controlling the maximum size (in bytes) of the overall message | ||
| 130 | text for a message to be processed by this filter module. Messages larger than | ||
| 131 | this value will never be processed, and thus will never match. If B<undef>, | ||
| 132 | there is no size limit. Defaults to B<1024**2> (1MB). | ||
| 133 | |||
| 134 | As MIME multipart and ZIP archive processing can be quite CPU- and | ||
| 135 | memory-intensive (although the B<Parts> filter module makes use of temporary | ||
| 136 | files since version 0.13), you should definitely restrict the message size to | ||
| 137 | some sensible value that easily fits in your server's memory. 1024**2 (1MB) | ||
| 138 | should be appropriate for most uses of this filter module. | ||
| 139 | |||
| 140 | The C<max_message_size> option was previously called C<max_size>, but the | ||
| 141 | latter is now deprecated and may not be supported in future versions of the | ||
| 142 | B<Parts> filter module. | ||
| 143 | |||
| 144 | =item B<max_part_size> | ||
| 145 | |||
| 146 | An integer value controlling the maximum size (in bytes) of any single message | ||
| 147 | part (i.e. MIME part in a message, or file in an archive) for that part to be | ||
| 148 | processed by this filter module. Parts larger than this value will never be | ||
| 149 | processed, and thus will never match. If B<undef>, there is no size limit. | ||
| 150 | |||
| 151 | Defaults to the value of the C<max_message_size> option, so you don't really | ||
| 152 | need to specify a part size limit if you are comfortable with using the same | ||
| 153 | value for both. See the C<max_message_size> option for its default. | ||
| 154 | |||
| 155 | If you make use of the B<'zip'> view, be aware of the risk posed by so-called | ||
| 156 | I<decompression bombs>, which allow messages to easily fall below the overall | ||
| 157 | message size limit, while a file in a small attached ZIP archive can decompress | ||
| 158 | to a huge size. The part size limit prevents huge files from being | ||
| 159 | decompressed. | ||
| 160 | |||
| 161 | =item B<signatures> | ||
| 162 | |||
| 163 | I<Required>. A reference to an array containing the list of I<signatures> | ||
| 164 | against which message parts are to be matched. A signature in turn is a | ||
| 165 | reference to a hash containing one or more so-called signature I<aspects> (as | ||
| 166 | key/value pairs) and any signature I<options> (also as key/value pairs). | ||
| 167 | |||
| 168 | I<Signature aspects> | ||
| 169 | |||
| 170 | Aspects may either be scalar values (for exact, case-sensitive matches), or | ||
| 171 | regular expression objects created with the C<qr//> operator (for inexact, | ||
| 172 | partial matches). For a signature to match a message part, I<all> of the | ||
| 173 | signature's specified aspects must match those of the message part. For the | ||
| 174 | filter module to match a message, I<any> of the signatures must match I<any> of | ||
| 175 | the message's parts. | ||
| 176 | |||
| 177 | A signature aspect can be any of the following: | ||
| 178 | |||
| 179 | =over | ||
| 180 | |||
| 181 | =item B<mime_type> | ||
| 182 | |||
| 183 | The MIME type of the message part ('type/sub-type'). | ||
| 184 | |||
| 185 | =item B<file_name> | ||
| 186 | |||
| 187 | The file name of the message part. | ||
| 188 | |||
| 189 | =item B<size> | ||
| 190 | |||
| 191 | The exact size (in bytes) of the decoded message part. | ||
| 192 | |||
| 193 | =item B<digest_md5> | ||
| 194 | |||
| 195 | The MD5 digest of the decoded message part (32 hex digits, as printed by | ||
| 196 | `md5sum`). | ||
| 197 | |||
| 198 | =item B<encrypted> | ||
| 199 | |||
| 200 | A boolean value denoting whether the message part is encrypted and its contents | ||
| 201 | are inaccessible to the B<Parts> filter module. | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | I<Signature options> | ||
| 206 | |||
| 207 | A signature option can be any of the following: | ||
| 208 | |||
| 209 | =over | ||
| 210 | |||
| 211 | =item B<views> | ||
| 212 | |||
| 213 | An arrayref containing the set of I<views> the filter module should apply to | ||
| 214 | message parts when matching I<this> signature against them. For a list of | ||
| 215 | supported views, see the description of the constructor's C<views> option. | ||
| 216 | Defaults to the global set of views specified to the constructor. | ||
| 217 | |||
| 218 | =item B<response> | ||
| 219 | |||
| 220 | A string that is to be returned as the match result in case of a match. | ||
| 221 | Defaults to B<"Prohibited message part detected.">. | ||
| 222 | |||
| 223 | =back | ||
| 224 | |||
| 225 | I<Example> | ||
| 226 | |||
| 227 | So for instance, a signature list could look like this: | ||
| 228 | |||
| 229 | signatures => [ | ||
| 230 | { | ||
| 231 | mime_type => qr/html/i, | ||
| 232 | response => 'No HTML mail, please.' | ||
| 233 | }, | ||
| 234 | { | ||
| 235 | file_name => qr/\.(com|exe|lnk|pif|scr|vbs)$/i, | ||
| 236 | response => 'Executable content detected' | ||
| 237 | }, | ||
| 238 | { | ||
| 239 | size => 106496, | ||
| 240 | digest_md5 => 'b09e26c292759d654633d3c8ed00d18d', | ||
| 241 | views => ['raw', 'zip'], # Look into ZIP archives, too! | ||
| 242 | response => 'Worm detected: W32.Swen' | ||
| 243 | }, | ||
| 244 | { | ||
| 245 | size => 22528, | ||
| 246 | # Cannot set a specific digest_md5 since W32.Mydoom | ||
| 247 | # is polymorphic. | ||
| 248 | response => 'Worm suspected: W32.Mydoom' | ||
| 249 | }, | ||
| 250 | { | ||
| 251 | encrypted => 1, | ||
| 252 | views => ['zip'], | ||
| 253 | response => 'Worm suspected ' . | ||
| 254 | '(only worms and fools use ZIP encryption)' | ||
| 255 | } | ||
| 256 | ] | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | All options of the B<Courier::Filter::Module> constructor are also supported | ||
| 261 | by the constructor of the B<Parts> filter module. Please see | ||
| 262 | L<Courier::Filter::Module/"new"> for their descriptions. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub new { | ||
| 267 | my ($class, %options) = @_; | ||
| 268 | |||
| 269 | my $mime_parser = MIME::Parser->new(); | ||
| 270 | #$mime_parser->output_to_core(TRUE); | ||
| 271 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 272 | $mime_parser->output_under(File::Spec->tmpdir); | ||
| 273 | #$mime_parser->tmp_to_core(TRUE); | ||
| 274 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 275 | $mime_parser->use_inner_files(TRUE); | ||
| 276 | |||
| 277 | my $self = $class->SUPER::new( | ||
| 278 | %options, | ||
| 279 | mime_parser => $mime_parser | ||
| 280 | ); | ||
| 281 | |||
| 282 | # Default "max_message_size" option to the deprecated "max_size" option, | ||
| 283 | # or to 1024**2 (1MB): | ||
| 284 | $self->{max_message_size} = ( | ||
| 285 | exists($self->{max_size}) ? $self->{max_size} : 1024**2 | ||
| 286 | ) | ||
| 287 | if not exists($self->{max_message_size}); | ||
| 288 | |||
| 289 | # Default "max_part_size" option to the "max_message_size" option: | ||
| 290 | $self->{max_part_size} = $self->{max_message_size} | ||
| 291 | if not exists($self->{max_part_size}); | ||
| 292 | |||
| 293 | # Default "views" option to 'raw': | ||
| 294 | my $views = $self->{views} || { 'raw' => TRUE }; | ||
| 295 | |||
| 296 | # Transform "views" option into hashref if it was given as an arrayref: | ||
| 297 | $views = { map(($_ => TRUE), @$views) } | ||
| 298 | if ref($views) eq 'ARRAY'; | ||
| 299 | |||
| 300 | my $used_views = { %$views }; | ||
| 301 | foreach my $signature ( @{$self->{signatures}} ) { | ||
| 302 | # Default "views" option to global "views" option: | ||
| 303 | my $signature_views = $signature->{views} || $views; | ||
| 304 | |||
| 305 | # Transform "views" option into hashref if it was given as an arrayref: | ||
| 306 | $signature_views = { map(($_ => TRUE), @$signature_views) } | ||
| 307 | if ref($signature_views) eq 'ARRAY'; | ||
| 308 | |||
| 309 | # Add any signature-specific views to the global set of used views: | ||
| 310 | %$used_views = (%$used_views, %$signature_views); | ||
| 311 | |||
| 312 | $signature->{views} = $signature_views; | ||
| 313 | |||
| 314 | $self->compile_signature($signature); | ||
| 315 | } | ||
| 316 | |||
| 317 | $self->{used_views} = $used_views; | ||
| 318 | |||
| 319 | return $self; | ||
| 320 | } | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | =head2 Instance methods | ||
| 325 | |||
| 326 | See L<Courier::Filter::Module/"Instance methods"> for a description of the | ||
| 327 | provided instance methods. | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | sub match { | ||
| 332 | my ($self, $message) = @_; | ||
| 333 | |||
| 334 | return undef | ||
| 335 | if defined($self->{max_message_size}) | ||
| 336 | and -s $message->file_name > $self->{max_message_size}; | ||
| 337 | |||
| 338 | #my $text = $message->text; | ||
| 339 | #my $part = $self->{mime_parser}->parse_data($text); | ||
| 340 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 341 | my $part = $self->{mime_parser}->parse_open($message->file_name); | ||
| 342 | my ($result, @code) = $self->match_mime_part($part); | ||
| 343 | |||
| 344 | $result &&= 'Parts: ' . $result; | ||
| 345 | |||
| 346 | $self->{mime_parser}->filer->purge(); | ||
| 347 | # In-memory processing doesn't work, see comments in match_mime_part(). | ||
| 348 | rmdir($self->{mime_parser}->filer->output_dir); | ||
| 349 | #if MIME::Tools->VERSION < 6.0; | ||
| 350 | # Purging also doesn't work properly | ||
| 351 | # (bug filed: <http://rt.cpan.org/NoAuth/Bug.html?id=7858>). | ||
| 352 | |||
| 353 | return ($result, @code); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub match_mime_part { | ||
| 357 | my ($self, $part) = @_; | ||
| 358 | |||
| 359 | if (my $body = $part->bodyhandle) { | ||
| 360 | # No sub-parts, match this part. | ||
| 361 | |||
| 362 | #my $handle = $body->open('r'); | ||
| 363 | # In-memory processing doesn't work because MIME::Body::open() | ||
| 364 | # doesn't provide a fully-IO::Handle-compatible I/O handle object | ||
| 365 | # (opened() method is missing, no bug filed). Working around that | ||
| 366 | # by alternatively creating a Perl 5.8 style in-memory file | ||
| 367 | # object... | ||
| 368 | # my $body_as_string = $body->as_string; | ||
| 369 | # open(my $handle, '+<', \$body_as_string); | ||
| 370 | # ...doesn't work either because Archive::Zip::_isSeekable() is | ||
| 371 | # broken (erroneously considers Perl 5.8 style in-memory IO::File | ||
| 372 | # objects not to be seekable, | ||
| 373 | # bug filed: <http://rt.cpan.org/NoAuth/Bug.html?id=7855>). | ||
| 374 | # All of this forces us to make MIME::Parser use temporary files | ||
| 375 | # instead of doing everything exclusively in-memory. Aaargh!! | ||
| 376 | |||
| 377 | # First, we gather signature makers for all possible (and enabled) | ||
| 378 | # views of the MIME part, then we actually test each view in turn | ||
| 379 | # against the configured test signatures. | ||
| 380 | |||
| 381 | my @views; | ||
| 382 | |||
| 383 | # Raw view (the MIME part itself) (if enabled): | ||
| 384 | my $rawsig = $self->make_signature_from_mime_part($part); | ||
| 385 | if ($self->{used_views}->{'raw'}) { | ||
| 386 | push( | ||
| 387 | @views, | ||
| 388 | { | ||
| 389 | name => 'raw', | ||
| 390 | sig_maker => sub { $rawsig } | ||
| 391 | } | ||
| 392 | ) | ||
| 393 | if not defined($self->{max_part_size}) | ||
| 394 | or $rawsig->{size} <= $self->{max_part_size}; | ||
| 395 | } | ||
| 396 | |||
| 397 | # ZIP archive members view (if enabled and MIME part is a ZIP archive): | ||
| 398 | if ( | ||
| 399 | $self->{used_views}->{'zip'} and | ||
| 400 | defined($rawsig->{file_name}) and | ||
| 401 | $rawsig->{file_name} =~ /\.zip$/i | ||
| 402 | ) { | ||
| 403 | require Archive::Zip; | ||
| 404 | |||
| 405 | my $archive = Archive::Zip->new(); | ||
| 406 | #$archive->readFromFileHandle($handle); | ||
| 407 | # In-memory processing doesn't work, see above. | ||
| 408 | $archive->read($body->path); | ||
| 409 | |||
| 410 | # Make a view for each archive member: | ||
| 411 | foreach my $member ($archive->members) { | ||
| 412 | push( | ||
| 413 | @views, | ||
| 414 | { | ||
| 415 | name => 'zip', | ||
| 416 | sig_maker => sub { | ||
| 417 | $self->make_signature_from_zip_archive_member($member) | ||
| 418 | } | ||
| 419 | } | ||
| 420 | ) | ||
| 421 | if not defined($self->{max_part_size}) | ||
| 422 | or $member->uncompressedSize <= $self->{max_part_size}; | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | # Now, for each view, try matching the configured signatures: | ||
| 427 | foreach my $view (@views) { | ||
| 428 | # Make signature from data view: | ||
| 429 | my $datasig = $view->{sig_maker}->(); | ||
| 430 | |||
| 431 | # Test that signature against the configured signatures: | ||
| 432 | foreach my $signature ( @{$self->{signatures}} ) { | ||
| 433 | # Skip this signature if it doesn't apply to the current view: | ||
| 434 | next if not $signature->{views}->{ $view->{name} }; | ||
| 435 | |||
| 436 | my ($result, @code) = $signature->{matcher}->($datasig); | ||
| 437 | return ($result, @code) if defined($result); | ||
| 438 | } | ||
| 439 | } | ||
| 440 | } | ||
| 441 | else { | ||
| 442 | # Match all sub-parts: | ||
| 443 | foreach my $subpart ($part->parts) { | ||
| 444 | my ($result, @code) = $self->match_mime_part($subpart); | ||
| 445 | return ($result, @code) if defined($result); | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | return undef; | ||
| 450 | } | ||
| 451 | |||
| 452 | sub make_signature_from_mime_part { | ||
| 453 | my ($self, $part) = @_; | ||
| 454 | |||
| 455 | my $head = $part->head; | ||
| 456 | my $body = $part->bodyhandle; | ||
| 457 | my $text = $body->as_string; | ||
| 458 | |||
| 459 | return { | ||
| 460 | mime_type => $head->mime_type, | ||
| 461 | file_name => $head->recommended_filename, | ||
| 462 | size => length($text), | ||
| 463 | digest_md5 => Digest::MD5::md5_hex($text), | ||
| 464 | encrypted => FALSE | ||
| 465 | }; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub make_signature_from_zip_archive_member { | ||
| 469 | my ($self, $member) = @_; | ||
| 470 | |||
| 471 | return { | ||
| 472 | mime_type => undef, | ||
| 473 | file_name => $member->fileName, | ||
| 474 | size => $member->uncompressedSize, | ||
| 475 | digest_md5 => $member->isEncrypted ? | ||
| 476 | undef | ||
| 477 | : Digest::MD5::md5_hex(scalar($member->contents)), | ||
| 478 | encrypted => $member->isEncrypted | ||
| 479 | }; | ||
| 480 | } | ||
| 481 | |||
| 482 | sub compile_signature { | ||
| 483 | my ($self, $signature) = @_; | ||
| 484 | |||
| 485 | my %matchers; | ||
| 486 | |||
| 487 | my @aspects = grep(!/^(?:response|views)$/, keys(%$signature)); | ||
| 488 | foreach my $aspect (@aspects) { | ||
| 489 | my $pattern = $signature->{$aspect}; | ||
| 490 | |||
| 491 | my $matcher; | ||
| 492 | if (ref($pattern) eq 'Regexp') { | ||
| 493 | $matcher = sub { $_[0] =~ $pattern }; | ||
| 494 | } | ||
| 495 | elsif (ref($pattern) eq 'CODE') { | ||
| 496 | $matcher = $pattern; | ||
| 497 | } | ||
| 498 | else { | ||
| 499 | if ($aspect =~ /^(?:encrypted)$/) { | ||
| 500 | # Aspect is of boolean type: | ||
| 501 | $matcher = sub { not ($_[0] xor $pattern) }; | ||
| 502 | } | ||
| 503 | else { | ||
| 504 | $matcher = sub { $_[0] eq $pattern }; | ||
| 505 | } | ||
| 506 | } | ||
| 507 | |||
| 508 | $matchers{$aspect} = $matcher; | ||
| 509 | } | ||
| 510 | |||
| 511 | my @response = | ||
| 512 | ref($signature->{response}) eq 'ARRAY' ? | ||
| 513 | @{ $signature->{response} } | ||
| 514 | : ($signature->{response} || $self->default_response); | ||
| 515 | |||
| 516 | my $matcher = sub { | ||
| 517 | # Closure with regard to %matchers. | ||
| 518 | my ($signature) = @_; | ||
| 519 | foreach my $aspect (keys(%matchers)) { | ||
| 520 | my $value = $signature->{$aspect}; | ||
| 521 | return undef | ||
| 522 | if not defined($value) | ||
| 523 | or not $matchers{$aspect}->($value); | ||
| 524 | } | ||
| 525 | return @response; | ||
| 526 | }; | ||
| 527 | |||
| 528 | $signature->{matcher} = $matcher; | ||
| 529 | |||
| 530 | return; | ||
| 531 | } | ||
| 532 | |||
| 533 | =head1 SEE ALSO | ||
| 534 | |||
| 535 | L<Courier::Filter::Module>, L<Courier::Filter::Overview>. | ||
| 536 | |||
| 537 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 538 | L<Courier::Filter::Overview>. | ||
| 539 | |||
| 540 | =head1 AUTHOR | ||
| 541 | |||
| 542 | Julian Mehnle <julian@mehnle.net> | ||
| 543 | |||
| 544 | =cut | ||
| 545 | |||
| 546 | TRUE; |