From 8227475beb531addd15a1ae76af2bcfb27b31e16 Mon Sep 17 00:00:00 2001 From: ramonn Date: Thu, 25 Aug 2011 20:52:42 +0000 Subject: [PATCH] 2011-08-25 Ramon Novoa * lib/PandoraFMS/NmapParser.pm: Updated package name to integrate Nmap::Parser into Pandora FMS. * lib/PandoraFMS/ReconServer.pm: Removed recursive calls. Now parent hosts are detected with a single call to nmap via Nmap::Parser. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4829 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 8 + pandora_server/lib/PandoraFMS/NmapParser.pm | 1818 +++++++++--------- pandora_server/lib/PandoraFMS/ReconServer.pm | 124 +- 3 files changed, 988 insertions(+), 962 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 2531e71c0c..3685047063 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,11 @@ +2011-08-25 Ramon Novoa + + * lib/PandoraFMS/NmapParser.pm: Updated package name to integrate + Nmap::Parser into Pandora FMS. + + * lib/PandoraFMS/ReconServer.pm: Removed recursive calls. Now parent + hosts are detected with a single call to nmap via Nmap::Parser. + 2011-08-25 Tomas Palacios * pandora_server/pandora_server.spec diff --git a/pandora_server/lib/PandoraFMS/NmapParser.pm b/pandora_server/lib/PandoraFMS/NmapParser.pm index 1759975f6c..dc4090be28 100644 --- a/pandora_server/lib/PandoraFMS/NmapParser.pm +++ b/pandora_server/lib/PandoraFMS/NmapParser.pm @@ -1,3 +1,5 @@ +# Modified for Pandora FMS. + # Copyright (c) <2003-2011> # MIT License @@ -20,26 +22,26 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -package Nmap::Parser; - -use strict; -use XML::Twig; -use Storable qw(dclone); -use vars qw($VERSION %D); - +package PandoraFMS::NmapParser; + +use strict; +use XML::Twig; +use Storable qw(dclone); +use vars qw($VERSION %D); + $VERSION = 1.21; - - -sub new { - + + +sub new { + my ( $class, $self ) = shift; $class = ref($class) || $class; - + %{ $self->{HOSTS} } = %{ $self->{SESSION} } = (); - + $self->{twig} = new XML::Twig( start_tag_handlers => { nmaprun => \&_nmaprun_start_tag_hdlr }, - + twig_roots => { scaninfo => \&_scaninfo_tag_hdlr, prescript => \&_prescript_tag_hdlr, @@ -57,30 +59,30 @@ sub new { taskprogress => 1 } ); - + bless( $self, $class ); return $self; -} - +} + #/*****************************************************************************/ # NMAP::PARSER OBJECT METHODS #/*****************************************************************************/ - -#Safe parse and parsefile will return $@ which will contain the error -#that occured if the parsing failed (it might be empty when no error occurred) -sub _init { + +#Safe parse and parsefile will return $@ which will contain the error +#that occured if the parsing failed (it might be empty when no error occurred) +sub _init { my $self = shift; $D{callback} = $self->{callback}; -} - -sub _clean { +} + +sub _clean { my $self = shift; $self->{SESSION} = dclone( $D{$$}{SESSION} ) if ( $D{$$}{SESSION} ); $self->{HOSTS} = dclone( $D{$$}{HOSTS} ) if ( $D{$$}{HOSTS} ); delete $D{$$}; delete $D{callback}; -} - +} + sub callback { my $self = shift; my $callback = shift; #first arg is CODE @@ -96,7 +98,7 @@ sub callback { return $self->{callback}{is_registered}; } -sub parse { +sub parse { my $self = shift; $self->_init(); $self->{twig}->safe_parse(@_); @@ -105,8 +107,8 @@ sub parse { $self->purge; return $self; } - -sub parsefile { + +sub parsefile { my $self = shift; $self->_init(); $self->{twig}->safe_parsefile(@_); @@ -115,22 +117,22 @@ sub parsefile { $self->purge; return $self; } - -sub parsescan { + +sub parsescan { my $self = shift; my $nmap = shift; my $args = shift; my @ips = @_; my $FH; - + if ( $args =~ /-o(?:X|N|G)/ ) { die "[Nmap-Parser] Cannot pass option '-oX', '-oN' or '-oG' to parsecan()"; } - + my $cmd; $self->_init(); - + #if output file is defined, point it to a localfile then call parsefile instead. if ( defined( $self->{cache_file} ) ) { $cmd = @@ -147,18 +149,18 @@ sub parsescan { $self->parse($FH); close $FH; } - + $self->_clean(); $self->purge; return $self; - -} - + +} + sub cache_scan { my $self = shift; $self->{cache_file} = shift || 'nmap-parser-cache.' . time() . '.xml'; } - + sub purge { my $self = shift; $self->{twig}->purge; @@ -186,33 +188,33 @@ sub addr_sort { ); } -#MAIN SCAN INFORMATION -sub get_session { +#MAIN SCAN INFORMATION +sub get_session { my $self = shift; - my $obj = Nmap::Parser::Session->new( $self->{SESSION} ); + my $obj = PandoraFMS::NmapParser::Session->new( $self->{SESSION} ); return $obj; } - -#HOST STUFF -sub get_host { + +#HOST STUFF +sub get_host { my ( $self, $ip ) = (@_); if ( $ip eq '' ) { warn "[Nmap-Parser] No IP address given to get_host()\n"; return undef; } $self->{HOSTS}{$ip}; -} - -sub del_host { +} + +sub del_host { my ( $self, $ip ) = (@_); if ( $ip eq '' ) { warn "[Nmap-Parser] No IP address given to del_host()\n"; return undef; } delete $self->{HOSTS}{$ip}; -} - -sub all_hosts { +} + +sub all_hosts { my $self = shift; my $status = shift || ''; @@ -220,41 +222,41 @@ sub all_hosts { my @hosts = grep { $_->{status} eq $status } ( values %{ $self->{HOSTS} } ); return @hosts; -} - -sub get_ips { +} + +sub get_ips { my $self = shift; my $status = shift || ''; - + return $self->addr_sort( keys %{ $self->{HOSTS} } ) if ( $status eq '' ); - + my @hosts = grep { $self->{HOSTS}{$_}{status} eq $status } ( keys %{ $self->{HOSTS} } ); return $self->addr_sort(@hosts); - + } #/*****************************************************************************/ # PARSING TAG HANDLERS FOR XML::TWIG #/*****************************************************************************/ -sub _nmaprun_start_tag_hdlr { - +sub _nmaprun_start_tag_hdlr { + my ( $twig, $tag ) = @_; - + $D{$$}{SESSION}{start_time} = $tag->{att}->{start}; $D{$$}{SESSION}{nmap_version} = $tag->{att}->{version}; $D{$$}{SESSION}{start_str} = $tag->{att}->{startstr}; $D{$$}{SESSION}{xml_version} = $tag->{att}->{xmloutputversion}; $D{$$}{SESSION}{scan_args} = $tag->{att}->{args}; - $D{$$}{SESSION} = Nmap::Parser::Session->new( $D{$$}{SESSION} ); - + $D{$$}{SESSION} = PandoraFMS::NmapParser::Session->new( $D{$$}{SESSION} ); + $twig->purge; -} - -sub _scaninfo_tag_hdlr { +} + +sub _scaninfo_tag_hdlr { my ( $twig, $tag ) = @_; my $type = $tag->{att}->{type}; my $proto = $tag->{att}->{protocol}; @@ -265,8 +267,8 @@ sub _scaninfo_tag_hdlr { $D{$$}{SESSION}{numservices}{$type} = $numservices; } $twig->purge; -} - +} + sub _prescript_tag_hdlr { my ( $twig, $tag ) = @_; my $scripts_hashref; @@ -277,7 +279,7 @@ sub _prescript_tag_hdlr { $D{$$}{SESSION}{prescript} = $scripts_hashref; $twig->purge; } - + sub _postscript_tag_hdlr { my ( $twig, $tag ) = @_; my $scripts_hashref; @@ -288,41 +290,41 @@ sub _postscript_tag_hdlr { $D{$$}{SESSION}{postscript} = $scripts_hashref; $twig->purge; } - -sub _finished_tag_hdlr { + +sub _finished_tag_hdlr { my ( $twig, $tag ) = @_; $D{$$}{SESSION}{finish_time} = $tag->{att}->{time}; $D{$$}{SESSION}{time_str} = $tag->{att}->{timestr}; $twig->purge; -} - -#parses all the host information in one swoop (calling __host_*_tag_hdlrs) -sub _host_tag_hdlr { +} + +#parses all the host information in one swoop (calling __host_*_tag_hdlrs) +sub _host_tag_hdlr { my ( $twig, $tag ) = @_; my $id = undef; - + return undef unless ( defined $tag ); - + #GET ADDRESS INFO my $addr_hashref; $addr_hashref = __host_addr_tag_hdlr($tag); - + #use this as the identifier $id = $addr_hashref->{ipv4} || $addr_hashref->{ipv6} || $addr_hashref->{mac}; #worstcase use MAC - + $D{$$}{HOSTS}{$id}{addrs} = $addr_hashref; - + return undef unless ( defined($id) || $id ne '' ); - + #GET HOSTNAMES $D{$$}{HOSTS}{$id}{hostnames} = __host_hostnames_tag_hdlr($tag); - + #GET STATUS $D{$$}{HOSTS}{$id}{status} = $tag->first_child('status')->{att}->{state}; - + #CONTINUE PROCESSING IF STATUS IS UP - OTHERWISE NO MORE XML if ( lc( $D{$$}{HOSTS}{$id}{status} ) eq 'up' ) { @@ -340,7 +342,7 @@ sub _host_tag_hdlr { } #CREATE HOST OBJECT FOR USER - $D{$$}{HOSTS}{$id} = Nmap::Parser::Host->new( $D{$$}{HOSTS}{$id} ); + $D{$$}{HOSTS}{$id} = PandoraFMS::NmapParser::Host->new( $D{$$}{HOSTS}{$id} ); if ( $D{callback}{is_registered} ) { &{ $D{callback}{coderef} }( $D{$$}{HOSTS}{$id} ); @@ -351,14 +353,14 @@ sub _host_tag_hdlr { } -sub __host_addr_tag_hdlr { +sub __host_addr_tag_hdlr { my $tag = shift; my $addr_hashref; - + #children() will return all children with tag name address for my $addr ( $tag->children('address') ) { if ( lc( $addr->{att}->{addrtype} ) eq 'mac' ) { - + #we'll assume for now, only 1 MAC address per system $addr_hashref->{mac}{addr} = $addr->{att}->{addr}; $addr_hashref->{mac}{vendor} = $addr->{att}->{vendor}; @@ -369,32 +371,32 @@ sub __host_addr_tag_hdlr { elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv6' ) { $addr_hashref->{ipv6} = $addr->{att}->{addr}; } - + } - + return $addr_hashref; } - -sub __host_hostnames_tag_hdlr { + +sub __host_hostnames_tag_hdlr { my $tag = shift; - + my $hostnames_tag = $tag->first_child('hostnames'); return undef unless ( defined $hostnames_tag ); - + my @hostnames; - + for my $name ( $hostnames_tag->children('hostname') ) { push @hostnames, $name->{att}->{name}; } return \@hostnames; -} - +} + sub __host_port_tag_hdlr { my $tag = shift; my ( $port_hashref, $ports_tag ); - + $ports_tag = $tag->first_child('ports'); return undef unless ( defined $ports_tag ); @@ -449,13 +451,13 @@ sub __host_port_tag_hdlr { } -sub __host_service_tag_hdlr { +sub __host_service_tag_hdlr { my $tag = shift; my $portid = shift; #need a way to remember what port this service runs on my $service = $tag->first_child('service[@name]'); my $service_hashref; $service_hashref->{port} = $portid; - + if ( defined $service ) { $service_hashref->{name} = $service->{att}->{name} || 'unknown'; $service_hashref->{version} = $service->{att}->{version}; @@ -473,12 +475,12 @@ sub __host_service_tag_hdlr { } return $service_hashref; -} - +} + sub __host_script_tag_hdlr { my $tag = shift; my $script_hashref; - + for ( $tag->children('script') ) { chomp($script_hashref->{ $_->{att}->{id} } = $_->{att}->{output}); @@ -487,7 +489,7 @@ sub __host_script_tag_hdlr { return $script_hashref; } -sub __host_os_tag_hdlr { +sub __host_os_tag_hdlr { my $tag = shift; my $os_tag = $tag->first_child('os'); my $os_hashref; @@ -512,7 +514,7 @@ sub __host_os_tag_hdlr { $os_fingerprint->{'att'}->{'fingerprint'} if ( defined $os_fingerprint ); - #This will go in Nmap::Parser::Host::OS + #This will go in PandoraFMS::NmapParser::Host::OS my $osmatch_index = 0; for my $osmatch ( $os_tag->children('osmatch') ) { $os_hashref->{osmatch_name}[$osmatch_index] = @@ -543,24 +545,24 @@ sub __host_os_tag_hdlr { return $os_hashref; -} - -sub __host_uptime_tag_hdlr { +} + +sub __host_uptime_tag_hdlr { my $tag = shift; my $uptime = $tag->first_child('uptime'); my $uptime_hashref; - + if ( defined $uptime ) { $uptime_hashref->{seconds} = $uptime->{att}->{seconds}; $uptime_hashref->{lastboot} = $uptime->{att}->{lastboot}; - + } return $uptime_hashref; } -sub __host_tcpsequence_tag_hdlr { +sub __host_tcpsequence_tag_hdlr { my $tag = shift; my $sequence = $tag->first_child('tcpsequence'); my $sequence_hashref; @@ -569,12 +571,12 @@ sub __host_tcpsequence_tag_hdlr { $sequence_hashref->{difficulty} = $sequence->{att}->{difficulty}; $sequence_hashref->{values} = $sequence->{att}->{values}; $sequence_hashref->{index} = $sequence->{att}->{index}; - + return $sequence_hashref; - + } -sub __host_ipidsequence_tag_hdlr { +sub __host_ipidsequence_tag_hdlr { my $tag = shift; my $sequence = $tag->first_child('ipidsequence'); my $sequence_hashref; @@ -582,10 +584,10 @@ sub __host_ipidsequence_tag_hdlr { $sequence_hashref->{class} = $sequence->{att}->{class}; $sequence_hashref->{values} = $sequence->{att}->{values}; return $sequence_hashref; - + } - -sub __host_tcptssequence_tag_hdlr { + +sub __host_tcptssequence_tag_hdlr { my $tag = shift; my $sequence = $tag->first_child('tcptssequence'); my $sequence_hashref; @@ -594,21 +596,21 @@ sub __host_tcptssequence_tag_hdlr { $sequence_hashref->{values} = $sequence->{att}->{values}; return $sequence_hashref; } - -sub __host_times_tag_hdlr { - my $tag = shift; - my $times = $tag->first_child('times'); - my $times_hashref; - - if(defined $times){ - $times_hashref->{srtt} = $times->{att}->{srtt}; - $times_hashref->{rttvar} = $times->{att}->{rttvar}; - $times_hashref->{to} = $times->{att}->{to}; - - } - - return $times_hashref; - + +sub __host_times_tag_hdlr { + my $tag = shift; + my $times = $tag->first_child('times'); + my $times_hashref; + + if(defined $times){ + $times_hashref->{srtt} = $times->{att}->{srtt}; + $times_hashref->{rttvar} = $times->{att}->{rttvar}; + $times_hashref->{to} = $times->{att}->{to}; + + } + + return $times_hashref; + } sub __host_hostscript_tag_hdlr { @@ -621,20 +623,20 @@ sub __host_hostscript_tag_hdlr { $script->{att}->{output}); } return $scripts_hashref; -} - +} + sub __host_distance_tag_hdlr { my $tag = shift; my $distance = $tag->first_child('distance'); return undef unless ($distance); return $distance->{att}->{value}; } - + sub __host_trace_tag_hdlr { my $tag = shift; my $trace_tag = $tag->first_child('trace'); my $trace_hashref = { hops => [], }; - + if ( defined $trace_tag ) { my $proto = $trace_tag->{att}->{proto}; @@ -646,7 +648,7 @@ sub __host_trace_tag_hdlr { for my $hop_tag ( $trace_tag->children('hop') ) { # Copy the known hop attributes, they will go in - # Nmap::Parser::Host::TraceHop + # PandoraFMS::NmapParser::Host::TraceHop my %hop_data; $hop_data{$_} = $hop_tag->{att}->{$_} for qw( ttl rtt ipaddr host ); delete $hop_data{rtt} if $hop_data{rtt} !~ /^[\d.]+$/; @@ -682,48 +684,48 @@ sub __host_trace_error_tag_hdlr { # NMAP::PARSER::SESSION #/*****************************************************************************/ -package Nmap::Parser::Session; -use vars qw($AUTOLOAD); - -sub new { +package PandoraFMS::NmapParser::Session; +use vars qw($AUTOLOAD); + +sub new { my $class = shift; $class = ref($class) || $class; my $self = shift || {}; bless( $self, $class ); return $self; -} - -#Support for: -#start_time, start_str, finish_time, time_str, nmap_version, xml_version, scan_args -sub AUTOLOAD { +} + +#Support for: +#start_time, start_str, finish_time, time_str, nmap_version, xml_version, scan_args +sub AUTOLOAD { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); no strict 'refs'; *$AUTOLOAD = sub { return $_[0]->{ lc $param } }; goto &$AUTOLOAD; -} - -sub numservices { +} + +sub numservices { my $self = shift; my $type = shift || ''; #(syn|ack|bounce|connect|null|xmas|window|maimon|fin|udp|ipproto) - + return unless ( ref( $self->{numservices} ) eq 'HASH' ); - + if ( $type ne '' ) { return $self->{numservices}{$type}; } else { my $total = 0; for ( values %{ $self->{numservices} } ) { $total += $_; } return $total; } #(else) total number of services together -} - +} + sub scan_types { return sort { $a cmp $b } ( keys %{ $_[0]->{type} } ) if ( ref( $_[0]->{type} ) eq 'HASH' ); } sub scan_type_proto { return $_[1] ? $_[0]->{type}{ $_[1] } : undef; } - + sub prescripts { my $self = shift; my $id = shift; @@ -734,7 +736,7 @@ sub prescripts { return $self->{prescript}{$id}; } } - + sub postscripts { my $self = shift; my $id = shift; @@ -750,17 +752,17 @@ sub postscripts { # NMAP::PARSER::HOST #/*****************************************************************************/ -package Nmap::Parser::Host; -use vars qw($AUTOLOAD); - -sub new { +package PandoraFMS::NmapParser::Host; +use vars qw($AUTOLOAD); + +sub new { my $class = shift; $class = ref($class) || $class; my $self = shift || {}; bless( $self, $class ); return $self; -} - +} + sub status { return $_[0]->{status}; } sub addr { @@ -768,18 +770,18 @@ sub addr { return $default; } -sub addrtype { +sub addrtype { if ( $_[0]->{addrs}{ipv4} ) { return 'ipv4'; } elsif ( $_[0]->{addrs}{ipv6} ) { return 'ipv6'; } } - + sub ipv4_addr { return $_[0]->{addrs}{ipv4}; } sub ipv6_addr { return $_[0]->{addrs}{ipv6}; } - + sub mac_addr { return $_[0]->{addrs}{mac}{addr}; } sub mac_vendor { return $_[0]->{addrs}{mac}{vendor}; } - -#returns the first hostname + +#returns the first hostname sub hostname { my $self = shift; my $index = shift || 0; @@ -788,13 +790,13 @@ sub hostname { $index = scalar @{ $self->{hostnames} } - 1; } return $self->{hostnames}[$index] if ( scalar @{ $self->{hostnames} } ); -} - +} + sub all_hostnames { return @{ $_[0]->{hostnames} }; } sub extraports_state { return $_[0]->{ports}{extraports}{state}; } sub extraports_count { return $_[0]->{ports}{extraports}{count}; } sub distance { return $_[0]->{distance}; } - + sub hostscripts { my $self = shift; my $id = shift; @@ -805,13 +807,13 @@ sub hostscripts { return $self->{hostscript}{$id}; } } - + sub all_trace_hops { - + my $self = shift; return unless defined $self->{trace}->{hops}; - return map { Nmap::Parser::Host::TraceHop->new( $_ ) } + return map { PandoraFMS::NmapParser::Host::TraceHop->new( $_ ) } @{ $self->{trace}->{hops} }; } @@ -833,12 +835,12 @@ sub _del_port { delete $self->{ports}{$proto}{$_} for (@portids); } -sub _get_ports { +sub _get_ports { my $self = shift; my $proto = pop; #param might be empty, so this goes first my $state = lc(shift); #open, filtered, closed or any combination my @matched_ports = (); - + #if $state eq '', then tcp_ports or udp_ports was called for all ports #therefore, only return the keys of all ports found if ( $state eq '' ) { @@ -860,65 +862,65 @@ sub _get_ports { return sort { $a <=> $b } @matched_ports; -} - -sub _get_port_state { +} + +sub _get_port_state { my $self = shift; my $proto = pop; #portid might be empty, so this goes first my $portid = lc(shift); - + return undef unless ( exists $self->{ports}{$proto}{$portid} ); return $self->{ports}{$proto}{$portid}{state}; } -#changed this to use _get_ports since it was similar code +#changed this to use _get_ports since it was similar code sub tcp_ports { return _get_ports( @_, 'tcp' ); } sub udp_ports { return _get_ports( @_, 'udp' ); } - + sub tcp_port_count { return $_[0]->{ports}{tcp_port_count}; } sub udp_port_count { return $_[0]->{ports}{udp_port_count}; } - + sub tcp_port_state { return _get_port_state( @_, 'tcp' ); } sub udp_port_state { return _get_port_state( @_, 'udp' ); } - + sub tcp_del_ports { return _del_port( @_, 'tcp' ); } sub udp_del_ports { return _del_port( @_, 'udp' ); } -sub tcp_service { +sub tcp_service { my $self = shift; my $portid = shift; if ( $portid eq '' ) { warn "[Nmap-Parser] No port number passed to tcp_service()\n"; return undef; } - return Nmap::Parser::Host::Service->new( + return PandoraFMS::NmapParser::Host::Service->new( $self->{ports}{tcp}{$portid}{service} ); } - -sub udp_service { + +sub udp_service { my $self = shift; my $portid = shift; if ( $portid eq '' ) { warn "[Nmap-Parser] No port number passed to udp_service()\n"; return undef; } - return Nmap::Parser::Host::Service->new( + return PandoraFMS::NmapParser::Host::Service->new( $self->{ports}{udp}{$portid}{service} ); - + } - -#usually the first one is the highest accuracy - -sub os_sig { return Nmap::Parser::Host::OS->new( $_[0]->{os} ); } - -#Support for: -#tcpsequence_class, tcpsequence_values, tcpsequence_index, -#ipidsequence_class, ipidsequence_values, tcptssequence_values, -#tcptssequence_class, uptime_seconds, uptime_lastboot -#tcp_open_ports, udp_open_ports, tcp_filtered_ports, udp_filtered_ports, -#tcp_closed_ports, udp_closed_ports, times_srtt, times_rttvar, times_to -sub AUTOLOAD { + +#usually the first one is the highest accuracy + +sub os_sig { return PandoraFMS::NmapParser::Host::OS->new( $_[0]->{os} ); } + +#Support for: +#tcpsequence_class, tcpsequence_values, tcpsequence_index, +#ipidsequence_class, ipidsequence_values, tcptssequence_values, +#tcptssequence_class, uptime_seconds, uptime_lastboot +#tcp_open_ports, udp_open_ports, tcp_filtered_ports, udp_filtered_ports, +#tcp_closed_ports, udp_closed_ports, times_srtt, times_rttvar, times_to +sub AUTOLOAD { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); my ( $type, $val ) = split /_/, lc($param); @@ -942,27 +944,27 @@ sub AUTOLOAD { goto &$AUTOLOAD; } else { die '[Nmap-Parser] method ->' . $param . "() not defined!\n"; } -} - +} + #/*****************************************************************************/ # NMAP::PARSER::HOST::SERVICE #/*****************************************************************************/ - -package Nmap::Parser::Host::Service; -use vars qw($AUTOLOAD); - -sub new { + +package PandoraFMS::NmapParser::Host::Service; +use vars qw($AUTOLOAD); + +sub new { my $class = shift; $class = ref($class) || $class; my $self = shift || {}; bless( $self, $class ); return $self; -} - +} + sub scripts { my $self = shift; my $id = shift; - + unless ( defined $id ) { return sort keys %{ $self->{script} }; } @@ -971,61 +973,61 @@ sub scripts { } } -#Support for: -#name port proto rpcnum owner version product extrainfo tunnel method confidence -#this will now only load functions that will be used. This saves -#on delay (increase speed) and memory - -sub AUTOLOAD { +#Support for: +#name port proto rpcnum owner version product extrainfo tunnel method confidence +#this will now only load functions that will be used. This saves +#on delay (increase speed) and memory + +sub AUTOLOAD { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); no strict 'refs'; *$AUTOLOAD = sub { return $_[0]->{ lc $param } }; goto &$AUTOLOAD; -} - +} + #/*****************************************************************************/ # NMAP::PARSER::HOST::OS #/*****************************************************************************/ - -package Nmap::Parser::Host::OS; -use vars qw($AUTOLOAD); - -sub new { + +package PandoraFMS::NmapParser::Host::OS; +use vars qw($AUTOLOAD); + +sub new { my $class = shift; $class = ref($class) || $class; my $self = shift || {}; bless( $self, $class ); return $self; -} - +} + sub portused_open { return $_[0]->{portused}{open}; } sub portused_closed { return $_[0]->{portused}{closed}; } sub os_fingerprint { return $_[0]->{os_fingerprint}; } - + sub name_count { return $_[0]->{osmatch_count}; } - -sub all_names { - my $self = shift; + +sub all_names { + my $self = shift; @_ = (); if ( $self->{osclass_count} < 1 ) { return @_; } if ( ref( $self->{osmatch_name} ) eq 'ARRAY' ) { return sort @{ $self->{osmatch_name} }; } - + } #given by decreasing accuracy - + sub class_count { return $_[0]->{osclass_count}; } - -#Support for: -#name,names, name_accuracy, osfamily, vendor, type, osgen, class_accuracy -sub AUTOLOAD { + +#Support for: +#name,names, name_accuracy, osfamily, vendor, type, osgen, class_accuracy +sub AUTOLOAD { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); no strict 'refs'; $param = lc($param); - + $param = 'name' if ( $param eq 'names' ); if ( $param eq 'name' || $param eq 'name_accuracy' ) { @@ -1037,9 +1039,9 @@ sub AUTOLOAD { *$AUTOLOAD = sub { _get_info( $_[0], $_[1], $param, 'osclass' ); }; goto &$AUTOLOAD; } -} - -sub _get_info { +} + +sub _get_info { my ( $self, $index, $param, $type ) = @_; $index ||= 0; @@ -1048,13 +1050,13 @@ sub _get_info { $index = $self->{ $type . '_count' } - 1; } return $self->{ $type . '_' . $param }[$index]; -} - +} + #/*****************************************************************************/ # NMAP::PARSER::HOST::TRACEHOP #/*****************************************************************************/ -package Nmap::Parser::Host::TraceHop; +package PandoraFMS::NmapParser::Host::TraceHop; use vars qw($AUTOLOAD); sub new { @@ -1083,136 +1085,136 @@ sub AUTOLOAD { else { die '[Nmap-Parser] method ->' . $param . "() not defined!\n"; } } -1; - -__END__ - -=pod - -=head1 NAME - -Nmap::Parser - parse nmap scan data with perl - -=head1 SYNOPSIS - - use Nmap::Parser; - my $np = new Nmap::Parser; - - $np->parsescan($nmap_path, $nmap_args, @ips); - #or - $np->parsefile($file_xml); - - my $session = $np->get_session(); - #a Nmap::Parser::Session object - - my $host = $np->get_host($ip_addr); - #a Nmap::Parser::Host object - - my $service = $host->tcp_service(80); - #a Nmap::Parser::Host::Service object - - my $os = $host->os_sig(); - #a Nmap::Parser::Host::OS object - - #--------------------------------------- - - my $np2 = new Nmap::Parser; - - $np2->callback(\&my_callback); - - $np2->parsefile($file_xml); - #or - $np2->parsescan($nmap_path, $nmap_args, @ips); - - sub my_callback { - - my $host = shift; - #Nmap::Parser::Host object - #.. see documentation for all methods ... - - } - - -I - -=head1 DESCRIPTION - -This module implements a interface to the information contained in an nmap scan. -It is implemented by parsing the xml scan data that is generated by nmap. This -will enable anyone who utilizes nmap to quickly create fast and robust security scripts -that utilize the powerful port scanning abilities of nmap. - +1; + +__END__ + +=pod + +=head1 NAME + +PandoraFMS::NmapParser - parse nmap scan data with perl + +=head1 SYNOPSIS + + use PandoraFMS::NmapParser; + my $np = new PandoraFMS::NmapParser; + + $np->parsescan($nmap_path, $nmap_args, @ips); + #or + $np->parsefile($file_xml); + + my $session = $np->get_session(); + #a PandoraFMS::NmapParser::Session object + + my $host = $np->get_host($ip_addr); + #a PandoraFMS::NmapParser::Host object + + my $service = $host->tcp_service(80); + #a PandoraFMS::NmapParser::Host::Service object + + my $os = $host->os_sig(); + #a PandoraFMS::NmapParser::Host::OS object + + #--------------------------------------- + + my $np2 = new PandoraFMS::NmapParser; + + $np2->callback(\&my_callback); + + $np2->parsefile($file_xml); + #or + $np2->parsescan($nmap_path, $nmap_args, @ips); + + sub my_callback { + + my $host = shift; + #PandoraFMS::NmapParser::Host object + #.. see documentation for all methods ... + + } + + +I + +=head1 DESCRIPTION + +This module implements a interface to the information contained in an nmap scan. +It is implemented by parsing the xml scan data that is generated by nmap. This +will enable anyone who utilizes nmap to quickly create fast and robust security scripts +that utilize the powerful port scanning abilities of nmap. + The latest version of this module can be found on here L -=head1 OVERVIEW - -This module has an internal framework to make it easy to retrieve the desired information of a scan. -Every nmap scan is based on two main sections of informations: the scan session, and the scan information of all hosts. -The session information will be stored as a Nmap::Parser::Session object. This object will contain its own methods -to obtain the desired information. The same is true for any hosts that were scanned using the Nmap::Parser::Host object. -There are two sub objects under Nmap::Parser::Host. One is the Nmap::Parser::Host::Service object which will be used to obtain -information of a given service running on a given port. The second is the Nmap::Parser::Host::OS object which contains the -operating system signature information (OS guessed names, classes, osfamily..etc). - - Nmap::Parser -- Core parser - | - +--Nmap::Parser::Session -- Nmap scan session information - | - +--Nmap::Parser::Host -- General host information - | | - | |-Nmap::Parser::Host::Service -- Port service information - | | - | |-Nmap::Parser::Host::OS -- Operating system signature information - - -=head1 METHODS - -=head2 Nmap::Parser - -The main idea behind the core module is, you will first parse the information -and then extract data. Therefore, all parse*() methods should be executed before -any get_*() methods. - -=over 4 - - -=item B - -=item B - -Parses the nmap scan information in $string. Note that is usually only used if -you have the whole xml scan information in $string or if you are piping the -scan information. - -=item B - -Parses the nmap scan data in $xml_file. This file can be generated from an nmap -scan by using the '-oX filename.xml' option with nmap. If you get an error or your program dies due to parsing, please check that the -xml information is compliant. The file is closed no matter how C returns. - -=item B - +=head1 OVERVIEW + +This module has an internal framework to make it easy to retrieve the desired information of a scan. +Every nmap scan is based on two main sections of informations: the scan session, and the scan information of all hosts. +The session information will be stored as a PandoraFMS::NmapParser::Session object. This object will contain its own methods +to obtain the desired information. The same is true for any hosts that were scanned using the PandoraFMS::NmapParser::Host object. +There are two sub objects under PandoraFMS::NmapParser::Host. One is the PandoraFMS::NmapParser::Host::Service object which will be used to obtain +information of a given service running on a given port. The second is the PandoraFMS::NmapParser::Host::OS object which contains the +operating system signature information (OS guessed names, classes, osfamily..etc). + + PandoraFMS::NmapParser -- Core parser + | + +--PandoraFMS::NmapParser::Session -- Nmap scan session information + | + +--PandoraFMS::NmapParser::Host -- General host information + | | + | |-PandoraFMS::NmapParser::Host::Service -- Port service information + | | + | |-PandoraFMS::NmapParser::Host::OS -- Operating system signature information + + +=head1 METHODS + +=head2 PandoraFMS::NmapParser + +The main idea behind the core module is, you will first parse the information +and then extract data. Therefore, all parse*() methods should be executed before +any get_*() methods. + +=over 4 + + +=item B + +=item B + +Parses the nmap scan information in $string. Note that is usually only used if +you have the whole xml scan information in $string or if you are piping the +scan information. + +=item B + +Parses the nmap scan data in $xml_file. This file can be generated from an nmap +scan by using the '-oX filename.xml' option with nmap. If you get an error or your program dies due to parsing, please check that the +xml information is compliant. The file is closed no matter how C returns. + +=item B + This method runs an nmap scan where $nmap is the path to the nmap executable or binary, -$args are the nmap command line parameters, and @ips are the list of IP addresses -to scan. parsescan() will automagically run the nmap scan and parse the information. +$args are the nmap command line parameters, and @ips are the list of IP addresses +to scan. parsescan() will automagically run the nmap scan and parse the information. If you wish to save the xml output from parsescan(), you must call cache_scan() method B you start the parsescan() process. This is done to conserve memory while parsing. cache_scan() will -let Nmap::Parser know to save the output before parsing the xml since Nmap::Parser purges everything that has +let PandoraFMS::NmapParser know to save the output before parsing the xml since PandoraFMS::NmapParser purges everything that has been parsed by the script to conserve memory and increase speed. -I - -I + +I - -If you get an error or your program dies due to parsing, please check that the -xml information is compliant. If you are using parsescan() or an open filehandle -, make sure that the nmap scan that you are performing is successful in returning -xml information. (Sometimes using loopback addresses causes nmap to fail). - + +If you get an error or your program dies due to parsing, please check that the +xml information is compliant. If you are using parsescan() or an open filehandle +, make sure that the nmap scan that you are performing is successful in returning +xml information. (Sometimes using loopback addresses causes nmap to fail). + =item B This function allows you to save the output of a parsescan() (or nmap scan) to the disk. $filename @@ -1226,177 +1228,177 @@ It returns the name of the file to be used as the cache. $np->parsescan('/usr/bin/nmap',$args,@IPS); -=item B - -Cleans the xml scan data from memory. This is useful if you have a program where -you are parsing lots of nmap scan data files with persistent variables. - -=item B - -Sets the parsing mode to be done using the callback function. It takes the parameter -of a code reference or a reference to a function. If no code reference is given, -it resets the mode to normal (no callback). - - $np->callback(\&my_function); #sets callback, my_function() will be called - $np->callback(); #resets it, no callback function called. Back to normal. - - -=item B - -Obtains the Nmap::Parser::Session object which contains the session scan information. - -=item B - -Obtains the Nmap::Parser::Host object for the given $ip_addr. - -=item B - -Deletes the stored Nmap::Parser::Host object whose IP is $ip_addr. - -=item B - -=item B - -Returns an array of all the Nmap::Parser::Host objects for the scan. If the optional -status is given, it will only return those hosts that match that status. The status -can be any of the following: C<(up|down|unknown|skipped)> - -=item B - -=item B - -Returns the list of IP addresses that were scanned in this nmap session. They are +=item B + +Cleans the xml scan data from memory. This is useful if you have a program where +you are parsing lots of nmap scan data files with persistent variables. + +=item B + +Sets the parsing mode to be done using the callback function. It takes the parameter +of a code reference or a reference to a function. If no code reference is given, +it resets the mode to normal (no callback). + + $np->callback(\&my_function); #sets callback, my_function() will be called + $np->callback(); #resets it, no callback function called. Back to normal. + + +=item B + +Obtains the PandoraFMS::NmapParser::Session object which contains the session scan information. + +=item B + +Obtains the PandoraFMS::NmapParser::Host object for the given $ip_addr. + +=item B + +Deletes the stored PandoraFMS::NmapParser::Host object whose IP is $ip_addr. + +=item B + +=item B + +Returns an array of all the PandoraFMS::NmapParser::Host objects for the scan. If the optional +status is given, it will only return those hosts that match that status. The status +can be any of the following: C<(up|down|unknown|skipped)> + +=item B + +=item B + +Returns the list of IP addresses that were scanned in this nmap session. They are sorted using addr_sort. If the optional status is given, it will only return -those IP addresses that match that status. The status can be any of the -following: C<(up|down|unknown|skipped)> - +those IP addresses that match that status. The status can be any of the +following: C<(up|down|unknown|skipped)> + =item B - + This function takes a list of IP addresses and returns the correctly sorted -version of the list. - -=back - -=head2 Nmap::Parser::Session - -This object contains the scan session information of the nmap scan. - - -=over 4 - - -=item B - -Returns the numeric time that the nmap scan finished. - -=item B - -Returns the version of nmap used for the scan. - -=item B - -=item B - -If numservices is called without argument, it returns the total number of services -that were scanned for all types. If $type is given, it returns the number of services -for that given scan type. See scan_types() for more info. - -=item B - -Returns a string which contains the nmap executed command line used to run the -scan. - -=item B - -Returns the protocol type of the given scan type (provided by $type). See scan_types() for -more info. - -=item B - -Returns the list of scan types that were performed. It can be any of the following: -C<(syn|ack|bounce|connect|null|xmas|window|maimon|fin|udp|ipproto)>. - -=item B - -Returns the human readable format of the start time. - -=item B - -Returns the numeric form of the time the nmap scan started. - -=item B - -Returns the human readable format of the finish time. - -=item B - -Returns the version of nmap xml file. - -=back - -=head2 Nmap::Parser::Host - -This object represents the information collected from a scanned host. - - -=over 4 - -=item B - -Returns the state of the host. It is usually one of these -C<(up|down|unknown|skipped)>. - -=item B - -Returns the main IP address of the host. This is usually the IPv4 address. If -there is no IPv4 address, the IPv6 is returned (hopefully there is one). - -=item B - -Returns the address type of the address given by addr() . - -=item B - -Returns a list of all hostnames found for the given host. - -=item B - -Returns the number of extraports found. - -=item B - -Returns the state of all the extraports found. - -=item B - -=item B - -As a basic call, hostname() returns the first hostname obtained for the given -host. If there exists more than one hostname, you can provide a number, which -is used as the location in the array. The index starts at 0; - - #in the case that there are only 2 hostnames - hostname() eq hostname(0); - hostname(1); #second hostname found - hostname(400) eq hostname(1) #nothing at 400; return the name at the last index - - -=item B - -Explicitly return the IPv4 address. - -=item B - -Explicitly return the IPv6 address. - -=item B - -Explicitly return the MAC address. - -=item B - -Return the vendor information of the MAC. - +version of the list. + +=back + +=head2 PandoraFMS::NmapParser::Session + +This object contains the scan session information of the nmap scan. + + +=over 4 + + +=item B + +Returns the numeric time that the nmap scan finished. + +=item B + +Returns the version of nmap used for the scan. + +=item B + +=item B + +If numservices is called without argument, it returns the total number of services +that were scanned for all types. If $type is given, it returns the number of services +for that given scan type. See scan_types() for more info. + +=item B + +Returns a string which contains the nmap executed command line used to run the +scan. + +=item B + +Returns the protocol type of the given scan type (provided by $type). See scan_types() for +more info. + +=item B + +Returns the list of scan types that were performed. It can be any of the following: +C<(syn|ack|bounce|connect|null|xmas|window|maimon|fin|udp|ipproto)>. + +=item B + +Returns the human readable format of the start time. + +=item B + +Returns the numeric form of the time the nmap scan started. + +=item B + +Returns the human readable format of the finish time. + +=item B + +Returns the version of nmap xml file. + +=back + +=head2 PandoraFMS::NmapParser::Host + +This object represents the information collected from a scanned host. + + +=over 4 + +=item B + +Returns the state of the host. It is usually one of these +C<(up|down|unknown|skipped)>. + +=item B + +Returns the main IP address of the host. This is usually the IPv4 address. If +there is no IPv4 address, the IPv6 is returned (hopefully there is one). + +=item B + +Returns the address type of the address given by addr() . + +=item B + +Returns a list of all hostnames found for the given host. + +=item B + +Returns the number of extraports found. + +=item B + +Returns the state of all the extraports found. + +=item B + +=item B + +As a basic call, hostname() returns the first hostname obtained for the given +host. If there exists more than one hostname, you can provide a number, which +is used as the location in the array. The index starts at 0; + + #in the case that there are only 2 hostnames + hostname() eq hostname(0); + hostname(1); #second hostname found + hostname(400) eq hostname(1) #nothing at 400; return the name at the last index + + +=item B + +Explicitly return the IPv4 address. + +=item B + +Explicitly return the IPv6 address. + +=item B + +Explicitly return the MAC address. + +=item B + +Return the vendor information of the MAC. + =item B Return the distance (in hops) of the target machine from the machine that performed the scan. @@ -1409,7 +1411,7 @@ contains only the part of the path that could be determined. =item B -Returns an array of Nmap::Parser::Host::TraceHop objects representing the path +Returns an array of PandoraFMS::NmapParser::Host::TraceHop objects representing the path to the target host. This array may be empty if Nmap did not perform the traceroute for some reason (same network, for example). @@ -1425,179 +1427,179 @@ Returns the name of the protocol used to perform the traceroute. Returns the port used to perform the traceroute. -=item B - -Returns an Nmap::Parser::Host::OS object that can be used to obtain all the -Operating System signature (fingerprint) information. See Nmap::Parser::Host::OS -for more details. - - $os = $host->os_sig; - $os->name; - $os->osfamily; - -=item B - -=item B - -=item B - -Returns the class, index and values information respectively of the tcp sequence. - -=item B - -=item B - -Returns the class and values information respectively of the ipid sequence. - -=item B - -=item B - -Returns the class and values information respectively of the tcpts sequence. - -=item B - -Returns the human readable format of the timestamp of when the host had last -rebooted. - -=item B - -Returns the number of seconds that have passed since the host's last boot from -when the scan was performed. - +=item B + +Returns an PandoraFMS::NmapParser::Host::OS object that can be used to obtain all the +Operating System signature (fingerprint) information. See PandoraFMS::NmapParser::Host::OS +for more details. + + $os = $host->os_sig; + $os->name; + $os->osfamily; + +=item B + +=item B + +=item B + +Returns the class, index and values information respectively of the tcp sequence. + +=item B + +=item B + +Returns the class and values information respectively of the ipid sequence. + +=item B + +=item B + +Returns the class and values information respectively of the tcpts sequence. + +=item B + +Returns the human readable format of the timestamp of when the host had last +rebooted. + +=item B + +Returns the number of seconds that have passed since the host's last boot from +when the scan was performed. + =item B - + =item B A basic call to hostscripts() returns a list of the names of the host scripts run. If C<$name> is given, it returns the text output of the script with that name, or undef if that script was not run. -=item B - -=item B - -Returns the sorted list of TCP|UDP ports respectively that were scanned on this host. Optionally -a string argument can be given to these functions to filter the list. - - $host->tcp_ports('open') #returns all only 'open' ports (even 'open|filtered') - $host->udp_ports('open|filtered'); #matches exactly ports with 'open|filtered' - -I - -=item B - -=item B - -Returns the total of TCP|UDP ports scanned respectively. - +=item B + +=item B + +Returns the sorted list of TCP|UDP ports respectively that were scanned on this host. Optionally +a string argument can be given to these functions to filter the list. + + $host->tcp_ports('open') #returns all only 'open' ports (even 'open|filtered') + $host->udp_ports('open|filtered'); #matches exactly ports with 'open|filtered' + +I + +=item B + +=item B + +Returns the total of TCP|UDP ports scanned respectively. + =item B =item B Deletes the current $portid from the list of ports for given protocol. -=item B - -=item B - -Returns the state of the given port, provided by the port number in $portid. - -=item B - -=item B - -Returns the list of open TCP|UDP ports respectively. Note that if a port state is -for example, 'open|filtered', it will appear on this list as well. - -=item B - -=item B - -Returns the list of filtered TCP|UDP ports respectively. Note that if a port state is -for example, 'open|filtered', it will appear on this list as well. - -=item B - -=item B - -Returns the list of closed TCP|UDP ports respectively. Note that if a port state is -for example, 'closed|filtered', it will appear on this list as well. - -=item B - -=item B - -Returns the Nmap::Parser::Host::Service object of a given service running on port, -provided by $portid. See Nmap::Parser::Host::Service for more info. - - $svc = $host->tcp_service(80); - $svc->name; - $svc->proto; - - -=back - -=head3 Nmap::Parser::Host::Service - -This object represents the service running on a given port in a given host. This -object is obtained by using the tcp_service($portid) or udp_service($portid) method from the -Nmap::Parser::Host object. If a portid is given that does not exist on the given -host, these functions will still return an object (so your script doesn't die). -Its good to use tcp_ports() or udp_ports() to see what ports were collected. - -=over 4 - - -=item B - -Returns the confidence level in service detection. - -=item B - -Returns any additional information nmap knows about the service. - -=item B - -Returns the detection method. - -=item B - -Returns the service name. - -=item B - -Returns the process owner of the given service. (If available) - -=item B - -Returns the port number where the service is running on. - -=item B - -Returns the product information of the service. - -=item B - -Returns the protocol type of the service. - -=item B - -Returns the RPC number. - -=item B - -Returns the tunnel value. (If available) - +=item B + +=item B + +Returns the state of the given port, provided by the port number in $portid. + +=item B + +=item B + +Returns the list of open TCP|UDP ports respectively. Note that if a port state is +for example, 'open|filtered', it will appear on this list as well. + +=item B + +=item B + +Returns the list of filtered TCP|UDP ports respectively. Note that if a port state is +for example, 'open|filtered', it will appear on this list as well. + +=item B + +=item B + +Returns the list of closed TCP|UDP ports respectively. Note that if a port state is +for example, 'closed|filtered', it will appear on this list as well. + +=item B + +=item B + +Returns the PandoraFMS::NmapParser::Host::Service object of a given service running on port, +provided by $portid. See PandoraFMS::NmapParser::Host::Service for more info. + + $svc = $host->tcp_service(80); + $svc->name; + $svc->proto; + + +=back + +=head3 PandoraFMS::NmapParser::Host::Service + +This object represents the service running on a given port in a given host. This +object is obtained by using the tcp_service($portid) or udp_service($portid) method from the +PandoraFMS::NmapParser::Host object. If a portid is given that does not exist on the given +host, these functions will still return an object (so your script doesn't die). +Its good to use tcp_ports() or udp_ports() to see what ports were collected. + +=over 4 + + +=item B + +Returns the confidence level in service detection. + +=item B + +Returns any additional information nmap knows about the service. + +=item B + +Returns the detection method. + +=item B + +Returns the service name. + +=item B + +Returns the process owner of the given service. (If available) + +=item B + +Returns the port number where the service is running on. + +=item B + +Returns the product information of the service. + +=item B + +Returns the protocol type of the service. + +=item B + +Returns the RPC number. + +=item B + +Returns the tunnel value. (If available) + =item B Returns the service fingerprint. (If available) -=item B - -Returns the version of the given product of the running service. - +=item B + +Returns the version of the given product of the running service. + =item B =item B @@ -1606,114 +1608,114 @@ A basic call to scripts() returns a list of the names of the scripts run for this port. If C<$name> is given, it returns the text output of the script with that name, or undef if that script was not run. -=back - -=head3 Nmap::Parser::Host::OS - -This object represents the Operating System signature (fingerprint) information -of the given host. This object is obtained from an Nmap::Parser::Host object -using the C method. One important thing to note is that the order of OS -names and classes are sorted by B. This is more important than -alphabetical ordering. Therefore, a basic call -to any of these functions will return the record with the highest accuracy. -(Which is probably the one you want anyways). - -=over 4 - -=item B - -Returns the list of all the guessed OS names for the given host. - -=item B - -=item B - -A basic call to class_accuracy() returns the osclass accuracy of the first record. -If C<$index> is given, it returns the osclass accuracy for the given record. The -index starts at 0. - -=item B - -Returns the total number of OS class records obtained from the nmap scan. - -=item B - -=item B - -=item B - -=item B - -A basic call to name() returns the OS name of the first record which is the name -with the highest accuracy. If C<$index> is given, it returns the name for the given record. The -index starts at 0. - -=item B - -=item B - -A basic call to name_accuracy() returns the OS name accuracy of the first record. If C<$index> is given, it returns the name for the given record. The -index starts at 0. - -=item B - -Returns the total number of OS names (records) for the given host. - -=item B - -=item B - -A basic call to osfamily() returns the OS family information of the first record. -If C<$index> is given, it returns the OS family information for the given record. The -index starts at 0. - -=item B - -=item B - -A basic call to osgen() returns the OS generation information of the first record. -If C<$index> is given, it returns the OS generation information for the given record. The -index starts at 0. - -=item B - -Returns the closed port number used to help identify the OS signatures. This might not -be available for all hosts. - -=item B - -Returns the open port number used to help identify the OS signatures. This might -not be available for all hosts. - +=back + +=head3 PandoraFMS::NmapParser::Host::OS + +This object represents the Operating System signature (fingerprint) information +of the given host. This object is obtained from an PandoraFMS::NmapParser::Host object +using the C method. One important thing to note is that the order of OS +names and classes are sorted by B. This is more important than +alphabetical ordering. Therefore, a basic call +to any of these functions will return the record with the highest accuracy. +(Which is probably the one you want anyways). + +=over 4 + +=item B + +Returns the list of all the guessed OS names for the given host. + +=item B + +=item B + +A basic call to class_accuracy() returns the osclass accuracy of the first record. +If C<$index> is given, it returns the osclass accuracy for the given record. The +index starts at 0. + +=item B + +Returns the total number of OS class records obtained from the nmap scan. + +=item B + +=item B + +=item B + +=item B + +A basic call to name() returns the OS name of the first record which is the name +with the highest accuracy. If C<$index> is given, it returns the name for the given record. The +index starts at 0. + +=item B + +=item B + +A basic call to name_accuracy() returns the OS name accuracy of the first record. If C<$index> is given, it returns the name for the given record. The +index starts at 0. + +=item B + +Returns the total number of OS names (records) for the given host. + +=item B + +=item B + +A basic call to osfamily() returns the OS family information of the first record. +If C<$index> is given, it returns the OS family information for the given record. The +index starts at 0. + +=item B + +=item B + +A basic call to osgen() returns the OS generation information of the first record. +If C<$index> is given, it returns the OS generation information for the given record. The +index starts at 0. + +=item B + +Returns the closed port number used to help identify the OS signatures. This might not +be available for all hosts. + +=item B + +Returns the open port number used to help identify the OS signatures. This might +not be available for all hosts. + =item B Returns the OS fingerprint used to help identify the OS signatures. This might not be available for all hosts. -=item B - -=item B - -A basic call to type() returns the OS type information of the first record. -If C<$index> is given, it returns the OS type information for the given record. The -index starts at 0. - -=item B - -=item B - -A basic call to vendor() returns the OS vendor information of the first record. -If C<$index> is given, it returns the OS vendor information for the given record. The -index starts at 0. - -=back - -=head3 Nmap::Parser::Host::TraceHop +=item B + +=item B + +A basic call to type() returns the OS type information of the first record. +If C<$index> is given, it returns the OS type information for the given record. The +index starts at 0. + +=item B + +=item B + +A basic call to vendor() returns the OS vendor information of the first record. +If C<$index> is given, it returns the OS vendor information for the given record. The +index starts at 0. + +=back + +=head3 PandoraFMS::NmapParser::Host::TraceHop This object represents a router on the IP path towards the destination or the destination itself. This is similar to what the C command outputs. -Nmap::Parser::Host::TraceHop objects are obtained through the -C and C Nmap::Parser::Host methods. +PandoraFMS::NmapParser::Host::TraceHop objects are obtained through the +C and C PandoraFMS::NmapParser::Host methods. =over 4 @@ -1736,37 +1738,37 @@ The host name of this hop, if known. =back -=head1 EXAMPLES - -I think some of us best learn from examples. These are a couple of examples to help -create custom security audit tools using some of the nice features -of the Nmap::Parser module. Hopefully this can double as a tutorial. +=head1 EXAMPLES + +I think some of us best learn from examples. These are a couple of examples to help +create custom security audit tools using some of the nice features +of the PandoraFMS::NmapParser module. Hopefully this can double as a tutorial. More tutorials (articles) can be found at L - + =head2 Real-Time Scanning - -You can run a nmap scan and have the parser parse the information automagically. -The only constraint is that you cannot use '-oX', '-oN', or '-oG' as one of your -arguments for nmap command line parameters passed to parsescan(). - - use Nmap::Parser; - - my $np = new Nmap::Parser; - my @hosts = @ARGV; #get hosts from cmd line - - #runs the nmap command with hosts and parses it automagically - $np->parsescan('/usr/bin/nmap','-sS O -p 1-1023',@hosts); - - for my $host ($np->all_hosts()){ - print $host->hostname."\n"; - #do mor stuff... - } - + +You can run a nmap scan and have the parser parse the information automagically. +The only constraint is that you cannot use '-oX', '-oN', or '-oG' as one of your +arguments for nmap command line parameters passed to parsescan(). + + use PandoraFMS::NmapParser; + + my $np = new PandoraFMS::NmapParser; + my @hosts = @ARGV; #get hosts from cmd line + + #runs the nmap command with hosts and parses it automagically + $np->parsescan('/usr/bin/nmap','-sS O -p 1-1023',@hosts); + + for my $host ($np->all_hosts()){ + print $host->hostname."\n"; + #do mor stuff... + } + If you would like to run the scan using parsescan() but also save the scan xml output, you can use cache_scan(). You must call cache_scan() BEFORE you initiate the parsescan() method. - - use Nmap::Parser; - my $np = new Nmap::Parser; + + use PandoraFMS::NmapParser; + my $np = new PandoraFMS::NmapParser; #telling np to save output $np->cache_scan('nmap.localhost.xml'); @@ -1775,114 +1777,114 @@ you can use cache_scan(). You must call cache_scan() BEFORE you initiate the par =head2 Callbacks -This is probably the easiest way to write a script with using Nmap::Parser, -if you don't need the general scan session information. During the parsing -process, the parser will obtain information of every host. The -callback function (in this case 'booyah()') is called after the parsing of -every host (sequentially). When the callback returns, the parser will delete all -information of the host it had sent to the callback. This callback function is -called for every host that the parser encounters. I - - use Nmap::Parser; - my $np = new Nmap::Parser; - - - $np->callback( \&booyah ); - - $np->parsefile('nmap_results.xml'); - # or use parsescan() - - sub booyah { - my $host = shift; #Nmap::Parser::Host object, just parsed - print 'IP: ',$host->addr,"\n"; - # ... do more stuff with $host ... - - #when it returns, host object will be deleted from memory - #(good for processing VERY LARGE files or scans) - } - - -=head2 Multiple Instances - (C) - -Using multiple instances of Nmap::Parser is extremely useful in helping -audit/monitor the network B

olicy (ohh noo! its that 'P' word!). -In this example, we have a set of hosts that had been scanned previously for tcp -services where the image was saved in I. We now will scan the -same hosts, and compare if any new tcp have been open since then -(good way to look for suspicious new services). Easy security Bompliance detection. -(ooh noo! The 'C' word too!). - - - use Nmap::Parser; - use vars qw($nmap_exe $nmap_args @ips); - my $base = new Nmap::Parser; - my $curr = new Nmap::Parser; - - - $base->parsefile('base_image.xml'); #load previous state - $curr->parsescan($nmap_exe, $nmap_args, @ips); #scan current hosts - - for my $ip ($curr->get_ips ) - { - #assume that IPs in base == IPs in curr scan - my $ip_base = $base->get_host($ip); - my $ip_curr = $curr->get_host($ip); - my %port = (); - - #find ports that are open that were not open before - #by finding the difference in port lists - my @diff = grep { $port{$_} < 2} - (map {$port{$_}++; $_} - ( $ip_curr->tcp_open_ports , $ip_base->tcp_open_ports )); - - print "$ip has these new ports open: ".join(',',@diff) if(scalar @diff); - - for (@diff){print "$_ seems to be ",$ip_curr->tcp_service($_)->name,"\n";} - - } - - -=head1 SUPPORT - -=head2 Discussion Forum - -If you have questions about how to use the module, or any of its features, you -can post messages to the Nmap::Parser module forum on CPAN::Forum. +This is probably the easiest way to write a script with using PandoraFMS::NmapParser, +if you don't need the general scan session information. During the parsing +process, the parser will obtain information of every host. The +callback function (in this case 'booyah()') is called after the parsing of +every host (sequentially). When the callback returns, the parser will delete all +information of the host it had sent to the callback. This callback function is +called for every host that the parser encounters. I + + use PandoraFMS::NmapParser; + my $np = new PandoraFMS::NmapParser; + + + $np->callback( \&booyah ); + + $np->parsefile('nmap_results.xml'); + # or use parsescan() + + sub booyah { + my $host = shift; #PandoraFMS::NmapParser::Host object, just parsed + print 'IP: ',$host->addr,"\n"; + # ... do more stuff with $host ... + + #when it returns, host object will be deleted from memory + #(good for processing VERY LARGE files or scans) + } + + +=head2 Multiple Instances - (C) + +Using multiple instances of PandoraFMS::NmapParser is extremely useful in helping +audit/monitor the network B

olicy (ohh noo! its that 'P' word!). +In this example, we have a set of hosts that had been scanned previously for tcp +services where the image was saved in I. We now will scan the +same hosts, and compare if any new tcp have been open since then +(good way to look for suspicious new services). Easy security Bompliance detection. +(ooh noo! The 'C' word too!). + + + use PandoraFMS::NmapParser; + use vars qw($nmap_exe $nmap_args @ips); + my $base = new PandoraFMS::NmapParser; + my $curr = new PandoraFMS::NmapParser; + + + $base->parsefile('base_image.xml'); #load previous state + $curr->parsescan($nmap_exe, $nmap_args, @ips); #scan current hosts + + for my $ip ($curr->get_ips ) + { + #assume that IPs in base == IPs in curr scan + my $ip_base = $base->get_host($ip); + my $ip_curr = $curr->get_host($ip); + my %port = (); + + #find ports that are open that were not open before + #by finding the difference in port lists + my @diff = grep { $port{$_} < 2} + (map {$port{$_}++; $_} + ( $ip_curr->tcp_open_ports , $ip_base->tcp_open_ports )); + + print "$ip has these new ports open: ".join(',',@diff) if(scalar @diff); + + for (@diff){print "$_ seems to be ",$ip_curr->tcp_service($_)->name,"\n";} + + } + + +=head1 SUPPORT + +=head2 Discussion Forum + +If you have questions about how to use the module, or any of its features, you +can post messages to the PandoraFMS::NmapParser module forum on CPAN::Forum. L - + =head2 Bug Reports and Enhancements - + Please submit any bugs or feature requests to: L - -B This can be done by running your scan with the I<-oX filename.xml> nmap switch. Please remove any important IP addresses for security reasons. It saves time in reproducing issues. - -=head1 SEE ALSO - - nmap, XML::Twig - -The Nmap::Parser page can be found at: L. -It contains the latest developments on the module. The nmap security scanner -homepage can be found at: L. - + +=head1 SEE ALSO + + nmap, XML::Twig + +The PandoraFMS::NmapParser page can be found at: L. +It contains the latest developments on the module. The nmap security scanner +homepage can be found at: L. + =head1 AUTHORS - + Anthony G Persaud L . Please see Changes file and CONTRIBUTORS file for a list of other great contributors. - + Additional Contributors: * Robin Bowes L * Daniel Miller L * See Changes file for other contributors. -=head1 COPYRIGHT - +=head1 COPYRIGHT + Copyright (c) <2003-2010> - + MIT License - + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights @@ -1900,5 +1902,5 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -=cut + +=cut diff --git a/pandora_server/lib/PandoraFMS/ReconServer.pm b/pandora_server/lib/PandoraFMS/ReconServer.pm index 2e6bf46067..6e33d697a1 100644 --- a/pandora_server/lib/PandoraFMS/ReconServer.pm +++ b/pandora_server/lib/PandoraFMS/ReconServer.pm @@ -131,7 +131,7 @@ sub data_consumer ($$) { } # Call nmap - my $np = new Nmap::Parser; + my $np = new PandoraFMS::NmapParser; eval { $np->parsescan($pa_config->{'nmap'},'-nsP', ($task->{'subnet'})); }; @@ -304,66 +304,82 @@ sub data_consumer ($$) { sub get_host_parent { my ($pa_config, $host, $dbh, $group, $max_depth, $resolve, $os_detect) = @_; - # Recursive exit condition - return 0 if ($max_depth == 0); - # Call nmap - my $nmap = $pa_config->{'nmap'}; - #my $traceroutetimeout = $pa_config->{'networktimeout'}; - my @output = `$nmap --traceroute -nsP $host`; - return 0 if ($? != 0); + my $np = new PandoraFMS::NmapParser; + eval { + $np->parsescan($pa_config->{'nmap'},'-nsP --traceroute', ($host)); + }; + if ($@) { + return 0; + } - # Parse nmap output - my $parent_addr = ''; - foreach my $line (@output) { - chomp ($line); + # Get hops + my ($h) = $np->all_hosts (); + return 0 unless defined ($h); + my @all_hops = $h->all_trace_hops (); + my @hops; + + # Skip target host + pop (@all_hops); + + # Save the last max_depth hosts in reverse order + for (my $i = 0; $i < $max_depth; $i++) { + my $hop = pop (@all_hops); + last unless defined ($hop); + push (@hops, $hop); + } + + # Parse hops from first to last + my $parent_id = 0; + for (my $i = 0; $i < $max_depth; $i++) { + my $hop = pop (@hops); + last unless defined ($hop); - if ($line =~ m/\d+\s+\S+\s+ms\s+(\S+)/) { - next if ($1 eq '*' || $1 eq $host); - $parent_addr = $1; + # Get host information + my $host_addr = $hop->ipaddr (); + + # Check if the host exists + my $agent_id = get_agent_from_addr ($dbh, $host_addr); + if (defined ($agent_id)) { + # Move to the next host + $parent_id = $agent_id; + next; } - } + + + # Add the new address if it does not exist + my $addr_id = get_addr_id ($dbh, $host_addr); + $addr_id = add_address ($dbh, $host_addr) unless ($addr_id > 0); - # No parent found - return 0 if ($parent_addr eq ''); + # Should not happen + if ($addr_id <= 0) { + logger($pa_config, "Could not add address '$host_addr'", 1); + return 0; + } + + # Get the host's name + my $host_name = undef; + if ($resolve == 1){ + $host_name = gethostbyaddr(inet_aton($host_addr), AF_INET); + } + $host_name = $host_addr unless defined ($host_name); + + # Detect host's OS + my $id_os = 11; + if ($os_detect == 1) { + $id_os = guess_os ($pa_config, $host_addr); + } - # Check if the parent host exists - my $parent = get_agent_from_addr ($dbh, $parent_addr); - my $parent_id = defined ($parent) ? $parent->{'id_agente'} : 0; - return $parent_id if ($parent_id > 0); - - # Add the new address if it does not exist - my $addr_id = get_addr_id ($dbh, $parent_addr); - $addr_id = add_address ($dbh, $parent_addr) unless ($addr_id > 0); - - # Should not happen - if ($addr_id <= 0) { - logger($pa_config, "Could not add address '$parent_addr'", 1); - return 0; + # Create the host + $agent_id = pandora_create_agent ($pa_config, $pa_config->{'servername'}, $host_name, $host_addr, $group, $parent_id, $id_os, '', 300, $dbh); + $agent_id = 0 unless defined ($parent_id); + db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`) + VALUES (?, ?)', $addr_id, $agent_id); + + # Move to the next host + $parent_id = $agent_id; } - - # Get the parent's name - my $parent_name = undef; - if ($resolve == 1){ - $parent_name = gethostbyaddr(inet_aton($parent_addr), AF_INET); - } - $parent_name = $parent_addr unless defined ($parent_name); - - # Detect parent's OS - my $id_os = 11; - if ($os_detect == 1) { - $id_os = guess_os ($pa_config, $parent_addr); - } - - # Get the parent's parent - my $parent_parent = get_host_parent ($pa_config, $parent_addr, $dbh, $group, $max_depth-1, $resolve, $os_detect); - - # Create the parent - my $agent_id = pandora_create_agent ($pa_config, $pa_config->{'servername'}, $parent_name, $parent_addr, $group, $parent_parent, $id_os, '', 300, $dbh); - db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`) - VALUES (?, ?)', $addr_id, $agent_id); - - return $agent_id; + return $parent_id; } ##############################################################################