Script Modificado - 24/02/08 -
Using perl Syntax Highlighting
#!/usr/bin/perl
BEGIN {
eval "use Nmap::Scanner";
if ( $@ ) {
warn "Error to load module: Nmap::Scanner\n"
. "Install Module:\n"
. "\t\tcpan\n"
. "\t\tcpan> install Nmap::Scanner\n";
exit ();
}
}
use CGI ':standard';
use Nmap::Scanner;
use POSIX 'strftime';
use strict;
use warnings;
if ( @ARGV < 2 ) {
die "\nUso: $0 <hosts.txt> <reporte.html>\n\n"
. " hosts.txt Archivo que contiene las direcciones IP a auditar\n"
. " reporte.html Archivo el cual nos mostrara el reporte final de la auditoria\n\n"
;
}
my ($lista,$fichero) = @ARGV;
my $hora = strftime("%Y-%m-%d", localtime);
my $scanner = Nmap::Scanner->new();
$scanner->register_scan_started_event(\&scan_started);
$scanner->register_port_found_event(\&port_found);
my $opciones_scan = "-sS -A -vv -iL $lista";
my $antes = time();
open HTML, ">$fichero" or die "ERROR: No puedo escribir en $fichero:$!\n";
print HTML
start_html({
title => "Nmap Report $hora Red: $lista",
script=> q(function cambia(ip) {
var tr = document.getElementById(ip);
if ( tr.style.display == "none" ) {
tr.style.display = "table-row";
}
else {
tr.style.display = "none";
}
})
}),
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#E6E6FA',
background =>'header.gif',
},
[
Tr([
td([
(
{rowspan =>'4', width =>'10%'},
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Reporte creado el dia: $hora"),
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Opciones Nmap: $opciones_scan"),
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Archivo de Hosts: $lista"),
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Caracteristicas: ") .
font({ face => 'Verdana', size => 1, color => '#f3c035' }, "hosts - ") .
font({ face => 'Verdana', size => 1, color => '#00ff00' }, "active - ").
font({ face => 'Verdana', size => 1, color => '#ff0000' }, "no responds - ").
font({ face => 'Verdana', size => 1, color => '#00ff00' }, "open - ").
font({ face => 'Verdana', size => 1, color => '#ff0000' }, "close - ").
font({ face => 'Verdana', size => 1, color => '#6666ff' }, "tcp - ").
font({ face => 'Verdana', size => 1, color => '#666600' }, "udp"),
),
]),
])
]
);
# Comenzamos el escaneo
my @escaneo_ip;
$scanner->scan($opciones_scan);
my $duracion = time() - $antes;
# Salida del resultado
print HTML
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#f2f2f2',
},
Tr([
map {
td( { bgcolor => '#848484', onclick => "cambia('$_->{addresses}')" },
font({ face => 'Verdana', size => 1, color => '#f3c035' }, "$_->{hostname} ($_->{addresses})"),
$_->{status},
),
} @escaneo_ip
])
),
br(),
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#F2F2F2',
},
map {
Tr(
{ id => $_->{addresses}, style => 'display:none' },
[
td( { bgcolor => '#e6e6fa' },
font({ face => 'Verdana', size => 1, color => '#242424' },
join q{<br />}, @{$_->{port}}
),
),
]
)
} grep { defined $_->{port} } @escaneo_ip
),
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#646464',
},
[
Tr([
td([
(
font({ face => 'Verdana', size => 1}, "Time to ejecution: $duracion seconds"),
),
]),
])
]
),
end_html
;
close HTML;
sub scan_started {
my $self = shift;
my $host = shift;
my $hostname = $host->hostname();
my $addresses = join(',', map { $_->addr() } $host->addresses());
my $status = $host->status();
$status = ( $status eq 'up' )
? font({ face => 'Verdana', size => 1, color => '#00FF00' }, 'Activo' )
: font({ face => 'Verdana', size => 1, color => '#FF0000' }, 'No responde' )
;
# Guardamos la dirección, para crear la tabla fuera
push @escaneo_ip, { hostname => $hostname, addresses => $addresses, status => $status };
}
sub port_found {
my $self = shift;
my $host = shift;
my $port = shift;
my $name = $host->hostname();
my $puerto = $port->state();
my $proto = $port->protocol();
my $puerto_estado = ( $puerto eq 'open' ) ?
font({color=>'#00FF00'}, 'open' ) :
font({color=>'#FF0000'}, 'closed') ;
my $puerto_proto = ( $proto eq 'tcp' ) ?
font({color=>'#6666ff'}, 'tcp' ) :
font({color=>'#666600'}, 'udp') ;
# Guardamos los puertos detectados
push @{ $escaneo_ip[-1]->{port} },
join q{ },
$puerto_estado,
$puerto_proto,
$port->portid(),
((defined $port->service()->name() ) ? $port->service()->name() : ''),
((defined $port->service()->version()) ? $port->service()->version() : ''),
;
}
__END__
BEGIN {
eval "use Nmap::Scanner";
if ( $@ ) {
warn "Error to load module: Nmap::Scanner\n"
. "Install Module:\n"
. "\t\tcpan\n"
. "\t\tcpan> install Nmap::Scanner\n";
exit ();
}
}
use CGI ':standard';
use Nmap::Scanner;
use POSIX 'strftime';
use strict;
use warnings;
if ( @ARGV < 2 ) {
die "\nUso: $0 <hosts.txt> <reporte.html>\n\n"
. " hosts.txt Archivo que contiene las direcciones IP a auditar\n"
. " reporte.html Archivo el cual nos mostrara el reporte final de la auditoria\n\n"
;
}
my ($lista,$fichero) = @ARGV;
my $hora = strftime("%Y-%m-%d", localtime);
my $scanner = Nmap::Scanner->new();
$scanner->register_scan_started_event(\&scan_started);
$scanner->register_port_found_event(\&port_found);
my $opciones_scan = "-sS -A -vv -iL $lista";
my $antes = time();
open HTML, ">$fichero" or die "ERROR: No puedo escribir en $fichero:$!\n";
print HTML
start_html({
title => "Nmap Report $hora Red: $lista",
script=> q(function cambia(ip) {
var tr = document.getElementById(ip);
if ( tr.style.display == "none" ) {
tr.style.display = "table-row";
}
else {
tr.style.display = "none";
}
})
}),
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#E6E6FA',
background =>'header.gif',
},
[
Tr([
td([
(
{rowspan =>'4', width =>'10%'},
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Reporte creado el dia: $hora"),
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Opciones Nmap: $opciones_scan"),
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Archivo de Hosts: $lista"),
),
]),
td([
(
font({ face => 'Verdana', size => 1, color => '#ffffff' }, "Caracteristicas: ") .
font({ face => 'Verdana', size => 1, color => '#f3c035' }, "hosts - ") .
font({ face => 'Verdana', size => 1, color => '#00ff00' }, "active - ").
font({ face => 'Verdana', size => 1, color => '#ff0000' }, "no responds - ").
font({ face => 'Verdana', size => 1, color => '#00ff00' }, "open - ").
font({ face => 'Verdana', size => 1, color => '#ff0000' }, "close - ").
font({ face => 'Verdana', size => 1, color => '#6666ff' }, "tcp - ").
font({ face => 'Verdana', size => 1, color => '#666600' }, "udp"),
),
]),
])
]
);
# Comenzamos el escaneo
my @escaneo_ip;
$scanner->scan($opciones_scan);
my $duracion = time() - $antes;
# Salida del resultado
print HTML
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#f2f2f2',
},
Tr([
map {
td( { bgcolor => '#848484', onclick => "cambia('$_->{addresses}')" },
font({ face => 'Verdana', size => 1, color => '#f3c035' }, "$_->{hostname} ($_->{addresses})"),
$_->{status},
),
} @escaneo_ip
])
),
br(),
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#F2F2F2',
},
map {
Tr(
{ id => $_->{addresses}, style => 'display:none' },
[
td( { bgcolor => '#e6e6fa' },
font({ face => 'Verdana', size => 1, color => '#242424' },
join q{<br />}, @{$_->{port}}
),
),
]
)
} grep { defined $_->{port} } @escaneo_ip
),
table(
{
border => 0,
align => 'center',
width => 700,
cellpadding => 0,
cellspacing => 0,
bgcolor => '#646464',
},
[
Tr([
td([
(
font({ face => 'Verdana', size => 1}, "Time to ejecution: $duracion seconds"),
),
]),
])
]
),
end_html
;
close HTML;
sub scan_started {
my $self = shift;
my $host = shift;
my $hostname = $host->hostname();
my $addresses = join(',', map { $_->addr() } $host->addresses());
my $status = $host->status();
$status = ( $status eq 'up' )
? font({ face => 'Verdana', size => 1, color => '#00FF00' }, 'Activo' )
: font({ face => 'Verdana', size => 1, color => '#FF0000' }, 'No responde' )
;
# Guardamos la dirección, para crear la tabla fuera
push @escaneo_ip, { hostname => $hostname, addresses => $addresses, status => $status };
}
sub port_found {
my $self = shift;
my $host = shift;
my $port = shift;
my $name = $host->hostname();
my $puerto = $port->state();
my $proto = $port->protocol();
my $puerto_estado = ( $puerto eq 'open' ) ?
font({color=>'#00FF00'}, 'open' ) :
font({color=>'#FF0000'}, 'closed') ;
my $puerto_proto = ( $proto eq 'tcp' ) ?
font({color=>'#6666ff'}, 'tcp' ) :
font({color=>'#666600'}, 'udp') ;
# Guardamos los puertos detectados
push @{ $escaneo_ip[-1]->{port} },
join q{ },
$puerto_estado,
$puerto_proto,
$port->portid(),
((defined $port->service()->name() ) ? $port->service()->name() : ''),
((defined $port->service()->version()) ? $port->service()->version() : ''),
;
}
__END__
Coloreado en 0.011 segundos, usando GeSHi 1.0.8.4
Saludos.