Sunday, August 2, 2009

Multi thread in perl,Scalars leaked is occured?

I'm a network administrator,and must scan my office network every week.I must scan ftp weak password and ssh password and resolve hostname,so do this script.But Scalars leaked is occured.Could you help me?





Code is too long to post here,I will post it as a answer.

Multi thread in perl,Scalars leaked is occured?
faint,I can't answer my own question.this is my friend's ID.





#!/usr/bin/perl -w





use strict;


use threads;


use Net::IP;


use Net::FTP;


use Net::SSH::Perl;





######################################...


# global variable


######################################...





my @users = ( 'root','user','ftp' );


my @pwds = ( '123', '12345', 'ftp', 'root' );





my $if_scan_ftp = 'false';


my $if_scan_ssh = 'false';


my @ip_list;





my $max_thread = 30;


my $current_thread = 0;


my @thread_array;





my $time_out = 3;





######################################...


# sub function


######################################...





sub Usage


{


print "\nTest to scan weak password,code by xingguo 060909\n";


print "Usage: scan_pass.pl %26lt;host%26gt; %26lt;option%26gt;\n";


print "Host :\n";


print " -ip use a ip range,just like -ip startip endip\n";


print " -list load ip list from a file,just like -list hostfile.one ip each line,or startip-endip each line\n";


print "option:\n";


print " -ftp scan ftp password\n";


print " -ssh scan ssh password\n";


}





# check arguments





if( @ARGV %26lt; 3 )


{


Usage( );


exit 0;


}





if( ( ($ARGV[0] ne '-ip') %26amp;%26amp; ($ARGV[0] ne '-list') ) )


{


Usage( );


exit 0;


}





foreach my $arg( @ARGV )


{


if( $arg eq '-ftp' )


{


$if_scan_ftp = 'true';


}





if( $arg eq '-ssh' )


{


$if_scan_ssh = 'true';


}


}





print "Load IP list.....\n";





MakeIPList( );





print "Starting scan.....\n";





foreach my $remote_host( @ip_list )


{


if( $current_thread %26gt; $max_thread )


{


foreach my $thread( @thread_array )


{


$thread -%26gt; join( );


}





$current_thread = 0;


}





$thread_array[$current_thread] = threads -%26gt; new( \%26amp;ScanHost, $remote_host );


$current_thread ++;





print "Now thread is $current_thread\n";


}





sub ScanHost


{


my $remote_host = shift;





if( $if_scan_ftp eq 'true' )


{


foreach my $user ( @users )


{


foreach my $pass( @pwds )


{


my $ftp = Net::FTP -%26gt; new( $remote_host, Timeout =%26gt; $time_out );





if( ! $ftp )


{


return -1;


}


my $flag = $ftp -%26gt; login( $user, $pass );





if( $flag )


{


print "Found ftp account from $remote_host: $user/$pass\n";


}


}


}





print "Scan ftp account from $remote_host done.\n";





return 0;


}


}





sub MakeIPList


{


my $start_ip;


my $end_ip;


my $string_ip;





if( $ARGV[0] eq '-ip' )


{


$start_ip = $ARGV[1];


$end_ip = $ARGV[2];





for( my $index = IntIP($start_ip); $index %26lt;= IntIP($end_ip); $index ++ )


{


$string_ip = StringIP( $index );


push( @ip_list, $string_ip );


}


}


elsif( $ARGV[0] eq '-list' )


{


open( RH, "%26lt;$ARGV[1]" ) || die "Open $ARGV[1] error....\n";





while( my $line = %26lt;RH%26gt; )


{


chomp( $line );





if( $line =~ /^(\d+\.\d+\.\d+\.\d+)-(\d+\.\d+\.\d+\.\... )


{


$start_ip = $1;


$end_ip = $2;





for( my $index = IntIP($start_ip); $index %26lt;= IntIP($end_ip); $index ++ )


{


$string_ip = StringIP( $index );


push( @ip_list, $string_ip );


}


}


elsif( $line =~ /^\d+\.\d+\.\d+\.\d+$/ )


{


push( @ip_list, $line );


}


}


}


}





#sub CheckPort


#{


# my $host = shift;


# my $port = shift;


#


# my $sock = IO::Socket::INET-%26gt;new( PeerAddr =%26gt; $host,


# PeerPort =%26gt; $port,


# Timeout =%26gt; $time_out );


#


# $sock -%26gt; close();


#


# return $sock;


#}





sub IntIP


{


my $ip = new Net::IP( shift );





return $ip -%26gt; intip( );


}





sub StringIP


{


my $in = shift;


my $oct = sprintf("%o",$in);





$in=~ s/^0//;





my %scale=( '0'=%26gt;'000',


'1'=%26gt;'001',


'2'=%26gt;'010',


'3'=%26gt;'011',


'4'=%26gt;'100',


'5'=%26gt;'101',


'6'=%26gt;'110',


'7'=%26gt;'111'


);


my @scale = split( "", $oct );


my $bin = "";





foreach( @scale )


{


$bin .= $scale{$_};


}





if( length($bin) %26lt; 32 )


{


my $less = 32 - length($bin);


my $before = '0' x $less;





$bin = $before.$bin;





goto strip;


}


while( length($bin) %26gt; 32 )


{


my @bin_array = split( "", $bin );





my $index;





my $number = @bin_array;





if( $bin_array[0] == 0 )


{


$bin = "";


for( $index = 1; $index %26lt;= $number; $index ++ )


{


$bin = $bin.$bin_array[$index];


}


}


else


{


last;


}


}


goto strip;





strip:





my $bin_ip1 = substr( $bin, 0, 8 );


my $bin_ip2 = substr( $bin, 8, 8 );


my $bin_ip3 = substr( $bin, 16, 8 );


my $bin_ip4 = substr( $bin, 24, 8 );





my $string_ip = b2d( $bin_ip1 ).".".b2d( $bin_ip2 ).".".b2d( $bin_ip3 ).".".b2d( $bin_ip4 );





return $string_ip;


}





sub b2o{


my $in=shift;


my %scale=( '000'=%26gt;'0',


'001'=%26gt;'1',


'010'=%26gt;'2',


'011'=%26gt;'3',


'100'=%26gt;'4',


'101'=%26gt;'5',


'110'=%26gt;'6',


'111'=%26gt;'7'


);





my $out="";


my $tmp="";


my $exitFlag=0;


while(!$exitFlag){


$tmp=substr($in,-3,3);


if(length($tmp)%26lt;2){


$tmp='00'.$tmp;


$exitFlag=1;


}


elsif(length($tmp)%26lt;3){


$tmp='0'.$tmp;


$exitFlag=1;


}


$out=$scale{$tmp}.$out;


$in=substr($in,0,-3);


}


$out;


}





sub o2d{


my $in=shift;


$in=~ s/^0//;$in=~s/^(\d)/0$1/;


my $out;


eval "\$out=sprintf(\"%d\",$in);";


$out;


}





sub b2d{


my $in=shift;


my $out=o2d(b2o($in));


}


No comments:

Post a Comment