| 1 | package LaBrea::Tarpit::Get; | ||
| 2 | |||
| 3 | #require 5.005_62; | ||
| 4 | use strict; | ||
| 5 | #use diagnostics; | ||
| 6 | #use warnings; | ||
| 7 | use vars qw($VERSION @ISA @EXPORT_OK); | ||
| 8 | |||
| 9 | $VERSION = do { my @r = (q$Revision: 1.05 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | ||
| 10 | |||
| 11 | use LaBrea::NetIO qw(open_tcp); | ||
| 12 | require Exporter; | ||
| 13 | @ISA = qw(Exporter); | ||
| 14 | @EXPORT_OK = qw( | ||
| 15 | parse_http_URL | ||
| 16 | open_http | ||
| 17 | parse_http_response | ||
| 18 | short_response | ||
| 19 | make_line | ||
| 20 | not_hour | ||
| 21 | not_day | ||
| 22 | auto_update | ||
| 23 | ); | ||
| 24 | |||
| 25 | ## No Autoload function, all subs are used at least once | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | LaBrea::Tarpit::Get | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use LaBrea::Tarpit::Get; | ||
| 34 | |||
| 35 | ($rv,$host,$port,$path)=parse_http_URL($url) | ||
| 36 | ($handle,$host,$port,$path)=open_http(*S,$url); | ||
| 37 | $rv=parse_http_response(\$buffer,\%response); | ||
| 38 | $rv=short_response($url,\%response,\%content,$timeout); | ||
| 39 | $line = make_line($url,$err,\%content); | ||
| 40 | $rv = not_hour($file); | ||
| 41 | $rv = not_day($file); | ||
| 42 | $rv=auto_update($url,$file,$cur_ver,$timeout); | ||
| 43 | |||
| 44 | =head1 DESCRIPTION - LaBrea::Tarpit::Get | ||
| 45 | |||
| 46 | Module connects to a web site running | ||
| 47 | LaBrea::Tarpit::Report::html_report.plx and retrieves a short_report as | ||
| 48 | described in LaBrea::Tarpit::Report. | ||
| 49 | |||
| 50 | Run C<examples/web_scan.pl> from a cron job hourly or | ||
| 51 | daily to update the statistics from all know sites running | ||
| 52 | LaBrea::Tarpit. A report can then be generated showing the activity | ||
| 53 | worldwide. | ||
| 54 | |||
| 55 | # MIN HOUR DAY MONTH DAYOFWEEK COMMAND | ||
| 56 | 30 * * * * ./web_scan.pl ./other_sites.txt ./tmp/site_stats | ||
| 57 | |||
| 58 | See: LaBrea::Tarpit::Report::other_sites | ||
| 59 | |||
| 60 | =over 2 | ||
| 61 | |||
| 62 | =item ($handle,$host,$port,$path)= parse_http_URL($url); | ||
| 63 | |||
| 64 | Separate an http URL into its components | ||
| 65 | |||
| 66 | input: URL of the form | ||
| 67 | http://www.foo.com[:8080]/file.html | ||
| 68 | |||
| 69 | https:// service is not supported | ||
| 70 | |||
| 71 | returns: (undef, error message) | ||
| 72 | or | ||
| 73 | (file_handle,hostname,port,path) | ||
| 74 | where port and path may be empty | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub parse_http_URL { | ||
| 79 | my ($url) = @_; | ||
| 80 | return (undef, 'URL must begin with http://') | ||
| 81 | unless $url =~ m|^http://|; | ||
| 82 | my $port = ''; | ||
| 83 | my $path = ''; | ||
| 84 | my $remote; | ||
| 85 | if ( $url =~ m|http://([a-zA-Z0-9\-\.]+)(/[^?]+)|i ) { | ||
| 86 | $remote = $1; | ||
| 87 | $path = $2; | ||
| 88 | } elsif ( $url =~ m|http://([a-zA-Z0-9\-\.]+):(\d+)(/[^?]+)|i ) { | ||
| 89 | $remote = $1; | ||
| 90 | $port = $2; | ||
| 91 | $path = $3; | ||
| 92 | } elsif ( $url =~ m|http://([a-zA-Z0-9\-\.]+)|i ) { | ||
| 93 | $remote = $1 | ||
| 94 | } else { | ||
| 95 | return (undef,'invalid URL'); | ||
| 96 | } | ||
| 97 | return (1,$remote,$port,$path); | ||
| 98 | } | ||
| 99 | |||
| 100 | =item ($handle,$host,$port,$path)=open_http(*S,$url); | ||
| 101 | |||
| 102 | Open connection to http target | ||
| 103 | |||
| 104 | input: *S,$url [default port = 80] | ||
| 105 | returns: (undef, error) on error | ||
| 106 | |||
| 107 | (file_handle, | ||
| 108 | hostname, | ||
| 109 | port | ||
| 110 | path ) on success | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub open_http { | ||
| 115 | my ($S,$x) = @_; | ||
| 116 | my ($s,$remote,$port,$path) = parse_http_URL($x); | ||
| 117 | return (undef,$remote) unless $s; # return error if any | ||
| 118 | return (undef,'missing filename') unless $path; | ||
| 119 | $port = 80 unless $port; | ||
| 120 | $x = open_tcp($S,$remote,$port); | ||
| 121 | return (undef,$x) if $x; | ||
| 122 | return ($S,$remote,$port,$path) | ||
| 123 | } | ||
| 124 | |||
| 125 | =item $rv=parse_http_response(\$buffer,\%response); | ||
| 126 | |||
| 127 | Parse an http server response into a hash of headers. | ||
| 128 | |||
| 129 | i.e. (representative, will vary) | ||
| 130 | |||
| 131 | rc => 200 | ||
| 132 | msg => OK | ||
| 133 | date => Wed, 24 Apr 2002 21:46:30 GMT | ||
| 134 | server => Apache/1.3.22 | ||
| 135 | protocol => HTTP/1.1 | ||
| 136 | content-type => text/plain | ||
| 137 | content-length => 92 | ||
| 138 | last-modified => Wed, 24 Apr 2002 21:46:34 GMT | ||
| 139 | expires => Wed, 24 Apr 2002 21:47:04 GMT | ||
| 140 | connection => close | ||
| 141 | content => (complete text buffer) | ||
| 142 | |||
| 143 | input: \$text_in, \%response | ||
| 144 | returns: true on success, %response filled | ||
| 145 | false on failure | ||
| 146 | |||
| 147 | NOTE: %response{rc} (server response code) | ||
| 148 | %response(msg} (server messages) | ||
| 149 | are ALWAYS filled with something. | ||
| 150 | In the case of server failure, the | ||
| 151 | cause of the failure will be inserted | ||
| 152 | into %response(msg} and undef returned. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | ################################################### | ||
| 157 | # parse_http_response | ||
| 158 | # | ||
| 159 | # input: \$buffer,\%response | ||
| 160 | # return: true on success, else false | ||
| 161 | # response is filled | ||
| 162 | # | ||
| 163 | sub parse_http_response { | ||
| 164 | my ($b,$r) = @_; | ||
| 165 | $$b =~ s/\r//g; # remove dos returns | ||
| 166 | @_ = split('\n',$$b); | ||
| 167 | %$r = (); | ||
| 168 | # get response protocol and response code | ||
| 169 | unless ( $_[0] =~ /([^\s]+)\s+(\d+)\s*(.*)/ ) { | ||
| 170 | $r->{rc} = ''; | ||
| 171 | $r->{msg} = 'unknown server response'; | ||
| 172 | return undef; | ||
| 173 | } else { | ||
| 174 | $r->{protocol} = $1; | ||
| 175 | $r->{rc} = $2; | ||
| 176 | $r->{msg} = $3 || ''; | ||
| 177 | return undef unless $2 == 200; # response OK | ||
| 178 | } | ||
| 179 | shift; # zap server response | ||
| 180 | unless (@_) { | ||
| 181 | $r->{msg} = 'no headers from server'; | ||
| 182 | return undef; | ||
| 183 | } | ||
| 184 | while( $_ = shift @_ ) { | ||
| 185 | last unless $_; | ||
| 186 | my ($key,$val) = split(/:\s+/,$_,2); | ||
| 187 | $r->{lc $key} = $val; | ||
| 188 | } | ||
| 189 | $r->{content} = ''; | ||
| 190 | unless (@_) { | ||
| 191 | $r->{msg} = 'no content, no data found'; | ||
| 192 | return undef; | ||
| 193 | } | ||
| 194 | while( @_ ) { | ||
| 195 | $r->{content} .= (shift @_) . "\n"; | ||
| 196 | } | ||
| 197 | 1; | ||
| 198 | } | ||
| 199 | |||
| 200 | =item $rv=short_response($url,\%response,\%content,$timeout); | ||
| 201 | |||
| 202 | Fetch the short report from C<$url> and place the headers in C<%response>, | ||
| 203 | the content, parsed, in C<%content>. Optional C<$timeout>, default is 60 | ||
| 204 | seocnds. | ||
| 205 | |||
| 206 | %response contains http headers | ||
| 207 | |||
| 208 | %content contains key => value pairs | ||
| 209 | |||
| 210 | LaBrea => version | ||
| 211 | Tarpit => version | ||
| 212 | Report => version | ||
| 213 | Util => version | ||
| 214 | now => seconds since epoch (local) | ||
| 215 | tz => time zone (i.e. -0700) | ||
| 216 | threads => number of threads | ||
| 217 | total_IPs => total IP's | ||
| 218 | bw => bandwidth | ||
| 219 | |||
| 220 | input: URL, # complete url | ||
| 221 | i.e. www.foo.com/html_report.plx | ||
| 222 | \%response, | ||
| 223 | \%content, | ||
| 224 | |||
| 225 | returns: false on success | ||
| 226 | error message on failure | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub short_response { | ||
| 231 | my ($url,$rsp,$cnt,$timr) = @_; | ||
| 232 | local *S; | ||
| 233 | my ($s,$r,$port,$path) = open_http(*S,$url); | ||
| 234 | return $r unless $s; | ||
| 235 | $timr = 60 unless $timr; | ||
| 236 | |||
| 237 | my $max = 1024; # maximum response size | ||
| 238 | # including headers | ||
| 239 | my $buffer = ''; | ||
| 240 | eval { | ||
| 241 | local $SIG{ALRM} = sub { | ||
| 242 | close $s; | ||
| 243 | die 'short_response TIMEOUT'; | ||
| 244 | }; | ||
| 245 | alarm $timr; | ||
| 246 | |||
| 247 | print $s qq | ||
| 248 | |GET $path?short HTTP/1.0 | ||
| 249 | Host: $r:$port | ||
| 250 | User-Agent: LaBrea::Tarpit::Get $VERSION | ||
| 251 | |||
| 252 | |; | ||
| 253 | while ( $_ = readline($s) ) { | ||
| 254 | $buffer .= $_; | ||
| 255 | last if length($buffer) > $max; | ||
| 256 | } | ||
| 257 | close $s; | ||
| 258 | alarm 0; | ||
| 259 | }; | ||
| 260 | return 'timeout, failed to get short response' | ||
| 261 | if $@ =~ /short_response TIMEOUT/; | ||
| 262 | return $@ if $@; # show other errors | ||
| 263 | return 'invalid short response, no data' | ||
| 264 | unless $buffer; | ||
| 265 | |||
| 266 | return $rsp->{rc} . ' ' . $rsp->{msg} | ||
| 267 | unless parse_http_response(\$buffer,$rsp); | ||
| 268 | |||
| 269 | return 'invalid content-type ' . $rsp->{'content-type'} | ||
| 270 | unless $rsp->{'content-type'} =~ m|text/plain|i; | ||
| 271 | |||
| 272 | %$cnt = split(/[=\n]/,$rsp->{content}); | ||
| 273 | return 'invalid data in short response' | ||
| 274 | unless exists $cnt->{LaBrea} && | ||
| 275 | exists $cnt->{Tarpit} && | ||
| 276 | exists $cnt->{Report} && | ||
| 277 | exists $cnt->{Util} && | ||
| 278 | exists $cnt->{now} && | ||
| 279 | exists $cnt->{tz} && | ||
| 280 | exists $cnt->{threads} && | ||
| 281 | exists $cnt->{total_IPs} && | ||
| 282 | exists $cnt->{bw}; | ||
| 283 | 0; | ||
| 284 | } | ||
| 285 | |||
| 286 | =item $line = make_line($url,$err,\%content); | ||
| 287 | |||
| 288 | Make a line of text summarizing the short report where C<$err> is the return | ||
| 289 | value from C<short_report> | ||
| 290 | |||
| 291 | Format: | ||
| 292 | |||
| 293 | url threads total_IPs bw time tz version:nn:nn:nn | ||
| 294 | or | ||
| 295 | url error message | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub make_line { | ||
| 300 | my ($url,$err,$cnt) = @_; | ||
| 301 | return "$url ", ($err || | ||
| 302 | "$cnt->{threads} $cnt->{total_IPs} $cnt->{bw} $cnt->{now} $cnt->{tz} $cnt->{LaBrea}:$cnt->{Tarpit}:$cnt->{Report}:$cnt->{Util}"); | ||
| 303 | } | ||
| 304 | |||
| 305 | =item $rv = not_hour($file); | ||
| 306 | |||
| 307 | Check if the file has been accessed this hour; | ||
| 308 | |||
| 309 | input: path/to/file | ||
| 310 | returns: true, not current hour | ||
| 311 | false if accessed this hour | ||
| 312 | or non-existent or not readable | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | sub not_hour { | ||
| 317 | return undef unless -e $_[0] && -r $_[0]; | ||
| 318 | my @old = localtime((stat($_[0]))[8]); | ||
| 319 | @_ = localtime(time); | ||
| 320 | return $old[2] != $_[2] || $old[3] != $_[3]; | ||
| 321 | } | ||
| 322 | |||
| 323 | =item $rv = not_day($file); | ||
| 324 | |||
| 325 | Check if the file has been accessed this day; | ||
| 326 | |||
| 327 | input: path/to/file | ||
| 328 | returns: true, not accessed this day | ||
| 329 | false if accessed this day | ||
| 330 | or non-existent or not readable | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub not_day { | ||
| 335 | return 1 unless -e $_[0] && -r $_[0]; | ||
| 336 | return (localtime((stat($_[0]))[8]))[3] != (localtime(time))[3]; | ||
| 337 | } | ||
| 338 | |||
| 339 | =item $rv=auto_update($url,$file,$cur_ver,$timeout); | ||
| 340 | |||
| 341 | Update the 'other_sites.txt' file from $url on a daily | ||
| 342 | basis only. | ||
| 343 | |||
| 344 | input: url, # complete url to 'other_sites.txt' | ||
| 345 | # http://scans.bizsystems.net/other_sites.txt | ||
| 346 | |||
| 347 | file, # path to your 'other_sites.txt' | ||
| 348 | |||
| 349 | cur_ver # optional current version | ||
| 350 | # the current file will be opened and scanned | ||
| 351 | # if this is not supplied | ||
| 352 | |||
| 353 | timeout # wait for http response | ||
| 354 | # default 60 seconds | ||
| 355 | returns: false on success or no update needed | ||
| 356 | error msg on failure | ||
| 357 | |||
| 358 | =back | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | sub auto_update { | ||
| 363 | my ($url,$file,$cur_ver,$timr,$debug) = @_; | ||
| 364 | $timr = 60 unless $timr; | ||
| 365 | local *S; | ||
| 366 | my ($S,$host,$port,$path)=open_http(*S,$url); | ||
| 367 | return $host unless $S; # return error message | ||
| 368 | my $buffer = ''; | ||
| 369 | eval { | ||
| 370 | local $SIG{ALRM} = sub { | ||
| 371 | close $S; | ||
| 372 | die 'auto_update TIMEOUT'; | ||
| 373 | }; | ||
| 374 | alarm $timr; | ||
| 375 | |||
| 376 | print $S <<EOF; | ||
| 377 | GET /$path HTTP/1.0 | ||
| 378 | Host: $host:$port | ||
| 379 | User-Agent: LaBrea::Tarpit::Get $VERSION | ||
| 380 | |||
| 381 | EOF | ||
| 382 | |||
| 383 | while (<$S>) { | ||
| 384 | $buffer .= $_; | ||
| 385 | } | ||
| 386 | close $S; | ||
| 387 | alarm 0; | ||
| 388 | }; # end eval | ||
| 389 | return 'url timed out' if $@ =~ /TIMEOUT/; | ||
| 390 | return $@ if $@; # return errors | ||
| 391 | my %response; | ||
| 392 | parse_http_response(\$buffer,\%response); | ||
| 393 | return 'failed to find version number' | ||
| 394 | unless $response{content} =~ /VERSION\s*=\s*(\d+)/; | ||
| 395 | my $new_ver = $1; | ||
| 396 | unless ($cur_ver) { # sigh.... must get old version number | ||
| 397 | # very inefficient | ||
| 398 | return "failed to open $file" | ||
| 399 | unless open(S,$file); | ||
| 400 | while (<S>) { | ||
| 401 | next unless $_ =~ /VERSION\s*=\s*(\d+)/; | ||
| 402 | $cur_ver = $1; | ||
| 403 | last; | ||
| 404 | } | ||
| 405 | close S; | ||
| 406 | } | ||
| 407 | return 'failed to find current version number' | ||
| 408 | unless $cur_ver; | ||
| 409 | if ( $cur_ver < $new_ver ) { | ||
| 410 | return "failed to open $file..tmp for update" | ||
| 411 | unless open(S,'>'.$file.'.tmp'); | ||
| 412 | $_ = select S; | ||
| 413 | $| = 1; | ||
| 414 | select $_; | ||
| 415 | print S $response{content}; | ||
| 416 | close S; | ||
| 417 | # atomic update | ||
| 418 | rename $file .'.tmp', $file unless $debug; | ||
| 419 | } | ||
| 420 | return undef; | ||
| 421 | } | ||
| 422 | 1; | ||
| 423 | __END__ |