This function decrypts a byte array using two XOR and rotation layers:
First Layer: XORs each byte with param_5, then rotates by param_6.
Second Layer: XORs the result with param_3, then rotates by param_4.
Stores Decrypted Byte: Updates the byte array with the final decrypted value.
This function is used to decrypt keys, flags, and taunts in the main function.
Finding the key
The x command in GDB is used for examining memory.
8b specifies that you want to view 8 bytes (the b stands for byte) of memory.
x/8bx tells GDB to show the bytes in hexadecimal format.
0x4010 is the memory address you are examining. You provided the address 0x4010 to GDB to inspect the contents at that specific location in memory.
gef➤ info address encrypted_key
Symbol "encrypted_key" is at 0x4010 in a file compiled without debugging.
Solution
def rotr(byte, shift):
return ((byte >> shift) | (byte << (8 - shift))) & 0xff
def two_layer_decrypt(data, param_3, param_4, param_5, param_6):
decrypted_data = []
for byte in data:
# First layer: XOR with param_5, then rotate right by param_6
bVar1 = rotr(byte ^ param_5, param_6)
# Second layer: XOR with param_3, then rotate right by param_4
uVar2 = rotr(bVar1 ^ param_3, param_4)
decrypted_data.append(uVar2)
return bytes(decrypted_data)
# Encrypted key extracted from memory
encrypted_key = bytes([0x59, 0x78, 0x39, 0x58, 0xd9, 0x19, 0xd8, 0x99])
# Decrypt the key using the parameters from the challenge
decrypted_key = two_layer_decrypt(encrypted_key, 0xaa, 3, 0x5f, 2)
# Print the decrypted key
print(f"Decrypted Key: {decrypted_key.decode()}")
Decrypted Key: elfmagic
Run the binary file and enter the key.
┌──(zwique㉿kali)-[~/Downloads/slv/Shimbles_the_E-L-F]
└─$ ./Shimbles-the-elf
__
.-' |
/ <\|
/ \
|_.- o-o
/ C -._)\
/', |
| `-,_,__,'
(,,)====[_]=|
'. ____/
| -|-|_
|____)_)
Schimbles: Hello, mortal! I am Schimbles, the enchanted gnome.
Schimbles: I've encrypted your precious data and you'll never see it again...
Schimbles: ...unless you can decrypt my file.
Schimbles: Enter the decryption key: elfmagic
Schimbles: Ha! You have bested me!
Schimbles: Here is your flag: BCC{n0t_t0day_e1f}