Anti-Debugging Tricks and How Analysts Notice Them
Some programs don't want to be watched. Malware checks for debuggers and sandboxes so that it behaves innocently under analysis; games and DRM-protected software check to slow down cheaters and crackers. An analyst who doesn't notice these checks draws the wrong conclusions: "the sample does nothing" may really mean "the sample saw your debugger". This lesson shows how the common checks work, runs two of them in a toy the session builds, and shows the analyst's side: how each check is spotted (in imports, strings, traces) and how its effect is confirmed. It teaches recognition, not evasion: nothing here hides a program from security tools.
The common checks
| Check | Platform | What it looks at | How an analyst notices |
|---|---|---|---|
ptrace(PTRACE_TRACEME) fails |
Linux | a process can have only one tracer; if a debugger is attached, the call returns -1 | ptrace import; strace shows ptrace(PTRACE_TRACEME) = -1 EPERM |
TracerPid in /proc/self/status |
Linux | the kernel reports the tracer's pid | strings /proc/self/status, TracerPid; an openat of that file in a trace |
IsDebuggerPresent, CheckRemoteDebuggerPresent |
Windows | the PEB's BeingDebugged byte, the debug port |
imports; reads of gs:[0x60] (the PEB) and offset 2 |
NtQueryInformationProcess(ProcessDebugPort / DebugFlags / DebugObjectHandle) |
Windows | kernel debug state | ntdll imports or direct syscalls |
PEB NtGlobalFlag (heap flags 0x70) |
Windows | set when a process is started by a debugger | reads at PEB offset 0xBC (x64) |
Timing (rdtsc, clock_gettime, QueryPerformanceCounter) |
all | single-stepping and breakpoints are slow | two timer reads with a comparison between them |
Scanning own code for 0xCC |
all | software breakpoints are int3 bytes |
loops reading the program's own .text; checksums of code |
| Hardware breakpoint registers | Windows | GetThreadContext and Dr0-Dr3 |
the API import and a check of the returned context |
| Sandbox and VM artefacts | all | CPU count, RAM size, MAC prefixes, VM tools' processes or drivers, user activity | strings naming VM vendors or analysis tools; cpuid instructions |
Every check needs some observable action: a system call, an API import, a string, a timer read or an unusual instruction. That is what makes them findable.
A toy with two checks
The session builds a toy that runs two Linux checks and reports what each saw, then runs it three ways: normally, under strace, and under gdb. (A real program would silently take a different path instead of printing.)
cat > watched.c <<'SRC'
#include <stdio.h>
#include <string.h>
#include <sys/ptrace.h>
static int tracer_pid(void) { /* check 1: ask the kernel */
char line[128]; int pid = 0;
FILE *f = fopen("/proc/self/status", "r");
if (!f) return -1;
while (fgets(line, sizeof line, f))
if (strncmp(line, "TracerPid:", 10) == 0) { pid = 0; sscanf(line + 10, "%d", &pid); }
fclose(f);
return pid;
}
int main(void) {
int t = tracer_pid();
long p = ptrace(PTRACE_TRACEME, 0, 0, 0); /* check 2: only one tracer allowed */
printf("TracerPid check: %s\n", t != 0 ? "DEBUGGER" : "clear");
printf("PTRACE_TRACEME check: %s\n", p == -1 ? "DEBUGGER" : "clear");
return 0;
}
SRC
gcc -O1 watched.c -o watched
echo "== normal run"; ./watched
echo "== under strace (output of the program only)"; strace -o /dev/null ./watched
echo "== under gdb"; gdb -q -batch -ex run ./watched 2>&1 | grep check== normal run
TracerPid check: clear
PTRACE_TRACEME check: clear
== under strace (output of the program only)
TracerPid check: DEBUGGER
PTRACE_TRACEME check: DEBUGGER
== under gdb
TracerPid check: DEBUGGER
PTRACE_TRACEME check: DEBUGGER
Both checks fire under both tools, because both strace and gdb are ptrace-based tracers. This is also why behaviour "under the tracer" can differ from behaviour in a sandbox that uses kernel-side monitoring.
How the analyst notices, before and during the run
cat > watched.c <<'SRC'
#include <stdio.h>
#include <string.h>
#include <sys/ptrace.h>
static int tracer_pid(void) {
char line[128]; int pid = 0;
FILE *f = fopen("/proc/self/status", "r");
if (!f) return -1;
while (fgets(line, sizeof line, f))
if (strncmp(line, "TracerPid:", 10) == 0) { pid = 0; sscanf(line + 10, "%d", &pid); }
fclose(f);
return pid;
}
int main(void) {
int t = tracer_pid();
long p = ptrace(PTRACE_TRACEME, 0, 0, 0);
printf("TracerPid check: %s\n", t != 0 ? "DEBUGGER" : "clear");
printf("PTRACE_TRACEME check: %s\n", p == -1 ? "DEBUGGER" : "clear");
return 0;
}
SRC
gcc -O1 watched.c -o watched
echo "== static: imports and strings"
nm -D watched | grep -E ' U (ptrace|fopen)'
strings watched | grep -E 'TracerPid|/proc/self'
echo "== dynamic: the checks in a syscall trace"
strace -e trace=openat,ptrace ./watched 2>&1 >/dev/null | grep -E 'status|ptrace'
echo "== confirming check 2 in gdb: make the ptrace syscall report success"
gdb -q -batch -ex 'catch syscall ptrace' -ex 'run' -ex 'continue' -ex 'set $rax = 0' -ex 'continue' ./watched 2>&1 \
| grep -E 'check|Catchpoint 1 \(' | sed -E 's/0x[0-9a-f]+ in //'== static: imports and strings
U fopen@GLIBC_2.2.5
U ptrace@GLIBC_2.2.5
/proc/self/status
TracerPid:
TracerPid check: %s
== dynamic: the checks in a syscall trace
openat(AT_FDCWD, "/proc/self/status", O_RDONLY) = 3
ptrace(PTRACE_TRACEME) = -1 EPERM (Operation not permitted)
== confirming check 2 in gdb: make the ptrace syscall report success
Catchpoint 1 (syscall 'ptrace' [101])
Catchpoint 1 (call to syscall ptrace), ptrace (request=PTRACE_TRACEME) at ../sysdeps/unix/sysv/linux/ptrace.c:45
Catchpoint 1 (returned from syscall ptrace), ptrace (request=PTRACE_TRACEME) at ../sysdeps/unix/sysv/linux/ptrace.c:45
TracerPid check: DEBUGGER
PTRACE_TRACEME check: clear
Static analysis flags both checks before anything runs: a ptrace import is rare in ordinary applications, and the strings /proc/self/status and TracerPid: say exactly what is being read. The trace then shows both checks happening, with the EPERM that gives the second one away. In gdb, a catchpoint stops on the ptrace system call's entry and again on its return; setting rax to 0 at the return makes the call look successful, and the second check now reports clear while the first still sees the tracer. That is the analyst's lesson in miniature: each check is a separate decision point; find them all (by imports, strings and traces), confirm each one's effect, and document them. Analysts do this on Windows with plugins such as ScyllaHide, which answers the common checks consistently.
How defenders use this
- Sandboxes monitor from outside the process (hypervisor- or kernel-level), so
TracerPidstays 0 andPTRACE_TRACEMEsucceeds; good ones also randomise VM artefacts. - Detection content: the presence of many anti-analysis checks is itself suspicious in a program with no reason to have them. capa has a whole rule namespace for anti-analysis, and YARA rules match strings such as
TracerPidor VM vendor names. - Report it: "the sample checks for a debugger via X and Y and exits early" is a finding, because it explains why a quick test showed nothing.
For developers: what anti-debugging is worth
- It slows down an analyst by minutes to hours; it never protects a secret stored in the program or a decision made on the client.
- It causes problems: false positives from security products, crash reports you can't reproduce, users with legitimate tools (accessibility software, profilers) locked out.
- For real protection, keep secrets and licence decisions on a server, sign your binaries, and design so that a modified client gains nothing (the crackme shows the difference).
Pitfalls
- Checks you can't see in imports: direct
syscallinstructions,ptraceresolved bydlsym,/proc/self/statusbuilt from encoded strings. Search the disassembly forsyscalland for reads of/proc. - Checks that run before
main: constructors (.init_array), TLS callbacks on Windows. - Timing checks fire when you step slowly; let the program run to a breakpoint after the check instead of stepping through it.
- Patching one check and assuming you're done: count the decision points.
Practice
1. Why did PTRACE_TRACEME fail under strace?
strace is already the process's tracer, and a process can have only one; the kernel refuses the second request with EPERM.
2. Which two strings in the binary pointed to the first check?
/proc/self/status and TracerPid:.
3. A Windows sample reads gs:[0x60] and then a byte at offset 2 of the result. What is it checking?
PEB->BeingDebugged, the same flag IsDebuggerPresent returns, read directly without the API (so no import shows it).
Test yourself
Why does ptrace(PTRACE_TRACEME) return -1 when a debugger is attached?
- Debuggers block all system calls
- A process can have only one tracer, and the debugger already is one
- ptrace is disabled on Linux
- The program lacks root
Which artefact revealed the TracerPid check without running the program?
- The file size
- The strings /proc/self/status and TracerPid:
- The ELF type
- The build id
In gdb, how was the result of the ptrace check confirmed?
- By patching the file
- A syscall catchpoint stopped at the return of ptrace, and rax was set to 0
- By removing the strings
- By running as root
What does anti-debugging protect in a commercial program?
- Secrets stored in the binary, permanently
- Nothing permanently: it slows analysis down, while secrets and decisions must live on a server
- The source code
- The user's files