| Parser(3pm) | User Contributed Perl Documentation | Parser(3pm) |
Nmap::Parser - parse nmap scan data with 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();
#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 ...
}
For a full listing of methods see the documentation corresponding to each object. You can also visit the website <https://github.com/modernistik/Nmap-Parser> for additional installation instructions.
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 <https://github.com/modernistik/Nmap-Parser>
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
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.
If you wish to save the xml output from parsescan(), you must call cache_scan() method BEFORE 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.
See section EXAMPLES for a short tutorial
Note: You cannot have one of the nmap options to be '-oX', '-oN' or '-oG'. Your program will die if you try and pass any of these options because it decides the type of output nmap will generate. The IP addresses can be nmap-formatted addresses see nmap(1)
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).
#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);
$np->callback(\&my_function); #sets callback, my_function() will be called
$np->callback(); #resets it, no callback function called. Back to normal.
This object contains the scan session information of the nmap scan.
This object represents the information collected from a scanned host.
#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
Some hops may be missing if Nmap could not figure out information about them. In this case there is a gap between the "ttl()" values of consecutive returned hops. See also "trace_error()".
$os = $host->os_sig;
$os->name;
$os->osfamily;
$host->tcp_ports('open') #returns all only 'open' ports (even 'open|filtered')
$host->udp_ports('open|filtered'); #matches exactly ports with 'open|filtered'
Note that if a port state is set to 'open|filtered' (or any combination), it will be counted as an 'open' port as well as a 'filtered' one.
$svc = $host->tcp_service(80);
$svc->name;
$svc->proto;
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.
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 "os_sig()" method. One important thing to note is that the order of OS names and classes are sorted by DECREASING ACCURACY. 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).
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 "traceroute" command outputs.
Nmap::Parser::Host::TraceHop objects are obtained through the "all_trace_hops()" and "trace_hop()" Nmap::Parser::Host methods.
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 <https://github.com/modernistik/Nmap-Parser>
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...
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. The callback function must be setup before parsing
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)
}
Using multiple instances of Nmap::Parser is extremely useful in helping audit/monitor the network Policy (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 base_image.xml. 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 Compliance 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";}
}
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. <https://github.com/modernistik/Nmap-Parser/issues>
Please submit any bugs or feature requests to: <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.
nmap, XML::Twig
The Nmap::Parser page can be found at: <https://github.com/modernistik/Nmap-Parser>. It contains the latest developments on the module. The nmap security scanner homepage can be found at: <http://www.insecure.org/nmap/>.
Origiinal author, Anthony Persaud <https://www.modernistik.com>. However, special thanks to: Daniel Miller <https://github.com/bonsaiviking> and Robin Bowes <http://robinbowes.com>. Please see Changes.md file for a list of other great contributors.
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.
| 2022-11-20 | perl v5.36.0 |