Skip to main content
Dynamic Analysis & Debugging (GDB, ltrace, x64dbg)
Lesson 5 of 11

Dynamic Analysis & Debugging (GDB, ltrace, x64dbg)

Observe live execution with ltrace, GDB, and x64dbg to inspect registers, heap buffers, and library calls, dump unpacked payloads, and recognise common anti-debugging checks.

Advanced 30 min Reverse engineering
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Trace dynamically linked library calls and system calls using ltrace and strace before attaching a full debugger
  • Set software, hardware, and watchpoint breakpoints while accounting for PIE/ASLR base addresses in GDB
  • Inspect registers, stack frames, and heap buffers in GDB and x64dbg to dump runtime-decrypted payloads
  • Identify and safely bypass basic anti-debugging checks such as ptrace self-tracing and IsDebuggerPresent in an authorised lab

Recommended first: CPU Registers, Memory Addressing & Calling ConventionsAssembly Basics: x86/x64 & ARM Control FlowELF & PE Binary Formats: Headers, Sections & ImportsStatic Analysis & Decompilation with Ghidra & IDA Concepts

#Why this matters

Static decompilation works best when all code and strings sit cleanly in .text and .rodata. Real programs and malware frequently build strings on the stack, decrypt configuration blocks on the heap, or unpack entire executables in memory at runtime.

Dynamic analysis observes the process while it runs inside a controlled environment. Instead of manually simulating twenty rounds of an XOR or RC4 loop on paper, you let the CPU run the decryption loop, pause immediately afterward, and read the plaintext directly from memory.

#Fast triage with ltrace and strace

Before opening a full interactive debugger, check what the binary asks the OS and standard libraries to do. On Linux, ltrace hooks calls across the Procedure Linkage Table (PLT) to shared libraries such as libc.so.6, while strace logs kernel system calls (openat, read, connect) via ptrace.

Tracing a lab license checker with ltrace
$ printf "test_key_123\n" | ltrace ./license_check
fgets("test_key_123\n", 64, 0x7f9a1c41a8e0)    = 0x7ffd8a2b1010
strlen("test_key_123")                         = 12
strcmp("test_key_123", "S4mSU-2026-KEY")       = 32
puts("Invalid license")                        = 16
+++ exited (status 1) +++

In four lines, ltrace reveals the expected string length and comparison without touching assembly. Remember one boundary: ltrace only sees calls through dynamic linking. If a binary is statically linked (-static) or performs inline string comparisons without calling strcmp, ltrace prints nothing for those internal checks, and you must move to strace or a debugger.

#Debugging Linux binaries with GDB

When you open a Position-Independent Executable (PIE) in Ghidra, addresses often look like 0x1011a9 (relative to base 0x100000 or 0x0). At runtime, the OS loader maps PIE code at a virtual base address. By default, GDB disables ASLR (set disable-randomization on), mapping a 64-bit PIE ELF at a predictable base such as 0x555555554000. Always check info proc mappings (or vmmap in GEF/pwndbg) once started (starti), then add the runtime base to the file offset before setting breakpoints.

Breakpoints come in three forms:

  • Software breakpoints (b *addr): GDB writes 0xCC (INT3) over the first byte of the instruction. Unlimited in number, but detectable by self-checksumming code.
  • Hardware breakpoints (hb *addr): Uses the CPU's four debug registers (DR0 to DR3 on x86/x64). Leaves code bytes untouched, making them ideal when integrity checks scan .text.
  • Watchpoints (watch *addr): Pauses whenever a specific memory word is modified, useful for catching the exact instruction that writes a decrypted byte to the heap.
How a software breakpoint pauses execution in GDBHow a software breakpoint pauses execution in GDBGDBTarget MemoryTarget CPUKernelOverwrite instruction byte at addr with 0xCC (INT3)Execute 0xCC, raise breakpoint trapDeliver SIGTRAP to tracerRestore original byte at addrAnalyst inspects registers (i r) and memory (x/s $rdi)Single-step original instruction, then re-arm 0xCC

Once paused, step deliberately: use ni (next instruction) to step over library calls like printf, si (step instruction) to step into a custom function, and finish to run until the current function returns. Inspect state with i r (info registers), x/16gx $rsp (16 64-bit hex words on the stack), or x/s $rdi (null-terminated string pointed to by RDI).

#Debugging Windows binaries with x64dbg

On Windows, x64dbg (for 64-bit PE files, alongside x32dbg for 32-bit) provides a graphical view with five synchronized panes: CPU disassembly, Registers, Stack, Memory Dump, and Memory Map.

Task GDB command x64dbg action or command
Break at address or API b *0x5555555551a9 or b strcmp bp VirtualAlloc in command bar (F2 in disassembly)
Hardware breakpoint hb *addr bph addr, x (or Breakpoint, Hardware in context menu)
Step over / Step into ni / si F8 / F7
Run / Execute till return c / finish F9 / Ctrl+F9
View pointer contents x/s $rdi or x/32bx $rax Right-click register, select Follow in Dump

Remember the calling convention shift: Windows x64 passes the first four integer or pointer arguments in RCX, RDX, R8, and R9, with the return value in RAX. To dump a runtime-unpacked PE from memory in x64dbg, set bp VirtualAlloc, run (F9), press Ctrl+F9 to execute until VirtualAlloc returns, follow RAX in the Dump pane, set a write/execute breakpoint or step until the decryption loop finishes, and save the clean memory region via the Memory Map tab.

#Recognising and handling anti-debugging

Binaries built for copy protection or malware evasion often check whether a debugger is attached before revealing their real behaviour:

  • Linux ptrace self-attach: A process calls ptrace(PTRACE_TRACEME, 0, 1, 0). Because a process can have only one tracer at a time, ptrace returns -1 under GDB.
  • Linux /proc/self/status: Reading TracerPid: reveals 0 normally and a non-zero debugger PID when traced.
  • Windows IsDebuggerPresent(): Reads the BeingDebugged byte inside the Process Environment Block (PEB). NtQueryInformationProcess (ProcessDebugPort) performs a kernel-backed check.
  • Timing checks: Comparing two rdtsc timestamps detects slow manual single-stepping.

In an authorised lab, you can bypass a ptrace or IsDebuggerPresent check non-destructively by breaking right after the call, setting $rax = 0 (or flipping the Zero Flag ZF before the conditional jump), or clearing the PEB BeingDebugged flag in x64dbg.

#Common mistakes

  • Forgetting the PIE/ASLR base offset: Typing a Ghidra relative offset like 0x1234 directly into b *0x1234 in GDB fails because the executable is mapped at 0x555555554000 + 0x1234.
  • Stepping into system libraries with si: Using si on printf or HeapAlloc drops you deep into libc or ntdll; use ni to step over standard APIs or finish (Ctrl+F9) to climb back out.
  • Placing 0xCC breakpoints inside self-checked code: Software breakpoints modify .text bytes and fail checksum checks or cause crashes if placed mid-instruction; use hardware breakpoints (hb / bph) instead.
  • Trusting ltrace on static binaries: Assuming a binary performs no checks because ltrace shows zero calls when the binary was statically linked or uses raw syscall instructions.

#Summary

Dynamic analysis pairs lightweight tracers (ltrace, strace) with interactive debuggers (GDB, x64dbg) to inspect live registers, stack frames, and heap buffers at runtime. Accounting for PIE base addresses, choosing between software, hardware, and watchpoint breakpoints, and following API allocations lets you capture unpacked or decrypted payloads without reversing every instruction statically. When anti-debugging checks appear in lab binaries, stepping past the check and adjusting the return register or flag keeps analysis moving safely inside an isolated VM.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge