forked from CooperRS/decrypt-otpauth-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_recovery.py
53 lines (41 loc) · 1.9 KB
/
create_recovery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import click
import hashlib
from bpylist import archiver
from Crypto.Cipher import AES
import otp
import otp.backup
import otp.document
archiver.update_class_map({'NSMutableData': otp.MutableData})
archiver.update_class_map({'NSMutableString': otp.MutableString})
archiver.update_class_map({'ACOTPFolder': otp.OTPFolder})
archiver.update_class_map({'ACOTPAccount': otp.OTPAccount})
def read_archive(encrypted_otpauth_account, key_name: str="Authenticator") -> dict:
# Get IV and key for wrapping archive
iv = bytes(16)
key = hashlib.sha256(key_name.encode('utf-8')).digest()
# Decrypt wrapping archive
data = AES.new(key, AES.MODE_CBC, iv).decrypt(encrypted_otpauth_account.read())
data = data[:-data[-1]]
# Decode wrapping archive
return archiver.Unarchive(data).top_object()
@click.command(name="Create OTP Recovery")
@click.argument("backup_file", type=click.File("rb"))
@click.argument("output_file", type=click.File("wb"))
@click.password_option(confirmation_prompt=False)
def main(backup_file, output_file, password):
''' Opens the OTPAuth backup file given by 'BACKUP_FILE' for reading and writes QR
codes for printing into 'OUTPUT_FILE'. Will prompt for password to decrypt the OTPAuth backup file
if not provided via commandline.
'''
# decode the wrapped archive first.
# This wrapped archive is AES encrypted for some reason with a fixed password.
# after this unarchiving has taken place we can decrypt the actuall contents with the user defindec passwords.
try:
r = read_archive(backup_file, key_name="Authenticator")
except RuntimeError:
r = read_archive(backup_file, key_name="OTPAuth")
accounts = otp.backup.decrypt(r, password)
click.echo(click.style("Success", fg="green") + f": Read OTP auth backup. Storing PDF file to {output_file}")
otp.document.to_pdf(accounts, output_file)
if __name__ == "__main__":
main()