perlterminalpromptinteractivenon-interactive

How to enter/answer a terminal prompt from a Perl script?


I'm trying to crack a forgotten password for a luks partition. I generated a list of combinations, and now I'm trying to decrypt the volume from a Perl script.

The problem is to enter the prompt from the script itself, since: system('cryptsetup', ('open', '/dev/sdd1', 'crypt-vol', '--type=luks')) just spits Enter passphrase for /dev/sdd1 and waits for me to enter it manually.

How can I approach this?

Many thankyous for any help.

* it's my volume and I haven't forgotten the password completely, so I created the list of combinations provided that I remember some details. It's like >6k of possibilities, so it should be feasible to break it.


Solution

  • Don't, use a 'keyfile' with cryptsetup. A key file can be STDIN.

    So:

    echo "passphrase_here" | cryptsetup -d - <other  options>
    

    In perl you could use IPC::Run2 for this, which allows you read/write to the FH, but if you just need a return code to test the passphrase, that's not needed.

    E.g. https://blog.sleeplessbeastie.eu/2019/03/27/how-to-test-luks-passphrase/

    So:

    open ( my $crypter, '|-', "cryptsetup luksOpen -d - --test-passphrase " )
    
    print {$crypter} "Your passphrase";
    
    close ( $crypter );
    print "Got return code of $?"