Skip to content

Commit

Permalink
Print better error when decrypting as the wrong type
Browse files Browse the repository at this point in the history
Decrypting a type 4 payload (digimobil style, CBC instead of ECB), using a key instead of S/N, raises an exception. This prints a more user-friendly error, when that's the case.
Closes mkst#18
  • Loading branch information
811Alex authored and mkst committed Oct 5, 2021
1 parent 233fa5a commit 9145371
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions examples/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,52 @@ def main():
print("Signature: " + signature)
payload_type = zcu.zte.read_payload_type(infile)
if payload_type in [2,4]:
if try_all_known_keys:
matched_key = None
start_pos = infile.tell()
for loop_key in zcu.known_keys.get_all_keys():
infile.seek(start_pos)
infile_dec = zcu.encryption.aes_decrypt(infile, loop_key, is_digi)
if zcu.zte.read_payload_type(infile_dec, False) != None:
infile = infile_dec
matched_key = loop_key
break
if matched_key == None:
error("No known key matched.")
else:
print("Matched key: " + matched_key.decode())
else:
if all(b == 0 for b in key):
key = zcu.known_keys.find_key(signature)
if key:
print("Using key: " + key.decode())
try:
if try_all_known_keys:
matched_key = None
start_pos = infile.tell()
for loop_key in zcu.known_keys.get_all_keys():
infile.seek(start_pos)
infile_dec = zcu.encryption.aes_decrypt(infile, loop_key, is_digi)
if zcu.zte.read_payload_type(infile_dec, False) != None:
infile = infile_dec
matched_key = loop_key
break
if matched_key == None:
error("No known key matched.")
else:
error("No known key for this signature, please specify one.")
print("Matched key: " + matched_key.decode())
else:
if all(b == 0 for b in key):
key = zcu.known_keys.find_key(signature)
if key:
print("Using key: " + key.decode())
else:
error("No known key for this signature, please specify one.")
return
infile = zcu.encryption.aes_decrypt(infile, key, is_digi)
if zcu.zte.read_payload_type(infile, False) == None:
error("Malformed decrypted payload, probably used the wrong key!")
check_type(is_digi, payload_type)
return
infile = zcu.encryption.aes_decrypt(infile, key, is_digi)
if zcu.zte.read_payload_type(infile, False) == None:
error("Malformed decrypted payload, probably used the wrong key!")
return
except ValueError as ex:
error("Failed to decrypt payload.")
if check_type(is_digi, payload_type):
raise ValueError(ex)
return
res, _ = zcu.compression.decompress(infile)
outfile.write(res.read())
print("Success!")

def check_type(is_digi, payload_type):
if is_digi and payload_type == 2:
error("Hint: payload type is 2, might need a key instead of a serial number.")
elif not is_digi and payload_type == 4:
error("Hint: payload type is 4, might need a serial number instead of a key.")
else:
return True
return False

def error(err):
print(err, file=sys.stderr)

Expand Down

0 comments on commit 9145371

Please sign in to comment.