1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-27 00:28:10 +00:00
trezor-firmware/python/tools/pwd_reader.py

180 lines
4.9 KiB
Python
Raw Normal View History

2018-02-21 15:45:56 +00:00
#!/usr/bin/env python3
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import hmac
import hashlib
import json
import os
from urllib.parse import urlparse
2018-02-21 15:45:56 +00:00
from trezorlib import misc, ui
2018-02-21 15:45:56 +00:00
from trezorlib.client import TrezorClient
from trezorlib.transport import get_transport
2018-05-28 13:47:57 +00:00
from trezorlib.tools import parse_path
2018-02-21 15:45:56 +00:00
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
# Return path by BIP-32
2018-05-28 13:47:57 +00:00
BIP32_PATH = parse_path("10016h/0")
2018-02-21 15:45:56 +00:00
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
# Deriving master key
def getMasterKey(client):
2018-05-28 13:47:57 +00:00
bip32_path = BIP32_PATH
2018-02-21 15:45:56 +00:00
ENC_KEY = 'Activate TREZOR Password Manager?'
ENC_VALUE = bytes.fromhex('2d650551248d792eabf628f451200d7f51cb63e46aadcbb1038aacb05e8c8aee2d650551248d792eabf628f451200d7f51cb63e46aadcbb1038aacb05e8c8aee')
key = misc.encrypt_keyvalue(
client,
2018-02-21 15:45:56 +00:00
bip32_path,
ENC_KEY,
ENC_VALUE,
True,
True
)
return key.hex()
2018-02-21 15:45:56 +00:00
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
# Deriving file name and encryption key
def getFileEncKey(key):
2018-03-02 15:47:29 +00:00
filekey, enckey = key[:len(key) // 2], key[len(key) // 2:]
2018-02-21 15:45:56 +00:00
FILENAME_MESS = b'5f91add3fa1c3c76e90c90a3bd0999e2bd7833d06a483fe884ee60397aca277a'
digest = hmac.new(str.encode(filekey), FILENAME_MESS, hashlib.sha256).hexdigest()
2018-02-21 15:45:56 +00:00
filename = digest + '.pswd'
return [filename, filekey, enckey]
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
# File level decryption and file reading
def decryptStorage(path, key):
cipherkey = bytes.fromhex(key)
2018-02-21 15:45:56 +00:00
with open(path, 'rb') as f:
iv = f.read(12)
tag = f.read(16)
cipher = Cipher(algorithms.AES(cipherkey), modes.GCM(iv, tag), backend=default_backend())
decryptor = cipher.decryptor()
data = ''
while True:
block = f.read(16)
# data are not authenticated yet
if block:
data = data + decryptor.update(block).decode()
else:
break
# throws exception when the tag is wrong
data = data + decryptor.finalize().decode()
return json.loads(data)
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
def decryptEntryValue(nonce, val):
cipherkey = bytes.fromhex(nonce)
2018-02-21 15:45:56 +00:00
iv = val[:12]
tag = val[12:28]
cipher = Cipher(algorithms.AES(cipherkey), modes.GCM(iv, tag), backend=default_backend())
decryptor = cipher.decryptor()
data = ''
inputData = val[28:]
while True:
block = inputData[:16]
inputData = inputData[16:]
if block:
data = data + decryptor.update(block).decode()
else:
break
# throws exception when the tag is wrong
data = data + decryptor.finalize().decode()
return json.loads(data)
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
# Decrypt give entry nonce
def getDecryptedNonce(client, entry):
print()
print('Waiting for Trezor input ...')
2018-02-21 15:45:56 +00:00
print()
if 'item' in entry:
item = entry['item']
else:
item = entry['title']
pr = urlparse(item)
if pr.scheme and pr.netloc:
item = pr.netloc
ENC_KEY = 'Unlock %s for user %s?' % (item, entry['username'])
ENC_VALUE = entry['nonce']
decrypted_nonce = misc.decrypt_keyvalue(
client,
2018-05-28 13:47:57 +00:00
BIP32_PATH,
2018-02-21 15:45:56 +00:00
ENC_KEY,
bytes.fromhex(ENC_VALUE),
2018-02-21 15:45:56 +00:00
False,
True
)
return decrypted_nonce.hex()
2018-02-21 15:45:56 +00:00
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
# Pretty print of list
def printEntries(entries):
print('Password entries')
print('================')
print()
for k, v in entries.items():
print('Entry id: #%s' % k)
print('-------------')
for kk, vv in v.items():
2018-03-02 15:47:29 +00:00
if kk in ['nonce', 'safe_note', 'password']:
continue # skip these fields
2018-02-21 15:45:56 +00:00
print('*', kk, ': ', vv)
print()
return
def main():
try:
transport = get_transport()
except Exception as e:
print(e)
2018-02-21 15:45:56 +00:00
return
client = TrezorClient(transport=transport, ui=ui.ClickUI())
2018-02-21 15:45:56 +00:00
print()
print('Confirm operation on Trezor')
2018-02-21 15:45:56 +00:00
print()
masterKey = getMasterKey(client)
# print('master key:', masterKey)
fileName = getFileEncKey(masterKey)[0]
# print('file name:', fileName)
home = os.path.expanduser('~')
path = os.path.join(home, 'Dropbox', 'Apps', 'TREZOR Password Manager')
2018-02-21 15:45:56 +00:00
# print('path to file:', path)
encKey = getFileEncKey(masterKey)[2]
# print('enckey:', encKey)
full_path = os.path.join(path, fileName)
2018-02-21 15:45:56 +00:00
parsed_json = decryptStorage(full_path, encKey)
# list entries
entries = parsed_json['entries']
printEntries(entries)
entry_id = input('Select entry number to decrypt: ')
entry_id = str(entry_id)
plain_nonce = getDecryptedNonce(client, entries[entry_id])
pwdArr = entries[entry_id]['password']['data']
2018-03-02 15:47:29 +00:00
pwdHex = ''.join([hex(x)[2:].zfill(2) for x in pwdArr])
print('password: ', decryptEntryValue(plain_nonce, bytes.fromhex(pwdHex)))
2018-02-21 15:45:56 +00:00
safeNoteArr = entries[entry_id]['safe_note']['data']
2018-03-02 15:47:29 +00:00
safeNoteHex = ''.join([hex(x)[2:].zfill(2) for x in safeNoteArr])
print('safe_note:', decryptEntryValue(plain_nonce, bytes.fromhex(safeNoteHex)))
2018-02-21 15:45:56 +00:00
return
2018-03-02 15:47:29 +00:00
2018-02-21 15:45:56 +00:00
if __name__ == '__main__':
main()