<?xml version="1.0" encoding="windows-1252"?>
<node id="151517" title="IISScanner" created="2002-03-13 15:46:31" updated="2005-08-15 16:24:56">
<type id="1748">
sourcecode</type>
<author id="149668">
zeroquo</author>
<data>
<field name="doctext">
&lt;code&gt;
use strict;
no strict "subs";
use Win32::Service;
use Socket;
use LWP::UserAgent;

system("cls") if ($ENV{OS});

my $script     = "IISScanner";
my $version    = "0.01";
my $written    = "13 Mar 2002";
my $revised    = "13 Mar 2002";
my $writer     = "Oscar Alarcon R";
my $Error_code = 1;
my($equipo, $status_servicio, $status_http, $port_ok);
my %options = (
		"l"	=&gt;	"Archive of machine list to be Scanning.",
		"m"	=&gt;	"UNC Path Machine name.",
		"log"	=&gt;	"Log File name.",
		"help"	=&gt;	"This Screen.",
		"nops"	=&gt;	"No port Scanning, only scanning port 80."
		);
my %options_detail = (
		"l"	=&gt;	"Archive",
		"m"	=&gt;	"\\\\UNCName",
		"log"	=&gt;	"Archive",
		"help"	=&gt;	" ",
		"nops"	=&gt;	" "
		);
my $example = $script." -m:\\server1 -nops -log:c:\\temp\\web.log";
$~ = BANNER;
write;
my %param;

(%param)=&amp;TAKE_Param();
&amp;SINTAX_Error() if($param{help});
&amp;ECHO_Param() if($param{debug});
print "SYSM &gt;&lt; Calling TAKE_HostName\n" if( $param{debug} );
my(@list_server)=&amp;TAKE_HostName();
print "SYSM &gt;&lt; Calling CHEK_Proccess\n" if( $param{debug} );
my(@list_out)=&amp;CHEK_Proccess(@list_server);
$~ = INFORME;
if($param{log}){
	print "SYSM &gt;&lt; Printing Log $param{log}\n" if( $param{debug} );
	open(LOG_HEADER, "&gt;$param{log}");
	write(LOG_HEADER);
	close(LOG_HEADER);
	open(LOG, "&gt;&gt;$param{log}");
}
foreach my $data_equ (@list_out){
	chomp($data_equ);
	($equipo, $status_servicio, $status_http, $port_ok)=split(/\,/, $data_equ);
	write;
	write(LOG); 
}
if($param{log}){
	close(LOG);
}
print "[END CODE]\n" if( $param{debug} );
exit;


sub CHEK_Proccess {
	my(@servers) = @_;
	my(@output)  = ();
	print "IN  CHEK_Proccess\n" if( $param{debug} );
	foreach my $linea (@servers){
		chomp($linea);
		print "	DATA &lt;&lt; $linea\n" if( $param{debug} );
		print "	SYSM &gt;&lt; Calling SCAN_IISSrv\n" if( $param{debug} );
		my($status)=&amp;SCAN_IISSrv($linea);
		if( $status eq "OK" ){
			print "		SYSM &gt;&lt; Calling SCAN_Ports\n" if( $param{debug} );
			my(@ports_server)=&amp;SCAN_Ports($linea);
			foreach my $port_v (@ports_server){
				chomp($port_v);
				print "			SYSM &gt;&lt; Calling SCAN_Https\n" if( $param{debug} );
				my($status2)=&amp;SCAN_Https($linea, $port_v);
				if( $status2 eq "OK" ){
					my $temporal = $linea.",".$status.",".$status2.",".$port_v;
					push(@output, $temporal);
				}	
			}
		}
	}
	print "OUT CHEK_Proccess\n" if( $param{debug} );
	return(@output);
}

sub SCAN_IISSrv {
	my($machine)   = @_;
	my $result     = "FAIL";
	my @service_l  = ( "W3SVC", "IISADMIN" );
	my %status1     = ();
	my %status2     = ();
	print "		IN  SCAN_IISSrv\n" if( $param{debug} );
	print "			DATA &lt;&lt; $machine\n" if( $param{debug} );
	print "			SYSM &gt;&lt; Scanning Services\n" if( $param{debug} );
	Win32::Service::GetStatus($machine, $service_l[0], \%status1);		
	Win32::Service::GetStatus($machine, $service_l[1], \%status2);		
	print "			SYSM &gt;&lt; Check Status of Services\n" if( $param{debug} );
	$result = "OK" if( $status1{CurrentState} eq 4 &amp;&amp; $status2{CurrentState} eq 4 );
	print "			DATA &gt;&gt; $result\n" if( $param{debug} );
	print "		OUT SCAN_IISSrv\n" if( $param{debug} );
	return($result);
}

sub SCAN_Ports {
	my($machine)   = @_;
	my(@listado_p) = ();
	my(@ports)     = ();
	print "			IN  SCAN_Ports\n" if( $param{debug} );
	print "				DATA &lt;&lt; $machine\n" if( $param{debug} );
	if( $param{nops}){
		@ports = ( 80 );
	}else{
		@ports = ( 80 .. 100, 940 .. 1000, 10000 );
	}
	foreach my $port (@ports){
		chomp($machine, $port);
		$machine =~ s/\\//eg;
		print "				SYSM &gt;&lt; Scanning Port $port\n" if( $param{debug} );
		socket(TO_SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
		my $internet_addr = inet_aton($machine);
		my $paddr = sockaddr_in($port, $internet_addr);
		if(connect(TO_SERVER, $paddr)){
			print "				DATA &gt;&gt; $port is Active\n" if( $param{debug} );		
			push(@listado_p, $port);
			close(TO_SERVER);
		}
	}
	print "			OUT SCAN_Ports\n" if( $param{debug} );
	return(@listado_p);
}
sub SCAN_Https {
	my($machine, $port)   = @_;
	my $result            = "FAIL";
	$machine              =~ s/\\//eg;
	my $url               = "http://".$machine.":".$port."/";
	print "				IN  SCAN_Https\n" if( $param{debug} );
	print "					DATA &lt;&lt; $machine on $port\n" if( $param{debug} );
	my $ua = LWP::UserAgent-&gt;new;
	my $request = HTTP::Request-&gt;new(GET =&gt; $url);
	my $respons = $ua-&gt;request($request);
	$ua-&gt;proxy(['http', 'ftp'] , $url);
	my $h = new HTTP::Headers;
	my %accepts = $h-&gt;clone;
	if ($respons-&gt;is_success) { 
		print "					DATA &gt;&gt; $port is Active\n" if( $param{debug} );		
		$result = "OK";
	}
	print "				OUT SCAN_Https\n" if( $param{debug} );
	return($result);
}

sub TAKE_HostName {
	my(@hostname) = ();
	print "IN  TAKE_HostName\n" if( $param{debug} );
	if( $param{l} ){
		open(SRC, "&lt;$param{l}")|| ($Error_code = 0);
		if( $Error_code eq 0){
			print "	SYSM &gt;&lt; Cant Open $param{l}\n" if( $param{debug} );
			print "	Brutal EXIT !\n" if( $param{debug} );
			exit;
		}
		print "	SYSM &gt;&lt; Read archive $param{l}\n" if( $param{debug} );
		while(&lt;SRC&gt;){
			my $linea = $_;
			chomp($linea);
			print "	DATA &gt;&gt; $linea\n" if( $param{debug} );
			push(@hostname, $linea );
		}
		close(SRC);
	}else{
		print "	Read DATA $param{m}\n" if( $param{debug} );
		print "	DATA &gt;&gt; $param{m}\n" if( $param{debug} );
		push(@hostname, $param{m} );
	}
	print "OUT  TAKE_HostName\n" if( $param{debug} );
	return(@hostname);
}

sub TAKE_Param{
	if( @ARGV ){
		foreach my $parame ( @ARGV ){
			chomp($parame);
			my($key, $content, $content2)=split( /:/, $parame);
			if($key =~ /-/ || $key =~ /\//){
				$key = lc($key);
				$key =~ s/-//eg if($key =~ /-/);
				$key =~ s/\///eg if($key =~ /\//);
				$content = " " if(!$content);
				$param{$key} = $content if(!($key eq "log"));
				$param{$key} = $content.":".$content2 if($key eq "log");
			}else{
				&amp;SINTAX_Error();
			}
		}
	}else{
		&amp;SINTAX_Error();
	}
	&amp;SINTAX_Error("Recursive") if($param{"l"} &amp;&amp; $param{"m"});
	return(%param);
}

sub ECHO_Param{
	print " Debugging Option \n\n";
	print " [INPUT Trace] \n";
	foreach my $key ( keys(%param) ){
		if(!(lc($key) eq "debug")){
			chomp($key);
			print "		$options{$key}	=&gt; $param{$key}\n" if(!($param{$key} eq " "));
			print "		$options{$key}\n" if($param{$key} eq " ");
		}
	}
	print "\n";
	print "[BEGIN CODE]\n";
	return();
}

sub SINTAX_Error{
	my($Error) = @_;
	print " Sintax Error\n\n" if(!$param{help} &amp;&amp; !($Error eq "Recursive"));
	print " Critical Sintax Error\n\n Recursive parameter YOU DONT USE -l parameter whith -m parameter !\n\n" if($Error eq "Recursive");
	print " Help Screen \n\n" if($param{help});
	print " Usage $script [ OPTIONS ]\n";
	print "\n";
	foreach my $linea ( keys(%options) ){
		chomp($linea);
		print "	-".$linea.":[".$options_detail{$linea}."]	$options{$linea}\n" if(!($options_detail{$linea} eq " "));
		print "	-".$linea."  ".$options_detail{$linea}." 	$options{$linea}\n" if($options_detail{$linea} eq " ");
	}
	print "\n Example :\n 	$example\n";
	exit;
}

format BANNER =
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                              Revised : @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$script, $version, $revised
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                           @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$writer, $written
--------------------------------------------------------------------------------

Machine        Srv IIS      HTTP       Port
.

format INFORME =
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$equipo, $status_servicio, $status_http, $port_ok
.

format LOG_HEADER =
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                              Revised : @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$script, $version, $revised
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;                           @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$writer, $written
--------------------------------------------------------------------------------

Machine        Srv IIS      HTTP       Port
.

format LOG =
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
$equipo, $status_servicio, $status_http, $port_ok
.
&lt;/code&gt;</field>
<field name="codedescription">
&lt;p&gt;This code was made to Scan a machine or list of machines, for get information about IIS Server, if active, on which port.&lt;/p&gt;

Enjoy.&lt;br&gt;
&lt;p&gt;&lt;small&gt;2002-03-13 Edit by [Corion] : Added CODE tags&lt;/small&gt;&lt;/p&gt;</field>
<field name="codecategory">
Networking / NT ADmin</field>
<field name="codeauthor">
Oscar Alarcon RodriguezNeuquen 1276 - Bernal OesteArgentina</field>
</data>
</node>
