{"id":220,"date":"2009-11-06T12:28:09","date_gmt":"2009-11-06T18:28:09","guid":{"rendered":"http:\/\/www.jedge.com\/wordpress\/?p=220"},"modified":"2012-07-05T22:41:44","modified_gmt":"2012-07-06T04:41:44","slug":"using-perl-to-parse-nmap-xml","status":"publish","type":"post","link":"https:\/\/www.jedge.com\/wordpress\/2009\/11\/using-perl-to-parse-nmap-xml\/","title":{"rendered":"Using Perl to Parse Nmap XML"},"content":{"rendered":"<p>As an auditor I liked to quickly analyze my <a href=\"http:\/\/nmap.org\">Nmap<\/a> scan results by parsing the XML output produced and loading it into my favorite spreadsheet application.<br \/>\nFrom there I could sort by host, port, service, or operating system for analysis.  The parsed results are a lot easier to add to reports and workpapers.  Just remember to keep the original Nmap results.<br \/>\nI&#8217;ve developed a LAMP <a href=\"http:\/\/www.jedge.com\/wordpress\/?page_id=62\">framework<\/a> to parse and load Nmap results into a database for reporting and analysis.  However if you are just looking to quickly parse the results of individual scans I&#8217;ve got a Perl script for you!<br \/>\n<!--more--><br \/>\nFirst a quick blurb on getting installing Perl and and Nmap-Parser module.<\/p>\n<p><strong>Windows<\/strong><\/p>\n<p>Download ActivePerl from the Active State website:  https:\/\/www.activestate.com\/activeperl\/downloads\/<br \/>\nOnce ActivePerl is installed you will need to install the <a href=\"http:\/\/search.cpan.org\/dist\/Nmap-Parser\/Parser.pm\">Nmap Parser<\/a> written by <a href=\"http:\/\/anthonypersaud.com\/\">Anthony Persaud<\/a>.<br \/>\nFrom the Command Prompt enter the following command:<br \/>\n<kbd><br \/>\nC:\\&gt;ppm install nmap-parser<br \/>\nDownloading Nmap-Parser-1.19...done<br \/>\nDownloading XML-Twig-3.32...done<br \/>\nUnpacking Nmap-Parser-1.19...done<br \/>\nUnpacking XML-Twig-3.32...done<br \/>\nGenerating HTML for Nmap-Parser-1.19...done<br \/>\nGenerating HTML for XML-Twig-3.32...done<br \/>\nUpdating files in site area...done<br \/>\n21 files installed<br \/>\n<\/kbd><\/p>\n<p><strong>Linux<\/strong><\/p>\n<p>For Ubuntu\/Debian you can install the package.<br \/>\n<kbd>#apt-get install libnmap-parser-perl<\/kbd><\/p>\n<p>For every Linux distro you can install the package via <a href=\"http:\/\/www.cpan.org\/\">CPAN<\/a>.<br \/>\n<kbd>#perl -MCPAN -e 'install Nmap::Parser'<\/kbd><\/p>\n<p>Copy the following Perl code below and save it as nmap_parse.pl.<br \/>\n<pre><code>\n#!\/usr\/bin\/perl\nuse Nmap::Parser;\n\nmy $np = new Nmap::Parser;\nmy $infile = @ARGV[0];\n\n$np-&gt;parsefile($infile);\n\n#GETTING SCAN INFORMATION\n\nprint &quot;Scan Information:\\n&quot;;\nmy $si = $np-&gt;get_session();\nprint\n&#039;Number of services scanned: &#039;.$si-&gt;numservices().&quot;\\n&quot;,\n&#039;Start Time: &#039;.$si-&gt;start_str().&quot;\\n&quot;,\n&#039;Finish Time: &#039;.$si-&gt;time_str().&quot;\\n&quot;,\n&#039;Scan Arguments: &#039;.$si-&gt;scan_args().&quot;\\n&quot;;\n\nprint &quot;Host Name,Ip Address,MAC Address,OS Name,OS Family,OS Generation,OS Accuracy,Port,Service Name,Service Product,Service Version,Service Confidence\\n&quot;;\nfor my $host ($np-&gt;all_hosts()){\n&nbsp;&nbsp;&nbsp;&nbsp;for my $port ($host-&gt;tcp_ports()){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $service = $host-&gt;tcp_service($port);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $os = $host-&gt;os_sig;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print $host-&gt;hostname().&quot;,&quot;.$host-&gt;ipv4_addr().&quot;,&quot;.$host-&gt;mac_addr().&quot;,&quot;.$os-&gt;name.&quot;,&quot;.$os-&gt;family.&quot;,&quot;.$os-&gt;osgen().&quot;,&quot;.$os-&gt;name_accuracy().&quot;,&quot;.$port.&quot;,&quot;.$service-&gt;name.&quot;,&quot;.$service-&gt;product.&quot;,&quot;.$service-&gt;version.&quot;,&quot;.$service-&gt;confidence().&quot;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n\n<\/code><\/pre><\/p>\n<p>Save the above code and run it from the command line as follows:<\/p>\n<p><kbd>C:\\&gt;nmap_parse.pl nmap_scan_output.xml &gt;&gt; results.csv<\/kbd><\/p>\n<p>Additional Information<\/p>\n<p>ppm &#8211; Perl Package Manager, version 4<br \/>\nhttp:\/\/docs.activestate.com\/activeperl\/5.10\/bin\/ppm.html<\/p>\n<p>ActiveState CPAN PPM Repository<br \/>\nhttp:\/\/ppm4.activestate.com\/<\/p>\n<p>Nmap Parser<br \/>\nhttp:\/\/search.cpan.org\/dist\/Nmap-Parser\/Parser.pm<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As an auditor I liked to quickly analyze my Nmap scan results by parsing the XML output produced and loading it into my favorite spreadsheet application. From there I could sort by host, port, service, or operating system for analysis. The parsed results are a lot easier to add to reports and workpapers. Just remember [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[21,77,33,78],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-scripts","tag-nmap","tag-parse","tag-perl","tag-xml"],"_links":{"self":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":15,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/posts\/220\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jedge.com\/wordpress\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}