#!/bin/bash # # Find linux installs and reset passwords # (c) 2008 Dennis Kaarsemaker # # Instructions: # To recover/reset passwords of an installed linux system, it's easiest to boot # from a live CD and run this script. To do so, follow this list: # - Boot from live cd # - Download this file to the desktop # - Run this command: bash ~/Desktop/reset_passwords # - Follow the instructions presented by the script # # For each user account it finds, it will prompt you to ask whether you want to # change the password. # Bugs: # - Doesn't work if /usr is on a separate filesystem # - Doesn't work for non-lsb distros # Warning, disclaimer if [ "$1" != "--nowarn" ]; then echo -e "\033[1;31mThis tool will let you reset the passwords of any accounts\033[0m" echo -e "\033[1;31mit finds on installed linux systems. Use with care!\033[0m" echo -n "Press enter to continue, or CTRL-C to abort " read discard fi if [ $UID -ne 0 ]; then echo -e "\033[0;37mYou are $USER, not root. Attempting to run sudo\033[0m" exec sudo bash "$0" --nowarn fi # Warn if things are mounted on /mnt mount | grep -q '/mnt' && { echo "A partition has been mounted under /mnt, aborting" echo "To unmount these partitions, run the following commands:" awk '/mnt/{ print "sudo umount " $2}' < /proc/mounts exit; } # Find partitions partitions=$(fdisk -l | sed -n -e 's@^\(/dev/[[:alnum:]]\+\).*@\1@p') # Iterate over partitions for part in $partitions; do echo -e "\033[0;37mTrying partition $part\033[0m" # Mount mount $part /mnt >/dev/null 2>&1 || { echo -e "\033[0;37mCouldn't mount $part\033[0m" continue } # Detect linux if [ ! -e /mnt/etc/shadow ] || [ ! -e /mnt/usr/bin/lsb_release ]; then echo "$part doesn't seem to be a linux rootpartition" umount /mnt continue fi echo -e "\033[4;30mFound the following distribution on $part\033[0m:" chroot /mnt /usr/bin/lsb_release -i -r -c -d echo -ne "\033[4;30mReset passwords in this system\033[0m? [y/n] " read cont case $cont in [yY]) ;; *) echo -e "\033[0;37mSkipping $part\033[0m" umount /mnt continue esac # Detect users with usable passwords users=$(chroot /mnt /usr/bin/passwd -S -a | sed -n -e 's/\([^[:space:]]\+\) P.*/\1/p') # Iterate over them for user in $users; do echo -ne "Reset the password for \033[4;30m$user\033[0m? [y/n] " read input case $input in [yY]) chroot /mnt /usr/bin/passwd $user ;; esac done umount /mnt done