#!/usr/bin/perl # # File : v2.0_enter_telnet_ssh_may.pl # Desc : Spawns multiple Telnet/SSH connections and check for # /etc/sudoers, .rhosts and series of other commands on May Networks # Date : 18-Oct-2003 # Author : Premson Rodriguez # # Added/Modified: # 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 # 16-Dec-2003 # - MAJOR CHANGES : This script was initially coded for Optus Connectivity, # now its modified for connecting to Mayne servers, primarily using telnet. # Most of the above mentioned revisions may or may not be applicable from here onwards # 17-Dec-2003 # - Added tricky prompt check (for some servers which dont prompt properly) # ############################################################################################ 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($pwd4) = ""; my($i, $flag); my($alternateTelnet) = ""; my($last_processed) = -1; my($comm1) = "telnet"; my($comm2) = "ssh -l"; my($comm3) = "ls -l /etc/sudoers"; my($comm4) = "cat /etc/sudoers"; my($comm5) = "grep sys /etc/group"; my($comm6) = "grep ':3:' /etc/passwd"; my($comm7) = "cat /var/adm/sulog | grep -v tsg_cust | grep -v osg_cust"; my($comm8) = "find / -name '.rhosts' -print -exec ls -l {} \\; | grep '^-'"; my($comm9) = "su -"; my($pwd4) = "M1n!takchilewis"; 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) for ($host2) 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) on ($host2)\n"; } else { $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - Unable to get prompt\n"; print "[$host3] - Authentication of user ($user2) on ($host2) Successful\n"; #$exp->log_stdout(0); ########### Added on 6th Nov 2003 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 { #### SU should perform from here ................ if ($exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/)) { print "[$host3] - SU(ing) to ($host3) ...... \n"; print $exp "$comm9\r"; if ($exp->expect(15, '-re', qr/((password\:)|(password\:\ ))/i)) { print "[$host3] - Sending ROOT password for ($host3)\n"; print $exp "$pwd4\r"; }} if ($exp->expect(15, '-re', qr/sorry/i)) { print "[$host3] - ERROR : Unable to SU to ($host3)\n"; die "[$host3] - ERROR : Unable to SU to ($host3)\n"; } elsif ($exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/)) { print "[$host3] - INFO : This login has a TRICKY PROMPT\n"; $exp->log_stdout(1); ########### Enabling Verbose Output print $exp "\r"; print "[$host3] - SU(ing) Successful\n"; if ($exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/)) { print "[$host3] - Executing command ($comm5)\n"; print $exp "$comm5\r"; $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm5) command.\n"; } else { die "[$host3] - ERR expecting prompt.\n"; } print "[$host3] - Executing command ($comm6)\n"; print $exp "$comm6\r"; $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm6) command.\n"; print "[$host3] - Executing command ($comm7)\n"; print $exp "$comm7\r"; $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm7) command.\n"; print "[$host3] - Executing command ($comm8)\n"; print $exp "$comm8\r"; $exp->expect(900, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm8) command.\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] - ($comm3) - DOES NOT EXIST.\n"; $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm3) command.\n"; } else { print "[$host3] - Catting file ($comm3)\n"; $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm3) command.\n"; print "[$host3] - Executing command ($comm4)\n"; print $exp "$comm4\r"; $exp->expect(15, '-re', qr/([\$#>:]$)|([\$#>:] $)/) || die "[$host3] - ERR expecting ($comm4) command.\n"; } ############################################################################################ $exp->log_stdout(0); ########### Disabling Verbose Output 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; /z/ and $pwd4 = $ARGV[$i+1], $last_processed += 2, last SWITCH; } } } } sub usage { print <