Home / Guides / Native

Recover a rule from machine code.

Start with a compiled program and one question. Read its instructions, follow the caller, pull the data it compares against, then write the rule down and check it against the program.

The target

A 20-character flag checker we wrote and compiled for this guide: Linux x86-64, built with gcc 14.2 at -O1, symbols kept. It asks for a flag and prints Correct! or Wrong.

Download the checker (zip: binary, source, checksum)

The source is in the zip so you can check every step afterwards. Do the investigation first without opening it.

Ask about the behaviour

Your coding agent

›Use REP on ./checker. Find the function that decides Correct or Wrong, list every check it makes, and recover the input it accepts. Show the instructions and the data you used.

Read the results

1 · The decision

The strings Correct! and Wrong. lead to main, which calls check. Its first test is the length:

check · start
120a: call   1040 <strlen@plt>
1217: cmp    rdx,0x14          ; 20 characters
121b: jne    1240 <check+0x3f>  ; else return 0

2 · The loop and its helper

check · the loop
1222: mov    esi,ebx
1224: mov    rdi,rbp
1227: call   11a0 <check_char>   ; (input, i)
122c: test   eax,eax
122e: je     1240               ; one miss = Wrong
1230: add    ebx,0x1
1233: cmp    ebx,0x14
1236: jne    1222
1238: mov    rdi,rbp
123b: call   11c2 <check_sum>
check_char
11a3: movzx  eax,BYTE PTR [rdi+rsi*1]   ; input[i]
11a7: lea    rdx,[rip+0xe92]          # 2040 <MASK>
11ae: xor    al,BYTE PTR [rdx+rsi*1]
11b1: lea    rdx,[rip+0xe68]          # 2020 <WANT>
11b8: cmp    al,BYTE PTR [rdx+rsi*1]
11bb: sete   al

3 · The data it compares against

.rodata
2020 3c0d0908 0d0fe1ea f6c6d4cf cbeadeba   WANT[0..15]
2030 beb4aba2                              WANT[16..19]
2040 5a61686f 767d848b 9299a0a7 aeb5bcc3   MASK[0..15]
2050 cad1d8df                              MASK[16..19]

4 · The last check

check_sum
11e0: movzx  edx,BYTE PTR [rdi+rax*1-0x1]  ; input[i-1]
11e5: imul   edx,eax                       ; × i
11e8: add    ecx,edx
11ee: cmp    rax,0x15
11f4: cmp    ecx,0x58ad                   ; 22701

Write the rule, then check it

recovered · C
int check(const char *s) {
  if (strlen(s) != 20) return 0;
  for (int i = 0; i < 20; i++)
    if ((s[i] ^ MASK[i]) != WANT[i]) return 0;     /* so s[i] = WANT[i] ^ MASK[i] */
  unsigned sum = 0;
  for (int i = 0; i < 20; i++) sum += s[i] * (i + 1);
  return sum == 0x58ad;
}

XOR undoes itself, so each character is WANT[i] ^ MASK[i]: 0x3c ^ 0x5a = 0x66 is f, 0x0d ^ 0x61 = 0x6c is l, and so on. The 20 characters spell flag{read_the_bytes}; their weighted sum is 22701 (0x58ad), so the last check passes too. Typed into the program: Correct!

Try the checker's rules live in the showcase →

Try it on your own binary

The same steps work on any executable or library you may examine: ask about one behaviour, let the agent find the function from a string, a symbol or an import, read its data, and check the rule by running the program or comparing outputs. For deep decompiling, REP can also drive Ghidra, IDA, Hopper or Cutter when you have them installed; rep doctor shows which helpers it finds.