Advertisement
rutera

dmzscan

Jan 8th, 2015
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.91 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3.  
  4. # * DMZScan - Simple Connect Port Scanner using PERL, Phil Robinson, IRMPLC 2005
  5. # *
  6. # * Useful if a Windows box is compromised and you don't want to
  7. # * reboot it, or haven't got administrator privileges (Hint: use PERL2EXE)
  8. # *
  9. # * This code is free software; you can redistribute it and/or
  10. # * modify it under the terms of the GNU General Public License
  11. # * as published by the Free Software Foundation; either version 2
  12. # * of the License, or (at your option) any later version.
  13. # *
  14. # * This code is distributed in the hope that it will be useful,
  15. # * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # * GNU General Public License for more details.
  18. # *
  19. # * You should have received a copy of the GNU General Public License
  20. # * along with this program; if not, write to the Free Software
  21. # * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. # * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  23.  
  24. use Getopt::Std;
  25. use IO::Socket;
  26.  
  27. sub parse_octet
  28. {
  29.   $octet = shift;
  30.   my @retlist;
  31.  
  32.   if ( $octet =~ /,/ || $octet =~ /-/ )
  33.   {
  34.     @elements = split(/,/, $octet);
  35.     foreach $element (@elements)
  36.     {
  37.       if ( $element =~ /-/ )
  38.       {
  39.         ($lower, $upper) = split(/-/, $element);
  40.         if ($lower > $upper) { die "Incorrect IP Range"; }
  41.         for ($x = $lower ; $x <= $upper ; $x++ ) { push @retlist, $x }
  42.       }
  43.       else
  44.       { push @retlist, $element }
  45.     }      
  46.   }
  47.   else { push @retlist, $octet }
  48.  
  49.   return @retlist;
  50. }
  51.  
  52. sub parse_ip
  53. {
  54.   my $ip = shift;
  55.   my @scanip;
  56.   ($first, $second, $third, $fourth) = split(/\./,$ip);
  57.  
  58.   @firstlist = &parse_octet($first);
  59.   @secondlist = &parse_octet($second);
  60.   @thirdlist = &parse_octet($third);
  61.   @fourthlist = &parse_octet($fourth);
  62.  
  63.   foreach $a (@firstlist)
  64.   {
  65.     foreach $b (@secondlist)
  66.     {
  67.       foreach $c (@thirdlist)
  68.       {
  69.         foreach $d (@fourthlist)
  70.         {
  71.           push @scanip, "$a.$b.$c.$d";
  72.         }
  73.       }
  74.     }
  75.   }
  76.  
  77.   return @scanip;
  78. }
  79.    
  80. sub parse_ports
  81. {
  82.   my $p = shift;
  83.   @plist = &parse_octet($p);
  84.   return (@plist);
  85. }
  86.  
  87.  
  88. $Usage = qq{
  89. IRM DMZ Scanner v0.1 - by Morfsta 2003
  90.  
  91. Usage: $0 [options] -h <IP Address / Range>
  92.  
  93. Options:
  94.          -h IP Address/Range e.g. 10.0.244-246.0,5,6,9
  95.      -l Print list of IP addresses that will be scanned and exit
  96.      -p Port list e.g. 1-1024,3128,8080
  97.         (default 21, 22, 23, 53, 80, 111, 135, 139, 445)
  98.      -t Connect timeout - e.g. 0.1 (100ms - default 0.1)
  99.         (default 50ms)
  100.      -o Output file (outputs open ports only)
  101. };
  102.  
  103. die $Usage if (!getopts('h:p:t:o:lc'));
  104. $ip = $opt_h || die "$Usage Need to specify an IP address range";
  105. $print = $opt_l || 0;
  106. $ports = $opt_p || 0;
  107. $timeout = $opt_t || 0.1;
  108. $logfile = $opt_o || "";
  109. @iplist = &parse_ip($ip);
  110.  
  111. if ($print)
  112. {
  113.   foreach $ipaddress (@iplist)
  114.   {
  115.     print "$ipaddress\n";
  116.   }
  117.   exit 0;
  118. }
  119.  
  120. if ($ports)
  121. {
  122.   @portlist = &parse_ports($ports);
  123. }
  124. else { @portlist = (21,22,23,53,80,111,135,139,445) }
  125.  
  126. if ($logfile)
  127. {
  128.   open(LOG, ">$logfile") || die "Cannot write to logfile";
  129. }
  130.  
  131. print "IRM DMZ Scanner v0.2 - by Morfsta 2004\n\n";
  132.  
  133. foreach $host (@iplist)
  134. {
  135.    foreach $port (@portlist)
  136.    {
  137.      if ($port =~ /\D/) { $port = getservbyname($port, tcp) }
  138.        $iaddr   = inet_aton($host);
  139.        $paddr   = sockaddr_in($port, $iaddr);
  140.        $proto   = getprotobyname('tcp');
  141.        socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
  142.        $sock = new IO::Socket::INET (PeerAddr => $host,
  143.                      PeerPort => $port,
  144.                      Proto => 'tcp',
  145.                      Timeout => $timeout);
  146.        if ( $sock )
  147.        {
  148.      print "$host:$port -> open\n";
  149.      if ( $logfile )
  150.      {
  151.        print LOG "$host:$port\n";
  152.            close ($sock) || die "close: $!";
  153.      }
  154.        }
  155.    }
  156. }
  157. close(LOG);
  158. print "\nFinished..\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement