Linux – Cracking root user: Automating logging into root using “su” from nonroot user

expectlinuxpasswordroot

I want to write a script that allows me to pass a password to the su command. The reason for this is to automate the process of logging into root from nonroot account. From my basic understanding of security, I know the dangers of this. I still need this to try to crack the root user password by reading passwords from a file.

I have heard about using the expect command to automate interactions but I do not know how it works, or if it is applicable in this situation. Any information about using expect would be greatly appreciated.

Please note that sudo is not an option as my user is not on the sudoer list.

Things I've tried
– Passing the password in using pipes
Didn't work because of "su must be used from terminal" type error
– Passing the password using echo
Whatever was echoed would simply show up after the password was entered

Best Answer

Doing this "oneline style"

expect -c 'spawn su - ;expect Password:;send "your-password\n";interact'

Take a look at the output of su - to guarantee if this is the right phrase that appears to capture a password. I'm using a translated version of Linux here so, the word would be "Senha:" instead of "Password:".

  • expect -c '': Run the following actions to this command
  • spawn su -: Execute this command
  • expect Password: Expect the word password to do something
  • send "your-password\n": Type in your password, followed by an Enter
  • interact: Keep interaction ready to the command.
Related Question