Files
elysium/tests/test_decrypt.py
T
tomas.kracmar 89c5e21002 fix(decrypt): read ELY2 and use PBKDF2-SHA256 to match the PowerShell side
decrypt.py could not decrypt any current export:
- It did not know the ELY2 format introduced in v2.4.6, so ELY2 files
  fell through to the legacy branch and failed.
- Its ELY1 branch called pycryptodome's PBKDF2 with the default PRF
  (HMAC-SHA1), while Extract-NTHashes.ps1 has used HMAC-SHA256 since
  ELY1 was introduced, so ELY1 files never decrypted either.

Now reads ELY2 (HMAC-SHA256 verified before decrypting, keys taken
from one 64-byte PBKDF2 output exactly like the two sequential
Rfc2898DeriveBytes.GetBytes(32) calls), ELY1, and the legacy format.
Key derivation uses stdlib hashlib.pbkdf2_hmac with the passphrase
encoded as UTF-8, matching .NET's string overload (pycryptodome
defaulted to latin-1). Padding is checked with Crypto.Util.Padding
instead of a length-byte range check.

Also: the passphrase prompt no longer echoes (getpass), an input path
not ending in .enc is refused instead of being overwritten by the
output, and failures exit non-zero.

tests/test_decrypt.py encrypts with the real Protect-FileWithAES
(extracted from Extract-NTHashes.ps1's AST, so the script body never
runs) via pwsh and decrypts with decrypt.py: ASCII and non-ASCII
passphrases round-trip; a wrong passphrase and a flipped ciphertext
bit are rejected by the HMAC check. With the old SHA1 derivation
patched back in, the test fails.
2026-11-04 09:24:37 +01:00

57 lines
2.3 KiB
Python

# ELY2 round trip: encrypt with the real Protect-FileWithAES from Extract-NTHashes.ps1 (via pwsh),
# decrypt with decrypt.py. Catches any drift between the two sides of the format.
# Run: python3 tests/test_decrypt.py (needs pwsh on PATH and pycryptodome)
import os
import subprocess
import sys
import tempfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
import decrypt # noqa: E402
# Loads only the function definition from the script's AST, so the script body never runs.
ENCRYPT_PS1 = r'''
param($Script, $In, $Out, $Pass)
$ErrorActionPreference = 'Stop'
$ast = [System.Management.Automation.Language.Parser]::ParseFile($Script, [ref]$null, [ref]$null)
$fn = $ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Protect-FileWithAES' }, $true)
. ([scriptblock]::Create($fn.Extent.Text))
Protect-FileWithAES -InputFile $In -OutputFile $Out -Passphrase $Pass 6>$null
'''
def encrypt_with_powershell(plaintext: bytes, passphrase: str) -> bytes:
with tempfile.TemporaryDirectory() as d:
src, dst, ps1 = (os.path.join(d, n) for n in ('in.zip', 'out.enc', 'encrypt.ps1'))
with open(src, 'wb') as f:
f.write(plaintext)
with open(ps1, 'w', encoding='utf-8') as f:
f.write(ENCRYPT_PS1)
subprocess.run(['pwsh', '-NoProfile', '-File', ps1,
'-Script', os.path.join(ROOT, 'Extract-NTHashes.ps1'),
'-In', src, '-Out', dst, '-Pass', passphrase], check=True)
with open(dst, 'rb') as f:
return f.read()
def assert_rejected(blob: bytes, passphrase: str):
try:
decrypt.decrypt(blob, passphrase)
except ValueError as e:
assert 'HMAC' in str(e), e
else:
raise AssertionError('tampered/wrong-passphrase input was accepted')
plaintext = os.urandom(1000)
for passphrase in ('correct horse battery staple', 'pässwörd-€'): # ASCII + non-ASCII
blob = encrypt_with_powershell(plaintext, passphrase)
assert blob[:4] == b'ELY2', blob[:4]
assert decrypt.decrypt(blob, passphrase) == plaintext, passphrase
assert_rejected(blob, 'wrong passphrase')
assert_rejected(blob[:40] + bytes([blob[40] ^ 1]) + blob[41:], passphrase) # flipped ciphertext bit
print('ok')