Skip to content

Lesson 9 of 13 · 18m

Tracing Before Debugging

strace, ltrace, ProcMon, and API Monitor let the program narrate its own file, network, and library-call behavior before you ever set a breakpoint.

Before you ever single-step, you watch the program talk to the kernel. Every file it opens, every socket it connects, every process it spawns is a syscall — and the kernel logs them all on request. The wrong mental model is "dynamic analysis means gdb." The cheapest dynamic tool is the OS's own interface log, and a binary that reads /etc/machine-id, connects to a hardcoded IP, and execs /bin/sh tells you its whole story in two hundred lines of strace — no disassembly required.

strace: the syscall ledger

$ strace -f -o trace.txt ./crackme
$ grep -E "open|connect|execve" trace.txt
openat(AT_FDCWD, "/etc/machine-id", O_RDONLY) = 3
openat(AT_FDCWD, "config.dat", O_RDONLY)     = -1 ENOENT
connect(4, {sa_family=AF_INET, sin_port=htons(4444),
          sin_addr=inet_addr("45.9.148.22")}, 16) = 0

-f follows children — always pass it, because the interesting half of most programs happens in subprocesses and threads. -o writes to a file so you can grep the ledger afterward; -e trace=%file restricts to file syscalls when the full stream is too loud; -e trace=%network does the same for sockets. -e read=3 -e write=4 dumps the actual buffer contents on those descriptors — the bytes on the wire, not just the lengths. -k prints the call-site stack trace at every syscall — which code path made that connect — and -tt wall-clock-stamps each line, which is how a nanosleep-based anti-sandbox delay shows itself.

The errors are map pins too. ENOENT on config.dat tells you a file the program wants; EACCES on a path tells you a privilege assumption. What the program failed to do is part of the story.

Two flags that pay for themselves immediately: -y annotates each file descriptor with what it actually is (read(3</etc/passwd>, ...) instead of read(3, ...) — no cross-referencing openat returns), and -c gives you a syscall-count summary instead of a log — a ten-second histogram that tells you whether a binary is file-bound, network-bound, or process-spawning before you read a single line.

ltrace: the library-call layer

strace sees the kernel boundary; ltrace sees the library boundary — strcmp, fopen, getenv, dlopen, every libc call with its arguments:

$ ltrace -C -f ./crackme
getenv("HOME")                               = "/home/user"
strcmp("hunter2", "s3cr3t_p4ss")             = -1
fopen("config.dat", "r")                     = nil

ltrace is the fastest license-check finder there is: run the program, watch the strcmp/strncmp/memcmp calls scroll by with both operands in cleartext. -C demangles C++ names. The catch: ltrace only sees calls that go through the PLT — a static binary, or calls the compiler inlined, are invisible to it. When ltrace goes quiet, that's information too: the interesting compare is inlined, and it's back to the listing.

The Windows equivalents

  • ProcMon (Sysinternals) is strace for Windows — a live filterable log of file, registry, process, and network activity. Filter to your process name, then read the story: which keys it opens, which files it drops, which RegSetValue makes it persist.
  • API Monitor is ltrace for Windows — hook per-DLL, see every CreateFileW, RegOpenKeyExW, send with full arguments.
  • LD_DEBUG=bindings,libs on Linux makes the dynamic linker narrate every symbol resolution — the cheapest view of which imports actually get used at runtime.

This is also what a sandbox is: automated strace + ltrace + ProcMon + a network tap, run for you. any.run-class reports are these same logs, rendered. Learning to read the raw versions is learning to do the job without the product.

What the ledger doesn't show is computation — user-space work leaves no kernel trace. When the trace goes quiet mid-run, the interesting part is happening between syscalls, and that's the debugger's job (next lesson).

Field notes

  • Trace before you debug. The syscall ledger tells you which five minutes of static analysis matter — the file it opens, the host it calls, the config it parses.
  • strace -p <pid> attaches to a running process; useful when the behavior you want happens on a timer or a signal.
  • Noisy binaries that fork and ptrace their own children are often self-monitoring — a debugger-hostile pattern (the packing/anti-debug lesson) you'll spot first in strace because ptrace is a syscall.
  • A connect() to a hardcoded address is a finding. Write down the IP; it goes in the report even if the socket is dead.

Keep reading

Related articles from the SudoSecurity blog.