art3m1sm00n
art3m1sm00n

Reputation: 409

How to replace a password in /etc/shadow using bash script?

For my security class we are supposed to use a badly written file copying program to gain a root shell. We are supposed to write our hack into a script. I know how to get access to the /etc/shadow file but I am not sure how, from within a bash script, to search for the root password and replace it with no password. I know the format of the shadow file, username:password:etc, but is it possible in a script to find where the password is and replace it?

I have found the command sed but that requires me knowing what the old password is. Is there another command I could use before that to grab the password out?

Note: I am working with a copy of /etc/shadow that I can then overwrite the original with

Upvotes: 0

Views: 5388

Answers (2)

Paolinux
Paolinux

Reputation: 177

To avoid parsing and using python/perl/awk/.... a possibile solution is to use the usermod command in this way:

usermod -R /var/lib/lxc/mycontainer/rootfs -p `mkpasswd -m sha-512 lol` root

In the above example I specified a different chroot (-R) and i used lol as password.

Upvotes: 1

candymanuu
candymanuu

Reputation: 110

This is your root hash

pass=`cat /etc/shadow | grep root| awk -F: '{print $2}'`

Upvotes: 1

Related Questions