16. 掃瞄

子網域掃瞄

Webscan.pm

package S3::Webscan;

# $Id: chapG.sgml,v 1.1.1.1 2003/08/14 00:26:12 ols3 Exp $

use 5.006;
use strict;
use warnings;
use Carp qw(croak);
require Exporter;

our @ISA = qw(Exporter);

use Socket;
use Net::Ping;

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use S3::Webscan ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
	
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	
);

our $VERSION = '0.02';


# Preloaded methods go here.


sub new {
	my $pkg = shift;
	my $self = { p =>  Net::Ping->new('icmp') };
	bless($self, $pkg);
	return $self;
}


sub pingit {
	my ($self, $host, $second)=@_;
	return $self->{p}->ping($host, $second);
}


sub printWebServer {

	my ($selft, $host)=@_;

	socket(FH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || croak("Socket failed: $!");
	my $sin=sockaddr_in(80, inet_aton($host));
	if (!connect(FH, $sin)) {
		print "Unable to connect to $host.\n";
		close(FH); return;
	}

	my $old_fh=select(FH);
	$|=1;
	select($old_fh);

	print FH "GET / HTTP/1.0\n\n";

	while (<FH>) {
		if (/^Server: (.+)/) {
			print "$host -\> $1\n"; close(FH); return;
		}
	}
	print "$host -> unknow web server type!\n";
}

sub closeping {
	my $self=shift;
	$self->{p}->close();

}

sub usage {

	print <<HERE;
Usage: 
-----------------------------------------------------------------
chmod +x ols3webscan.pl 

then

./ols3webscan.pl IP

OR

./ols3webscan.pl net/mask
-----------------------------------------------------------------
ex1:
./ols3webscan.pl 10.0.0.1

ex2:
./ols3webscan.pl 10.0.0.0/24    for 1C subnetwork
./ols3webscan.pl 10.0.0.0/25    for 1/2C subnetwork
./ols3webscan.pl 10.0.0.0/26    for 1/4C subnetwork

note:
./ols3webscan.pl 10.0.0.1/32 is same as ./ols3webscan.pl 10.0.0.1
-----------------------------------------------------------------
HERE

	exit;
}

sub getnodes {
	my ($self, $j)=@_;
	if (!$j) { return 1; }
	$j == 24 && do { return 253; };
	$j == 25 && do { return 126; };
	$j == 26 && do { return 61;  };
	$j == 27 && do { return 29;  };
	$j == 28 && do { return 13;  };
	$j == 29 && do { return 5;   };
	$j == 30 && do { return 1;   };
	$j == 32 && do { return 1;   };
	return 1;
}


1;
__END__
#

=head1 NAME

S3::Webscan - Perl extension for scaning web server in your network.

=head1 SYNOPSIS

  use S3::Webscan;
  

=head1 ABSTRACT

  S3::Webscan.

=head1 DESCRIPTION

Perl extension for scaning web server in your network.

=head2 EXPORT

None by default.


=head1 SEE ALSO

http://teacher.mdjh.tnc.edu.tw/~ols3/

=head1 AUTHOR

OLS3 <lt>ols3@www.tnc.edu.tw<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2003 by OLS3

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut

ols3webscan.pl

#! /usr/bin/perl
# $Id: chapG.sgml,v 1.1.1.1 2003/08/14 00:26:12 ols3 Exp $
#
# 目前只提供 scan C class,若要 scan B class,請自行修改。

use S3::Webscan;
use strict;

my $ping_time_out=1;
my $nethost=$ARGV[0];
my $n = S3::Webscan->new();

unless ($nethost) { $n->usage(); }

my ($net, $mask)=split(/\//, $nethost);

if ($net !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { $n->usage(); }

my $nodes=$n->getnodes($mask);

my ($n1,$n2,$n3,$n4)=split(/\./, $net);

for (my $i<=0; $i<$nodes; $i++) {
	if ($n4 < 1) { $n4=1; }
	if ($n4 > 253) { $n4=253; }
	my $host="$n1.$n2.$n3.$n4";
	unless ($n->pingit($host, $ping_time_out)) {
		print "$host is not alive!\n";
	} else {
		$n->printWebServer($host);
	}
	if ($n4++==253) { last; }
} 

$n->closeping();