Decrypting D-Link Encrypted Firmware (SHRS)

 

Introduction

 D-Link firmware with SHRS magic bytes contain firmware encrypted using AES 128 CBC with key as 0xC05FBF1936C99429CE2A0781F08D6AD8. Original firmware can be obtained by extracting the encrypted block from the firmware and then decrypting using this key.

Header Structure

Header structure of firmware along with corresponding values for a sample firmware DIR-867_FW1.30B07.bin is given in table.

OffsetSize (Bytes)ItemValue (DIR-867_FW1.30B07)
04Magic BytesSHRS
44Decrypted FW Size0x9D2AF9
84Encrypted Block Size0x9D2B00
0xC16IV0x67C6697351FF4AEC29CDBAABF2FBE346
0x1C64SHA512(Decrypted FW + Key)0x7139.......AA94
0x5C64SHA512(Decrypted FW)0xDAC3.......5DA7
0x9C64SHA512(Encrypted Block)0x7D3F.......12D2
0xDC512Unused00
0x2DC512Decrypted Block Signature0x0253.......F178
0x4DC512Encrypted Block Signature0x81D4.......7383

Encrypted block starts at offset 0x6DC.

1. Extract encrypted block:
dd iflag=skip_bytes,count_bytes if=enc_fw.bin of=enc_block.bin skip=$((0x6DC)) count=$((0x009d2b00))

2. Verify SHA512 of enc_block.bin is same as that in header.
sha512sum enc_block.bin

3. Decrypt encrypted block:
openssl aes-128-cbc -d -p -nopad -nosalt -K "c05fbf1936c99429ce2a0781f08d6ad8" -iv "67c6697351ff4aec29cdbaabf2fbe346" -in enc_block.bin -out dec_block.bin

4. Get original decrypted firmware:
dd iflag=skip_bytes,count_bytes if=dec_block.bin of=dec_fw.bin count=$((0x009D2AF9))

5. Verify SHA512 of decrypted firmware is same as that in header.
sha512sum dec_fw.bin

6. Append encryption key to decrypted firmware and calculate SHA512 and verify with header.
cp dec_fw.bin dec_fw_plus_key.bin

perl -e 'print pack "H*", "c05fbf1936c99429ce2a0781f08d6ad8"'>> dec_fw_plus_key.bin

sha512sum dec_fw_plus_key.bin

7. Extract filesytem, copy /etc_ro/public.pem and change its format:
binwalk -eM dec_fw.bin

openssl rsa -RSAPublicKey_in -in public.pem -out publickey -pubout

8. Extract signature of encrypted block:
dd iflag=skip_bytes,count_bytes if=enc_fw.bin of=enc_block.signature skip=$((0x4DC)) count=512

9. Verify the signature of encrypted block:
openssl dgst -sha512 -verify publickey -signature enc_block.signature enc_block.bin

10. Extract signature of decrypted firmware:
dd iflag=skip_bytes,count_bytes if=enc_fw.bin of=dec_fw.signature skip=$((0x2DC)) count=512

11. Verify the signature of decrypted firmware:
openssl dgst -sha512 -verify publickey -signature dec_fw.signature dec_fw.bin

Demo



Comments

  1. whare did you get this key from, ...can you brother explain to us how to extract such key with more explanation ,please

    ReplyDelete
  2. 0xC05FBF1936C99429CE2A0781F08D6AD8.

    ReplyDelete
  3. Hi, can you decrypt files encrypt with Cr1pt0r? Thanks.

    ReplyDelete
  4. plese need help about dlink 2750u firmware

    ReplyDelete

Post a Comment

Popular posts from this blog

Decrypt TP-Link config.bin Configuration Backup File

Firmadyne Installation & Emulation of Firmware

Extract / Create Cramfs File System from Ubuntu 20.04