From 83b1d12b79aea186ac2ffd7b8cb30d3032375bec Mon Sep 17 00:00:00 2001 From: Anthony Persaud Date: Thu, 2 Oct 2008 23:17:12 +0000 Subject: [PATCH] Added support for tcp_del_ports and udp_del_ports. Added tests for these new functions. --- Parser.pm | 28 +++- t/instance.xml | 126 ++++++++-------- t/nmap_results.xml | 356 ++++++++++++++++++++++----------------------- t/parser.t | 18 ++- t/scanner.t | 9 +- 5 files changed, 288 insertions(+), 249 deletions(-) diff --git a/Parser.pm b/Parser.pm index 45da254..0d30c0a 100644 --- a/Parser.pm +++ b/Parser.pm @@ -6,7 +6,7 @@ use XML::Twig; use Storable qw(dclone); use vars qw($VERSION %D); -$VERSION = 1.16; +$VERSION = 1.17; sub new { @@ -625,6 +625,20 @@ sub extraports_state { return $_[0]->{ports}{extraports}{state}; } sub extraports_count { return $_[0]->{ports}{extraports}{count}; } sub distance { return $_[0]->{distance}; } +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 @@ -674,6 +688,9 @@ sub udp_port_count { return $_[0]->{ports}{udp_port_count}; } sub tcp_port_state { return _get_port_state( @_, 'tcp' ); } sub udp_port_state { return _get_port_state( @_, 'udp' ); } +sub tcp_del_ports { return _del_port( @_, 'tcp' ); } +sub udp_del_ports { return _del_port( @_, 'udp' ); } + sub tcp_service { my $self = shift; my $portid = shift; @@ -1204,13 +1221,18 @@ be counted as an 'open' port as well as a 'filtered' one.> Returns the total of TCP|UDP ports scanned respectively. +=item B + +=item B + +Deletes the current $portid from the list of ports for given protocol. + =item B =item B Returns the state of the given port, provided by the port number in $portid. - =item B =item B @@ -1544,7 +1566,7 @@ Anthony G Persaud L =head1 COPYRIGHT -Copyright (c) <2003-2008> +Copyright (c) <2003-2009> MIT License diff --git a/t/instance.xml b/t/instance.xml index 60cf0de..a1e4f53 100644 --- a/t/instance.xml +++ b/t/instance.xml @@ -1,68 +1,68 @@ - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/t/nmap_results.xml b/t/nmap_results.xml index 4b1698a..468c48b 100644 --- a/t/nmap_results.xml +++ b/t/nmap_results.xml @@ -1,184 +1,184 @@ - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/t/parser.t b/t/parser.t index af92de1..178890d 100644 --- a/t/parser.t +++ b/t/parser.t @@ -5,7 +5,7 @@ use blib; use Nmap::Parser; use File::Spec; use Cwd; -use Test::More tests => 150; +use Test::More tests => 153; use constant HOST1 => '127.0.0.1'; use constant HOST2 => '127.0.0.2'; @@ -142,6 +142,8 @@ sub host_1 { [ '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' ); @@ -198,6 +200,20 @@ sub host_1 { '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(), diff --git a/t/scanner.t b/t/scanner.t index 9b8ae70..e569537 100644 --- a/t/scanner.t +++ b/t/scanner.t @@ -19,15 +19,15 @@ 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 + + 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 +#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 @@ -47,7 +47,8 @@ SKIP: { } sub find_nmap { - #I think I borrowed this from someone (or from a Cookbook) + + #I think I borrowed this from someone (or from a Cookbook) my $exe_to_find = 'nmap'; $exe_to_find =~ s/\.exe//; local ($_);