Skip to content

Instantly share code, notes, and snippets.

@foursixnine
Last active April 2, 2017 20:16
Show Gist options
  • Save foursixnine/16d001f555e285d03d9d29ccbae01a5d to your computer and use it in GitHub Desktop.
Save foursixnine/16d001f555e285d03d9d29ccbae01a5d to your computer and use it in GitHub Desktop.
Add LUKS passphrase without knowing the actual password
# This will work if you have still access to the partition
# and of course, you need root access for this :)
export firstpassword="This is the old password"
export new_password="This is the new password"
export loopdev=/dev/loop0
export device=/tmp/$(date +%Y%M%d)-testdev.img
export volume_name=/dev/mapper/crypt_fun
dd if=/dev/zero of=testdev bs=1M count=500
losetup $loopdev $device
echo -n "$firstpassword" | cryptsetup luksFormat -c blowfish -s 256 $loopdev -
echo -n "$firstpassword" | cryptsetup luksOpen $loopdev crypt_fun
mkfs.btrfs $volume_name
dmsetup table --showkeys $volume_name | awk '{ print $5 }' | xxd -r -p > data.bin
# (Type $new_password)
echo -n "$new_password" | cryptsetup luksAddKey $device --master-key-file <(cat data.bin)
# Verify
echo -n "$new_password" | cryptsetup -v open $loopdev $volume_name testcrypt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment