The last lesson ended by comparing a program's source with its disassembly and noticing how much was missing. This lesson explains where it went. We take one small C program and follow it through the compiler one stage at a time, checking after each stage what is still there and what is gone. Then we turn around and look at the same path from the reverse engineer's side.
Knowing the forward trip pays off every time you open a decompiler. When it shows you a variable called local_c, or a number that appears nowhere in the program's documentation, you will know which stage is responsible and whether the original is recoverable.
This lesson's chall asks for a PIN. Unlike last time, we start with the source, because the point is to watch it change:
1#include <stdio.h>
2
3#define KEY 0x5a
4#define It is short, but it has one of everything we want to track: a comment, two macros, a helper function, named local variables, string literals, calls into the C library, and two numeric constants.
The download on this page holds source.c and the finished chall. You need gcc to follow along. Compilers differ between versions, so your intermediate output may not match ours line for line, but the same things will be missing at the same stages.
Running gcc source.c looks like one step. It is four, and gcc can be told to stop after each one. The Compilation lesson in C Programming covers these stages from the programmer's side. Here we care about what each one destroys.
The preprocessor handles every line that starts with #. It pastes in the contents of each #include, replaces each macro with its value, and deletes comments. gcc -E stops after this stage and prints the result, and -P leaves out some line-number bookkeeping:
gcc -E -P source.c | wc -l324gcc -E -P source.c | tail -n 17static int scramble(int pin){ int mixed = pin ^ 0x5a; return mixed & 0xff;}int main(void){ int pin = 0; printf("PIN: "); if (scanf("%d", &pin) != 1) return 1; if (scramble(pin) == 0x3c) puts("Unlocked."); else puts("Locked."); return 0;}Our program grew to hundreds of lines, nearly all of them declarations pasted in from stdio.h. The last 17 are ours, and two things are already gone. The comment explaining scramble was deleted. The names KEY and TARGET were replaced with 0x5a and 0x3c. The output is still C and the compiler proper has not even started, yet nothing after this point will ever know those names existed.
This is the first rule to carry into reverse engineering: comments and macro names are never in a binary. No tool can recover them, because they were removed before there was anything to recover them from.
The compiler turns the preprocessed C into assembly. gcc -S stops here and writes source.s. This is where most of the loss happens:
gcc -std=gnu17 -S -masm=intel source.cgrep -v cfi source.s | head -n 27 .file "source.c" .intel_syntax noprefix .text .type "scramble", @function"scramble":.LFB0: push rbp mov rbp, rsp mov DWORD PTR [rbp-20], edi mov eax, DWORD PTR [rbp-20] xor eax, 90 mov DWORD PTR [rbp-4], eax mov eax, DWORD PTR [rbp-4] movzx eax, al pop rbp ret.LFE0: .size "scramble", .-"scramble" .section .rodata.LC0: .string "PIN: ".LC1: .string "%d".LC2: .string "Unlocked.".LC3: .string "Locked."The grep hides some lines meant for debuggers, and -std=gnu17 pins the C standard so the output matches across compiler versions. Recent versions of GCC put quotes around names like "scramble"; older ones do not.
Read scramble against its source:
pin is now [rbp-20] and the local mixed is [rbp-4]. Variable names exist for the programmer. The processor only needs to know where the value lives, so a stack offset is all that remains.int is gone as well. What is left is a hint: every access is a DWORD PTR, four bytes wide. Whether those four bytes were an int, an unsigned int or a float is no longer written down anywhere.pin ^ KEY became xor eax, 90. The compiler printed the constant in decimal. It does not remember that the programmer wrote it in hexadecimal, let alone that it once had a name.mixed & 0xff became movzx eax, al, an instruction that keeps the low byte and zeroes the rest. The compiler is free to pick any instructions with the same result, so even at this stage the assembly does not map back to the source one operator at a time.Some things did survive. The function is still called scramble. The four string literals are intact, each behind a label like .LC0. And further down the file, outside what head showed, are call lines that still name printf, scanf and puts.
The assembler turns each line of assembly into the bytes the processor runs and packs them into an object file. gcc -c stops here:
gcc -std=gnu17 -c source.cfile source.osource.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not strippednm source.o U __isoc99_scanf0000000000000018 T main U printf U puts0000000000000000 t scramblenm lists a file's symbol table: the names it defines and the names it needs. A T or t marks a function defined in this file. The lowercase t on scramble means it is visible to this file only, which is what static asked for. A U marks a name that is undefined here and has to come from somewhere else.
Almost nothing is lost in this stage. Instructions and bytes correspond one to one, which is why a disassembler can turn the bytes back into the same instructions. The only casualties are the compiler's private labels, such as .LC0 and .LFB0. They became plain offsets, and none of them appear in the symbol table.
The object file cannot run yet. Its addresses start at zero, and it calls functions it does not contain. The linker assigns real addresses, adds the startup code that runs before main, and records which library each undefined name should come from:
gcc -no-pie -o chall source.ofile challchall: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=82f393c917043f278f518cbf9926cfe802403765, for GNU/Linux 3.2.0, not strippednm chall | wc -l31file now reports an executable where it said relocatable before. The symbol table grew from five names to 31. We wrote two of those functions. The rest belong to startup and shutdown code that every C program on Linux carries. That code sits in the binary beside ours and looks no different to a disassembler, which matters later: part of reading a binary is recognizing what the author did not write and skipping it.
A fair question at this point is why anything readable is left. The answer is that the binary keeps exactly what is needed to run, plus a few things nobody bothered to remove.
The symbol table is in the second group. A finished program does not need the names main and scramble to run, and strip removes them:
strip -o stripped challnm strippednm: stripped: no symbolsnm -D stripped w __gmon_start__ U __isoc99_scanf@GLIBC_2.7 U __libc_start_main@GLIBC_2.34 U printf@GLIBC_2.2.5 U puts@GLIBC_2.2.5The first nm finds nothing. The second, with -D, reads a different table, the dynamic symbols, and that one is untouched. It has to be. The names printf and puts are how the system finds those functions in the C library when the program starts, so a dynamically linked binary cannot run without them. Stripped Binaries is about working with only this much.
Putting the whole trip together:
| In the source | In the binary | Removed by |
|---|---|---|
| Comments | Gone | Preprocessor |
Macro names (KEY, TARGET) | Gone; their values remain as numbers | Preprocessor |
| Local variable names | Gone; stack offsets and registers | Compiler |
| Types | Gone; only the size of each access | Compiler |
if, else, loops | Gone; compares and jumps | Compiler |
| Your function names | Kept, unless the binary is stripped | strip, if it is run |
| String literals | Kept | Nothing |
| Library function names | Kept | Nothing |
| Numeric constants | Kept as values, unless the optimizer merges them | Optimizer, sometimes |
The bottom rows are the ones the program needs while it runs. It has to print those strings, call those library functions, and compare against those numbers. This is why reverse engineers reach for strings, imports and constants first. They are the parts of the source that could not be thrown away.
Everything so far was compiled without optimization, which is the default and the friendliest case. Software that ships is normally built with -O2, so here is the same source built that way:
gcc -std=gnu17 -O2 -no-pie -o chall-O2 source.cnm chall-O2 | grep -c scramble0We did not strip this binary, and scramble is missing anyway. The disassembly of main shows what happened to it:
objdump -d -M intel --no-show-raw-insn --disassemble=main chall-O200000000004003a0 <main>: 4003a0: sub rsp,0x18 4003a4: mov edi,0x401210 4003a9: xor eax,eax 4003ab: mov DWORD PTR [rsp+0xc],0x0 4003b3: call 400380 <printf@plt> 4003b8: xor eax,eax 4003ba: lea rsi,[rsp+0xc] 4003bf: mov edi,0x401216 4003c4: call 400390 <__isoc99_scanf@plt> 4003c9: cmp eax,0x1 4003cc: jne 4003e3 <main+0x43> 4003ce: cmp BYTE PTR [rsp+0xc],0x66 4003d3: je 4003ed <main+0x4d> 4003d5: mov edi,0x401223 4003da: call 400370 <puts@plt> 4003df: xor eax,eax 4003e1: jmp 4003e8 <main+0x48> 4003e3: mov eax,0x1 4003e8: add rsp,0x18 4003ec: ret 4003ed: mov edi,0x401219 4003f2: call 400370 <puts@plt> 4003f7: xor eax,eax 4003f9: jmp 4003e8 <main+0x48>There is no call to scramble. The compiler copied its body into main, which is called inlining, and then deleted the function because nothing else used it. It did not stop there. No xor with 0x5a appears, and no compare with 0x3c. In their place is one instruction: cmp BYTE PTR [rsp+0xc],0x66.
The compiler worked out that XORing a byte with 0x5a and comparing the result with 0x3c gives the same answer as comparing the original byte with one precomputed value:
printf '%#x\n' $((0x5a ^ 0x3c))0x66So the optimized binary contains neither constant from the source. It contains 0x66, a number the author never typed. In decimal that is 102, and since only the low byte of the PIN is checked, 102 should open the lock:
echo 102 | ./challPIN: Unlocked.For a reverse engineer this cuts both ways. The optimized check is easier to solve, because the compiler did the arithmetic for us. It is also further from the source: a function is missing, and anyone searching the binary for a key of 0x5a will not find one. Reading Optimized Code in Assembly 101 covers more of what -O2 does to a program.
A reverse engineer makes the same trip in the other direction, and how well each step works depends on how much the matching forward stage destroyed.
Disassembly undoes the assembler. That stage lost almost nothing, so disassembly is reliable. The instructions objdump printed above are the instructions the compiler emitted, not an estimate of them.
Decompilation tries to undo the compiler, and that stage lost nearly everything. A decompiler cannot look up the missing names, types and structure, because they are not in the file. It makes them up. Here is what Ghidra, the decompiler this course uses, gives back for the unoptimized chall:
1byte scramble(byte param_1)
2{
3 return param_1 ^ 0x5a;
4}The logic is right, and everything the binary kept is here: the function names, the strings, the library calls, and both constants. Everything else is invented. pin is local_c, named after its position on the stack, and its type is undefined4, a placeholder meaning four bytes of something. The two return statements were merged into a variable, uVar2, that the author never declared. scramble takes and returns a byte where the source said int, because the decompiler saw that only the low byte of the result matters and typed the whole function to fit. In the stripped copy, with no symbol table to read, the same two functions come out as FUN_00400486 and FUN_0040049e, named after their addresses.
Nothing undoes the preprocessor. If you want a magic number to have a name like KEY again, you are the one who names it.
There is a deeper limit, too. Here is a different program, with no helper function and no key:
1#include <stdio.h>
2
3int main(void)
4{
5 intCompiled with -O2, this produces exactly the same instructions for main as chall-O2 above. Given only that binary, no tool and no person can say which of the two sources it came from. Many different programs compile to the same machine code, so the question "what was the original source?" often has no answer.
That changes what the goal is. Decompiled C is one program that would behave like the binary, written by a tool that has never seen the original. The job is to understand what the binary does, and to fix up the decompiler's guess with better names and types until it is easy to read. Getting the author's exact code back is not possible, and not needed either.
gcc -E, -S and -c.strip.Next up is Orienting Ghidra, where we install the decompiler and see what it makes of programs like this one.