#!/usr/bin/perl #--------------------------------------------------------- # nmap_parse.pl # ============= # Small PERL script for parsing human readable nmap results # # Nicholas de Jong (nick@securify.com) # 1st November 2000. # Version 0.2 # #--------------------------------------------------------- $PACKAGE = "nmap_parse.pl"; $VERSION = "0.2"; $OUTPUT_FILE = "nmap_parse.output"; $INPUT_FILE = ""; Main(); exit; #--------------------------------------------------------- # sub Main{ SplashScreen(); $INPUT_FILE = $ARGV[0]; print " Loading and parsing the file $INPUT_FILE\n"; local $results = ParseNmapResults(LoadFile($INPUT_FILE)); print " Writing results to file $OUTPUT_FILE\n"; WriteFile($OUTPUT_FILE,$results) } #--------------------------------------------------------- # sub SplashScreen { print "---------------------------------------------------------\n"; print " nmap_parse.pl\n"; print " =============\n"; print " PERL script for parsing human readable NMAP results\n"; print " Nicholas de Jong - 1st November 2000, Version 0.2\n"; print " \n"; print " Useage : perl nmap_parse.pl NMAP.output.file\n"; print "\n"; } #--------------------------------------------------------- # sub ParseNmapResults{ local @output = LoadFile("$INPUT_FILE"); local $results = ""; for (local $line=0;$line<@output;$line++){ local $ip = ""; local $port=""; if ($output[$line] =~ /\AInteresting/){ $output[$line]=~ /\(.+\)/; $ip = $&; $ip =~ s/^.//; $ip =~ s/.$//; local $nextline=""; local $nextlinesearch=$line+1; while ($nextline eq ""){ if ($output[$nextlinesearch] =~ /\AInteresting/){ $nextline = $nextlinesearch; } $nextlinesearch++; if ($nextlinesearch >= @output){ $nextline = @output; } } for (local $nline=$line;$nline<$nextline;$nline++){ if ($output[$nline] =~ /^(\d+)/g) { $port = $&; if ($output[$nline] =~ /open/g) { $results = "$results$ip:$port:open\n"; } if ($output[$nline] =~ /closed/g) { $results = "$results$ip:$port:closed\n"; } if ($output[$nline] =~ /filtered/g) { $results = "$results$ip:$port:filtered\n"; } if ($output[$nline] =~ /unfiltered/g) { $results = "$results$ip:$port:unfiltered\n"; } } } } } return $results; } #--------------------------------------------------------- # Writes a file sub WriteFile { local $filename = $_[0]; local $data = $_[1]; unless (open (FILE,">$filename")){ ErrorHandle("Cannot write to \"$filename\"");} print FILE "$data"; close (FILE); return; } #--------------------------------------------------------- # Handle errors in the local sense sub ErrorHandle { local $errormessage = $_[0]; print "\n !Error! - $PACKAGE - Version $VERSION\n"; print " $errormessage\n"; exit; } #--------------------------------------------------------- # Loads a file in the local module context and returns it as an array sub LoadFile { local $filename = $_[0]; unless (open (FILE,"$filename")){ ErrorHandle("Cannot open file \"$filename\"");} local @file = ; close (FILE); chomp (@file); return @file; }