Anti-debugging and Anti-analysis
Software that doesn’t want to be analyzed —malware, license protections, DRM, anti-cheat— incorporates techniques to detect and obstruct the analyst: check whether a debugger is attached, whether it runs in a virtual machine or sandbox, whether it’s being instrumented, or whether the binary itself has been modified. On detection, it changes behavior, exits, or runs decoy logic. For the reverser it’s a cat-and-mouse game: recognize these defenses and bypass them.
What it is
Section titled “What it is”Anti-analysis is a set of checks the program runs on itself and its environment to decide whether it’s under observation. They don’t change what the program “could” do, only whether it does it: the same binary behaves normally on a real machine and aborts (or deceives) in an analysis environment. The defender’s goal is to raise the cost of reversing; the analyst’s, to locate the check and neutralize it.
Theory: the cat and mouse
Section titled “Theory: the cat and mouse”flowchart TD
A["protected binary runs"] --> B{"debugger attached?"}
B -->|"Yes"| X["abort / decoy logic"]
B -->|"No"| C{"VM / sandbox?"}
C -->|"Yes"| X
C -->|"No"| D{"binary modified? (checksum)"}
D -->|"Yes"| X
D -->|"No"| E["run the real logic"]
X -.->|"the analyst patches the check"| E
Anti-debugging (detect the debugger)
Section titled “Anti-debugging (detect the debugger)”- Linux:
ptrace(PTRACE_TRACEME)—a process can only be traced once, so if it traces itself, a debugger can’t attach—; reading/proc/self/statusand checkingTracerPid != 0. - Windows:
IsDebuggerPresent(),CheckRemoteDebuggerPresent(), thePEB->BeingDebuggedflag,NtQueryInformationProcess(ProcessDebugPort). - Generic: timing checks (code under a debugger is much slower:
rdtsc,GetTickCount); breakpoint detection by searching for0xCC(int3) bytes in its own code; handlingSIGTRAPin a way that betrays the debugger.
Anti-VM / anti-sandbox (detect the analysis environment)
Section titled “Anti-VM / anti-sandbox (detect the analysis environment)”- Virtualization artifacts: hypervisor bit in
cpuid, VMware/VirtualBox MAC addresses, characteristic files/processes/registry keys. - Sandbox detection: little user activity, low uptime, few cores/little RAM, absence of human interaction (no mouse movement).
- Long sleeps: sandboxes cap analysis time, so malware “sleeps” to run out the clock and execute its logic outside the observed window.
Anti-tampering (detect modification)
Section titled “Anti-tampering (detect modification)”- Checksums/hashes of its own code: if someone patches the binary, the hash doesn’t match.
- Runtime integrity checks (sometimes of specific sections).
- Self-modifying or encrypted code: static doesn’t see the real code (see Static Analysis).
Obfuscation (hinder comprehension)
Section titled “Obfuscation (hinder comprehension)”- Control flow flattening — flatten the flow so the graph is unreadable.
- Opaque predicates — always-true/false conditions that confuse the decompiler.
- String encryption — strings decrypted only at runtime.
- Dead code / junk — junk instructions with no effect.
- Code virtualization — a custom interpreter runs custom bytecode (VMProtect, Themida); the hardest to reverse.
Exploitation (lab): bypassing the protections
Section titled “Exploitation (lab): bypassing the protections”The principle is that nearly all reduce to a check that, once located, can be patched or forced.
# anti-debug (Linux): neutralize ptrace with an LD_PRELOAD that always returns 0cat > fakeptrace.c <<'EOF'long ptrace(int r, int p, void *a, void *d) { return 0; }EOFgcc -shared -fPIC fakeptrace.c -o fakeptrace.soLD_PRELOAD=./fakeptrace.so gdb -q ./binary# in GDB: force the result of the check(gdb) b *0x401250 # right after the IsDebuggerPresent/ptrace call(gdb) run(gdb) set $rax = 0 # fake "no debugger"(gdb) continueSummary by category:
- Anti-debug: patch the
if(NOP), force the value in GDB,LD_PRELOADof a fakeptrace(Linux), ScyllaHide (Windows). - Anti-VM: more realistic VMs (hide artifacts), bare-metal for very evasive samples, or patch the checks.
- Anti-sandbox (sleeps): speed up/patch the
sleepcalls, hookGetTickCount. - Obfuscation: dynamic (see Dynamic Analysis) to dump the decrypted content; deobfuscators; angr.
- Anti-tampering: patch the integrity check too (before the rest).
Mitigations / Blue Team (developer)
Section titled “Mitigations / Blue Team (developer)”For whoever wants to protect their own software or delay the pirate:
- Combine several layers (a single one is easily patched): anti-debug + anti-VM + integrity + obfuscation.
- Commercial protectors (VMProtect/Themida) to raise the cost a lot.
- Remember that none fully protects against a determined analyst: for malware, the answer is detection (EDR/sandbox); for intellectual property, delay.
Checklist
Section titled “Checklist”- Detect anti-debug (
ptrace/IsDebuggerPresent/timing) - Bypass it (patch/force/
LD_PRELOAD/ScyllaHide) - Detect anti-VM/anti-sandbox (artifacts, sleeps)
- Evade (realistic VM / patch / speed up time)
- Detect anti-tampering (checksums) and patch it
- Identify obfuscation/packing and fight it (dynamic/angr)
- Combine static + dynamic on protected code
- Document the protections found