Skip to content

Instantly share code, notes, and snippets.

@HexF
Created October 16, 2023 10:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HexF/81f027aeaa250f38e7a1f8740ab3d1e6 to your computer and use it in GitHub Desktop.
Save HexF/81f027aeaa250f38e7a1f8740ab3d1e6 to your computer and use it in GitHub Desktop.
DLink HDR4 Decryptor
# awful code for decrypting DLink TCLinux firmware files
# requires pycryptodome
import struct
import binascii
from Crypto.Cipher import AES
KEY = bytes.fromhex("11223344556677889900aabbccddeeff")
IV = bytes.fromhex("a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5")
f = open("enc.bin", "rb").read()
assert f[:4] == b'4RDH', "unsupported file"
magic, flen, crc32 = struct.unpack(">LxxxxLL", f[:16])
print(f"{magic=:X} {flen=:X} {crc32=:X}")
cipher = AES.new(KEY, AES.MODE_CBC, iv=IV)
dec = cipher.decrypt(f[0x100:flen - (flen % 16) + 16])
dec = dec[:flen-0x100]
calc_crc = binascii.crc32(dec)
calc_crc ^= 0xa5a5a5a5 ^ 0xFFFFFFFF
print(f"{calc_crc=:X} {crc32^calc_crc=:X}")
assert crc32 == calc_crc, "invalid crc"
open("dec.bin", "wb").write(f[:0x100] + dec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment