Luks Add Key With Masterkey

Today I realized that I forgot the password to decrypt one of my hosts encrypted using Luks. Luckily it was still running and root-access still granted.

As you may know root can do anything, even reading the masterkey of your unlocked Luks-devices.

By using

dmsetup table /dev/mapper/supercrypt --showkeys

you can get hold of the masterkey.

Since you only unlock the masterkey with your password, this information can be used to modify every slot.

In my case adding a new password to a new slot saved me from losing the content of this host:

cryptsetup luksAddKey /dev/sdc --master-key-file <(dmsetup table --showkey /dev/mapper/supercrypt | awk '{print$5}' | xxd -r -p)

Found in the redhat knowledgebase.