For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > port scanner









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author port scanner
nonet

2006-02-19, 3:55 am

Hi ,

This is my first networking perl program, it is a basic port scanner
that grabs the banners from
specific ports. I fairly new to perl but I've also made programs to
administrator and secure freshly
install linux os. This program is for educational purposes only, I
planning working on this program
more, so advice is welcome.

MODULES NEED:
IO::Socket
Net::Ping
Net::Telnet

BUGS FOUND:
1. When identifing host webserver on port 80, it hangs when the
server request username/password.
2. The program exits when the port banner be grab sends a eof.

#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use Net::Ping;
use Net::Telnet;

our (
$port,$socket,@ports,
$pong,$current,$service,
$session,$prematch,$match
);


&usage if ! $ARGV[0];

sub usage {
print "
Portscanner 0.1-beta

Usage: alpha-scan <host>\n\n\n\n";
exit;
}


@ports=('21','22','23','25','42',
'53','69','79','80',109..110,'135',
'139','161','443','445',512..515,989..995,
'1194','1433','2049','2998',6000..6009,
'6667','8080','65301'
);

unless (-d "/root/database") {
print `mkdir "/root/database"`;
}


for (1..255) {
print qx`clear`,"[ HOST ]: ","$ARGV[0]".".$_";
$pong= Net::Ping->new("tcp",'1');
if ($pong->ping("$ARGV[0]".".$_")) {
$current="$ARGV[0]".".$_";
unless (-d "/root/database/$ARGV[0]") {
print `mkdir "/root/database/$ARGV[0]"`;
}
} elsif (! $pong->ping("$ARGV[0]".".$_")) {
next;
}

unless (-d "/root/database/$ARGV[0]/$current") {
print `mkdir "/root/database/$ARGV[0]/$current"`;
}
open LOG,">/root/database/$ARGV[0]/$current/$current".".services";
open BANNER, ">/root/database/$ARGV[0]/$current/$current".".banner";

for ($current) {
print qx`clear`,"\n[ HOST ] $_ \n";
print LOG "\n\n[ HOST ] $_ \n";
foreach $port (@ports) {
$socket = IO::Socket::INET->new(
PeerAddr => "$current",
PeerPort => "$port",
Timeout => '1'
);

print "[ $port ] \n";

if ($socket) {

if ("$port" eq 23) {
$session=Net::Telnet->new( Host => "$current",
Port => "$port",
Errmode => 'reture'
);
$session->open("$current");
($prematch, $match)=$session->waitfor(
Match => '/Login: ?$/i',
Match => '/User: ?$/i',
Match => '/Username: ?$/i',
Match => '/Password: ?$/i',
Timeout => '5'
);
print BANNER "\n-=-=- $port -=-=-\n","$prematch $match\n";
$session->close;
} elsif ("$port" eq 21) {
$session=Net::Telnet->new( Host => "$current",
Port => "$port",
Errmode => 'return'
);
$session->open("$current");
($prematch, $match)=$session->waitfor(
Match => '/^\d\d\d .*$/',
Timeout => '10'
);
print BANNER "\n-=-=- $port -=-=-\n","$prematch $match\n";
$session->close;
} elsif ("$port" eq 25) {
$session=Net::Telnet->new( Host => "$current",
Port => "$port",
Errmode => 'return'
);
$session->open("$current");
($prematch, $match)=$session->waitfor(
Match => '/^\d\d\d .*$/',
Timeout => '10'
);
print BANNER "\n-=-=- $port -=-=-\n","$prematch $match\n";
$session->close;
} elsif ("$port" eq 110) {
$session=Net::Telnet->new( Host => "$current",
Port => "$port",
Errmode => 'return'
);
$session->open("$current");
($prematch, $match)=$session->waitfor(
Match => '/^\+OK .*$/i',
Timeout => '10'
);
print BANNER "\n-=-=- $port -=-=-\n","$prematch $match\n";
$session->close;
} elsif ("$port" eq 22) {
$session=Net::Telnet->new( Host => "$current",
Port => "$port",
Errmode => 'return'
);
$session->open("$current");
($prematch, $match)=$session->waitfor(
Match => '/SSH.*$/i',
Timeout => '5'
);
print BANNER "\n-=-=- $port -=-=-\n","$prematch $match\n";
$session->close;
} elsif ("$port" eq 2049) {
$session=Net::Telnet->new( Host => "$current",
Port => "$port",
Errmode => 'return'
);
$session->open("$current");
($prematch, $match)=$session->waitfor(
Match => '/Login: ?$/i',
Match => '/User: ?$/i',
Match => '/Username: ?$/i',
Match => '/Password: ?$/i',
Timeout => '5'
);
print BANNER "\n-=-=- $port -=-=-\n","$prematch $match\n";
$session->close;
} elsif ("$port" eq 80) {
print BANNER "\n-=-=- $port -=-=-", qx`HEAD -t 5 HTTP/1.1
"$current"`,"\n";
}

print "[\ $port \]", " " x (10-length("$port"));
print "open\n";
$service=getservbyport ("$port", "tcp");
print LOG "$port", " " x (10-length("$port")), "$service\n";
}
}
}
}

close LOG;
close BANNER;
exit;

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com