| 1 | package NetworkInfo::Discovery::Scan; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use NetworkInfo::Discovery::Detect; | ||
| 7 | use base ("NetworkInfo::Discovery::Detect"); | ||
| 8 | |||
| 9 | use Socket; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | NetworkInfo::Discovery::Scan - host/port scanner | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use NetworkInfo::Discovery; | ||
| 18 | use NetworkInfo::Discovery::Register; | ||
| 19 | use NetworkInfo::Discovery::Scan; | ||
| 20 | |||
| 21 | my $disc = new NetworkInfo::Discovery::Register ( | ||
| 22 | 'file' => '/tmp/scan.register', | ||
| 23 | 'autosave' => 1 | ||
| 24 | ) | ||
| 25 | || warn ("failed to make new obj"); | ||
| 26 | |||
| 27 | my $scan = new NetworkInfo::Discovery::Scan ( | ||
| 28 | hosts=>["localhost", "127.0.0.1"], | ||
| 29 | ports=>[53,99,1000..1004], | ||
| 30 | timeout=>1, | ||
| 31 | 'wait'=>0, | ||
| 32 | protocol => 'tcp' | ||
| 33 | ); | ||
| 34 | |||
| 35 | $scan->do_it(); | ||
| 36 | $disc->add_interface($_) for ($scan->get_interfaces); | ||
| 37 | |||
| 38 | foreach my $h ($scan->get_interfaces) { | ||
| 39 | print $h->{ip} . "\n"; | ||
| 40 | print " has tcp ports: " . join(',',@{$h->{tcp_open_ports}}) . "\n" if (exists $h->{tcp_open_ports}) ; | ||
| 41 | } | ||
| 42 | |||
| 43 | $scan->{protocol} = 'tcp'; | ||
| 44 | $scan->{ports} = [20..110]; | ||
| 45 | $scan->do_it(); | ||
| 46 | $disc->add_interface($_) for ($scan->get_interfaces); | ||
| 47 | |||
| 48 | foreach my $h ($scan->get_interfaces) { | ||
| 49 | print $h->{ip} . "\n"; | ||
| 50 | print " has tcp ports: " . join(',',@{$h->{tcp_open_ports}}) . "\n" if (exists $h->{tcp_open_ports}) ; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | C<NetworkInfo::Discovery::Scan> is a host/port scanner that is used to | ||
| 57 | find hosts that are too quiet for C<NetworkInfo::Discovery::Sniff> to | ||
| 58 | find. | ||
| 59 | It is a detection module subclassed from C<NetworkInfo::Discovery::Detect>. | ||
| 60 | We can probe tcp or udp ports. | ||
| 61 | There is the ability to set a timeout on the connection so that we don't | ||
| 62 | wait all day for the scan to finish. | ||
| 63 | There is a wait attribute that keeps us from scanning the network too fast | ||
| 64 | and affecting it in a negetive manner. | ||
| 65 | The hosts attribute takes hostnames, ipaddresses, or CIDR-like network/bitmask | ||
| 66 | lists. | ||
| 67 | |||
| 68 | =head1 METHODS | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | =pod | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item new | ||
| 77 | |||
| 78 | returns a new Scan object, and takes the arguments shown in this example: | ||
| 79 | |||
| 80 | $obj = NetworkInfo::Discovery::Scan->new( | ||
| 81 | hosts => ["hostname", "1.2.3.4", "4.3.2.1/26"], | ||
| 82 | [protocol => ("tcp"|"udp")], | ||
| 83 | [timeout => 3 ], #in seconds | ||
| 84 | [wait => 100], #in miliseconds | ||
| 85 | [ports => [80,23,100..300] ], | ||
| 86 | ); | ||
| 87 | |||
| 88 | timeout is the amount of time to wait in seconds before giving up on a connection attempt. | ||
| 89 | wait is how long to wait in miliseconds between each probe. | ||
| 90 | protocol is either tcp or udp -- we will only try to connect to that type of port. | ||
| 91 | hosts is an array ref of hosts to try. The CIDR-like addresses will be expanded out | ||
| 92 | (i.e., 172.16.1.129/24 is really 172.16.1.(0-255)). If this type of addres is used, we | ||
| 93 | do not scan the top and bottom of the range, as this should be the network and broadcast addresses. | ||
| 94 | ports is and arrey ref of numeric ports to scan for each host. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub new { | ||
| 99 | my $classname = shift; | ||
| 100 | my %args = @_; | ||
| 101 | |||
| 102 | my $class = ref($classname) || $classname; | ||
| 103 | |||
| 104 | |||
| 105 | my $self = { | ||
| 106 | # this will hold our expanded hosts | ||
| 107 | _hosts => {}, # and this is our private version | ||
| 108 | |||
| 109 | # set defaults | ||
| 110 | timeout => 5, # don't hang on connect forever | ||
| 111 | 'wait' => 25, # this is a wait between connect attempts in miliseconds | ||
| 112 | protocol => 'tcp', | ||
| 113 | ports => [80, 23, 22], | ||
| 114 | |||
| 115 | # these are from Detect.pm | ||
| 116 | hostlist => [], | ||
| 117 | hoplist => [], | ||
| 118 | |||
| 119 | }; | ||
| 120 | |||
| 121 | bless ($self, $class); | ||
| 122 | |||
| 123 | if ( $args{hosts} ) { | ||
| 124 | $self->hosts($args{hosts}); | ||
| 125 | delete $args{hosts}; | ||
| 126 | } | ||
| 127 | |||
| 128 | while (my ($k, $v) = each (%args) ) { | ||
| 129 | $self->{$k} = $v; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | return $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =pod | ||
| 138 | |||
| 139 | =item do_it | ||
| 140 | |||
| 141 | Runs the scan, builds up hosts out of what we found open, and returns the hostlist. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub do_it { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | $self->scan; | ||
| 149 | $self->make_hosts; | ||
| 150 | |||
| 151 | return $self->get_interfaces; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | =pod | ||
| 156 | |||
| 157 | =item hosts ([$aref] | ||
| 158 | |||
| 159 | Builds up our list of hosts to scan, or returns that list. | ||
| 160 | The aref can contain any of the following: | ||
| 161 | "hostname" | ||
| 162 | "1.2.3.4" | ||
| 163 | "1.2.3.4/26" | ||
| 164 | |||
| 165 | The CIDR-like address will be expanded into an address range and all the | ||
| 166 | hosts in that range (minus the top one, and the bottom one) will be | ||
| 167 | added to the host list to be scanned. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub hosts { | ||
| 172 | my $self = shift; | ||
| 173 | my $aref = shift; | ||
| 174 | |||
| 175 | #figure out what host format they are using | ||
| 176 | foreach (@$aref) { | ||
| 177 | # print "hosts: $_\n"; | ||
| 178 | # if is is a regular ipaddress, use it like regular | ||
| 179 | if (m#^\d+\.\d+\.\d+\.\d+$#) { | ||
| 180 | push (@{$self->{hosts}}, $_); | ||
| 181 | # print "hosts -- single host: $_\n"; | ||
| 182 | |||
| 183 | # if it is in CIDR notation, expand it | ||
| 184 | } elsif (m#^(\d+\.\d+\.\d+\.\d+)(?:/(\d+))$#) { | ||
| 185 | # print "hosts -- CIDR host: $_\n"; | ||
| 186 | |||
| 187 | # 0.0.0.0/0 matches all | ||
| 188 | if (($1 eq "0.0.0.0") and ($2 eq 0)) { | ||
| 189 | next; # please ignore an "all" internet scan | ||
| 190 | } | ||
| 191 | |||
| 192 | # put the ip addr into machine representation | ||
| 193 | my $mbits = $2 ; | ||
| 194 | my $bits = 32 - ($2 || 32) ; | ||
| 195 | my $baseIP = unpack("N", pack("C4", split(/\./, $1))); | ||
| 196 | my $mask = unpack("N", pack("B32", "1" x $mbits . "0" x $bits)); | ||
| 197 | my $maskedIP = $baseIP & $mask; | ||
| 198 | |||
| 199 | # starting at the baseIP ending at the mask limit, | ||
| 200 | # increment the ipaddress, | ||
| 201 | # convert it back to a dotted quad, and add it to the list | ||
| 202 | # print "base = $baseIP,masked =$maskedIP, bits = $bits, 2**bits = " . (2**$bits -2) . "\n"; | ||
| 203 | for (my $i=1; $i <= (2**$bits-2) ; $i++ ) { | ||
| 204 | $maskedIP += 1; | ||
| 205 | my $ip = join(".",unpack("C4", pack("N",$maskedIP))); | ||
| 206 | # print "i=$i, i < " .(2**$bits-2) . " ip=$ip \n"; | ||
| 207 | push (@{$self->{hosts}}, $ip); | ||
| 208 | } | ||
| 209 | |||
| 210 | # perhaps this is just the dns name? | ||
| 211 | } elsif (m#([\w.]+)# ) { | ||
| 212 | push (@{$self->{hosts}}, $_); | ||
| 213 | } | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | return $self->{'hosts'}; | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | =pod | ||
| 222 | |||
| 223 | =item scan | ||
| 224 | |||
| 225 | Runs a tcp or udp scan against the hosts in our host list. This method | ||
| 226 | uses alarm if you have set the "timeout" attribute, so please keep that | ||
| 227 | in mind and don't use your own alarm while running this. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | sub scan { | ||
| 232 | my $self = shift; | ||
| 233 | |||
| 234 | foreach my $host (@{$self->{hosts}}) { | ||
| 235 | # print "scanning $host\n"; | ||
| 236 | |||
| 237 | foreach my $port (@{$self->{ports}} ) { | ||
| 238 | my $success; | ||
| 239 | |||
| 240 | # print " port $port\n"; | ||
| 241 | select(undef, undef, undef, $self->{'wait'}/100) if ($self->{'wait'}); | ||
| 242 | |||
| 243 | # print " done waiting\n"; | ||
| 244 | |||
| 245 | # build the address of the remote machine | ||
| 246 | my $internet_addr = inet_aton($host) | ||
| 247 | or (warn "Couldn't convert $host into an Internet address: $!\n" && next); | ||
| 248 | my $paddr = sockaddr_in($port, $internet_addr); | ||
| 249 | |||
| 250 | # create a socket | ||
| 251 | if ($self->{protocol} eq "udp") { | ||
| 252 | socket(HOST, PF_INET, SOCK_DGRAM, getprotobyname("udp")) | ||
| 253 | or (warn "failed to open socket: $!" && next); | ||
| 254 | |||
| 255 | # udp dosn't use "connect", send and recv instead | ||
| 256 | $success = $self->try_timeout( | ||
| 257 | sub { | ||
| 258 | my $msg = "This is not an attack." . | ||
| 259 | " NetworkInfo::Discovery 0.07."; | ||
| 260 | my $reply; | ||
| 261 | send (HOST, $msg, 0, $paddr) | ||
| 262 | || die ("udp send to $host:$port : $!"); | ||
| 263 | recv(HOST, $reply, 0, 0) | ||
| 264 | || die ("udp recv from $host:$port : $!"); | ||
| 265 | } | ||
| 266 | ); | ||
| 267 | } elsif ($self->{protocol} eq "tcp") { | ||
| 268 | socket(HOST, PF_INET, SOCK_STREAM, getprotobyname('tcp')) | ||
| 269 | or (warn "failed to open socket: $!" && next); | ||
| 270 | |||
| 271 | # try the connect | ||
| 272 | $success = $self->try_timeout( | ||
| 273 | sub { | ||
| 274 | connect(HOST, $paddr) | ||
| 275 | || die "connect error: $!"; | ||
| 276 | } | ||
| 277 | ); | ||
| 278 | } else { | ||
| 279 | warn "unknown protocol: " . $self->{protocol} . "\n"; | ||
| 280 | next; | ||
| 281 | } | ||
| 282 | |||
| 283 | if ($success) { | ||
| 284 | $self->{_hosts}{$host}{$port} = $success; | ||
| 285 | } | ||
| 286 | |||
| 287 | # ... do something with the socket | ||
| 288 | #print HOST "Why don't you call me anymore?\n\n\n"; | ||
| 289 | |||
| 290 | # and terminate the connection when we're done | ||
| 291 | # print " closing the socket\n"; | ||
| 292 | close(HOST); | ||
| 293 | } | ||
| 294 | |||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | =pod | ||
| 299 | |||
| 300 | =item make_hosts | ||
| 301 | |||
| 302 | Builds our hostlist according to how | ||
| 303 | C<NetworkInfo::Discovery::Detect> requires. | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | sub make_hosts { | ||
| 308 | my $self = shift; | ||
| 309 | |||
| 310 | my @hostname; | ||
| 311 | |||
| 312 | while (my ($host, $href) = each %{$self->{_hosts}} ) { | ||
| 313 | # print "found host $host\n"; | ||
| 314 | my $ports; | ||
| 315 | foreach my $port ( keys (%{$href} ) ) { | ||
| 316 | # print " found port $port\n"; | ||
| 317 | push (@{$ports}, $port); | ||
| 318 | } | ||
| 319 | |||
| 320 | my $hostObj; | ||
| 321 | if ($self->{protocol} eq 'udp') { | ||
| 322 | $self->add_interface({ | ||
| 323 | ip=>$host, | ||
| 324 | dns=>$self->lookup_ip($host), | ||
| 325 | udp_open_ports=> $ports, | ||
| 326 | }); | ||
| 327 | } else { | ||
| 328 | $self->add_interface({ | ||
| 329 | ip=>$host, | ||
| 330 | dns=>$self->lookup_ip($host), | ||
| 331 | tcp_open_ports=> $ports, | ||
| 332 | }); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | } | ||
| 337 | |||
| 338 | =pod | ||
| 339 | |||
| 340 | =item try_timeout | ||
| 341 | |||
| 342 | This is a wrapper function to wrap our connection attempts in an | ||
| 343 | alarmed eval. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | sub try_timeout { | ||
| 348 | my $self = shift; | ||
| 349 | my $sub = shift; | ||
| 350 | |||
| 351 | |||
| 352 | eval { | ||
| 353 | local $SIG{ALRM} = sub { die "timeout" }; | ||
| 354 | |||
| 355 | # print " setting alarm for " . $self->{timeout} ." seconds\n"; | ||
| 356 | alarm($self->{timeout}); | ||
| 357 | |||
| 358 | &$sub(); # long-time operations here | ||
| 359 | |||
| 360 | alarm(0); | ||
| 361 | # print " unsetting alarm for $self->{timeout} seconds\n"; | ||
| 362 | }; | ||
| 363 | |||
| 364 | # if there was an error in the eval | ||
| 365 | if ($@) { | ||
| 366 | # print " bailing -- croaked out of the eval: "; | ||
| 367 | if ($@ =~ /^timeout$/) { | ||
| 368 | # print "with a timeout\n"; | ||
| 369 | return 0; | ||
| 370 | } elsif ($@ =~ /^connect error/) { | ||
| 371 | # print "with an $@\n"; | ||
| 372 | return 0; | ||
| 373 | } else { | ||
| 374 | # print "with an unknown error: $@\n"; | ||
| 375 | alarm(0); # clear the still-pending alarm | ||
| 376 | return undef; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | # print " SUCCESS: got connection!\n"; | ||
| 380 | |||
| 381 | return 1; | ||
| 382 | } | ||
| 383 | |||
| 384 | =pod | ||
| 385 | |||
| 386 | =item lookup_ip ($ip) | ||
| 387 | |||
| 388 | Does a reverse lookup on the IP and returns a list of the names we found. | ||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | sub lookup_ip { | ||
| 393 | my $self = shift; | ||
| 394 | my $ip = shift; | ||
| 395 | |||
| 396 | my @name_lookup; | ||
| 397 | my @resolved_ips; | ||
| 398 | |||
| 399 | my $claimed_hostname = gethostbyaddr($ip, AF_INET) || return undef; | ||
| 400 | #print "looking up $ip hostname: $claimed_hostname, @name_lookup, @resolved_ips"; | ||
| 401 | @name_lookup = gethostbyname($claimed_hostname) | ||
| 402 | or die "Could not look up $claimed_hostname : $!\n"; | ||
| 403 | |||
| 404 | @resolved_ips = map { inet_ntoa($_) } | ||
| 405 | @name_lookup[ 4 .. $#name_lookup ]; | ||
| 406 | #print "looking up $ip hostname: $claimed_hostname, @name_lookup, @resolved_ips"; | ||
| 407 | } | ||
| 408 | sub lookup { | ||
| 409 | my $self = shift; | ||
| 410 | my $sock = shift; | ||
| 411 | |||
| 412 | my $remote = getpeername($sock) | ||
| 413 | or die "Couldn't identify other end: $!\n"; | ||
| 414 | my ($port, $iaddr) = unpack_sockaddr_in($remote); | ||
| 415 | my $actual_ip = inet_ntoa($iaddr); | ||
| 416 | my $claimed_hostname = gethostbyaddr($iaddr, AF_INET); | ||
| 417 | my @name_lookup = gethostbyname($claimed_hostname) | ||
| 418 | or die "Could not look up $claimed_hostname : $!\n"; | ||
| 419 | |||
| 420 | my @resolved_ips = map { inet_ntoa($_) } | ||
| 421 | @name_lookup[ 4 .. $#name_lookup ]; | ||
| 422 | #print "looking up $actual_ip: hostname: $claimed_hostname, @name_lookup, @resolved_ips"; | ||
| 423 | } | ||
| 424 | |||
| 425 | =back | ||
| 426 | |||
| 427 | =head1 AVAILABILITY | ||
| 428 | |||
| 429 | This module can be found in CPAN at http://www.cpan.org/authors/id/T/TS/TSCANLAN/ | ||
| 430 | or at http://they.gotdns.org:88/~tscanlan/perl/ | ||
| 431 | =head1 AUTHOR | ||
| 432 | |||
| 433 | Tom Scanlan <tscanlan@they.gotdns.org> | ||
| 434 | |||
| 435 | =head1 AUTHOR | ||
| 436 | |||
| 437 | Tom Scanlan <tscanlan@they.gotdns.org> | ||
| 438 | |||
| 439 | =head1 SEE ALSO | ||
| 440 | |||
| 441 | L<NetworkInfo::Discovery::Detect> | ||
| 442 | |||
| 443 | L<NetworkInfo::Discovery::Sniff> | ||
| 444 | |||
| 445 | L<NetworkInfo::Discovery::Traceroute> | ||
| 446 | |||
| 447 | =head1 BUGS | ||
| 448 | |||
| 449 | Please send any bugs to Tom Scanlan <tscanlan@they.gotdns.org> | ||
| 450 | |||
| 451 | =head1 COPYRIGHT AND LICENCE | ||
| 452 | |||
| 453 | Copyright (c) 2002 Thomas P. Scanlan IV. All rights reserved. | ||
| 454 | This program is free software; you can redistribute it and/or | ||
| 455 | modify it under the same terms as Perl itself. | ||
| 456 | |||
| 457 | =cut | ||
| 458 | |||
| 459 | 1; | ||
| 460 | |||
| 461 | 1; |