PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/sweep.pl

https://github.com/gitpan/Nmap-Parser-XML
Perl | 313 lines | 102 code | 37 blank | 174 comment | 30 complexity | d87fafd1b11612b9aa5ece15d7825f8e MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/perl
  2. #Anthony G. Persaud
  3. #sweep.pl
  4. #Description:
  5. # It takes in a nmap xml file and prints a list of active and inactive
  6. # hosts.
  7. #This program is free software; you can redistribute it and/or modify it under
  8. #the terms of the GNU General Public License as published by the Free Software
  9. #Foundation; either version 2 of the License, or (at your option) any later
  10. #version.
  11. #
  12. #This program is distributed in the hope that it will be useful, but WITHOUT ANY
  13. #WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  14. #PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. #
  16. # Changelog:
  17. # APS 01/29/2004: Changed run_nmap_scan to use parsescan().
  18. # $nmap_exe is set to default 'nmap' if find_exe returns empty
  19. # APS 02/03/2004: Added ability to read IPs from a file
  20. # APS 02/05/2004: Added ability output active IP (up state) to a file
  21. #
  22. #
  23. #
  24. #
  25. #
  26. #
  27. use strict;
  28. use Nmap::Parser::XML;
  29. use constant TEST_FILE => 'example.xml';
  30. use constant CMD1 => '-sP --randomize_hosts';
  31. use File::Spec;
  32. use Getopt::Long;
  33. use Pod::Usage;
  34. use vars qw(%G);
  35. Getopt::Long::Configure('bundling');
  36. my $p = new Nmap::Parser::XML;
  37. print "\nsweep.pl - ( http://npx.sourceforge.net )\n",
  38. ('-'x50),"\n\n";
  39. GetOptions(
  40. 'help|h|?' => \$G{helpme},
  41. 'v+' => \$G{verbose},
  42. 'i=s' => \$G{usefile},
  43. 'L=s' => \$G{ipfile},
  44. 'o=s' => \$G{output_active}
  45. ) or (pod2usage(-exitstatus => 0, -verbose => 2));
  46. if($G{helpme} || (!$G{usefile} && scalar @ARGV == 0 && !$G{ipfile}))
  47. {pod2usage(-exitstatus => 0, -verbose => 2)}
  48. if($G{usefile} eq ''){$p = run_nmap_scan(@ARGV);}
  49. else {
  50. #use the input file
  51. print 'Using InputFile: '.$G{usefile}."\n" if($G{verbose} > 0);
  52. if(not -e $G{usefile})
  53. {print STDERR "ERROR: File $G{usefile} does not exists!\n"; exit;}
  54. $p->parsefile($G{usefile});
  55. }
  56. if($G{output_active}){
  57. open OUTPUT ,">$G{output_active}" ||
  58. die "ERROR: Could open $G{output_active} for writing!\n$!\n";
  59. }
  60. print "Active Hosts Scanned:\n";
  61. my (@ipa,@ipb);
  62. for my $ip ( $p->get_host_list('up')){
  63. print "\t$ip\n";
  64. if($G{output_active}){
  65. print OUTPUT "$ip\n";
  66. }
  67. }
  68. if($G{output_active}){close OUTPUT;}
  69. print "\n";
  70. #printing inactive hosts
  71. print "Inactive Hosts Scanned:\n";
  72. for my $ip ( $p->get_host_list('down')){print "\t$ip\n";}
  73. if($G{output_active}){print "\nSaved output file: $G{output_active}\n";}
  74. ################################################################################
  75. ## Utility Functions ##
  76. ################################################################################
  77. sub find_exe {
  78. my $exe_to_find = shift;
  79. $exe_to_find =~ s/\.exe//;
  80. local($_);
  81. local(*DIR);
  82. for my $dir (File::Spec->path()) {
  83. opendir(DIR,$dir) || next;
  84. my @files = (readdir(DIR));
  85. closedir(DIR);
  86. my $path;
  87. for my $file (@files) {
  88. $file =~ s/\.exe$//;
  89. next unless($file eq $exe_to_find);
  90. $path = File::Spec->catfile($dir,$file);
  91. # Should symbolic link be considered? Helps me on cygwin but ...
  92. next unless -r $path && (-x _ || -l _);
  93. return $path;
  94. last DIR;
  95. }
  96. }
  97. }
  98. sub run_nmap_scan {
  99. my @ips = @_;
  100. my($NMAP,$cmd);
  101. if($G{ipfile} && -e $G{ipfile})
  102. {push @ips ,read_ips_from_file($G{ipfile});
  103. if($G{verbose} > 0){
  104. print STDERR "\nIP file contains:\n";
  105. for(@ips){print STDERR "\t$_\n";}
  106. print "\n";}
  107. }
  108. elsif($G{ipfile} && !-e $G{ipfile})
  109. {warn "WARNING: IP file $G{ipfile} does not exist!\n";}
  110. $cmd = join ' ', (CMD1, @ips);
  111. my $nmap_exe = find_exe('nmap');
  112. if($nmap_exe eq '')
  113. {warn "ERROR: nmap executable not found in \$PATH\n";
  114. $nmap_exe = 'nmap';}
  115. print 'Running: '.$nmap_exe.' '.$cmd."\n" if($G{verbose} > 0);
  116. $p->parsescan($nmap_exe,$cmd);
  117. return $p;
  118. }
  119. sub read_ips_from_file {
  120. my $filename = shift;
  121. my @ips;
  122. open FILE, "$filename" || die "ERROR: Could not open $filename! \nERROR: $!";
  123. for(<FILE>){
  124. chomp; # no newline
  125. s/#.*//; # no comments
  126. s/^\s+//; # no leading white
  127. s/\s+$//; # no trailing white
  128. next unless length; # anything left?
  129. push @ips , $_; #it might be a host name too, so don't expect only numbers
  130. }
  131. close FILE;
  132. return @ips;
  133. }
  134. __END__
  135. =pod
  136. =head1 NAME
  137. status_check - scans multiple hosts to determine their network status
  138. =head1 SYNOPSIS
  139. status_check.pl [OPTS] <IP_ADDR> [<IP.ADDR> ...]
  140. =head1 DESCRIPTION
  141. This script uses the nmap security scanner with the Nmap::Parser::XML module
  142. in order to run a quick PING sweep against specific hosts. It will then inform
  143. of which hosts were active (up) and inactive (down).
  144. =head1 OPTIONS
  145. These options are passed as command line parameters.
  146. =over 4
  147. =item B<-i nmapscan.xml>
  148. Runs the script using the given xml file (which is nmap xml scan data) instead
  149. of actually running a scan against the given set of hosts. This is useful if
  150. you only have the xml data on a given machine, and not nmap.
  151. =item B<-h,--help,-?>
  152. Shows this help information.
  153. =item B<-L ips.txt>
  154. Reads IP addresses from filename.txt to run a scan against. The IP addresses
  155. should be in the target specification format explained below.
  156. =item B<-o output.txt>
  157. Saves the IP addresses found to be active (in state 'up') to a given file. This
  158. file contains each of the active IP addresses found, one on each line. This is
  159. useful if you wish to use the file with other programs or scripts.
  160. =item B<-v>
  161. This runs the script in verbose mode. The more times used, the more verbose
  162. the script will be.
  163. =back 4
  164. =head1 TARGET SPECIFICATION
  165. This documentation was taken from the nmap man page. The IP address inputs
  166. to this scripts should be in the nmap target specification format.
  167. The simplest case is listing single hostnames or IP addresses onthe command
  168. line. If you want to scan a subnet of IP addresses, you can append '/mask' to
  169. the hostname or IP address. mask must be between 0 (scan the whole internet) and
  170. 32 (scan the single host specified). Use /24 to scan a class 'C' address and
  171. /16 for a class 'B'.
  172. You can use a more powerful notation which lets you specify an IP address
  173. using lists/ranges for each element. Thus you can scan the whole class 'B'
  174. network 128.210.*.* by specifying '128.210.*.*' or '128.210.0-255.0-255' or
  175. even use the mask notation: '128.210.0.0/16'. These are all equivalent.
  176. If you use asterisks ('*'), remember that most shells require you to escape
  177. them with back slashes or protect them with quotes.
  178. Another interesting thing to do is slice the Internet the other way.
  179. Examples:
  180. status_check.pl 127.0.0.1
  181. status_check.pl target.example.com
  182. status_check.pl target.example.com/24
  183. status_check.pl 10.210.*.1-127
  184. status_check.pl *.*.2.3-5
  185. status_check.pl 10.[10-15].10.[2-254]
  186. =head1 OUTPUT EXAMPLE
  187. These are ONLY examples of how the output would look like.
  188. Status Check
  189. -------------------------------------------
  190. Active Hosts Scanned:
  191. 127.0.0.5
  192. 127.0.0.6
  193. 127.0.0.2
  194. 127.0.0.1
  195. 127.0.0.4
  196. Inactive Hosts Scanned:
  197. 127.0.0.3
  198. 192.168.0.1
  199. 192.168.0.2
  200. 192.168.2.4
  201. The output of the file if using the '-o file.txt' option will look like (using
  202. the IPs from the previous example):
  203. 127.0.0.5
  204. 127.0.0.6
  205. 127.0.0.2
  206. 127.0.0.1
  207. 127.0.0.4
  208. =head1 BUG REPORTS
  209. Please submit any bugs to:
  210. L<http://sourceforge.net/tracker/?group_id=97509&atid=618345>
  211. =head1 SEE ALSO
  212. L<Nmap::Parser::XML>
  213. The Nmap::Parser::XML page can be found at: L<http://npx.sourceforge.net/>.
  214. It contains the latest developments on the module. The nmap security scanner
  215. homepage can be found at: L<http://www.insecure.org/nmap/>.
  216. =head1 AUTHOR
  217. Anthony G Persaud <ironstar@iastate.edu>
  218. =head1 COPYRIGHT
  219. This program is free software; you can redistribute it and/or modify it under
  220. the terms of the GNU General Public License as published by the Free Software
  221. Foundation; either version 2 of the License, or (at your option) any later
  222. version.
  223. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  224. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  225. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  226. L<http://www.opensource.org/licenses/gpl-license.php>
  227. =cut