KCTF{w3Lc0m3_T0_tHE_r3_w0rLD}Name: rem3
Category: Reverse Engineering
Flag Format: KCTF{flag}
We are given an ELF 64-bit binary named rem3.ks. Let's start by running file and strings:
$ file rem3.ks
rem3.ks: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=..., for GNU/Linux 3.2.0, stripped
Running strings reveals some interesting fake flags and messages:
=== KCTF Reverse Challenge ===
Congrats — you got a flag! But... not sure you'll get points for it :)
Enter flag:
Failed!
Success! Real flag accepted.
KCTF{fake_flag_for_reversers}
KCTF{hash_passes_but_fake!!!}
KCTF{str1ngs_lie_dont_trust!}
These strings suggest that there are decoys. Indeed, running the program and entering these flags results in "Failed!" or a misleading success message.
We disassembled the binary using objdump -d -M intel rem3.ks. The main function (or the function calling the main logic) is somewhat obscured, but we identified the interesting logic starting around offset 0x10c0.
The program:
fgets.0x1d).memcmp against a hardcoded string KCTF{fake_flag_for_reversers} (at offset 0x20f0). If it matches, it prints the "Congrats" message but this is the fake path.0x1470 to verify the real flag.The function at 0x1470 performs a complex encryption on the input string and compares it against a 29-byte target buffer stored in .rodata.
The encryption loop runs 29 times (once for each character). It maintains a state variable ESI which changes every iteration.
The constants used are:
r10 = 0x2f910ed35ca71942r9 = 0x6a124de908b17733esi starts at 0xffffffc3 (-61)The target bytes (29 bytes) can be extracted from the binary:dc 6b bb 4d fd 25 e4 7e c3 26 f5 72 ab 96 fc 8d 55 10 93 c1 fd 81 46 5b 7e 33 83 8f 2f
For each index i from 0 to 28:
T2 = ((r10 >> ((i%8)*8)) + (i*29)) & 0xFFFFFFFFinput_byte is XORed with the lowest byte of T2.rol) by a count derived from r9.esi is added to the 32-bit accumulator.r9.ror) by the lowest byte of esi (note: esi at this point, not the initial one).This produces the encrypted byte for that position.
To recover the flag, we need to reverse the operations. However, since the state esi is updated based on the result of the operations on the correct input, we can't simply run it backward without knowing the correct esi sequence.
Wait! The esi update depends on eax which depends on the input. But actually, we know the target values! The target values are the results of the encryption.
So we can reverse step-by-step for each character:
target[i].ror(target[i], esi). Note: The forward code did ror(al, esi), so we reverse with rol(val, esi).r9.esi.rol) by using ror.T2.This gives us the input character guess[i].
Crucially, after finding the input character, we must simulate the forward pass to calculate the correct next state for esi so we can decrypt the next character.
import struct
def rol(val, r_bits, max_bits=8):
return (val << r_bits%max_bits) & (2**max_bits - 1) | \
((val & (2**max_bits - 1)) >> (max_bits - (r_bits%max_bits)))
def ror(val, r_bits, max_bits=8):
return ((val & (2**max_bits - 1)) >> r_bits%max_bits) | \
(val << (max_bits - (r_bits%max_bits)) & (2**max_bits - 1))
r10 = 0x2f910ed35ca71942
r9 = 0x6a124de908b17733
esi = 0xffffffc3
# Target bytes extracted from binary
b1 = bytes.fromhex("dc 6b bb 4d fd 25 e4 7e c3 26")
b2 = bytes.fromhex("f5 72 ab 96 fc 8d 55 10 93 c1")
b3 = bytes.fromhex("fd 81 46 5b 7e 33 83 8f 2f")
target = b1 + b2 + b3
result_flag = []
for i in range(29):
# Calculate loop constants
shift_r10 = (i % 8) * 8
eax_initial = (r10 >> shift_r10) & 0xFFFFFFFF
edi = i * 29
T2_val_full = (eax_initial + edi) & 0xFFFFFFFF
T2_byte = T2_val_full & 0xFF
shift_r9_1 = (i*8 + 16) & 0x38
cl_1 = (r9 >> shift_r9_1) & 0xFF
r14_val = r9 >> shift_r10
r8d = i * 17
ecx_val = (r14_val & 0xFFFFFFFF) ^ r8d
# --- REVERSE PASS ---
val = target[i]
# 6. al = ror(al, esi & 0xFF) -> Reverse: rol
y = rol(val, esi & 0xFF)
# 5. eax = eax ^ ecx_val
y = y ^ (ecx_val & 0xFF)
# 4. eax = eax + esi -> Reverse: sub
y = (y - (esi & 0xFF)) & 0xFF
# 3. al = rol(al, cl_1) -> Reverse: ror
y = ror(y, cl_1)
# 2. al = al ^ T2_byte
inp = y ^ T2_byte
result_flag.append(inp)
# --- FORWARD PASS (Update ESI) ---
eax = T2_val_full
al = (eax & 0xFF) ^ inp
eax = (eax & 0xFFFFFF00) | al
al = rol(al, cl_1)
eax = (eax & 0xFFFFFF00) | al
eax = (eax + esi) & 0xFFFFFFFF
eax = eax ^ (ecx_val & 0xFFFFFFFF)
al = (eax & 0xFF)
al = ror(al, esi & 0xFF)
eax = (eax & 0xFFFFFF00) | al
# Update esi logic from asm
shift_esi = (i*8 + 24) & 0x38
rbx_val = r10 >> shift_esi
rcx_next = (rbx_val & 0xFFFFFFFF) ^ 0xffffffa5
rcx_next = (rcx_next + esi) & 0xFFFFFFFF
esi = (eax + rcx_next) & 0xFFFFFFFF
print("Flag:", "".join([chr(x) for x in result_flag]))
Running the solver gives the flag:
KCTF{w3Lc0m3_T0_tHE_r3_w0rLD}