Nmap-Parser-1.37/000755 000765 000024 00000000000 13423136067 014545 5ustar00apersaudstaff000000 000000 Nmap-Parser-1.37/tools/000755 000765 000024 00000000000 13423136067 015705 5ustar00apersaudstaff000000 000000 Nmap-Parser-1.37/LICENSE000644 000765 000024 00000002070 13423134270 015543 0ustar00apersaudstaff000000 000000 Copyright (c) <2003-2013> 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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.Nmap-Parser-1.37/MANIFEST000644 000765 000024 00000000571 13423136067 015701 0ustar00apersaudstaff000000 000000 Changes.md LICENSE Makefile.PL MANIFEST Parser.pm README.md t/callback.t t/instance.t t/instance.xml t/nmap_results.xml t/parser.t t/scanner.t tools/nmap2db.pl tools/nmap2sqlite.pl tools/pdev.pl tools/scan.pl META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Nmap-Parser-1.37/t/000755 000765 000024 00000000000 13423136067 015010 5ustar00apersaudstaff000000 000000 Nmap-Parser-1.37/README.md000644 000765 000024 00000012062 13423134270 016017 0ustar00apersaudstaff000000 000000 # Nmap-Parser Parse nmap scan data with perl. 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. See *[API Reference](http://search.cpan.org/~apersaud/Nmap-Parser/Parser.pm)* The latest version of this module can be found on here [http://modernistik.github.com/Nmap-Parser/](http://modernistik.github.com/Nmap-Parser/) ### Code Status [![CPAN version](https://badge.fury.io/pl/Nmap-Parser.svg)](http://search.cpan.org/~apersaud/Nmap-Parser/Parser.pm) [![Build Status](https://travis-ci.org/modernistik/Nmap-Parser.svg?branch=master)](https://travis-ci.org/modernistik/Nmap-Parser) ## Overview Here is a small example on how to use the module. You can view the more detailed API and examples in the POD documentation. ```perl 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(); my $host = $np->get_host($ip_addr); my $service = $host->tcp_service(80); my $os = $host->os_sig(); ``` Another method is to setup callbacks for each section. ```perl 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 ... } ``` ## CPAN Installation (recommended) The easiest way to install a Perl module is to use span. Run this in the command prompt to install directly from CPAN (may need root priviledges): ```bash $ perl -MCPAN -e 'install Nmap::Parser' # or cpan bin $ cpan install Nmap::Parser ``` ## Manual Install Download the file and unpack. This is usually done by: $ tar zxvf Nmap-Parser-x.xx.tar.gz Where x.xx is the version number. Next change into the newly created directory. To install this module type the following: $ cpanm --quiet --installdeps --notest . $ perl Makefile.PL $ make $ make test $ make install If you would like to install Nmap-Parser in a different directory (or if you do not have root access use `perl Makefile.PL PREFIX=/install/path`, where `/install/path` is the directory in which you want to install the module. Note that you should then use the "use lib '/install/path'" in your scripts. ## ActiveState Perl (Windows only) To install it on Windows, you may need to have installed the ActiveState Perl Package Manager (PPM). Run this in the command prompt: ppm install Nmap-Parser This should contact the ActiveState respository, download the file and install it automagically. ### Dependencies This module requires these other modules and libraries: * XML::Twig 3.16+ * Storable (comes with Perl 5+) In addition, you will need nmap 3.00+. You don't exactly need it, but this version of nmap supports the xml output that this module can parse. So, you do not really need the executable, but the xml output that you will be parsing (or able to parse), must be from this version onward. ### Issues, Bugs and Feature Requests Please submit any bugs or feature requests to: [https://github.com/modernistik/Nmap-Parser/issues](https://github.com/modernistik/Nmap-Parser/issues) Please make sure that you submit the xml-output file of the scan which you are having trouble with. This can be done by running your scan with the `-oX filename.xml` nmap switch. Please remove any important IP addresses for security reasons. It saves time in reproducing issues. ### Releasing To release a new version of the module, update the version number in Parser.pm and type the following commands: $ perl Makefile.PL $ make $ make test $ make dist This will generate a `tar.gz` to upload to https://pause.perl.org. ### Copyright and License Copyright (C) 2003-2018 Anthony Persaud [https://www.modernistik.com](https://www.modernistik.com) 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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. Nmap-Parser-1.37/Parser.pm000644 000765 000024 00000161722 13423135565 016352 0ustar00apersaudstaff000000 000000 package Nmap::Parser; use strict; use XML::Twig; our $VERSION = 1.37; sub new { my ( $class, $self ) = shift; $class = ref($class) || $class; $self->{twig} = new XML::Twig( start_tag_handlers => { nmaprun => sub {$self->_nmaprun_start_tag_hdlr(@_)} }, twig_roots => { scaninfo => sub {$self->_scaninfo_tag_hdlr(@_)}, prescript => sub {$self->_prescript_tag_hdlr(@_)}, postscript => sub {$self->_postscript_tag_hdlr(@_)}, finished => sub {$self->_finished_tag_hdlr(@_)}, host => sub {$self->_host_tag_hdlr(@_)}, }, ignore_elts => { addport => 1, debugging => 1, verbose => 1, hosts => 1, taskbegin => 1, taskend => 1, 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 callback { my $self = shift; my $callback = shift; #first arg is CODE if ( ref($callback) eq 'CODE' ) { $self->{callback}{coderef} = $callback; $self->{callback}{is_registered} = 1; } else { $self->{callback}{is_registered} = 0; } #returns if a callback is registered or not return $self->{callback}{is_registered}; } sub _parse { my $type = shift; my $self = shift; $self->{HOSTS} = undef; $self->{SESSION} = undef; { file => sub { $self->{twig}->safe_parsefile(@_); }, string => sub { $self->{twig}->safe_parse(@_); }, }->{$type}->(@_); if ($@) { die $@; } $self->purge; return $self; } sub parse { return _parse('string', @_); } sub parsefile { return _parse('file', @_); } 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; #if output file is defined, point it to a localfile then call parsefile instead. if ( defined( $self->{cache_file} ) ) { $cmd = "$nmap $args -v -v -v -oX " . $self->{cache_file} . " " . ( join ' ', @ips ); `$cmd`; #remove output from STDOUT $self->parsefile( $self->{cache_file} ); } else { $cmd = "$nmap $args -v -v -v -oX - " . ( join ' ', @ips ); open $FH, "$cmd |" || die "[Nmap-Parser] Could not perform nmap scan - $!"; $self->parse($FH); close $FH; } 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; return $self; } sub addr_sort { my $self = shift if ref $_[0]; return ( map { unpack("x16A*", $_) } sort { $a cmp $b } map { my @vals; if( /:/ ) { #IPv6 @vals = split /:/; @vals = map { $_ eq '' ? (0) x (8-$#vals) : hex } @vals } else { #IPv4 my @v4 = split /\./; # Sort as IPv4-mapped IPv6, per RFC 4291 Section 2.5.5.2 @vals = ( (0) x 5, 0xffff, map { 256*$v4[$_] + $v4[$_+1] } (0,2) ); } pack("n8A*", @vals, $_) } @_ ); } #MAIN SCAN INFORMATION sub get_session { my $self = shift; my $obj = Nmap::Parser::Session->new( $self->{SESSION} ); return $obj; } #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 { 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 { my $self = shift; my $status = shift || ''; return ( values %{ $self->{HOSTS} } ) if ( $status eq '' ); my @hosts = grep { $_->{status} eq $status } ( values %{ $self->{HOSTS} } ); return @hosts; } 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 { my ($self, $twig, $tag ) = @_; $self->{SESSION}{start_time} = $tag->{att}->{start}; $self->{SESSION}{nmap_version} = $tag->{att}->{version}; $self->{SESSION}{start_str} = $tag->{att}->{startstr}; $self->{SESSION}{xml_version} = $tag->{att}->{xmloutputversion}; $self->{SESSION}{scan_args} = $tag->{att}->{args}; $self->{SESSION} = Nmap::Parser::Session->new( $self->{SESSION} ); $twig->purge; } sub _scaninfo_tag_hdlr { my ( $self, $twig, $tag ) = @_; my $type = $tag->{att}->{type}; my $proto = $tag->{att}->{protocol}; my $numservices = $tag->{att}->{numservices}; if ( defined($type) ) { #there can be more than one type in one scan $self->{SESSION}{type}{$type} = $proto; $self->{SESSION}{numservices}{$type} = $numservices; } $twig->purge; } sub _prescript_tag_hdlr { my ($self, $twig, $tag ) = @_; my $scripts_hashref; for my $script ( $tag->children('script') ) { $scripts_hashref->{ $script->{att}->{id} } = __script_tag_hdlr( $script ); } $self->{SESSION}{prescript} = $scripts_hashref; $twig->purge; } sub _postscript_tag_hdlr { my ($self, $twig, $tag ) = @_; my $scripts_hashref; for my $script ( $tag->children('script') ) { $scripts_hashref->{ $script->{att}->{id} } = __script_tag_hdlr( $script ); } $self->{SESSION}{postscript} = $scripts_hashref; $twig->purge; } sub _finished_tag_hdlr { my ($self, $twig, $tag ) = @_; $self->{SESSION}{finish_time} = $tag->{att}->{time}; $self->{SESSION}{time_str} = $tag->{att}->{timestr}; $twig->purge; } #parses all the host information in one swoop (calling __host_*_tag_hdlrs) sub _host_tag_hdlr { my ($self, $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 $self->{HOSTS}{$id}{addrs} = $addr_hashref; return undef unless ( defined($id) || $id ne '' ); #GET HOSTNAMES $self->{HOSTS}{$id}{hostnames} = __host_hostnames_tag_hdlr($tag); #GET STARTTIME $self->{HOSTS}{$id}{starttime} = $tag->{att}->{starttime}; #GET STATUS $self->{HOSTS}{$id}{status} = $tag->first_child('status')->{att}->{state}; #CONTINUE PROCESSING IF STATUS IS UP - OTHERWISE NO MORE XML if ( lc( $self->{HOSTS}{$id}{status} ) eq 'up' ) { $self->{HOSTS}{$id}{ports} = __host_port_tag_hdlr($tag); $self->{HOSTS}{$id}{os} = __host_os_tag_hdlr($tag); $self->{HOSTS}{$id}{uptime} = __host_uptime_tag_hdlr($tag); $self->{HOSTS}{$id}{tcpsequence} = __host_tcpsequence_tag_hdlr($tag); $self->{HOSTS}{$id}{ipidsequence} = __host_ipidsequence_tag_hdlr($tag); $self->{HOSTS}{$id}{tcptssequence} = __host_tcptssequence_tag_hdlr($tag); $self->{HOSTS}{$id}{hostscript} = __host_hostscript_tag_hdlr($tag); $self->{HOSTS}{$id}{distance} = __host_distance_tag_hdlr($tag); #returns simple value $self->{HOSTS}{$id}{trace} = __host_trace_tag_hdlr($tag); $self->{HOSTS}{$id}{trace_error} = __host_trace_error_tag_hdlr($tag); } #CREATE HOST OBJECT FOR USER $self->{HOSTS}{$id} = Nmap::Parser::Host->new( $self->{HOSTS}{$id} ); if ( $self->{callback}{is_registered} ) { &{ $self->{callback}{coderef} }( $self->{HOSTS}{$id} ); delete $self->{HOSTS}{$id}; } $twig->purge; } 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}; } elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv4' ) { $addr_hashref->{ipv4} = $addr->{att}->{addr}; } #support for ipv6? we'll see elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv6' ) { $addr_hashref->{ipv6} = $addr->{att}->{addr}; } } return $addr_hashref; } 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 ); #Parsing Extraports my $extraports_tag = $ports_tag->first_child('extraports'); if ( defined $extraports_tag && $extraports_tag ne '' ) { $port_hashref->{extraports}{state} = $extraports_tag->{att}->{state}; $port_hashref->{extraports}{count} = $extraports_tag->{att}->{count}; } #Parsing regular port information my ( $tcp_port_count, $udp_port_count ) = ( 0, 0 ); for my $port_tag ( $ports_tag->children('port') ) { my $proto = $port_tag->{att}->{protocol}; my $portid = $port_tag->{att}->{portid}; my $state = $port_tag->first_child('state'); my $owner = $port_tag->first_child('owner') || undef; $tcp_port_count++ if ( $proto eq 'tcp' ); $udp_port_count++ if ( $proto eq 'udp' ); $port_hashref->{$proto}{$portid}{state} = $state->{att}->{state} || 'unknown' if ( $state ne '' ); $port_hashref->{$proto}{$portid}{reason_ttl} = $state->{att}->{reason_ttl} || 'unknown' if($state ne ''); #GET SERVICE INFORMATION $port_hashref->{$proto}{$portid}{service} = __host_service_tag_hdlr( $port_tag, $portid ) if ( defined($proto) && defined($portid) ); #GET SCRIPT INFORMATION $port_hashref->{$proto}{$portid}{service}{script} = __host_script_tag_hdlr( $port_tag, $portid) if ( defined($proto) && defined($portid) ); #GET OWNER INFORMATION $port_hashref->{$proto}{$portid}{service}{owner} = $owner->{att}->{name} if ( defined($owner) ); #These are added at the end, otherwise __host_service_tag_hdlr will overwrite #GET PORT STATE } $port_hashref->{tcp_port_count} = $tcp_port_count; $port_hashref->{udp_port_count} = $udp_port_count; return $port_hashref; } 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}; $service_hashref->{product} = $service->{att}->{product}; $service_hashref->{devicetype} = $service->{att}->{devicetype}; $service_hashref->{extrainfo} = $service->{att}->{extrainfo}; $service_hashref->{proto} = $service->{att}->{proto} || $service->{att}->{protocol} || 'unknown'; $service_hashref->{rpcnum} = $service->{att}->{rpcnum}; $service_hashref->{tunnel} = $service->{att}->{tunnel}; $service_hashref->{method} = $service->{att}->{method}; $service_hashref->{confidence} = $service->{att}->{conf}; $service_hashref->{fingerprint} = $service->{att}->{servicefp}; } return $service_hashref; } sub __host_script_tag_hdlr { my $tag = shift; my $script_hashref; for ( $tag->children('script') ) { $script_hashref->{ $_->{att}->{id} } = __script_tag_hdlr($_); } return $script_hashref; } sub __host_os_tag_hdlr { my $tag = shift; my $os_tag = $tag->first_child('os'); my $os_hashref; my $portused_tag; my $os_fingerprint; if ( defined $os_tag ) { #get the open port used to match os $portused_tag = $os_tag->first_child("portused[\@state='open']"); $os_hashref->{portused}{open} = $portused_tag->{att}->{portid} if ( defined $portused_tag ); #get the closed port used to match os $portused_tag = $os_tag->first_child("portused[\@state='closed']"); $os_hashref->{portused}{closed} = $portused_tag->{att}->{portid} if ( defined $portused_tag ); #os fingerprint $os_fingerprint = $os_tag->first_child("osfingerprint"); $os_hashref->{os_fingerprint} = $os_fingerprint->{'att'}->{'fingerprint'} if ( defined $os_fingerprint ); #This will go in Nmap::Parser::Host::OS my $osmatch_index = 0; my $osclass_index = 0; for my $osmatch ( $os_tag->children('osmatch') ) { $os_hashref->{osmatch_name}[$osmatch_index] = $osmatch->{att}->{name}; $os_hashref->{osmatch_name_accuracy}[$osmatch_index] = $osmatch->{att}->{accuracy}; $osmatch_index++; for my $osclass ( $osmatch->children('osclass') ) { $os_hashref->{osclass_osfamily}[$osclass_index] = $osclass->{att}->{osfamily}; $os_hashref->{osclass_osgen}[$osclass_index] = $osclass->{att}->{osgen}; $os_hashref->{osclass_vendor}[$osclass_index] = $osclass->{att}->{vendor}; $os_hashref->{osclass_type}[$osclass_index] = $osclass->{att}->{type}; $os_hashref->{osclass_class_accuracy}[$osclass_index] = $osclass->{att}->{accuracy}; $osclass_index++; } } $os_hashref->{'osmatch_count'} = $osmatch_index; #parse osclass tags for my $osclass ( $os_tag->children('osclass') ) { $os_hashref->{osclass_osfamily}[$osclass_index] = $osclass->{att}->{osfamily}; $os_hashref->{osclass_osgen}[$osclass_index] = $osclass->{att}->{osgen}; $os_hashref->{osclass_vendor}[$osclass_index] = $osclass->{att}->{vendor}; $os_hashref->{osclass_type}[$osclass_index] = $osclass->{att}->{type}; $os_hashref->{osclass_class_accuracy}[$osclass_index] = $osclass->{att}->{accuracy}; $osclass_index++; } $os_hashref->{'osclass_count'} = $osclass_index; } return $os_hashref; } 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 { my $tag = shift; my $sequence = $tag->first_child('tcpsequence'); my $sequence_hashref; return undef unless ($sequence); $sequence_hashref->{class} = $sequence->{att}->{class}; $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 { my $tag = shift; my $sequence = $tag->first_child('ipidsequence'); my $sequence_hashref; return undef unless ($sequence); $sequence_hashref->{class} = $sequence->{att}->{class}; $sequence_hashref->{values} = $sequence->{att}->{values}; return $sequence_hashref; } sub __host_tcptssequence_tag_hdlr { my $tag = shift; my $sequence = $tag->first_child('tcptssequence'); my $sequence_hashref; return undef unless ($sequence); $sequence_hashref->{class} = $sequence->{att}->{class}; $sequence_hashref->{values} = $sequence->{att}->{values}; return $sequence_hashref; } sub __host_hostscript_tag_hdlr { my $tag = shift; my $scripts = $tag->first_child('hostscript'); my $scripts_hashref; return undef unless ($scripts); for my $script ( $scripts->children('script') ) { $scripts_hashref->{ $script->{att}->{id} } = __script_tag_hdlr( $script ); } 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}; $trace_hashref->{proto} = $proto if defined $proto; my $port = $trace_tag->{att}->{port}; $trace_hashref->{port} = $port if defined $port; for my $hop_tag ( $trace_tag->children('hop') ) { # Copy the known hop attributes, they will go in # Nmap::Parser::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.]+$/; push @{ $trace_hashref->{hops} }, \%hop_data; } } return $trace_hashref; } sub __host_trace_error_tag_hdlr { my $tag = shift; my $trace_tag = $tag->first_child('trace'); if ( defined $trace_tag ) { my $error_tag = $trace_tag->first_child('error'); if ( defined $error_tag ) { # If an error happens, always provide a true value even if # it doesn't contains a useful string my $errorstr = $error_tag->{att}->{errorstr} || 1; return $errorstr; } } return; } sub __script_tag_hdlr { my $tag = shift; my $script_hashref = { output => $tag->{att}->{output} }; chomp %$script_hashref; if ( not $tag->is_empty()) { $script_hashref->{contents} = __script_table($tag); } return $script_hashref; } sub __script_table { my $tag = shift; my ($ref, $subref); my $fc = $tag->first_child(); if ($fc) { if ($fc->is_text) { $ref = $fc->text; } else { if ($fc->{att}->{key}) { $ref = {}; $subref = sub { $ref->{$_->{att}->{key}} = shift; }; } else { $ref = []; $subref = sub { push @$ref, shift; }; } for ($tag->children()) { if ($_->tag() eq "table") { $subref->(__script_table( $_ )); } else { $subref->($_->text); } } } } return $ref } #/*****************************************************************************/ # NMAP::PARSER::SESSION #/*****************************************************************************/ package Nmap::Parser::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 { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); no strict 'refs'; *$AUTOLOAD = sub { return $_[0]->{ lc $param } }; goto &$AUTOLOAD; } 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; unless ( defined $id ) { return sort keys %{ $self->{prescript} }; } else { return $self->{prescript}{$id}; } } sub postscripts { my $self = shift; my $id = shift; unless ( defined $id ) { return sort keys %{ $self->{postscript} }; } else { return $self->{postscript}{$id}; } } #/*****************************************************************************/ # NMAP::PARSER::HOST #/*****************************************************************************/ package Nmap::Parser::Host; use vars qw($AUTOLOAD); sub new { my $class = shift; $class = ref($class) || $class; my $self = shift || {}; bless( $self, $class ); return $self; } sub starttime { return $_[0]->{starttime}; } sub status { return $_[0]->{status}; } sub addr { my $default = $_[0]->{addrs}{ipv4} || $_[0]->{addrs}{ipv6}; return $default; } 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 sub hostname { my $self = shift; my $index = shift || 0; if ( ref( $self->{hostnames} ) ne 'ARRAY' ) { return ''; } if ( scalar @{ $self->{hostnames} } <= $index ) { $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; unless ( defined $id ) { return sort keys %{ $self->{hostscript} }; } else { return $self->{hostscript}{$id}; } } sub all_trace_hops { my $self = shift; return unless defined $self->{trace}->{hops}; return map { Nmap::Parser::Host::TraceHop->new( $_ ) } @{ $self->{trace}->{hops} }; } sub trace_port { return $_[0]->{trace}->{port} } sub trace_proto { return $_[0]->{trace}->{proto} } sub trace_error { return $_[0]->{trace_error} } sub _del_port { my $self = shift; my $proto = pop; #portid might be empty, so this goes first my @portids = @_; @portids = grep { $_ + 0 } @portids; unless ( scalar @portids ) { warn "[Nmap-Parser] No port number given to del_port()\n"; return undef; } delete $self->{ports}{$proto}{$_} for (@portids); } sub _get_ports { my $self = shift; my $proto = pop; #param might be empty, so this goes first my $state = shift; #open, filtered, closed or any combination my @matched_ports = (); #if $state is undef, then tcp_ports or udp_ports was called for all ports #therefore, only return the keys of all ports found if ( not defined $state ) { return sort { $a <=> $b } ( keys %{ $self->{ports}{$proto} } ); } else { $state = lc($state) } #the port parameter can be set to either any of these also 'open|filtered' #can count as 'open' and 'filetered'. Therefore I need to use a regex from now on #if $param is empty, then all ports match. for my $portid ( keys %{ $self->{ports}{$proto} } ) { #escape metacharacters ('|', for example in: open|filtered) #using \Q and \E push( @matched_ports, $portid ) if ( $self->{ports}{$proto}{$portid}{state} =~ /\Q$state\E/ ); } return sort { $a <=> $b } @matched_ports; } 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}; } sub _get_port_state_ttl { my $self = shift; my $proto = pop; my $portid = lc(shift); return undef unless ( exists $self->{ports}{$proto}{$portid} ); return $self->{ports}{$proto}{$portid}{reason_ttl}; } #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_ttl { return _get_port_state_ttl( @_, 'tcp' ); } 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 { 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( $self->{ports}{tcp}{$portid}{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( $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 sub AUTOLOAD { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); my ( $type, $val ) = split /_/, lc($param); #splits the given method name by '_'. This will determine the function and param no strict 'refs'; if ( ( $type eq 'tcp' || $type eq 'udp' ) && ( $val eq 'open' || $val eq 'filtered' || $val eq 'closed' ) ) { #they must be looking for port info: tcp or udp. The $val is either open|filtered|closed *$AUTOLOAD = sub { return _get_ports( $_[0], $val, $type ); }; goto &$AUTOLOAD; } elsif ( defined $type && defined $val ) { #must be one of the 'sequence' functions asking for class/values/index *$AUTOLOAD = sub { return $_[0]->{$type}{$val} }; 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 { 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} }; } else { return $self->{script}{$id}; } } #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 { 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; @_ = (); 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 { ( 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' ) { *$AUTOLOAD = sub { _get_info( $_[0], $_[1], $param, 'osmatch' ); }; goto &$AUTOLOAD; } else { *$AUTOLOAD = sub { _get_info( $_[0], $_[1], $param, 'osclass' ); }; goto &$AUTOLOAD; } } sub _get_info { my ( $self, $index, $param, $type ) = @_; $index ||= 0; #type is either osclass or osmatch if ( $index >= $self->{ $type . '_count' } ) { $index = $self->{ $type . '_count' } - 1; } return $self->{ $type . '_' . $param }[$index]; } #/*****************************************************************************/ # NMAP::PARSER::HOST::TRACEHOP #/*****************************************************************************/ package Nmap::Parser::Host::TraceHop; use vars qw($AUTOLOAD); sub new { my $class = shift; $class = ref($class) || $class; my $self = shift || {}; bless( $self, $class ); return $self; } sub AUTOLOAD { ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms; return if ( $param eq 'DESTROY' ); no strict 'refs'; $param = lc($param); # Supported accessors: my %subs; @subs{ qw( ttl rtt ipaddr host ) } = 1; if ( exists $subs{$param} ) { *$AUTOLOAD = sub { $_[0]->{$param} }; goto &$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 You can also visit the website L for additional installation instructions. =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 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. 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 been parsed by the script to conserve memory and increase speed. 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). =item B This function allows you to save the output of a parsescan() (or nmap scan) to the disk. $filename is the name of the file you wish to save the nmap scan information to. It defaults to nmap-parser-cache.xml It returns the name of the file to be used as the cache. #Must be called before parsescan(). $np->cache_scan($filename); #output set to nmap-parser-cache.xml #.. do other stuff to prepare for parsescan(), ex. setup callbacks $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 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)> =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. =item B =item B A basic call to prescripts() returns a list of the names of the NSE scripts run in the pre-scanning phase. If C<$name> is given, it returns the text output of the a reference to a hash with "output" and "contents" keys for the script with that name, or undef if that script was not run. The value of the "output" key is the text output of the script. The value of the "contents" key is a data structure based on the XML output of the NSE script. =item B =item B A basic call to postscripts() returns a list of the names of the NSE scripts run in the post-scaning phase. If C<$name> is given, it returns the text output of the a reference to a hash with "output" and "contents" keys for the script with that name, or undef if that script was not run. The value of the "output" key is the text output of the script. The value of the "contents" key is a data structure based on the XML output of the NSE script. =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. =item B Return the distance (in hops) of the target machine from the machine that performed the scan. =item B Returns a true value (usually a meaningful error message) if the traceroute was performed but could not reach the destination. In this case C 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 to the target host. This array may be empty if Nmap did not perform the traceroute for some reason (same network, for example). Some hops may be missing if Nmap could not figure out information about them. In this case there is a gap between the C values of consecutive returned hops. See also C. =item B Returns the name of the protocol used to perform the traceroute. =item B 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 =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 a reference to a hash with "output" and "contents" keys for the script with that name, or undef if that script was not run. The value of the "output" key is the text output of the script. The value of the "contents" key is a data structure based on the XML output of the NSE script. =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 Returns the 'reason_ttl' value present in nmap xml result. =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 Returns the service fingerprint. (If available) =item B Returns the version of the given product of the running service. =item B =item B A basic call to scripts() returns a list of the names of the NSE scripts run for this port. If C<$name> is given, it returns a reference to a hash with "output" and "contents" keys for the script with that name, or undef if that script was not run. The value of the "output" key is the text output of the script. The value of the "contents" key is a data structure based on the XML output of the NSE script. =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. =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 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. =over 4 =item B The Time To Live is the network distance of this hop. =item B The Round Trip Time is roughly equivalent to the "ping" time towards this hop. It is not always available (in which case it will be undef). =item B The known IP address of this hop. =item B 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. 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... } 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; #telling np to save output $np->cache_scan('nmap.localhost.xml'); $np->parsescan('/usr/bin/nmap','-F','localhost'); #do other stuff... =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. L =head2 Bug Reports, Enhancements, Merge Requests 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 AUTHORS Origiinal author, Anthony Persaud L. However, special thanks to: Daniel Miller L and Robin Bowes L. Please see Changes.md file for a list of other great contributors. =head1 COPYRIGHT Copyright (c) <2003-2017> L 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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 Nmap-Parser-1.37/META.yml000644 000765 000024 00000000677 13423136067 016030 0ustar00apersaudstaff000000 000000 --- abstract: 'Parse nmap scan data with perl' author: - 'Anthony Persaud' build_requires: {} dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.66, CPAN::Meta::Converter version 2.133380' license: mit meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Nmap-Parser no_index: directory: - t - inc resources: {} version: 1.37 x_homepage: https://github.com/modernistik/Nmap-Parser Nmap-Parser-1.37/Changes.md000644 000765 000024 00000035035 13423134623 016441 0ustar00apersaudstaff000000 000000 Revision history for Perl extension Nmap::Parser. Bleeding Edge version: https://github.com/modernistik/Nmap-Parser For a full list of changes and contributors see: https://github.com/modernistik/Nmap-Parser/commits/master ### Changes for 1.37 - Added starttime attributes and subroutine thanks to @jcrochon (PR #19) ### Changes for 1.35 - Updated build configuration thanks to @mperry2 (PR #17) ### Changes for 1.34 - Added devicetype thanks to @jcrochon (PR #16) ### Changes for 1.33 - Add tcp_port_state_ttl() function for export from nmap xml results. Thanks to @matrix. - Document fixes thanks to @zOrg1331 ### Changes for 1.32 - Updated website: https://github.com/modernistik/Nmap-Parser - Eliminate global variables %D - thanks to bonsaiviking ### Changes for 1.30 - Merged features of pull request #6 (bonsaiviking) https://github.com/modernistik/Nmap-Parser/commit/7ccf752af - Allow osclass elements within osmatch, Nmap XML format changed in 6.00 ### Changes for 1.21 - Added support for hostscript and script tags - Changed ipv4_sort() to use a 10x faster sort method ### Changes for 1.20 - Solved Issue 2: Host-specific start_time and end_time. - Applied Patch provided by briandlong on retrieving start_time and end_time attributes for host. - Solved Issue 6: \_del_port not removing port 0. - Thomas Equeter submitted patch to support traceroute in nmap output. ### Changes for 1.19 - Added enhancement request by stevekatieterabyte for tcp_del_port and udp_del_port (Thanks!). Modified the patch to work with a list of ports. - Added Robin Bowes' modification of nmap2sqlite as nmap2db to support MySQL (Thanks!) ### Changes for 1.16 - Fixed minor bug in scanner.t where the number of tests to skip when nmap was not found was incorrect. - Repackaged to remove all the .\_* files from the package. - Fixed POD errors and added more documentation ### Changes for 1.14 - Added cache_scan() to save the output of a parsescan() to a file before parsing. - Added new tests for servicefp fingerpriting and cache_scan(). - Ran PerlTidy against module and other tools - Updated documentation ### Changes for 1.13 - Added fingerprint() to Service object (thanks jpomiane) - Added documentation. ### Changes for 1.12 - Added references to Google Code Project page. ### Changes for 1.11 - Added parsing of distance information. - Fixed bug #1671876 on tcp_service() always returning null - Added ignoring of taskend,taskbegin and taskprogress information. - Added tests for nmap 4.20. - Changed lisence to MIT. ### Changes for 1.06 - Added patch for new OS fingerprint (Thanks Okan Demirmen) - New os_fingerprint() method for Nmap::Parser::Host::OS - Updated documentation - Updated scan.pl to also read xml files (good for debugging) ### Changes for 1.05 - Major speed improvements (less compile time) - Major reduction in unwanted memory usage - Redundant functions (or less used functions) are now created dynamically. (AUTOLOAD) - Documentation fixes ### Changes for 1.00 - To see the changes, please read over the new documentation - Internal code is much (MUCH) cleaner and readable - removed 'ducttape' fixes and made stable & roubust changes - improved performance, removed unwanted code (legacy) - complete overhaul of internal code - new Framework - support for IPv6 addresses - data overwrite (overflow) protection - better support for multiple instances - fixed some minor bugs - process owner information obtained - all OS accuracy information obtained - some functions now take new parameters (more concise) - some functions renamed for clarity - new shortcut functions (for doing repetitive tasks easier) - Removed parsing filters (finally) - All indexes now start at 0 (not at 1). - Removed internal OS generic matching function since this is given by nmap now in the osclass tags - Removed the use of constants for indexes - Nmap::Parser::Host::Service object provides OO interface to service information for a given port - Nmap::Parser::Host::OS object provides OO interface to OS signature information for a given host - Nmap::Parser::Session replaces old Nmap::Parser::ScanInfo package - Nmap2SQLite security script included - removed old security tools - rewrote scan.pl (from scanhost.pl) - rewrote old tools to fit new framework - Fully updated documentation ### Changes for 0.80 - Support for multiple instances of Nmap::Parser objects without overwriting data - All data (except filters) is are localized per object - The use of Storable (dclone) to correctly make duplicate of data structured - When filters are used to skip portinfo, all ports return state of 'closed'. - Nmap::Parser::XML no longer supported in distribution (you should change all calls to Nmap::Parser::XML to Nmap::Parser) ### Changes for 0.79 - fixed ports that were declared as 'open|filtered' these now are counted as both 'open' and 'filtered' when using tcp_ports() and udp_ports(). - sent a patch for XML::Twig in order to fix. It is now released in XML:Twig 3.16 - added start_str() and time_str() : they return the human readable format of the scan start time and scan finish time (respectively). - updated for Nmap 3.81 - documentation changes ### Changes for 0.78 - updated documentation - now included tcp/udp service product - added new methods for new nmap command switch (-A) mac_addr, mac_vendor, ipv4_addr - verified xml format is still valid for nmap 3.55 ### Changes for 0.77 - updated documentation - added patches from Jeremy S. - tcp_service and udp_service tunnel, accuracy, confidence - os_accuracy method implemented - added ident/owner information ### Changes for 0.76 - new module name: Nmap::Parser - leagcy file still ok to use for now: Nmap::Parser::XML - fixed problem using 'our' with older versions of perl - updated requirement for oldest usable version of XML::Twig => 3.11 - you can extract owner information from running -I scanning tcp_service_owner and udp_service_owner ### Changes for 0.74 - fixed ip address input bug - updated example scripts - fixed nmap not found bug - updated authorship informatin - raised verbose level to 2 in example scripts (help pages) - test script 4_scanner does not test. If nmap not installed it skips all tests. - updated tests - some example scripts read IP addresses from file - placed sourceforge image link on documentation - status_check is now called sweep - sweep.pl outputs active IP's to a file with a new command line switch - scan_host.pl is renamed to scanhost.pl - get_host_list,filter_by_osfamily, filter_by_status returns IP addresses in sorted IP order. ( uses sort_ips() ) - sort_ips is a new function which will take a series of IPs and sort them correctly by comparing each quad in the address to each other. - example scripts use --randomize_hosts to be more stealthy - updated information on some example scripts because they require nmap 3.50+ for the version scanning. - updated parser tests, to check for sorted ip order ### Changes for 0.73 - removed safe_* functions and placed them in the actual parsing functions. (I don't think they were never used. - updated BUG REPORT information ### Changes for 0.72 - removed IGNORE_ADDPORTS constant - added contribution from Sebastian: nmap2csv. - edited the links to the sourceforge project site. - added parsescan() function to peroform quick nmap scans and parsing. - added more documentation - fixed some example scripts - added more tests for the new functionality ### Changes for 0.71 - fixed a small bug in the installation under MSWin32 (PM_FILTER) which caused all tests to fail. (It was removing things that weren't comments. ### Changes for 0.70 - updated changes from 0.69 - updated documentation - fixed documentation bug of all the example scripts - updated examples script: they are more robust. Can either take the example file as input, or actually run scans. ### Changes for 0.69 - new utility script : scan_host.pl - added EXAMPLES seciton in documentation - parses new 'version', 'extrainfo', and 'product' att from service tag (3.40+) - added \*\_service_version to \*::Host - added xml_version to \*::ScanInfo - more error prevention mechanisms - added os_osfamily, os_gen, os_vendor, os_type added - added OSINFO filter - ::ScanInfo::scan_types does not return number of scan types in scalar format. It will always return an array containing the scan types. - osfamily does not return the actual string (comma delimited), it always returns an array of os matches. - DEPRECATED: tcpsequence, ipidsequence, tcptssequence now use: tcpsequence_class, tcpsequence_values, tcpsequence_index ipidsequence_class, ipidsequence_values tcptssequence_class, tcptssequence_values ### Changes for 0.68 - Licensing changes, now under GPL - added signatures for wireless access points (wap) - added os_match shortcut function - Problem with Makefile.PL, didn't pass correct dependencies. - tcp_port_state() and udp_port_state() return the state of the port when passed a port number. - Sorted port order when using tcp_ports and udp_ports - extraports tag parsing. It is also set up as a filter 'extraports' filtering. Added extraports_state and extraports_count to Nmap::Parser::Host class. - Added and fix some documentation - tcp_ports and udp_ports can take a parameter to filter what port list you wish to receive. It selects states based on port content state tag: filtered, closed, or open - previous versions (0.64 or earlier) of the parser, no arguments to tcp_ports and udp_ports would return the whole hashref of all the ports, this is now deprecated. Use the newly created functions tcp_service_name, tcp_service_proto, tcp_service_rpcnum, udp_service_name, udp_service_proto, and udp_service_rpcnum. - changed default filter for solaris to include 'sun' and not 'sunos' - more example scripts - no more wantarray usage for tcp_ports and udp_ports - more test cases ### Changes for 0.66 - added short-cut function hostname() to return first hostname - added preliminary callback functionality (for registering events). This includes register_host_callback, and reset_host_callback - tcp_ports and udp_ports do not return hashref of all ports, only if passed a port number as an argument.The argument must be a port number. They default to returning an array of port numbers. - added short-cuts tcp_ports_count and udp_ports_count functions - added tcp_service_proto and udp_service_proto - added tcp_service_rpcnum and udp_service_rpcnum - POD fixes. - speed improvements ### Changes for 0.64 - nmaprun filter bug fixed - important documentation changes ### Changes for 0.63 - added vendor to os_class - fixed division by zero on one of the efficiency test. - it now checks to make sure Time::HiRes is installed before performing tests. - minor warning problems removed on Win32 systems. ### Changes for 0.62 - stable release with all new changes. ### Changes for 0.6_4 - changes to parse filter tags. All previously called PARSE_* have the PARSE_ removed from them. Ex: PARSE_OSFAMILY is now OSFAMILY. - osclass tag added. - a bug found with the sequences is fixed - making use of ignore_elts to save when creating objects - parse_filters completly excludes tags that you decide not to parse. Much faster parsing and memory usage efficiency. efficiency.t tests this benchmark to make sure that the twig parser does not do any work it doesn't have to. - permanently excluding some static tags using ignore_elts. - added SCANINFO filter. ### Changes for 0.60_3 - os_port_used, now can return the open or closed port used in OS detection depending on the given parameter. ### Changes for 0.60_2 - Bug #2968: fixed bogus 'use 5.008' in Makefile.PL (runs on 5.6 also) instead using 5.004 fixed minor warnings when compiling with -w added 'use warnings' to \_methods test ### Changes for 0.60_1 - fixed a bug with the test script (finding test file) - made a separate test to test the actual method existance - portability when running the tests using File::Spec. ### Changed for 0.60 - better memory management using twig_roots - some bugs with output types and filters - generic_os and all references are now refereed to as 'osfamily' I thought it better resembles what it stands for. - fixed some documentation problems - parse_filter_* have been replaced with parse_filters(), which can enable multiple different filters through a hashref. Filters available: ONLY_ACTIVE, PARSE_OSFAMILY, PARSE_UPTIME, PARSE_PORTINFO, PARSE_SEQUENCES - added parse information of tcpsequence, ipidsequence, tcptssequence - additions to Nmap::Parser::Host methods tcpsequence, ipidsequence, tcptssequence ### Changes for 0.50 - faster loading module - added more documentation - minor speed improvements - added methods to Nmap::Parser parse_filter_generic_os($bool) (see doc) - renamed only_active() to parse_filter_status($bool) (see doc) - Nmap::Parser::Host changed hostnames() to take a value a number (see doc) changed os_matches() to take a value a number (see doc) ### Changes for 0.40 - added new package called ScanInfo (Nmap::Parser::ScanInfo this contains methods that make it easier to access the scan information - added new package called Host (Nmap::Parser::Host), which makes it easier to access values for each of the hosts found. See documentation. Host trees are now full of these \*::Host objects. - fixed minor bugs with parsing the xml files. - some memory usage improvements. COPYRIGHT AND LICENSE Copyright (C) 2003-2017 Anthony Persaud L 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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. Nmap-Parser-1.37/Makefile.PL000644 000765 000024 00000001470 13423136033 016512 0ustar00apersaudstaff000000 000000 use 5.004; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. my %depends = ( XML::Twig => 3.16, Storable => 2 ); WriteMakefile( ABSTRACT => 'Parse nmap scan data with perl', AUTHOR => 'Anthony Persaud', LICENSE => 'mit', clean => { FILES => "*.tar *.old pod2htm* *.htm*" }, #EXE_FILES => [ map {"tools/$_"} @programs], #soon NAME => 'Nmap::Parser', PREREQ_PM => \%depends, VERSION_FROM => 'Parser.pm', # finds $VERSION META_MERGE => { "meta-spec" => { version => 2 }, homepage => 'https://github.com/modernistik/Nmap-Parser', resources => { repository => "https://github.com/modernistik/Nmap-Parser", }, }, ); Nmap-Parser-1.37/META.json000644 000765 000024 00000001151 13423136067 016164 0ustar00apersaudstaff000000 000000 { "abstract" : "Parse nmap scan data with perl", "author" : [ "Anthony Persaud" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.66, CPAN::Meta::Converter version 2.133380", "license" : [ "mit" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Nmap-Parser", "no_index" : { "directory" : [ "t", "inc" ] }, "release_status" : "stable", "resources" : {}, "version" : "1.37", "x_homepage" : "https://github.com/modernistik/Nmap-Parser" } Nmap-Parser-1.37/t/instance.xml000644 000765 000024 00000006313 13423134270 017333 0ustar00apersaudstaff000000 000000

Nmap-Parser-1.37/t/parser.t000644 000765 000024 00000045153 13423134270 016473 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl use strict; use blib; use Nmap::Parser; use File::Spec; use Cwd; use Test::More tests => 177; use constant HOST1 => '127.0.0.1'; use constant HOST2 => '127.0.0.2'; use constant HOST3 => '127.0.0.3'; use constant HOST4 => '127.0.0.4'; my @UP_HOSTS = ( HOST1, HOST3, HOST4 ); my @DOWN_HOSTS = (HOST2); use constant TOTAL_HOSTS => 4; use constant TEST_FILE => 'nmap_results.xml'; use vars qw($host $np $session $svc $os $FH); $FH = File::Spec->catfile( cwd(), 't', TEST_FILE ); $FH = File::Spec->catfile( cwd(), TEST_FILE ) unless ( -e $FH ); my $np = new Nmap::Parser; parser_test(); session_test(); host_1(); host_2(); host_3(); host_4(); sub parser_test { ok( $np->parsefile($FH), "Parsing nmap data: $FH" ); #TESTING GET_IPS() is_deeply( [ $np->get_ips() ], [ HOST1, HOST2, HOST3, HOST4 ], 'Testing get_ips for correct number of hosts' ); is_deeply( [ $np->get_ips('up') ], [ HOST1, HOST3, HOST4 ], 'Testing get_ips for correct hosts with status = up' ); is_deeply( [ $np->get_ips('down') ], [HOST2], 'Testing get_ips for correct hosts for with status = down' ); #TESTING IPV4_SORT my @hosts = ( HOST3, HOST1, HOST4, HOST2 ); is_deeply( [ $np->addr_sort(@hosts) ], [ HOST1, HOST2, HOST3, HOST4 ], 'Testing addr_sort' ); #TESTING ALL_HOSTS() my $total_hosts = 0; for my $h ( $np->all_hosts() ) { isa_ok( $h, 'Nmap::Parser::Host' ); isnt( $h->status, undef, 'Testing host object ' . $h->addr . ' in all_hosts()' ); $total_hosts++; } is( $total_hosts, TOTAL_HOSTS, "Testing correct number of hosts with all_hosts()" ); #TESTING ALL_HOSTS(UP) my $total_uphosts = 0; for my $h ( $np->all_hosts('up') ) { isnt( $h->addr, HOST2, 'Testing host ' . $h->addr . ' in all_hosts(up)' ); $total_uphosts++; } is( $total_uphosts, scalar(@UP_HOSTS), "Testing correct number of UP hosts with all_hosts(up)" ); #TESTING ALL_HOSTS(DOWN) my $total_downhosts = 0; for my $h ( $np->all_hosts('down') ) { is( $h->addr, HOST2, 'Testing host ' . $h->addr . ' in all_hosts(up)' ); $total_downhosts++; } is( $total_downhosts, scalar(@DOWN_HOSTS), "Testing correct number of DOWN hosts with all_hosts(down)" ); } sub session_test { isa_ok( $session = $np->get_session(), 'Nmap::Parser::Session' ); is( $session->numservices(), 1023 + 1023, 'Session: total number of services' ); is( $session->numservices('connect'), 1023, 'Session: numservices type = connect' ); is( $session->numservices('udp'), 1023, 'Session: numservices type = udp' ); is( $session->start_time(), 1057088883, 'Session: start_time' ); is( $session->start_str(), 'Tue Jul 1 14:48:03 2003', 'Session: start_str' ); is( $session->finish_time(), 1057088900, 'Session: finish_time' ); is( $session->time_str(), 'Tue Jul 1 14:48:20 2003', 'Session: time_str' ); is( $session->nmap_version(), '3.80', 'Session: nmap_version' ); is( $session->xml_version(), '1.04', 'Session: xml_version' ); is( $session->scan_args(), 'nmap -v -v -v -oX test.xml -O -sTUR -p 1-1023 127.0.0.[1-4]', 'Session: scan_args' ); is( $session->scan_type_proto(), undef, 'Session: scan_type_proto()' ); is( $session->scan_type_proto('connect'), 'tcp', 'Session: scan_type_proto(connect)' ); is( $session->scan_type_proto('udp'), 'udp', 'Session: scan_type_proto(udp)' ); is_deeply( [ $session->prescripts ], [ 'broadcast-dropbox-listener' ], 'Session has prescripts' ); like( $session->prescripts('broadcast-dropbox-listener')->{output}, qr/\ndisplayname .* 10422330$/s, 'Prescript output correct' ); is_deeply( [ $session->postscripts ], [ 'ssh-hostkey' ], 'Session has postscripts' ); like( $session->postscripts('ssh-hostkey')->{output}, qr/Possible .* 192.168.1.124$/s, 'Postscript output correct' ); } sub host_1 { isa_ok( $host = $np->get_host(HOST1), 'Nmap::Parser::Host', 'GET ' . HOST1 ); is( $host->status, 'up', 'Host1: status' ); is( $host->addr, HOST1, 'Host1: addr' ); is( $host->addrtype, 'ipv4', 'Host1: addrtype' ); is( $host->ipv4_addr, HOST1, 'Host1: ipv4_addr' ); is( $host->mac_addr, '00:09:5B:3F:7D:5E', 'Host1: mac_addr' ); is( $host->mac_vendor, 'Netgear', 'Host1: mac_vendor' ); is( $host->hostname, 'host1', 'Host1: hostname()' ); is( $host->hostname(0), $host->hostname, 'Host1: hostname(0)' ); is( $host->hostname(1), 'host1_2', 'Host1: hostname(1)' ); is_deeply( [ $host->all_hostnames() ], [ 'host1', 'host1_2' ], 'Host1: all_hostnames' ); #Testing Port Information is( $host->extraports_state(), 'closed', 'Host1: extraports_state' ); is( $host->extraports_count(), 2038, 'Host1: extraports_count' ); is( $host->tcp_port_count(), 8, 'Host1: tcp_port_count' ); is( $host->udp_port_count(), 2, 'Host1: udp_port_count' ); is_deeply( [ $host->tcp_ports() ], [qw(22 25 80 111 443 555 631 4903)], 'Host1: tcp_ports()' ); is_deeply( [ $host->tcp_ports('open') ], [qw(80 111 443 555 631)], 'Host1: tcp_ports(open)' ); is_deeply( [ $host->tcp_ports('closed') ], [qw(4903)], 'Host1: tcp_ports(closed)' ); is_deeply( [ $host->tcp_ports('filtered') ], [qw(22 25 555)], 'Host1: tcp_ports(filtered)' ); is_deeply( [ $host->tcp_ports('open|filtered') ], [qw(555)], 'Host1: tcp_ports(open|filtered)' ); is_deeply( [ $host->udp_ports() ], [qw(111 937)], 'Host1: udp_ports()' ); is_deeply( [ $host->udp_ports('open') ], [qw(111)], 'Host1: udp_ports(open)' ); is_deeply( [ $host->udp_ports('filtered') ], [qw(937)], 'Host1: udp_ports(filtered)' ); is_deeply( [ $host->udp_ports('closed') ], [qw()], 'Host1: udp_ports(closed)' ); is( $host->tcp_ports('open'), $host->tcp_open_ports(), 'Host1: tcp_open_ports' ); is( $host->tcp_ports('filtered'), $host->tcp_filtered_ports(), 'Host1: tcp_filtered_ports' ); is( $host->tcp_ports('closed'), $host->tcp_closed_ports(), 'Host1: tcp_closed_ports' ); is( $host->udp_ports('open'), $host->udp_open_ports(), 'Host1: udp_open_ports' ); is( $host->udp_ports('filtered'), $host->udp_filtered_ports(), 'Host1: udp_filtered_ports' ); is( $host->udp_ports('closed'), $host->udp_closed_ports(), 'Host1: udp_closed_ports' ); $host->tcp_del_ports('80'); is_deeply( [ $host->tcp_ports('open') ], [qw(111 443 555 631)], 'Host1: tcp_del_ports(80) (should not be open)' ); $host->tcp_del_ports( 111, 443 ); is_deeply( [ $host->tcp_ports('open') ], [qw(555 631)], 'Host1: tcp_del_ports(111,443) (should not be open)' ); is_deeply( [ $host->tcp_ports() ], [qw(22 25 555 631 4903)], 'Host1: tcp_ports() after deleting 80,111,443' ); is( $host->uptime_seconds(), '1973', 'Host1: uptime_seconds' ); is( $host->uptime_lastboot(), 'Tue Jul 1 14:15:27 2003', 'Host1: uptime_lastboot' ); is( $host->tcpsequence_index(), 4336320, 'Host1: tcpsequence_index' ); is( $host->tcpsequence_class(), 'random positive increments', 'Host1: tcpsequence_class' ); is( $host->tcpsequence_values(), 'B742FEAF,B673A3F0,B6B42D41,B6C710A1,B6F23FC4,B72FA3A8', 'Host1: tcpsequence_values' ); is( $host->ipidsequence_class(), 'All zeros', 'Host1: ipidsequence_class' ); is( $host->ipidsequence_values(), '0,0,0,0,0,0', 'Host1: ipidsequence_values' ); is( $host->tcptssequence_class(), '100HZ', 'Host1: tcptssequence_class' ); is( $host->tcptssequence_values(), '30299,302A5,302B1,302BD,302C9,302D5', 'Host1: tcptssequence_values' ); is( $host->distance(), 1, 'Host1: distance = 1' ); isa_ok( $np->del_host(HOST1), 'Nmap::Parser::Host', 'DEL ' . HOST1 ); is( $np->get_host(HOST1), undef, 'Testing deletion of ' . HOST1 ); #TESTING SERVICE OBJECT FOR HOST 1 my $svc; isa_ok( $svc = $host->tcp_service(22), 'Nmap::Parser::Host::Service', 'TCP port 22' ); is( $svc->name, 'ssh', 'Service: name' ); is( $svc->method, 'table', 'Service: method' ); is( $svc->confidence, 3, 'Service: confidence' ); isa_ok( $svc = $host->udp_service(111), 'Nmap::Parser::Host::Service', 'UDP port 111' ); is( $svc->name, 'rpcbind', 'Service: name' ); is( $svc->proto, 'rpc', 'Service: proto' ); is( $svc->rpcnum, '100000', 'Service: rpcnum' ); #TESTING OS OBJECT FOR HOST 1 my $os; my $fingerprint = " SEQ(SP=C5%GCD=1%ISR=C7%TI=Z%II=I%TS=8) ECN(R=Y%DF=Y%T=40%W=16D0%O=M5B4NNSNW2%CC=N%Q=) T1(R=Y%DF=Y%T=40%S=O%A=S+%F=AS%RD=0%Q=) T2(R=N) T3(R=Y%DF=Y%T=40%W=16A0%S=O%A=S+%F=AS%O=M5B4ST11NW2%RD=0%Q=) T4(R=Y%DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=) T7(R=Y%DF=Y%T=40%W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=) U1(R=Y%DF=N%T=40%TOS=C0%IPL=164%UN=0%RIPL=G%RID=G%RIPCK=G%RUCK=G%RUL=G%RUD=G) IE(R=Y%DFI=N%T=40%TOSI=S%CD=S%SI=S%DLI=S) "; isa_ok( $os = $host->os_sig(), 'Nmap::Parser::Host::OS', 'os_sig()' ); is( $os->os_fingerprint(), $fingerprint, 'HOST1: os_fingerprint()' ); #TESTING NON-EXISTENT TRACE FOR HOST1 ok( !$host->all_trace_hops(), 'Host1 has no trace information' ); #TESTING NON-EXISTENT HOSTSCRIPT FOR HOST1 ok( !$host->hostscripts(), 'Host1 has no hostscript information' ); } sub host_2 { isa_ok( $host = $np->get_host(HOST2), 'Nmap::Parser::Host', 'GET ' . HOST2 ); is( $host->addr, HOST2, 'Host2: addr' ); is( $host->status, 'down', 'Host2: status = down' ); isa_ok( $np->del_host(HOST2), 'Nmap::Parser::Host', 'DEL ' . HOST2 ); is( $np->get_host(HOST2), undef, 'Testing deletion of ' . HOST2 ); } sub host_3 { isa_ok( $host = $np->get_host(HOST3), 'Nmap::Parser::Host', 'GET ' . HOST3 ); #TESTING SERVICE OBJECTS my $svc; isa_ok( $svc = $host->tcp_service(22), 'Nmap::Parser::Host::Service', 'tcp_service(22) for ' . HOST3 ); is( $svc->owner, 'root', 'TCP Service: owner' ); is( $svc->name, 'ssh', 'TCP Service: name' ); is( $svc->product, 'OpenSSH', 'TCP Service: product' ); is( $svc->version, '3.4p1', 'TCP Service: version' ); is( $svc->extrainfo, 'protocol 1.99', 'TCP Service: extrainfo' ); is_deeply( [ $svc->scripts() ], [ 'ssh-hostkey' ], 'Port has scripts' ); { my $output = $svc->scripts('ssh-hostkey')->{output}; like( $output, qr/^1024 .* \(RSA\)$/s, 'Script output ok'); } isa_ok( $svc = $host->udp_service(80), 'Nmap::Parser::Host::Service', 'udp_service(780) for ' . HOST3 ); is( $svc->owner, 'www-data', 'UDP Service: owner' ); is( $svc->name, 'http', 'UDP Service: name' ); is( $svc->product, 'Apache httpd', 'UDP Service: product' ); is( $svc->version, '1.3.26', 'UDP Service: version' ); is( $svc->extrainfo, '(Unix) Debian GNU/Linux', 'UDP Service: extrainfo' ); my $servicefp = 'SF-Port25-TCP:V=3.48%D=11/5%Time=3FA9032C%r(NULL,57,"220\x20s0002\.berger\.com\x20ESMTP\x20Oracle\x20Email\x20Server\x20SMTP\x20Inbound\x20Server\t9\.0\.4\.0\.0\x20\t\x20\x20Ready\r\n")%r(Help,17D,"220\x20s0002\.berger\.com\x20ESMTP\x20Oracle\x20Email\x20Server\x20SMTP\x20Inbound\x20Server\t9\.0\.4\.0\.0\x20\t\x20\x20Ready\r\n214-2\.3\.0\x20This\x20is\x20Oracle\x20eMail\x20SMTP\x20Server\n214-2\.3\.0\x20\x20\x20\x20\x20\x20\x20\x20\x20HELO\x20\x20\x20\x20EHLO\x20\x20\x20\x20MAIL\x20\x20\x20\x20RCPT\x20\x20\x20\x20DATA\n214-2\.3\.0\x20\x20\x20\x20\x20\x20\x20\x20\x20RSET\x20\x20\x20\x20NOOP\x20\x20\x20\x20QUIT\x20\x20\x20\x20HELP\x20\x20\x20\x20DSN\n214-2\.3\.0\x20For\x20more\x20info\x20use\x20\"HELP\x20\"\.\n214-2\.3\.0\x20For\x20local\x20information\x20send\x20email\x20to\x20Postmaster\x20at\x20your\x20site\.\n214\x202\.3\.0\x20End\x20of\x20HELP\x20info\n");'; isa_ok( $svc = $host->tcp_service(25), 'Nmap::Parser::Host::Service', 'tcp_service(25) for ' . HOST3 ); is( $svc->fingerprint(), $servicefp, 'Verifying correct servicefp parsing on tcp_service(25) for ' . HOST3 ); $servicefp = 'SF-Port21-TCP:V=3.48%D=11/5%Time=3FA9032C%r(NULL,32,"220\x20Oracle\x20Internet\x20File\x20System\x20FTP\x20Server\x20ready\r\n")%r(GenericLines,53,"220\x20Oracle\x20Internet\x20File\x20System\x20FTP\x20Server\x20ready\r\n200\x20Connection\x20closed,\x20good\x20bye\r\n")%r(Help,57,"220\x20Oracle\x20Internet\x20File\x20System\x20FTP\x20Server\x20ready\r\n500\x20HELP:\x20command\x20not\x20understood\.\r\n");'; isa_ok( $svc = $host->tcp_service(953), 'Nmap::Parser::Host::Service', 'tcp_service(953) for ' . HOST3 ); is( $svc->fingerprint(), $servicefp, 'Verifying correct servicefp parsing on tcp_service(953) for ' . HOST3 ); isa_ok( $svc = $host->tcp_service(500), 'Nmap::Parser::Host::Service', 'Verifying good reference returned on unscanned port service (500)' ); isa_ok( $np->del_host(HOST3), 'Nmap::Parser::Host', 'DEL ' . HOST3 ); is( $np->get_host(HOST3), undef, 'Testing deletion of ' . HOST3 ); #TESTING TRACE FOR HOST3 my $hops_count = $host->all_trace_hops(); is( $hops_count, 2, 'Host3 has trace information' ); is( $host->trace_error(), 'Error', 'Host3 trace is in error' ); #TESTING HOSTSCRIPT FOR HOST3 is_deeply( [ $host->hostscripts() ], [ 'nbstat' ], 'Host3 has one hostscript' ); { my $output = $host->hostscripts('nbstat')->{output}; is( substr($output,0,16), "\n NetBIOS name:", 'Host3 hostscript correct' ); } } sub host_4 { isa_ok( $host = $np->get_host(HOST4), 'Nmap::Parser::Host', 'GET ' . HOST4 ); my $os; #TESTING OS OBJECTS isa_ok( $os = $host->os_sig, 'Nmap::Parser::Host::OS', 'os_sig for ' . HOST4 ); is( $os->portused_open, 22, 'OS: portused open' ); is( $os->portused_closed, 1, 'OS: portused closed' ); is( $os->name_count, 2, 'OS: name count' ); is( $os->name, 'Linux Kernel 2.4.0 - 2.5.20', 'OS: name()' ); is( $os->name(0), $os->name, 'OS: name(0)' ); is( $os->name(1), 'Solaris 9', 'OS: name(1)' ); is_deeply( [ $os->all_names ], [ 'Linux Kernel 2.4.0 - 2.5.20', 'Solaris 9' ], 'OS: all_names' ); is( $os->name_accuracy(), 100, 'OS: name_accuracy()' ); is( $os->name_accuracy(0), $os->name_accuracy(), 'OS: name_accuracy(0)' ); is( $os->name_accuracy(1), 99, 'OS: name_accuracy(1)' ); my $count = 11; is( $os->class_count(), $count, 'OS: class_count MAX = ' . $count ); is( $os->osfamily(), 'AOS', 'OS: osfamily()' ); is( $os->osfamily(0), $os->osfamily(), 'OS: osfamily(0)' ); is( $os->osfamily($count), 'Linux', 'OS: osfamily(MAX)' ); is( $os->osfamily(15), $os->osfamily($count), 'OS: osfamily(15) = osfamily(MAX)' ); is( $os->vendor(), 'Redback', 'OS: vendor()' ); is( $os->vendor(0), $os->vendor(), 'OS: vendor(0)' ); is( $os->vendor($count), 'Linux', 'OS: vendor(MAX)' ); is( $os->vendor(15), $os->vendor($count), 'OS: vendor(15) = vendor(MAX)' ); is( $os->osgen(), undef, 'OS: osgen()' ); is( $os->osgen(0), $os->osgen(), 'OS: osgen(0)' ); is( $os->osgen($count), '2.4.x', 'OS: osgen(MAX)' ); is( $os->osgen(15), $os->osgen($count), 'OS: osgen(15) = osgen(MAX)' ); is( $os->type(), 'router', 'OS: type()' ); is( $os->type(0), $os->type(), 'OS: type(0)' ); is( $os->type($count), 'general purpose', 'OS: type(MAX)' ); is( $os->type(15), $os->type($count), 'OS: type(15) = type(MAX)' ); is( $os->class_accuracy(), 97, 'OS: class_accuracy()' ); is( $os->class_accuracy(0), $os->class_accuracy(), 'OS: class_accuracy(0)' ); is( $os->class_accuracy($count), 50, 'OS: class_accuracy(MAX)' ); is( $os->class_accuracy(15), $os->class_accuracy($count), 'OS: class_accuracy(15) = type(MAX)' ); my $fingerprint = " SCAN(V=4.20%D=6/11%OT=22%CT=%CU=%PV=N%DS=1%G=N%M=001321%TM=466DE2F1%P=i686-pc-windows-windows) T5(Resp=Y%DF=Y%W=0%ACK=S++%Flags=AR%Ops=) T6(Resp=Y%DF=Y%W=0%ACK=O%Flags=R%Ops=) T7(Resp=Y%DF=Y%W=0%ACK=S++%Flags=AR%Ops=) PU(Resp=Y%DF=N%TOS=C0%IPLEN=164%RIPTL=148%RID=E%RIPCK=E%UCK=E%ULEN=134%DAT=E) "; isa_ok( $os = $host->os_sig(), 'Nmap::Parser::Host::OS', 'os_sig()' ); is( $os->os_fingerprint(), $fingerprint, 'HOST4: os_fingerprint()' ); is( $host->tcptssequence_values, undef, 'HOST4: tcptssequence_values = undef' ); is( $host->distance(), 10, 'Host4: distance = 10' ); isa_ok( $np->del_host(HOST4), 'Nmap::Parser::Host', 'DEL ' . HOST4 ); is( $np->get_host(HOST4), undef, 'Testing deletion of ' . HOST4 ); #TESTING TRACE FOR HOST4 my @hops = $host->all_trace_hops(); ok( !$host->trace_error(), 'Host4 trace is not in error' ); is( $host->trace_port(), 80, 'Host4 trace port information' ); is( $host->trace_proto(), 'tcp', 'Host4 trace proto information' ); is( ( scalar @hops ), 3, 'Host4 trace size' ); is( $hops[0]->ttl(), 1, 'Host4 hop1 TTL' ); ok( !$hops[0]->rtt(), 'Host4 hop1 has no RTT' ); is( $hops[0]->ipaddr(), '192.168.1.1', 'Host4 hop1 IP address' ); ok( !$hops[0]->host(), 'Host4 hop1 has no hostname' ); is( $hops[2]->ttl(), 4, 'Host4 hop4 TTL' ); is( $hops[2]->rtt(), 26.48, 'Host4 hop4 RTT' ); is( $hops[2]->ipaddr(), '1.1.1.1', 'Host4 hop4 IP address' ); is( $hops[2]->host(), 'www.straton-it.fr', 'Host4 hop4 hostname' ); } Nmap-Parser-1.37/t/scanner.t000644 000765 000024 00000004075 13423134270 016626 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl use strict; use blib; use File::Spec; use Cwd; use Test::More tests => 8; use constant IP => '127.0.0.1'; use Nmap::Parser; my $np = new Nmap::Parser; can_ok( $np, 'cache_scan' ); can_ok( $np, 'parsescan' ); my $nmap_path = find_nmap(); SKIP: { skip '[Nmap-Parser] Could not find nmap executable in path', 6 if ( $nmap_path eq '' ); ok( $nmap_path, "Exe Path: $nmap_path" ); skip "[Nmap-Parser] No self scanning with MSWin32", 4 if ( $^O eq 'MSWin32' || $^O =~ /cygwin/ ); ok( $np->parsescan( $nmap_path, '-p 1-80', IP ), 'Running parsescan against ' . IP ); #if everything passed we can do another scan using the new cache_scan() function skip "[Nmap-Parser] Current user does not have read/write permissions in this directory.", 3 unless ( -w '.' && -r '.' ); my $cache_file = 'cache.' . ( rand(10000) % 10000 ) . '.xml'; $np->cache_scan($cache_file); ok( $np->parsescan( $nmap_path, '-p 1-80', IP ), 'Running parsescan /w cache enabled against ' . IP ); ok( -s $cache_file, 'Testing if cache file was created and written' ); ok( $np->parsefile($cache_file), 'Verifying cache file is nmap xml compatible.' ); is( unlink($cache_file), 1, 'Unlinking created cache file' ); } sub find_nmap { #I think I borrowed this from someone (or from a Cookbook) my $exe_to_find = 'nmap'; $exe_to_find =~ s/\.exe//; local ($_); local (*DIR); for my $dir ( File::Spec->path() ) { opendir( DIR, $dir ) || next; my @files = ( readdir(DIR) ); closedir(DIR); my $path; for my $file (@files) { $file =~ s/\.exe$//; next unless ( $file eq $exe_to_find ); $path = File::Spec->catfile( $dir, $file ); # Should symbolic link be considered? Helps me on cygwin but ... next unless -r $path && ( -x _ || -l _ ); return $path; last DIR; } } } Nmap-Parser-1.37/t/instance.t000644 000765 000024 00000003763 13423134270 017004 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl use strict; use blib; use File::Spec; use Cwd; use Test::More tests => 12; use Nmap::Parser; use constant HOST1 => '127.0.0.1'; use constant HOST2 => '127.0.0.2'; use constant HOST3 => '127.0.0.3'; use constant HOST4 => '127.0.0.4'; use constant BASE_FILE => 'instance.xml'; use constant CURR_FILE => 'nmap_results.xml'; use vars qw($base $curr $BASE $CURR); $BASE = File::Spec->catfile( cwd(), 't', BASE_FILE ); $BASE = File::Spec->catfile( cwd(), BASE_FILE ) unless ( -e $BASE ); $CURR = File::Spec->catfile( cwd(), 't', CURR_FILE ); $CURR = File::Spec->catfile( cwd(), CURR_FILE ) unless ( -e $CURR ); $curr = new Nmap::Parser; $base = new Nmap::Parser; isa_ok( $curr, 'Nmap::Parser' ); isa_ok( $base, 'Nmap::Parser' ); ok( $base->parsefile($BASE), 'Parsing from nmap data base image file' ); ok( $curr->parsefile($CURR), 'Parsing from nmap data current image file' ); my $host_curr = $curr->get_host(HOST3); my $host_base = $base->get_host(HOST3); isa_ok( $host_curr, 'Nmap::Parser::Host', 'host_curr' ); isa_ok( $host_base, 'Nmap::Parser::Host', 'host_base' ); cmp_ok( $host_curr->tcp_port_count, '!=', $host_base->tcp_port_count, 'Object instance difference: TCP COUNT' ); cmp_ok( $host_curr->udp_port_count, '!=', $host_base->udp_port_count, 'Object instance difference: UDP COUNT' ); my %port = (); my @diff_open = grep { $port{$_} < 2 } ( map { $port{$_}++; $_ } ( $host_curr->tcp_open_ports, $host_base->tcp_open_ports ) ); is( scalar @diff_open, 2, "Open port difference: " . ( join '', @diff_open ) ); my @diff_filtered = grep { $port{$_} < 2 } ( map { $port{$_}++; $_ } ( $host_curr->tcp_filtered_ports, $host_base->tcp_filtered_ports ) ); is( scalar @diff_filtered, 1, "Filtered port difference: " . ( join '', @diff_filtered ) ); is( $base->get_host(HOST2), undef, 'Base image should not have ' . HOST2 ); isnt( $curr->get_host(HOST2), undef, 'Current image should have ' . HOST2 ); Nmap-Parser-1.37/t/nmap_results.xml000644 000765 000024 00000040110 13423134270 020234 0ustar00apersaudstaff000000 000000 Nmap-Parser-1.37/t/callback.t000644 000765 000024 00000003043 13423134270 016723 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl use strict; use blib; use File::Spec; use Cwd; use Test::More tests => 15; use Nmap::Parser; use constant TEST_FILE => 'nmap_results.xml'; use constant HOST1 => '127.0.0.1'; use constant HOST2 => '127.0.0.2'; use constant HOST3 => '127.0.0.3'; use constant HOST4 => '127.0.0.4'; use constant TOTAL_ADDRS => 4; use vars qw($FH $TOTAL @UP_HOSTS @DOWN_HOSTS); $FH = File::Spec->catfile( cwd(), 't', TEST_FILE ); $FH = File::Spec->catfile( cwd(), TEST_FILE ) unless ( -e $FH ); my $np = new Nmap::Parser; isa_ok( $np, 'Nmap::Parser' ); ok( $np->callback( \&my_callback ), 'Registering callback function' ); is( $np->callback(), 0, 'Unregistering callback function' ); ok( $np->callback( \&my_callback ), 're-registering callback function' ); $TOTAL = 0; $np->parsefile($FH); is( $TOTAL, TOTAL_ADDRS, 'Making sure all hosts were parsed in callback' ); ok( eq_set( [@UP_HOSTS], [ HOST1, HOST3, HOST4 ] ), 'Testing for correct UP hosts' ); ok( eq_set( [@DOWN_HOSTS], [HOST2] ), 'Testing for correct DOWN hosts' ); for my $host ( HOST1, HOST2, HOST3, HOST4 ) { is( $np->get_host($host), undef, 'Making sure ' . $host . ' does not exists' ); } sub my_callback { my $host = shift; my $addr = $host->addr; if ( $addr =~ /^127\.0\.0/ ) { $TOTAL++; } isa_ok( $host, 'Nmap::Parser::Host', $host->addr ); if ( $host->status eq 'up' ) { push @UP_HOSTS, $addr; } elsif ( $host->status eq 'down' ) { push @DOWN_HOSTS, $addr; } } Nmap-Parser-1.37/tools/nmap2db.pl000644 000765 000024 00000027032 13423134270 017563 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl # THIS IS A MODIFIED VERSION OF NMAP2SQLITE SCRIPT ORIGINALLY DEVELOPED BY ANTHONY G PERSAUD # BUT MODIFIED TO WORK WITH MYSQL DATABASES BY ROBIN BOWES. # nmap2db.pl # Description: # It takes in a nmap xml file and stores it into a SQLite database using DBI for # searching, storing and better reporting. This is just an example of how an # IP network database can be created using Nmap-Parser and automation. # # # 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 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # 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. # use strict; use DBI; use Nmap::Parser 1.00; use vars qw(%S %G); use File::Spec::Functions; use Pod::Usage; use Carp; #Will use in the future use Getopt::Long; Getopt::Long::Configure('bundling'); GetOptions( 'help|h|?' => \$G{helpme}, 'nmap=s' => \$G{nmap}, 'xml' => \$G{file}, 'scan' => \$G{scan}, 'dbhost=s' => \$G{DBHOST}, 'db=s' => \$G{DBNAME}, 'dbtype=s' => \$G{DBTYPE}, 'table=s' => \$G{TABLE}, 'dbuser=s' => \$G{DBUSER}, 'dbpass=s' => \$G{DBPASS}, ) or ( pod2usage( -exitstatus => 0, -verbose => 2 ) ); unless ( $G{file} || $G{scan} ) { pod2usage( -exitstatus => 0, -verbose => 2 ); } print "\n$0 - ( http://nmapparser.wordpress.com )\n", ( '-' x 50 ), "\n\n"; if ( $G{scan} && $G{nmap} eq '' ) { $G{nmap} = find_exe(); } $G{DBNAME} ||= 'ip.db'; $G{TABLE} ||= 'hosts'; $G{DBTYPE} ||= 'SQLite'; if ( $G{DBTYPE} eq 'mysql' ) { $G{DBHOST} ||= 'localhost'; } print "Using DATABASE : $G{DBNAME}\n"; print "Database type : $G{DBTYPE}\n"; if ( $G{DBTYPE} eq 'mysql' ) { print "Using host : $G{DBHOST}\n"; print "Using user : $G{DBUSER}\n" if $G{DBUSER}; } print "Using TABLE : $G{TABLE}\n"; print "Using NMAP_EXE : $G{nmap}\n" if ( $G{scan} ); #Schema for table, simple for now $S{CREATE_TABLE} = qq{ CREATE TABLE } . $G{TABLE} . qq{ ( ip VARCHAR(15) PRIMARY KEY NOT NULL, mac VARCHAR(17), status VARCHAR(7) DEFAULT 'down', hostname TEXT, open_ports TEXT, filtered_ports TEXT, osname TEXT, osfamily TEXT, osgen TEXT, last_scanned TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE (ip)) }; $S{INSERT_HOST} = qq{REPLACE INTO } . $G{TABLE} . qq{ (ip, mac, status, hostname, open_ports, filtered_ports, osname, osfamily, osgen) VALUES (?,?,?,?,?,?,?,?,?)}; my $np = new Nmap::Parser; $np->callback( \&insert_host ); #not implemented in this script, will finish later... ;-) #$np->parsescan($PATH_TO_NMAP, $NMAP_ARGS, @IPS); # Set up the DB connection my $dsn = "DBI:$G{DBTYPE}"; $dsn .= ":host=$G{DBHOST}" if $G{DBTYPE} eq 'mysql'; $dsn .= ":$G{DBNAME}"; my $dbh = eval { DBI->connect( $dsn, $G{DBUSER}, $G{DBPASS} ) }; croak $@ if ($@); # Check if table exists... if ( !table_exists( $dbh, $G{DBNAME}, $G{TABLE} ) ) { print "\nGenerating table: $G{TABLE} ...\n"; eval { $dbh->do( $S{CREATE_TABLE} ) }; croak $@ if ($@); } #do stuff my $sth_ins = eval { $dbh->prepare_cached( $S{INSERT_HOST} ) }; croak $@ if ($@); #for every host scanned, insert or updated it in the table if ( $G{file} ) { for my $file (@ARGV) { print "\nProcessing file $file...\n"; $np->parsefile($file); } } elsif ( $G{scan} && $G{nmap} ) { print "\nProcessing scan: " . $G{nmap} . ' -sT -O -F ' . join( ' ', @ARGV ); $np->parsescan( $G{nmap}, '-sT -O -F', @ARGV ); } #Booyah! $sth_ins->finish; $dbh->disconnect(); #This function will insert the host, or update it if it already exists #Of course, we can always check the last_scanned entry in the database to #make sure the latest information is there, but this is just beta version. sub insert_host { my $host = shift; my $os = $host->os_sig(); #ip, mac, status, hostname, open_ports, filtered_ports, os_family, os_gen my @input_values = ( $host->addr, $host->mac_addr || undef, $host->status || undef, $host->hostname || undef, join( ',', $host->tcp_open_ports ) || undef, join( ',', $host->tcp_filtered_ports ) || undef, $os->name || undef, $os->osfamily || undef, $os->osgen || undef ); my $rv = $sth_ins->execute(@input_values) ? "ok" : "OOPS! - " . DBI->errstr; printf( "\t..> %-15s : (%4s) : %-s\n", $host->addr, $host->status, $rv ); } sub find_exe { my $exe_to_find = 'nmap'; $exe_to_find =~ s/\.exe//; local ($_); local (*DIR); for my $dir ( File::Spec->path() ) { opendir( DIR, $dir ) || next; my @files = ( readdir(DIR) ); closedir(DIR); my $path; for my $file (@files) { $file =~ s/\.exe$//; next unless ( $file eq $exe_to_find ); $path = File::Spec->catfile( $dir, $file ); next unless -r $path && ( -x _ || -l _ ); return $path; last DIR; } } warn "[Nmap2SQLite] No nmap in your PATH: use '--nmap nmap_path' option\n"; exit; } sub table_exists { my ( $dbh, $dbname, $tblname ) = @_; my @names = eval { $dbh->tables( '', $dbname, $tblname, "TABLE" ) }; croak $@ if ($@); my %names_h; @names_h{@names} = (); my $sql_quote_char = $dbh->get_info(29); return ( exists( $names_h{ $sql_quote_char . $tblname . $sql_quote_char } ) ); } __END__ =pod =head1 NAME nmap2db - store nmap scan data into entries in SQLite/MySQL database =head1 SYNOPSIS nmap2db.pl [options] --xml [ ...] nmap2db.pl [options] --scan [ ...] Examples connecting to a MySQL database (Robin Bowes): nmap2db.pl --dbtype mysql --dbname netdb --dbuser netuser --dbpass secret --xml 192.168.25.0.xml =head1 DESCRIPTION This script uses the nmap security scanner with the Nmap::Parser module in order to take an xml output scan file from nmap (-oX option), and place the information into a SQLite database (ip.db), into table (hosts). This is a modified version of the nmap2sqlite.pl script written originally by Anthony Persaud but modified by Robin Bowes to support MySQL databases. Here is the schema for the table stored in the SQLite database ip TEXT PRIMARY KEY NOT NULL, mac TEXT, status TEXT, hostname TEXT, open_ports TEXT, filtered_ports TEXT, osname TEXT, osfamily TEXT, osgen TEXT, last_scanned TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE (ip)) =head1 OPTIONS These options are passed as command line parameters. Please use EITHER --scan or --xml. NOT both. =over 4 =item B<--dbhost DBHOST> Connect to the DB on server DBHOST. Default: localhost. =item B<--db DBNAME> Sets the database name to DBNAME. Default: ip.db =item B<--dbtype DBTYPE> Sets the type of databases to use. Currently supported values are: mysql, SQLite Default: SQLite =item B<--table TABLE_NAME> Sets the table name to use in the database as TABLE_NAME. Default: hosts =item B<--dbuser DBUSER> Connect to the database as user DBUSER. Default: current user =item B<--dbpass DBPASS> Connect to the database with password DBPASS Default: no password =item B<-h,--help,-?> Shows this help information. =item B<--nmap> The path to the nmap executable. This should be used if nmap is not on your path. =item B<--scan> This will use parsescan() for the scan and take the arguments as IP addreses. =item B<--xml> This will use parsefile() for the input and take the arguments as nmap scan xml files. =back 4 =head1 TARGET SPECIFICATION This documentation was taken from the nmap man page. The IP address inputs to this scripts should be in the nmap target specification format. The simplest case is listing single hostnames or IP addresses onthe command line. If you want to scan a subnet of IP addresses, you can append '/mask' to the hostname or IP address. mask must be between 0 (scan the whole internet) and 32 (scan the single host specified). Use /24 to scan a class 'C' address and /16 for a class 'B'. You can use a more powerful notation which lets you specify an IP address using lists/ranges for each element. Thus you can scan the whole class 'B' network 128.210.*.* by specifying '128.210.*.*' or '128.210.0-255.0-255' or even use the mask notation: '128.210.0.0/16'. These are all equivalent. If you use asterisks ('*'), remember that most shells require you to escape them with back slashes or protect them with quotes. Another interesting thing to do is slice the Internet the other way. Examples: nmap2db.pl --scan 127.0.0.1 nmap2db.pl --scan target.example.com nmap2db.pl --scan target.example.com/24 nmap2db.pl --scan 10.210.*.1-127 nmap2db.pl --scan *.*.2.3-5 nmap2db.pl --scan 10.[10-15].10.[2-254] Examples connecting to a MySQL database: nmap2db.pl --dbtype mysql --dbname netdb --dbuser netuser --dbpass secret --xml 192.168.25.0.xml =head1 OUTPUT EXAMPLE See the SQLite database that is created. Default ip.db =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. L =head2 Bug Reports Please submit any bugs 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. =head2 Feature Requests Please submit any requests to: L =head1 SEE ALSO L 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 AUTHOR Anthony Persaud L Additional features and improvements by: Robin Bowes L Daniel Miller L =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. L =cut Nmap-Parser-1.37/tools/pdev.pl000755 000765 000024 00000030112 13423134270 017172 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl =pod Copyright (c) <2013> L 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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 use Data::Dumper; use File::Basename; $Data::Dumper::Purity = 1; use vars qw(%_GLOBALS $ans @_HISTORY $VERSION); $VERSION = 0.5; _glob_init(); print "\nPerl-Dev [" . ($0) . " - $VERSION]\n\n"; if ( -e $_GLOBALS{cfg} ) { do "$_GLOBALS{cfg}"; print "Loaded $_GLOBALS{cfg} ok.\n"; } print "Use 'our' instead of 'my' for persistent variables.\n"; print "Type ':?' for help.\n\n"; if ( scalar @ARGV ) { $_GLOBALS{input} = join ' ', @ARGV; print "\t[AutoExe] $_GLOBALS{input}\n"; exec_src(); } #Main look for the shell while (1) { $_GLOBALS{input} = ''; $_GLOBALS{input} = prompt(); if ( $_GLOBALS{debug} ) { print "Input: $_GLOBALS{input}\n"; } if ( $_GLOBALS{input} =~ s/^:// ) { parse_scmd( $_GLOBALS{input} ); } elsif ( $_GLOBALS{input} =~ s/( )\\ *?$/$1/ ) { push @{ $_GLOBALS{code} }, $_GLOBALS{input} . "\n"; print "[Appending] " . join( '', @{ $_GLOBALS{code} } ) . "\n" if ( $_GLOBALS{debug} ); $_GLOBALS{p_type} = 1; } else { exec_src(); $_GLOBALS{p_type} = 0; } } sub _glob_init { %_GLOBALS = ( input => '', scmd => '', p_type => 0, code => '', debug => 0, feed => '', regex => qr//, ans => '', dir => dirname($0), script => basename($0), cfg => dirname($0) . '/.pdev' ); } sub exec_src { push @{ $_GLOBALS{code} }, $_GLOBALS{input}; my $_exe = join '', @{ $_GLOBALS{code} }; next if ( $_exe eq '' ); print "\t[EXE] $_exe\n" if ( $_GLOBALS{debug} ); $_exe .= ';' if ( $_exe !~ /;$/ ); push @_HISTORY, $_exe; my @_return = ( eval $_exe ); $_GLOBALS{ans} = $ans = join ' ', @_return; print "\n\t[ANS] ", $ans, "\n" if ($ans); print_err($@); @{ $_GLOBALS{code} } = (); $_GLOBALS{scmd} = ''; } sub print_err { my @err = @_; print( "\n\t[ERR] ", @err, "\n" ) if ( join( '', @err ) =~ /\w+/ ); #if errors } sub prompt { my $in = ''; if ( $_GLOBALS{p_type} > 0 ) { print " ..> "; } #else { print '[{', $_GLOBALS{hist}++, '}> ';} else { print 'pdev> '; } chomp( $in = ); return $in; } sub parse_scmd { my $scmd = join '', @_; my @tokens = split ' ', $scmd; my $type = lc( shift @tokens ); $_GLOBALS{scmd} = $type; print "Type: $type\nTokens: ", join( ' ', @tokens ), "\n" if ( $_GLOBALS{debug} ); if ( $type eq 'dump' ) { _dump( $type, @tokens ); } elsif ( $type eq 'pop' ) { _pop( $type, @tokens ); } elsif ( $type =~ /\d+/ ) { _num( $type, @tokens ); } elsif ( $type eq 'hist' ) { _hist( $type, @tokens ); } elsif ( $type eq 'run' ) { _run( $type, @tokens ); } elsif ( $type eq 'feed' ) { _feed( $type, @tokens ); } elsif ( $type eq 'rgx' ) { _rgx( $type, @tokens ); } elsif ( $type eq 'rgxtest' ) { _rgxtest( $type, @tokens ); } elsif ( $type eq 'debug' ) { _debug( $type, @tokens ); } elsif ( $type eq 'rst' ) { _rst( $type, @tokens ); } elsif ( $type eq 'output' ) { _output( $type, @tokens ); } elsif ( $type eq 'sys' ) { _sys( $type, @tokens ); } elsif ( $type eq 'env' ) { _env( $type, @tokens ); } elsif ( $type eq 'help' || $type eq '?' ) { _help( $type, @tokens ); } else { print "Unknown special command: $type\n"; } } #------------------------------------------------------------------------------# # Special Command Functions # #------------------------------------------------------------------------------# sub _output { my $__type = shift; my $file = shift || 'pdev.output'; if ( open FILE, ">$file" ) { for my $line (@_HISTORY) { print "$line\n" if ( $_GLOBALS{debug} ); print FILE $line . "\n"; } close FILE; print "[Output Saved] $file\n"; } else { print "[Err] Could not open output file $file: $!\n"; } } sub _env { my $__type = shift; my @__tokens = @_; print "[GLOBALS]\n"; for my $key ( keys %_GLOBALS ) { printf( "\t%10s: %s\n", $key, $_GLOBALS{$key} ); } } sub _sys { my $__type = shift; my @__tokens = @_; my $__sys_cmd = join ' ', @__tokens; $_GLOBALS{input} = 'qx{' . $__sys_cmd . '}'; if ( $__sys_cmd eq '' ) { print "[Err] - no system command given $__sys_cmd\n"; } else { exec_src(); } } sub _rst { %_GLOBALS = (); $_GLOBALS{ans} = $ans = ''; @_HISTORY = (); _glob_init(); print "[Env Reset] - ok\n"; } sub _debug { my $type = shift; my $switch = lc(shift); if ( $switch eq 'off' ) { $_GLOBALS{debug} = 0; print "debugging is off\n"; } elsif ( $switch eq 'on' ) { $_GLOBALS{debug} = 1; print "debugging is on\n"; } else { print "Choose either 'on' or 'off'. ex. :debug off\n"; } } sub _dump { my $__type = shift; my @__tokens = @_; for my $__var (@__tokens) { print "Dump: $__var\n" if ( $_GLOBALS{debug} ); print Data::Dumper->Dump( [ eval("$__var") ], ["$__var"] ) . "\n"; } } sub _pop { my $__type = shift; my @__tokens = @_; if ( scalar @__tokens == 0 ) { @__tokens = ($#_HISTORY); } for my $t (@__tokens) { print "[Removed History] $_HISTORY[$t]\n"; $_HISTORY[$t] = undef; } @_HISTORY = grep { $_ ne undef } @_HISTORY; } sub _num { my $__type = shift; my @__tokens = @_; $__type = ( $__type + 0 ) || 0; $_GLOBALS{input} = $_HISTORY[$__type]; if ( $_GLOBALS{input} eq '' ) { print "\n\t[Err] - no command history at $__type\n"; } else { exec_src(); } } sub _hist { my $__type = shift; my @__tokens = @_; print "[History]\n"; for my $line ( 0 .. $#_HISTORY ) { print "\t[$line] " . $_HISTORY[$line] . "\n"; } } sub _run { my $__type = shift; my @__tokens = @_; print "[Running Feed]\n"; $_GLOBALS{input} = $_GLOBALS{feed}; exec_src(); } sub _feed { my $__type = shift; my @__tokens = @_; $_GLOBALS{feed} = ''; print "[Feeding] until :end or :run\n"; my $_curr = $_GLOBALS{input} = ''; until ( $_curr =~ /^:end/i || $_curr =~ /^:run/i ) { $_GLOBALS{input} .= $_curr . "\n"; print "\t[...> "; chomp( $_curr = ); } $_GLOBALS{feed} = $_GLOBALS{input}; print "[Feed Stored]\n"; if ( $_curr =~ /^:run/i ) { _run(); #runs feed automatically } } sub _rgx { my $__type = shift; my @__tokens = @_; my $rgx = join '', @__tokens; $_GLOBALS{regex} = eval $rgx; print "[regex] $_GLOBALS{regex}\n"; } sub _rgxtest { my $__type = shift; my @__tokens = @_; my $__test_str = join '', @__tokens; print "Regex Stored: $_GLOBALS{regex}\n" if ( $_GLOBALS{debug} ); print "Test String : $__test_str\n" if ( $_GLOBALS{debug} ); if ( $__test_str =~ $_GLOBALS{regex} ) { print "\n[REGEX MATCH] OK.\n\n"; print "Prematch : $`\n"; print "Match : $&\n"; print "Postmatch : $'\n"; print "Lastmatch : $+\n"; print "[Captured Matches]\n"; for my $m ( 1 .. 9 ) { print "\t[\$$m] : " . ${$m} . "\n" if ( ${$m} ne '' ); } } else { print "\n[REGEX MATCH] NONE.\n\n"; } } sub _help { my $__type = shift; my @__tokens = @_; print q{ [Special Commands] All special commands must be be prefixed with ':', for example ':help'. dump - Data::Dump of a variable :dump $var debug - turns debugging 'on' or 'off' (use 'on' or 'off' as parameters) :debug 'on' #turns debuggin on rgx - store regular expression :rgx qr/\w+_(\w+)/i env - prints the current environment variables rgxtest - tests the regex in rgx against text :rgxtest my_test feed - starts storing line-by-line perl code and stores it. end - use to end the feeding process of feed. run - executes the code stored by feed. pop - removes the last code line that has been stored. If number is given, it removes that entry from the history. :pop 3 4 #removes entry 3 and 4 from history. [num] - runs the command in history denoted by num. :1 #runs command in history 1 hist - lists command history. rst - resets all internal variables (cleans the shell) sys - run a system command using ``; log - outputs history information to file. :out script.txt help - this help information ? - same thing as 'help' [Special Variables] [Do not use any variables with underscore '__' (double underscore)] $ans - contains the results of the return value of last operation pdev> 4+5 [ANS] 9 pdev> $ans + 1 [ANS] 10 @_HISTORY - contains the listing of history commands %_GLOBALS - contains the global environment variables [Special Files] .pdev - if this file is located in the current directory, pdev will 'eval' the file. This is usefule to load regularly used libraries, variables or configurations. [Extras] 1. Use 'our' instead of 'my' for persistent variables. 2. You can also auto execute command line strings before entering the shell: pdev.pl print(5+4); Perl-Dev [U:\pdev.pl] [AutoExe] print(5+4) 9 pdev> Anthony Persaud L Copyright (c) <2003-2013> 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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. }; } Nmap-Parser-1.37/tools/nmap2sqlite.pl000644 000765 000024 00000024350 13423134270 020477 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl #Anthony Persaud #nmap2sqlite.pl #Description: # It takes in a nmap xml file and stores it into a SQLite database using DBI for # searching, storing and better reporting. This is just an example of how an # IP network database can be created using Nmap-Parser and automation. # # #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 #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the Software is #furnished to do so, subject to the following conditions: # #The above copyright notice and this permission notice shall be included in #all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #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. # use strict; use DBI; use Nmap::Parser 1.00; use vars qw(%S %G); use File::Spec::Functions; use Pod::Usage; #Will use in the future use Getopt::Long; Getopt::Long::Configure('bundling'); GetOptions( 'help|h|?' => \$G{helpme}, 'nmap=s' => \$G{nmap}, 'xml' => \$G{file}, 'scan' => \$G{scan}, 'db=s' => \$G{DBNAME}, 'table=s' => \$G{TABLE} ) or ( pod2usage( -exitstatus => 0, -verbose => 2 ) ); unless ( $G{file} || $G{scan} ) { pod2usage( -exitstatus => 0, -verbose => 2 ); } print "\nnmap2sqlite.pl - ( https://github.com/modernistik/Nmap-Parser )\n", ( '-' x 50 ), "\n\n"; if ( $G{scan} && $G{nmap} eq '' ) { $G{nmap} = find_exe(); } $G{DBNAME} ||= 'ip.db'; $G{TABLE} ||= 'hosts'; print "Using DATABASE: $G{DBNAME}\n"; print "Using TABLE : $G{TABLE}\n"; print "Using NMAP_EXE: $G{nmap}\n" if ( $G{scan} ); #Schema for table, simple for now $S{CREATE_TABLE} = qq{ CREATE TABLE } . $G{TABLE} . qq{ ( ip VARCHAR(15) PRIMARY KEY NOT NULL, mac VARCHAR(17), status VARCHAR(7) DEFAULT 'down', hostname TEXT, open_ports TEXT DEFAULT 'none', filtered_ports TEXT DEFAULT 'none', osname TEXT, osfamily TEXT, osgen TEXT, last_scanned TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE (ip)) }; $S{INSERT_HOST} = qq{INSERT OR REPLACE INTO } . $G{TABLE} . qq{ (ip, mac, status, hostname, open_ports, filtered_ports, osname, osfamily, osgen) VALUES (?,?,?,?,?,?,?,?,?)}; my $np = new Nmap::Parser; $np->callback( \&insert_host ); #not implemented in this script, will finish later... ;-) #$np->parsescan($PATH_TO_NMAP, $NMAP_ARGS, @IPS); #SQLite is used for simplicity. my $dsn = 'DBI:SQLite:' . $G{DBNAME}; my $dbh = DBI->connect($dsn) or die "Could not connect to $G{DBNAME}: " . DBI->errstr; #Check if tables exists... my @tables_exists = $dbh->tables( undef, undef, $G{TABLE}, undef ); #if it doesn't - create it if ( scalar @tables_exists == 0 ) { print "\nGenerating table: $G{TABLE} ...\n"; $dbh->do( $S{CREATE_TABLE} ); } #do stuff my $sth_ins = $dbh->prepare_cached( $S{INSERT_HOST} ); #for every host scanned, insert or updated it in the table if ( $G{file} ) { for my $file (@ARGV) { print "\nProcessing file $file...\n"; $np->parsefile($file); } } elsif ( $G{scan} && $G{nmap} ) { print "\nProcessing scan: " . $G{nmap} . ' -sT -O -F ' . join( ' ', @ARGV ); $np->parsescan( $G{nmap}, '-sT -O -F', @ARGV ); } #Booyah! $sth_ins->finish; $dbh->disconnect(); #This function will insert the host, or update it if it already exists #Of course, we can always check the last_scanned entry in the database to #make sure the latest information is there, but this is just beta version. sub insert_host { my $host = shift; my $os = $host->os_sig(); #ip, mac, status, hostname, open_ports, filtered_ports, os_family, os_gen my @input_values = ( $host->addr, $host->mac_addr || undef, $host->status || undef, $host->hostname || undef, join( ',', $host->tcp_open_ports ) || undef, join( ',', $host->tcp_filtered_ports ) || undef, $os->name || undef, $os->osfamily || undef, $os->osgen || undef ); my $rv = $sth_ins->execute(@input_values) ? "ok" : "OOPS! - " . DBI->errstr; printf( "\t..> %-15s : (%4s) : %-s\n", $host->addr, $host->status, $rv ); } sub find_exe { my $exe_to_find = 'nmap'; $exe_to_find =~ s/\.exe//; local ($_); local (*DIR); for my $dir ( File::Spec->path() ) { opendir( DIR, $dir ) || next; my @files = ( readdir(DIR) ); closedir(DIR); my $path; for my $file (@files) { $file =~ s/\.exe$//; next unless ( $file eq $exe_to_find ); $path = File::Spec->catfile( $dir, $file ); next unless -r $path && ( -x _ || -l _ ); return $path; last DIR; } } warn "[Nmap2SQLite] No nmap in your PATH: use '--nmap nmap_path' option\n"; exit; } __END__ =pod =head1 NAME nmap2sqlite - turn nmap scan data into entries in SQLite DB =head1 SYNOPSIS nmap2sqlite.pl [--db ip.db][--table hosts] --xml [ ...] nmap2sqlite.pl [--db ip.db][--table hosts] --scan [ ...] =head1 DESCRIPTION This script uses the nmap security scanner with the Nmap::Parser module in order to take an xml output scan file from nmap (-oX option), and place the information into a SQLite database (ip.db), into table (hosts). Here is the schema for the table stored in the SQLite database ip TEXT PRIMARY KEY NOT NULL, mac TEXT, status TEXT DEFAULT 'down', hostname TEXT, open_ports TEXT DEFAULT 'none', filtered_ports TEXT DEFAULT 'none', osname TEXT, osfamily TEXT, osgen TEXT, last_scanned TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE (ip)) =head1 OPTIONS These options are passed as command line parameters. Please use EITHER --scan or --xml. NOT both. =over 4 =item B<--db DBNAME> Sets the database name to DBNAME. =item B<-h,--help,-?> Shows this help information. =item B<--nmap> The path to the nmap executable. This should be used if nmap is not on your path. =item B<--scan> This will use parsescan() for the scan and take the arguments as IP addreses. =item B<--table TABLE_NAME> Sets the table name to use in the database as TABLE_NAME. =item B<--xml> This will use parsefile() for the input and take the arguments as nmap scan xml files. =back 4 =head1 TARGET SPECIFICATION This documentation was taken from the nmap man page. The IP address inputs to this scripts should be in the nmap target specification format. The simplest case is listing single hostnames or IP addresses onthe command line. If you want to scan a subnet of IP addresses, you can append '/mask' to the hostname or IP address. mask must be between 0 (scan the whole internet) and 32 (scan the single host specified). Use /24 to scan a class 'C' address and /16 for a class 'B'. You can use a more powerful notation which lets you specify an IP address using lists/ranges for each element. Thus you can scan the whole class 'B' network 128.210.*.* by specifying '128.210.*.*' or '128.210.0-255.0-255' or even use the mask notation: '128.210.0.0/16'. These are all equivalent. If you use asterisks ('*'), remember that most shells require you to escape them with back slashes or protect them with quotes. Another interesting thing to do is slice the Internet the other way. Examples: nmap2sqlite.pl --scan 127.0.0.1 nmap2sqlite.pl --scan target.example.com nmap2sqlite.pl --scan target.example.com/24 nmap2sqlite.pl --scan 10.210.*.1-127 nmap2sqlite.pl --scan *.*.2.3-5 nmap2sqlite.pl --scan 10.[10-15].10.[2-254] =head1 OUTPUT EXAMPLE See the SQLite database that is created. Default ip.db =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. L =head2 Bug Reports Please submit any bugs 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. =head2 Feature Requests Please submit any requests to: L =head1 SEE ALSO L 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 AUTHOR Anthony Persaud L =head1 COPYRIGHT Copyright (c) <2003-2013> 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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 Nmap-Parser-1.37/tools/scan.pl000644 000765 000024 00000024353 13423134270 017167 0ustar00apersaudstaff000000 000000 #!/usr/bin/perl #Anthony Persaud use strict; use Nmap::Parser; use Getopt::Long; use File::Spec; use Pod::Usage; use vars qw(%G); $G{nmap_exe} = find_exe(); my $np = new Nmap::Parser; print "\nscan.pl - ( https://github.com/modernistik/Nmap-Parser )\n", ( '-' x 80 ), "\n\n"; GetOptions( 'help|h|?' => \$G{helpme}, 'nmap=s' => \$G{nmap}, 'xml=s' => \$G{file} ) or ( pod2usage( -exitstatus => 0, -verbose => 2 ) ); $np->callback( \&host_handler ); if ( -e $G{file} ) { print "Parsing file: " . $G{file} . "\n\n"; $np->parsefile( $G{file} ); } elsif ( $G{nmap} && scalar @ARGV ) { print "Using nmap exe: " . $G{nmap} . "\n\n"; $np->parsescan( $G{nmap}, '-sVU -O -F --randomize_hosts', @ARGV ); } else { pod2usage( -exitstatus => 0, -verbose => 2 ) } sub host_handler { my $host = shift; print ' > ' . $host->ipv4_addr . "\n"; print "\t[+] Status: (" . uc( $host->status ) . ")\n"; if ( $host->status eq 'up' ) { tab_print( "Hostname(s)", $host->all_hostnames() ); tab_print( "Uptime", ( $host->uptime_seconds() ) . " seconds" ) if ( $host->uptime_seconds() ); tab_print( "Last Rebooted", $host->uptime_lastboot() ) if ( $host->uptime_lastboot ); os_sig_print($host); port_service_print($host); } print "\n\n"; } sub os_sig_print { my $host = shift; my $os = $host->os_sig(); print "\t[+] OS Names :\n" if ( $os->name_count > 0 ); for my $name ( $os->all_names() ) { print "\t\t$name\n"; } if ( $os->class_count > 0 ) { print "\t[+] OS Classes :\n"; printf( "\t\t%-16s %10s (%8s) [%3s] {%2s}\n", 'TYPE', 'VENDOR', 'OSFAMILY', 'VERSION', 'ACCURACY' ); print "\t\t" . ( '-' x 60 ) . "\n"; for ( my $i = 0 ; $i < $os->class_count() ; $i++ ) { printf( "\t\t%-16s %10s (%8s) [%7s] %4s%%\n", $os->type($i), $os->vendor($i), $os->osfamily($i), $os->osgen($i), $os->class_accuracy($i) ); } } } sub port_service_print { my $host = shift; print "\t[+] TCP Ports :\n" if ( $host->tcp_port_count ); printf( "\t\t%-6s %-10s (%-14s) [%-8s] %s\n", 'PORT', 'SERVICE', 'PRODUCT', 'VERSION', 'EXTRA' ); print "\t\t" . ( '-' x 60 ) . "\n"; for my $port ( $host->tcp_open_ports ) { my $svc = $host->tcp_service($port); printf( "\t\t%-6s %-10s (%-14s) [%-8s] %s\n", $port, $svc->name, $svc->product, $svc->version, $svc->extrainfo ); } print "\t[+] UDP Ports :\n" if ( $host->udp_port_count ); for my $port ( $host->udp_open_ports ) { my $svc = $host->udp_service($port); printf( "\t\t%-6s %-10s (%-14s) [%-8s] %s\n", 'PORT', 'SERVICE', 'PRODUCT', 'VERSION', 'EXTRA' ); print "\t\t" . ( '-' x 60 ) . "\n"; printf( "\t\t%-6s %-10s (%-14s) [%-8s] %s\n", $port, $svc->name, $svc->product, $svc->version, $svc->extrainfo ); } } sub tab_print { my $title = shift; print "\t[+] $title :\n"; for my $a (@_) { print "\t\t$a\n"; } } sub find_exe { my $exe_to_find = 'nmap'; $exe_to_find =~ s/\.exe//; local ($_); local (*DIR); for my $dir ( File::Spec->path() ) { opendir( DIR, $dir ) || next; my @files = ( readdir(DIR) ); closedir(DIR); my $path; for my $file (@files) { $file =~ s/\.exe$//; next unless ( $file eq $exe_to_find ); $path = File::Spec->catfile( $dir, $file ); next unless -r $path && ( -x _ || -l _ ); return $path; last DIR; } } } __END__ =pod =head1 NAME scan - a scanning script to gather port and OS information from hosts =head1 SYNOPSIS scan.pl [--nmap ] [ ...] scan.pl --xml =head1 DESCRIPTION This script uses the nmap security scanner with the Nmap::Parser module in order to run quick scans against specific hosts, and gather all the information that is required to know about that specific host which nmap can figure out. This script can be used for quick audits against machines on the network and an educational use for learning how to write scripts using the Nmap::Parser module. B =head1 OPTIONS These options are passed as command line parameters. =over 4 =item B<--nmap> The path to the nmap executable. This should be used if nmap is not on your path. =item B<-h,--help,-?> Shows this help information. =item B<--xml> Processes the given nmap xml scan file. This file is usually generated by using the '-oX filename.xml' command line parameter with nmap. =back 4 =head1 TARGET SPECIFICATION This documentation was taken from the nmap man page. The IP address inputs to this scripts should be in the nmap target specification format. The simplest case is listing single hostnames or IP addresses onthe command line. If you want to scan a subnet of IP addresses, you can append '/mask' to the hostname or IP address. mask must be between 0 (scan the whole internet) and 32 (scan the single host specified). Use /24 to scan a class 'C' address and /16 for a class 'B'. You can use a more powerful notation which lets you specify an IP address using lists/ranges for each element. Thus you can scan the whole class 'B' network 128.210.*.* by specifying '128.210.*.*' or '128.210.0-255.0-255' or even use the mask notation: '128.210.0.0/16'. These are all equivalent. If you use asterisks ('*'), remember that most shells require you to escape them with back slashes or protect them with quotes. Another interesting thing to do is slice the Internet the other way. Examples: scan.pl 127.0.0.1 scan.pl target.example.com scan.pl target.example.com/24 scan.pl 10.210.*.1-127 scan.pl *.*.2.3-5 scan.pl 10.[10-15].10.[2-254] =head1 OUTPUT EXAMPLE These are ONLY examples of how the output would look like. Not the specs to my machine Scan Host -------------------------------------------------- [>] 127.0.0.1 [+] Status: (UP) [+] Hostname(s) : host1 host1_2 [+] Uptime : 1973 seconds [+] Last Rebooted : Tue Jul 1 14:15:27 2003 [+] OS Names : Linux Kernel 2.4.0 - 2.5.20 Solaris 9 [+] OS Classes : TYPE VENDOR (OSFAMILY) [VERSION] {ACCURACY} ------------------------------------------------------------ router Redback ( AOS) [ ] 97% broadband router Thomson (embedded) [ ] 97% switch Fore (embedded) [ ] 92% printer Xerox (embedded) [ ] 91% broadband router Redback (embedded) [ ] 90% firewall SonicWall (embedded) [ ] 90% switch Enterasys (embedded) [ ] 90% WAP Cisco (embedded) [ ] 90% broadband router Alcatel (embedded) [ ] 90% general purpose Sun ( SunOS) [ ] 90% general purpose Linux ( Linux) [ 2.4.x] 50% [+] TCP Ports : PORT SERVICE (PRODUCT ) [VERSION ] EXTRA ------------------------------------------------------------ 21 ftp (ProFTPD ) [1.2.5rc1] 22 ssh (OpenSSH ) [3.4p1 ] protocol 1.99 25 smtp (Exim smtpd ) [3.35 ] 26 ssh (OpenSSH ) [3.6.1p1 ] protocol 1.99 112 rpcbind ( ) [2 ] 113 ident (OpenBSD identd) [ ] 953 rndc ( ) [ ] [+] UDP Ports : PORT SERVICE (PRODUCT ) [VERSION ] EXTRA ------------------------------------------------------------ 80 http (Apache httpd ) [1.3.26 ] (Unix) Debian GNU/Linux =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. L =head2 Bug Reports Please submit any bugs 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. =head2 Feature Requests Please submit any requests to: L =head1 SEE ALSO L 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 AUTHOR Anthony Persaud L =head1 COPYRIGHT Copyright (c) <2003-2013> 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 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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