C
Chad Brown
I put together a script for scaning a network. Features are DNS
resolution, selective port scan, scanning of multiple addresses at one
time, and ping sweep. Ports can be customized depending on what is
being sought on a network. If anyone decides to add more ideas to this
please send me a copy. Im very interested in input.
http://onager.guild.net/~vrai/
(e-mail address removed)
#!/usr/bin/perl
#usage--> netsweep 20.0.0.1
use Socket;
use Net:
ing;
@ports = (21,22,23,80,110,119,1080,8080);
$Max_Processes = 20;
$Target_IP = $ARGV[0];
#---Startup
if ( $Target_IP eq "" ) {
die "need target ip...\n";
}
@Target_IP_Sep = IP_Seperate($Target_IP);
$TA = @Target_IP_Sep[0];
$TB = @Target_IP_Sep[1];
$TC = @Target_IP_Sep[2];
$Start_Addy = $TA.".".$TB.".".$TC.".0";
$End_Addy = $TA.".".$TB.".".$TC.".255";
$Current_Long = Dot2Long_IP($Start_Addy);
$End_Long = Dot2Long_IP($End_Addy);
#---Main workload control routines
$stp = 0;
$npids = 0;
while () {
#forker
FORK: {
if ($pid=fork) {
#return $pids;
}
elsif (defined $pid) {
Connect_IP(Long2Dot_IP($Current_Long));
}
elsif ($! == EAGAIN) {
sleep 3;
redo FORK;
}
else {
die "cant fork!\n";
}
}
#fork control
$npids++;
if($npids>=$Max_Processes){
$wait_ret=wait();
if($wait_ret>0){
$npids--;
}
}
#iterate to next IP
$Current_Long++;
#look for end IP
if ($Current_Long eq $End_Long+1) {
sleep(2);
exit(0);
};
}
#---Sub Functions
sub Dot2Long_IP {
return unpack('N', inet_aton(shift));
}
sub Long2Dot_IP {
return inet_ntoa(pack('N', shift));
}
sub IP_Seperate {
my ($IP_Address) = @_ ;
@IP_SepArr = split(/\./,$IP_Address);
return @IP_SepArr;
}
sub Connect_IP($IP_Addy){
my ($tghost) = @_ ;
$connect_list = "";
$connect_list = $tghost;
#getting host name
$host_ipaddr = inet_aton($tghost);
$host_name = gethostbyaddr($host_ipaddr, AF_INET);
if ($host_name eq "") {
$host_name = "NR";
}
$connect_list = $connect_list." $host_name";
#pinging target
$p = Net:
ing->new("icmp");
if ($p->ping($tghost)) {
$connect_list = $connect_list." TG_A"; #for returns
}
else {
$connect_list = $connect_list." TG_N"; #for negatives
}
$p->close();
#start scan on ports
foreach $port (@ports) {
$AF_INET=2;
$SOCK_STREAM=1;
$sockaddr='S n a4 x8';
($name,$aliases,$proto)=getprotobyname('tcp');
($name,$aliases,$type,$len,$thataddr)=gethostbyname($tghost);
$this=pack($sockaddr,$AF_INET,0,$thisaddr);
$that=pack($sockaddr,$AF_INET,$port,$thataddr);
die "unknown host $tghost\n" if($thataddr eq "");
socket(S,$AF_INET,$SOCK_STREAM,$proto) or die $!;
bind(S,$this) or die $!;
if(connect(S,$that)) {
$connect_list = $connect_list." ".$port;
close(S)
}
else {
close(S);
}
}
print "$connect_list\n";
$connect_list = "";
exit();
}
resolution, selective port scan, scanning of multiple addresses at one
time, and ping sweep. Ports can be customized depending on what is
being sought on a network. If anyone decides to add more ideas to this
please send me a copy. Im very interested in input.
http://onager.guild.net/~vrai/
(e-mail address removed)
#!/usr/bin/perl
#usage--> netsweep 20.0.0.1
use Socket;
use Net:
@ports = (21,22,23,80,110,119,1080,8080);
$Max_Processes = 20;
$Target_IP = $ARGV[0];
#---Startup
if ( $Target_IP eq "" ) {
die "need target ip...\n";
}
@Target_IP_Sep = IP_Seperate($Target_IP);
$TA = @Target_IP_Sep[0];
$TB = @Target_IP_Sep[1];
$TC = @Target_IP_Sep[2];
$Start_Addy = $TA.".".$TB.".".$TC.".0";
$End_Addy = $TA.".".$TB.".".$TC.".255";
$Current_Long = Dot2Long_IP($Start_Addy);
$End_Long = Dot2Long_IP($End_Addy);
#---Main workload control routines
$stp = 0;
$npids = 0;
while () {
#forker
FORK: {
if ($pid=fork) {
#return $pids;
}
elsif (defined $pid) {
Connect_IP(Long2Dot_IP($Current_Long));
}
elsif ($! == EAGAIN) {
sleep 3;
redo FORK;
}
else {
die "cant fork!\n";
}
}
#fork control
$npids++;
if($npids>=$Max_Processes){
$wait_ret=wait();
if($wait_ret>0){
$npids--;
}
}
#iterate to next IP
$Current_Long++;
#look for end IP
if ($Current_Long eq $End_Long+1) {
sleep(2);
exit(0);
};
}
#---Sub Functions
sub Dot2Long_IP {
return unpack('N', inet_aton(shift));
}
sub Long2Dot_IP {
return inet_ntoa(pack('N', shift));
}
sub IP_Seperate {
my ($IP_Address) = @_ ;
@IP_SepArr = split(/\./,$IP_Address);
return @IP_SepArr;
}
sub Connect_IP($IP_Addy){
my ($tghost) = @_ ;
$connect_list = "";
$connect_list = $tghost;
#getting host name
$host_ipaddr = inet_aton($tghost);
$host_name = gethostbyaddr($host_ipaddr, AF_INET);
if ($host_name eq "") {
$host_name = "NR";
}
$connect_list = $connect_list." $host_name";
#pinging target
$p = Net:
if ($p->ping($tghost)) {
$connect_list = $connect_list." TG_A"; #for returns
}
else {
$connect_list = $connect_list." TG_N"; #for negatives
}
$p->close();
#start scan on ports
foreach $port (@ports) {
$AF_INET=2;
$SOCK_STREAM=1;
$sockaddr='S n a4 x8';
($name,$aliases,$proto)=getprotobyname('tcp');
($name,$aliases,$type,$len,$thataddr)=gethostbyname($tghost);
$this=pack($sockaddr,$AF_INET,0,$thisaddr);
$that=pack($sockaddr,$AF_INET,$port,$thataddr);
die "unknown host $tghost\n" if($thataddr eq "");
socket(S,$AF_INET,$SOCK_STREAM,$proto) or die $!;
bind(S,$this) or die $!;
if(connect(S,$that)) {
$connect_list = $connect_list." ".$port;
close(S)
}
else {
close(S);
}
}
print "$connect_list\n";
$connect_list = "";
exit();
}