Saturday, December 11, 2004

Port Scanner script in Perl

#!/usr/usc/bin/perl
#nishant@purecode.us
use strict;
use IO::Socket;
#this is the victim machine :)
#tell how to use if user didnt enter any machine ip or name
my $peer = shift @ARGV;
if (!$peer) {
print "Usage: perl scan.pl [victim] \n";
exit;
}
#get the string of well-known ports 1 to 1023
my $wellknown = &getWellKnownPortList;
#now get a hash which has keys as port number and value as the well known service
my %hash = split /:/, $wellknown;
%hash = reverse %hash;
#now get a list of port numbers in sorted order
my @ports = sort {$a <=> $b} keys %hash;
#try to create a new socket on each of these ports , the $peer is the machine on which you want to run port scanner
my $sock;
foreach (@ports) {
$sock = IO::Socket::INET->new("$peer:$_");
print "\nPort $_ $hash{$_} open\n" if ($sock);
}
print "Done.\n";
exit;
#this is the long list of well known ports downloaded from internet , port numbers 1 to 1023
#the file is in /etc/services
sub getWellKnownPortList {

"tcpmux:1:compressnet:2:compressnet:3:rje:5:echo:7:discard:9:systat:11: .... and so on, I dont
put all ports here for clarity... mail me for complete code or look up on website at www.purecode.us }

0 Comments:

Post a Comment

<< Home