#!/usr/bin/perl # # File : v3.8_enter_telnet_ssh_opt.pl # Desc : Spawns multiple Telnet/SSH connections and check for # /.rhosts, /etc/hosts.equiv and rlogin's existence # Date : 18-Oct-2003 # Author : Premson Rodriguez # # Added: # 27-Oct-2003 # 28-Oct-2003 # - Added log_stdout to extract info on outputs # - Added log_stdout to telnet module to see the error prompted during authentication # - Modified command to include ssh and the path of ssh # - Increased speed of the program by reducing timeouts wherever necessary # 30-Oct-2003 # - Removed Ternary Operators since die was giving problems with outputing messages to logs # 31-Oct-2003 # - Improved code for accurately determining the SSH version # 03-Nov-2003 # - Corrected code for matching incorrect login messages in telnet module # - Improved SSH version determination in SSH Module # 04-Nov-2003 # - Added check for account locked problem in SSH module and finalize an else # - Merged SSH Public Key acceptance and regular SSH login module to a common one # - Added host info in print and die commands for easy filtering # 05-Nov-2003 # - Added Check when permission is denied for a user via SSH # 06-Nov-2003 # - Updated check for accurately checking password prompts during SSH login # so that it does not confuse whenever an expire message encounters # - Added check for determining older version of SSH in SSH and Telnet module # - Added check for checking lifetime expiriy in SSH module # ############################################################################################ use strict; use Expect; &usage unless @ARGV; $|=1; $Expect::Log_Stdout=1; my($stdin) = Expect->exp_init(\*STDIN); my($stdout) = Expect->exp_init(\*STDOUT); my($host1) = ""; my($host2) = ""; my($host3) = ""; my($user1) = ""; my($pwd1) = ""; my($user2) = ""; my($pwd2) = ""; my($user3) = ""; my($pwd3) = ""; my($extra) = "/usr/local/bin/ssh -V ; ssh -V ; /usr/local/sbin/sshd -v ; ps -ef | grep sshd | grep -v grep | awk '{print \$NF}' | sed -n 1p"; my($i, $flag); my($alternateTelnet) = ""; my($last_processed) = -1; my($comm1) = "telnet"; my($comm2) = "ssh -l"; my($comm3) = "ls -l /.rhosts"; my($comm4) = "cat /.rhosts"; my($comm5) = "ls -l /etc/hosts.equiv"; my($comm6) = "cat /etc/hosts.equiv"; my($comm7) = "cat /etc/inetd.conf | grep 'rlogin'"; my($comm8) = "exit"; my($comm9) = "su -"; my($pwd4) = "pASSwORD"; my($space) = " "; &parseCmdLine; &connect; sub connect { (my $exp=Expect->spawn("$comm1$space$host1")) ? (print "[$host3] - Entering ($host1)\n") : (die "[$host3] - ERR: Spawn failed: $?\n"); #$exp->exp_internal(1); ## Use this only when you need extreme verbose $exp->log_stdout(0); ($exp->expect(15, '-re', qr/((login\:)|(login\:\ ))/i)) ? (print "[$host3] - Entering Login Info\n") : (die "[$host3] - ERR: Telnet Failed on ($host1)\n"); print "[$host3] - Authenticating username ($user1)\n"; print $exp "$user1\r"; #$exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting username\n"; ($exp->expect(15, '-re', qr/((Password\:)|(Password\:\ ))/i)) ? (print "[$host3] - Entering User Info\n") : (die "[$host3] - ERR: Error expecting ($user1)\n"); print "[$host3] - Authenticating password\n"; print $exp "$pwd1\r"; ($exp->expect(15, '-re', qr/^login\:/i)) ? (die "[$host3] - ERR in authenticating user ($user1)\n") : ($exp->expect(15, '-re', qr'[$#>:] $')); print "[$host3] - Authentication of user ($user1) Successful\n"; print "[$host3] - Executing command ($comm2$space$user2$space$host2)\n"; print $exp "$comm2$space$user2$space$host2\r"; ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) ? (print "[$host3] - Prompted for Password by ($user2)\n") : (die "[$host3] - ERR: Error expecting ($user2)\n"); print "[$host3] - Sending Password for user ($user2)\n"; print $exp "$pwd2\r"; if ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) { die "[$host3] - ERR: Authentication Failed for ($user2)\n"; } else { $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - Unable to get prompt\n"; print "[$host3] - Authentication of user ($user2) Successful\n"; } print "[$host3] - Executing third command ($comm2$space$user3$space$host3) from host ($host2)\n"; print $exp "$comm2$space$user3$space$host3\r"; if ($exp->expect(15, '-re', qr/(yes\/no)/i)) { print "[$host3] - SSH Public Key ... accepting connection for ($user3)\n"; print "[$host3] - Prompted (first time) for Password by ($user3)\n"; print $exp "yes\r"; } if ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) { print "[$host3] - Prompted for Password by ($user3)\n"; print "[$host3] - Sending Password for user ($user3)\n"; print $exp "$pwd3\r"; if ($exp->expect(15, '-re', qr/old/i)) { print "[$host3] - SSH ERR: Prompted for Password Change .... please change and come back\n"; die "[$host3] - EXITING PROGRAM : SSH ERR: Prompted for Password Change .... please change and come back\n"; } elsif ($exp->expect(15, '-re', qr/Permission denied/i)) { print "[$host3] - SSH ERR: Permission denied ... please check and come back\n"; die "[$host3] - EXITING PROGRAM : SSH ERR: Permission Denied .... please check and come back\n"; } elsif ($exp->expect(15, '-re', qr/disabled/i)) { print "[$host3] - SSH ERR: Account is disabled .... please change and come back\n"; die "[$host3] - EXITING PROGRAM : SSH ERR: Account is disabled .... please change and come back\n"; } elsif ($exp->expect(15, '-re', qr/password lifetime/i)) { print "[$host3] - SSH ERR: Password Lifetime has passed .... please change and come back\n"; die "[$host3] - EXITING PROGRAM : SSH ERR: Password Lifetime has passed .... please change and come back\n"; } elsif ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) { print "[$host3] - SSH ERR: Incorrect password .... please change and come back\n"; die "[$host3] - EXITING PROGRAM : SSH ERR: Incorrect password .... please change and come back\n"; } elsif ($exp->expect(15, '-re', qr/locked/i)) { print "[$host3] - SSH ERR: Account is Locked .... please unlock and come back\n"; die "[$host3] - EXITING PROGRAM : SSH ERR: Account is Locked .... please unlock and come back\n"; } elsif ($exp->expect(15, '-re', qr/cdun1410.optus.com.au>/i)) { print "[$host3] - SSH ERR: Unable to Login ($host3) .... please investigate\n"; $alternateTelnet = "2"; } else { $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting shell\n"; } if ($alternateTelnet != "2") { $exp->log_stdout(1); ########### Added on 27th Oct 2003 print "[$host3] - Authentication of user ($user3) Successful\n"; #print "[$host3] - Executing command ($extra)\n"; print "[$host3] - Executing command ($comm3)\n"; print $exp "$comm3\r"; if ($exp->expect(15, '-re', qr/((no such file)|(not found))/i)) { print "[$host3] - (/.rhosts) - DOES NOT EXIST.\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } else { print "[$host3] - Catting file (/.rhosts)\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting third command.\n"; print "[$host3] - Executing command ($comm4)\n"; print $exp "$comm4\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } print "[$host3] - Executing command ($comm5)\n"; print $exp "$comm5\r"; if ($exp->expect(15, '-re', qr/((no such file)|(not found))/i)) { print "[$host3] - (/etc/hosts.equiv) - DOES NOT EXIST.\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } else { print "[$host3] - Catting file (/etc/hosts.equiv)\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting third command.\n"; print "[$host3] - Executing command ($comm6)\n"; print $exp "$comm6\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } print "[$host3] - Executing command ($comm7)\n"; print $exp "$comm7\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; $exp->log_stdout(0); ########### Added on 27th Oct 2003 $alternateTelnet = "1"; }} else { print "[$host3] - SSH ERR: Error Connecting host ($host3) using user ($user3)\n"; $alternateTelnet = "2"; } if ($alternateTelnet == 2) { $exp->log_stdout(0); ########### Added on 6th Nov 2003 $exp->expect(15, '-re', qr'[$#>:] $') || print "[$host3] - SSH ERR: No response through SSH for ($host3)\n"; print "[$host3] - Trying Telnet ($comm1$space$host3)\n"; print $exp "$comm1$space$host3\r"; if ($exp->expect(15, '-re', qr/unknown/i)) { print "[$host3] - TELNET ERR: Unknown Host\n"; die "[$host3] - EXITING PROGRAM : TELNET ERR: Unknown Host\n"; } if ($exp->expect(15, '-re', qr/((login\:)|(login\:\ ))/i)) { print "[$host3] - Entering Login Info\n"; } else { print "[$host3] - EXITING PROGRAM : ERR: Telnet not responding for host : ($host3)\n"; die "[$host3] - ERR: Telnet not responding for host : ($host3)\n"; } print "[$host3] - Authenticating username ($user3) through telnet\n"; print $exp "$user3\r"; if ($exp->expect(15, '-re', qr/((Password\:)|(Password\:\ ))/i)) { print "[$host3] - Entering User Info\n"; } else { print "[$host3] - ERR: Error expecting password for ($user3)\n"; die "[$host3] - EXITING PROGRAM ERR: Error expecting password for ($user3)\n"; } print "[$host3] - Authenticating password\n"; print $exp "$pwd3\r"; if ($exp->expect(15, '-re', qr/((^login\:)|(^login\:\ )|(password\:)|(password\:\ )|(incorrect))/i)) { print "[$host3] - TELNET ERR : Incorrect user or password for ($user3).. \n"; die "[$host3] - EXITING PROGRAM : TELNET ERR .... incorrect user or password\n"; } elsif ($exp->expect(15, '-re', qr/disabled/i)) { print "[$host3] - TELNET ERR in authenticating user ($user3) ... account is disabled\n"; die "[$host3] - EXITING PROGRAM : TELNET ERR ... account is disabled\n"; } elsif ($exp->expect(15, '-re', qr/locked/i)) { print "[$host3] - TELNET ERR in authenticating user ($user3) ... account is locked\n"; die "[$host3] - EXITING PROGRAM : TELNET ERR ... account is locked\n"; } else { $exp->expect(15, '-re', qr'[$#>:] $'); } $exp->log_stdout(1); ########### Added on 27th Oct 2003 print "[$host3] - Authentication of user ($user3) Successful\n"; #print "[$host3] - Executing command ($extra)\n"; print "[$host3] - Executing command ($comm3)\n"; print $exp "$comm3\r"; if ($exp->expect(15, '-re', qr/((no such file)|(not found))/i)) { print "[$host3] - (/.rhosts) - DOES NOT EXIST.\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } else { print "[$host3] - Catting file (/.rhosts)\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting third command.\n"; print "[$host3] - Executing command ($comm4)\n"; print $exp "$comm4\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } print "[$host3] - Executing command ($comm5)\n"; print $exp "$comm5\r"; if ($exp->expect(15, '-re', qr/((no such file)|(not found))/i)) { print "[$host3] - (/etc/hosts.equiv) - DOES NOT EXIST.\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } else { print "[$host3] - Catting file (/etc/hosts.equiv)\n"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting third command.\n"; print "[$host3] - Executing command ($comm6)\n"; print $exp "$comm6\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; } print "[$host3] - Executing command ($comm7)\n"; print $exp "$comm7\r"; $exp->expect(15, '-re', qr'[$#>:] $') || die "[$host3] - ERR expecting rhosts listing.\n"; $exp->log_stdout(0); ########### Added on 27th Oct 2003 $alternateTelnet = "0"; die "[$host3] - Task Complete .... EXITING PROGRAM.\n"; } return 0; } sub parseCmdLine { foreach $i (0...$#ARGV) { if ( $ARGV[$i] =~ /(^\-(\w)$)/ ) { $flag = $1; $_ = $flag; SWITCH: { /i/ and &usage, last SWITCH; /s/ and $host1 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /d/ and $host2 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /r/ and $host3 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /u/ and $user1 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /p/ and $pwd1 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /n/ and $user2 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /o/ and $pwd2 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /l/ and $user3 = $ARGV[$i+1], $last_processed += 2, last SWITCH; /m/ and $pwd3 = $ARGV[$i+1], $last_processed += 2, last SWITCH; } } } } sub usage { print <