port scanner



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;

.



Relevant Pages

  • First networking program .... port scanner
    ... scanner that grabs banners. ... When using the HEAD method on port 80 for server identification it ... Timeout => '1' ... Errmode => 'reture' ...
    (perl.beginners)
  • Re: Capturing Port Data
    ... my $telnet = new Net::Telnet ( ... Timeout => $timeout, ... Errmode => 'die', ... Port => $port,); ...
    (comp.lang.perl.misc)
  • Re: rs232 -help!
    ... Okay, I never really found out how to set the comm timeout, however I got it ... I can read the port and the Read functon ... rs232 port asides from using the SerialWnd class.... ... posts from everyone, and then I read over the rs232 documentation, and then I ...
    (microsoft.public.vc.language)
  • Problem with USB thumbdrive using 5.3-BETA7 [resend].
    ... scbus0 on ata0 bus 0: ... port 1 addr 0 should never happen! ... umass0: BBB bulk-in clear stall failed, TIMEOUT ...
    (freebsd-current)
  • Problem using USB thumbdrive using 5.3-BETA6.
    ... scbus0 on ata0 bus 0: ... port 1 addr 0 should never happen! ... umass0: BBB bulk-in clear stall failed, TIMEOUT ...
    (freebsd-current)