Hacking Linux Exposed

About
Authors
Contents
Reviews
Foreword
Purchase

Articles
Books
Sourcecode
Tools
Errata

Home

 


(view this code in a separate window)

#!/usr/bin/perl
#
# pasv_ports.pl -- determine if an FTP server uses sequential
#                  ports in response to the PASV command
#
# Copyright 2001, Bri Hatch
# Released under the GPL.
#

use FileHandle;
$|=1;

$hostname = shift @ARGV;

$username=shift @ARGV || 'anonymous' if @ARGV;
$password=shift @ARGV || 'mozilla@'  if @ARGV;
        
die "Usage: $0 ftpserver [username [password] ]" if @ARGV or !$hostname;

defined ($pid = open NETCAT, "-|" ) || die "open";

if ( $pid ) {           # parent
    NETCAT->autoflush(1);
    for ( <NETCAT> ) {
        push @ports, $1*256+$2 if /\( \d+,\d+,\d+,\d+, (\d+),(\d+) \)/x;
        #                               IP ADDRESS         PORT
    }
} else {
    open NC, "|nc $hostname 21" or die "Can't fork netcat";
    NC->autoflush(1);

    print NC "USER $username\nPASS $password\n";
    for ( 1..10 ) { sleep 1; print NC "PASV\n"; }
    print NC "QUIT\n";

    close NC;
    exit 0;
}

print "The passive ports opened were:\n@ports\n";