TExcelTAD
Home › Reverse Engineering & Binary Analysis › Reverse Engineering Exercises

Reverse Engineering Exercises

These exercises cover the whole course in its order. Each names the lesson it practises; try it before opening the answer. Every binary is one an exercise builds itself, and the usual rule applies to anything you take from here: analyse only software you are entitled to analyse (introduction).

Exercise 1: which jump?

A function contains cmp edi, 100 followed by jg .Lbig. Another contains cmp rdi, 100 followed by ja .Lbig. For each, is the argument treated as signed or unsigned, and is .Lbig taken for the value -5? Lesson: x86-64 Registers and Flags.

Answer

jg is signed: -5 is not greater than 100, not taken. ja is unsigned: -5 is 0xFFFFFFFFFFFFFFFB, far above 100, taken. The second function treats a negative number as huge, which is often the safe direction for a bounds check and the dangerous one for a size computation.

Exercise 2: arguments on two platforms

Before call parse, a Linux caller does mov edx, 64, mov rsi, rbx, lea rdi, [rsp+0x10]. A Windows caller of the same function does mov r8d, 64, mov rdx, rbx, lea rcx, [rsp+0x30]. Write the C call, and explain why the Windows buffer is at rsp+0x30 rather than rsp+0x10. Lesson: Calling Conventions.

Answer

parse(buffer_on_stack, rbx_value, 64) on both. The Windows caller must keep the lowest 32 bytes above its rsp free as shadow space for the callee, so its locals start higher.

Exercise 3: frame arithmetic

A function starts push rbp; mov rbp, rsp; sub rsp, 0x50 and later does lea rax, [rbp-0x40] then passes rax and 0x60 to read(0, rax, 0x60). Is there a problem? What protections would notice? Lesson: Stack Frames.

Answer

Yes: the buffer at rbp-0x40 has at most 0x40 = 64 bytes before the saved rbp, but read may write 0x60 = 96 bytes, reaching the saved rbp (at rbp) and the return address (at rbp+8), and the canary at rbp-8 if there is one. A stack canary aborts on return; ASan would report a stack-buffer-overflow in a test build. The fix is read(0, buf, sizeof buf).

Exercise 4: ARM64 at a glance

Explain this ARM64 function: cmp w0, #0 / cneg w0, w0, mi / ret. Lesson: ARM64 Basics.

Answer

abs(x) for a 32-bit int: compare with 0, and if the result is negative (mi, minus), negate. No branch, the ARM counterpart of a cmov-based absolute value.

Exercise 5: find the magic value

The session builds a toy that accepts one secret number. Without reading the C source, find the number from the disassembly, then confirm by running it. Lessons: Compiled Control Flow, objdump.

bash · try it yourself
cat > magic.c <<'SRC'
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    long v = argc > 1 ? strtol(argv[1], NULL, 0) : 0;
    if ((v ^ 0x5a5a) == 0x6b6d) puts("unlocked");
    else puts("locked");
    return 0;
}
SRC
gcc -O1 magic.c -o magic && strip magic
objdump -d -M intel --no-show-raw-insn magic | grep -E 'cmp +r[a-z0-9]+,0x|xor +r[a-z0-9]+,0x' | sed 's/^ *//'
1189:	cmp    rax,0x3137
Answer

The compiler folded the XOR into the comparison: v ^ 0x5a5a == 0x6b6d is the same as v == 0x5a5a ^ 0x6b6d = 0x3137, so the listing compares with 0x3137 directly (12599 in decimal). ./magic 0x3137 prints unlocked. The optimiser did part of the reverse engineering for you, a common effect.

Exercise 6: parse an ELF header

The bytes below are the first 64 bytes of an ELF file. Complete the parser so it prints class, type, machine and entry point, then answer: what kind of file is it? Lesson: ELF Headers.

python · try it yourself
import struct
hdr = bytes.fromhex(
    "7f454c46020101000000000000000000"
    "0300b7000100000040060000000000004000000000000000"
    "30110000000000000000000040003800090040001c001b00")
assert hdr[:4] == b"\x7fELF"
cls = {1: "32-bit", 2: "64-bit"}[hdr[4]]
e_type, e_machine, _, e_entry = struct.unpack_from("<HHIQ", hdr, 16)
types = {1: "REL", 2: "EXEC", 3: "DYN"}
machines = {3: "x86", 62: "x86-64", 183: "AArch64", 243: "RISC-V"}
print(cls, types[e_type], machines[e_machine], hex(e_entry))
64-bit DYN AArch64 0x640
Answer

A 64-bit DYN file for AArch64 with entry 0x640: a position-independent ARM64 executable or shared library (a PT_INTERP segment would tell which). Disassemble it as ARM64, not x86-64.

Exercise 7: RVA to file offset

A PE has these sections: .text VirtualAddress 0x1000, VirtualSize 0x2340, PointerToRawData 0x400; .rdata VirtualAddress 0x4000, VirtualSize 0x900, PointerToRawData 0x2800. At which file offsets are RVAs 0x1a10, 0x4123 and 0x3500? Lesson: PE Headers.

Answer

0x1a10 is in .text: 0x1a10 - 0x1000 + 0x400 = 0xe10. 0x4123 is in .rdata: 0x4123 - 0x4000 + 0x2800 = 0x2923. 0x3500 is in neither section (.text ends at 0x3340): it is in the alignment gap and has no file offset; a parser should report that instead of guessing.

Exercise 8: what does the entropy say?

A PE's sections: .text 6.3, .rdata 4.8, .data 2.1, .rsrc 7.98 (900 KB). The entry point is in .text; imports look normal and include FindResourceW, LoadResource, CryptDecrypt. Packed? What next? Lessons: Strings and Entropy, Packing, Imports.

Answer

The code is not packed (normal .text entropy and entry point), but a large resource is encrypted or compressed, and the program loads a resource and decrypts something. Hypothesis: an embedded encrypted payload (a dropper pattern) or simply protected content. Next: extract the resource, look at the code around CryptDecrypt for the key handling, and confirm in the lab.

Exercise 9: CFG numbers

A function has 11 basic blocks and 15 edges. What is its cyclomatic complexity, and how many conditional branches does that suggest if every block has at most two successors? Lesson: Recovering Control Flow.

Answer

M = 15 - 11 + 2 = 6, which corresponds to 5 two-way decisions (M = decisions + 1 for a single-entry, single-exit function).

Exercise 10: the right tool

For each question, pick the tool: (a) "Which files does it open?", (b) "What is the third argument of the second call to memcpy?", (c) "What is this string compared with?", (d) "Is it built with full RELRO?", (e) "Which function writes the global at offset 0x4010?". Lessons: gdb, strace and ltrace, Relocations.

Answer

(a) strace -e trace=file. (b) gdb: a breakpoint on memcpy, continue to the second hit, p $rdx. (c) ltrace -e strcmp+memcmp (or gdb at the comparison). (d) checksec or readelf -lW plus readelf -d (GNU_RELRO and BIND_NOW). (e) a gdb watchpoint on that address.

Exercise 11: spot the anti-analysis

A Linux sample's strings include /proc/self/status, TracerPid, /proc/cpuinfo, hypervisor and VBOX. Its imports include ptrace and clock_gettime. List the checks you expect and how you'd confirm each. Lesson: Anti-Debugging.

Answer

A tracer check through TracerPid; a PTRACE_TRACEME check; a VM check through /proc/cpuinfo (the hypervisor CPU flag) and the VirtualBox vendor string; probably a timing check with clock_gettime. Confirm each with strace (the openat calls and ptrace result), breakpoints on clock_gettime, and by comparing behaviour inside and outside a VM; document them as findings.

Exercise 12: fix the design

A desktop app checks a licence key with if (strcmp(key, decrypt(KEY_BLOB)) == 0) unlock_pro(); and the "pro" features are local. Name two problems and propose a design that holds up against reverse engineering. Lesson: the crackme.

Answer

The decrypted key is in memory and visible to ltrace/gdb at the strcmp; the decryption key ships with the app; and the single branch to unlock_pro() can be patched. Better: signed licences verified with an embedded public key (unforgeable), and for anything valuable, move the "pro" functionality or data to a server that checks entitlement per request. Accept that a user can patch their own copy of purely local features.

Test yourself

Which jump would make cmp rdi, 100; jX .Lbig taken for rdi = -5?

  1. jg
  2. ja
  3. jl
  4. je

In Exercise 5, why did the listing compare with 0x3137 instead of XOR-ing first?

  1. The program was packed
  2. The compiler folded (v ^ 0x5a5a) == 0x6b6d into v == 0x3137
  3. objdump decoded it wrongly
  4. Because of PIE

An RVA falls between the end of one section's virtual size and the start of the next. What should a PE parser do?

  1. Use the previous section's raw pointer
  2. Report that the RVA has no file offset
  3. Round down to the nearest section
  4. Read from offset 0

Which tool answers 'which files does it open?' most directly?

  1. objdump
  2. strace
  3. nm
  4. checksec