http://www.elitehackers.info/forums/showthread.php?t=4556


<Usage>
c:\perl\bin> perl port-scan.pl -l c:\ip.lst -p 3200,3300,3600,3700-3711



#!/perl/bin/perl.exe

#####################
# Perl Port Scanner #
#####################
# port-scan.pl

use IO::Socket;
use Getopt::Std;

%opts = ();
getopts("t:l:p:r:T:P:h", \%opts);

if (((!$opts{t}) and (!$opts{l})) or ($opts{h})) {
print "Usage: portscan.pl (-t target | -l file with list of targets) [arguments]\n\n";
print "Arguments:\t-t -- Specify the Target.\n";
print "\t\t-l -- Specify a List of Targets in a Plain Text file.\n";
print "\t\t-p -- Specify a single port or multiple ports seperated by commas.\n";
print "\t\t-r -- Specify a range of ports in this format: 1-1024\n";
print "\t\t-T -- Specify the Timeout Value. (default is 1)\n";
print "\t\t-P -- Specify the Protocol to use (TCP or UDP) (default is TCP).\n\n";
print "Example:\tportscan.pl -t 192.168.1.2 -p 22 -T 3\n";
print "\t\tportscan.pl -t 192.168.1.2 -p 21,22,25,110\n";
print "\t\tportscan.pl -l targets.txt -r 1-200 -P UDP\n";
print "\nIf no Ports are specified then the default scan of\nports 1-1024 is performed against the target\n";
print "\n";
exit 1;
}
# Get the Ports.
if (($opts{p}) and (!$opts{r})) {
# Use a single or multiple user-specified ports seperated by commas.
@ports = split(",", $opts{p});
foreach $x (@ports) {
if ($x !~ /^[0-9]+$/) {
print "Error: Ports are Numerical.\n";
exit 1;
}
if ($x < 1) {
print "Error: Ports need to be 1 or above.\n";
exit 1;
}
}
$scantype = 1;
}
elsif ((!$opts{p}) and ($opts{r})) {
# Use a range of ports.
if ($opts{r} !~ /-/) {
print "Error: Invalid use of Port Range switch. (see -h for help).\n";
exit 1;
}
$start = ((split("-", $opts{r}))[0]);
if ($start !~ /^[0-9]+$/) {
print "Error: Ports are Numerical.\n";
exit 1;
}
if ($start < 1) {
print "Error: Ports need to be 1 or above.\n";
exit 1;
}
$end = ((split("-", $opts{r}))[1]);
if ($end !~ /^[0-9]+$/) {
print "Error: Ports are Numerical.\n";
exit 1;
}
if ($end < 1) {
print "Error: Ports need to be 1 or above.\n";
exit 1;
}
$scantype = 2;
}
else {
# Perform the Default scan with ports 1-1024.
$scantype = 3;
}

# Get the Timeout Value specified by user, if none specified use default "1".
if ($opts{T}) {
$timeout = $opts{T};
if ($timeout !~ /^[0-9]+$/) {
print "Error: Timeout Value needs to be Numerical. (see -h for help).\n";
exit 1;
}
}
else {
$timeout = 1;
}

# Get the Protocol specified by user, if none specified use default of "TCP".
if ($opts{P}) {
$proto = $opts{P};
$proto =~ tr/a-z/A-Z/;
if (($proto ne "TCP") and ($proto ne "UDP")) {
print "Error: The Protocol you Specified is Invalid.\n";
exit 1;
}
}
else {
$proto = "TCP";
}

# Time to scan!
# If the user has specifed a single target instead of a list.
if (($opts{t}) and (!$opts{l})) {
# Use a single target.
$target = $opts{t};

$openports = 0;
$totalports = 0;
print "Initializing Scan...\n-\n\n";

# Type of Scan to Perform.
if ($scantype == 1) {
$then = time;
foreach $port (@ports) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $port,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $port is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
}
}
elsif ($scantype == 2) {
if ($start < $end) {
$then = time;
while ($start <= $end) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $start,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $start is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
$start++;
}
}
elsif ($start > $end) {
$then = time;
$x = $start;
$start = $end;
$end = $x;
while ($start <= $end) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $start,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $start is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
$start++;
}
}
elsif ($start == $end) {
$then = time;
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $start,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $start is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
}
}
elsif ($scantype == 3) {
# Perform the default scan (ports 1-1024).
$a = 1;
$b = 1024;
$then = time;
while ($a <= $b) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $a,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $a is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
$a++;
}
}
$now = time;
$dur = $now-$then;
print "\n-\nScan Finished!\n";
print "Target: $target\n";
print "Open Ports Found: $openports\n";
print "Total Ports Scanned: $totalports\n";
print "Duration: $dur seconds\n\n";
}
elsif ((!$opts{t}) and ($opts{l})) {
$target = $opts{t};

# Get the list of targets from a file.
$file = $opts{l};

$openports = 0;
$totalports = 1;

if ($file !~ /^[a-zA-Z0-9]+\.{1}[a-zA-Z0-9]+$/) {
print "Error: Invalid Filename.\n";
exit 1;
}
if (!open(IN, "<$file")) {
print "Error: Unable to open the file \"$file\".\n";
exit 1;
}
@list = <IN>;
# Read in each target and scan it accordingly.
foreach $target (@list) {
chomp($target);

# Type of Scan to Perform.
if ($scantype == 1) {
$then = time;
print "Initializing Scan against $target...\n-\n\n";
foreach $port (@ports) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $port,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $port is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
}
}
elsif ($scantype == 2) {
if ($start < $end) {
$then = time;
print "Initializing Scan against $target...\n-\n\n";
while ($start <= $end) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $start,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $port is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
$start++;
}
}
elsif ($start > $end) {
$then = time;
$x = $start;
$start = $end;
$end = $x;
print "Initializing Scan against $target...\n-\n\n";
while ($start <= $end) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $start,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $port is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
$start++;
}
}
elsif ($start == $end) {
print "Initializing Scan against $target...\n-\n\n";
$sock = new IO::Socket::INET (
PeerAddr => $target,
Peerport => $start,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $start is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
}

}
elsif ($scantype == 3) {
# Perform the Default scan. (ports 1-1024).
$a = 1;
$b = 1024;
$now = time;
print "Initializing Scan against $target...\n-\n\n";
while ($a <= $b) {
$sock = new IO::Socket::INET (
PeerAddr => $target,
PeerPort => $a,
Proto => $proto,
Timeout => $timeout
);
if ($sock) {
print "Port $a is open.\n";
close(SOCK);
$openports++;
}
$totalports++;
$a++;
}
}

$now = time;
$dur = $now-$then;
print "\n-\nScan Finished!\n";
print "Target: $target\n";
print "Open Ports Found: $openports\n";
print "Total Ports Scanned: $totalports\n";
print "Duration: $dur seconds\n\n-----------------------------------------------\n\n";
}
}
else {
print "Error: You onl y need to specify one  of the target switches. (see -h for help).\n";
exit 1;
}

+ Recent posts