Skip to main content
Be Bitwise

Evasion Techniques & Detection

Red Team Tooling Internals · Advanced Exploitation track

5 min read

In this module

The Evasion Landscape

Evasion techniques are how attackers sidestep security controls. Learning them tells you what your defences will face. Every technique in this lesson maps to a detection gap you can close.

Userland Unhooking

EDR products hook ntdll.dll functions to monitor API calls. Unhooking restores the original function bytes, removing the EDR's monitoring:

Disk-based unhooking: Read the original ntdll.dll from disk (C:\Windows\System32\ntdll.dll), map it into memory, and copy the original .text section over the hooked in-memory copy.

KnownDlls unhooking: Open \KnownDlls\ntdll.dll (a cached, clean copy maintained by the Windows kernel) and map it to get a fresh, unhooked ntdll.

Syscall extraction: Instead of unhooking, read the syscall numbers from the hooked ntdll (the syscall number is typically still present in the hooked function) and use direct syscalls.

Detection: Monitor for processes that map multiple copies of ntdll.dll, read ntdll from disk unexpectedly, or have .text section contents that differ from the EDR's hooks.

Direct Syscalls

Instead of calling hooked ntdll functions, the implant executes the syscall instruction directly:

nasm
; Direct syscall: NtAllocateVirtualMemory (syscall number 0x18)
mov r10, rcx
mov eax, 0x18
syscall

This bypasses all ntdll hooks because ntdll is never called. The implant includes its own syscall stubs with the correct syscall numbers for the target Windows version. Those numbers change between builds, so techniques like Hell's Gate read them at runtime from ntdll's own stubs in memory, taking the immediate from each stub's mov eax, imm32. If an EDR hook has overwritten that stub, Halo's Gate reads a neighbouring, unhooked stub instead and adjusts the number, since stubs are laid out in syscall-number order.

Indirect syscalls tighten the technique: instead of executing syscall from inside the implant (which leaves the implant's own address on the call stack), the implant jumps into the syscall instruction already present inside the real ntdll function. The return address then points legitimately to ntdll rather than to anonymous memory.

Detection: telemetry generated inside the kernel doesn't care which instruction made the syscall. The Microsoft-Windows-Threat-Intelligence ETW provider reports sensitive operations such as allocating or writing memory in another process and queuing APCs to its threads, whichever route the call took. Call stack analysis flags syscalls whose chain runs through unbacked memory regions. Attackers answer that with call-stack spoofing, building fake frames so the stack appears to come through legitimate, file-backed modules, and defenders in turn check that each frame is a plausible call site.

The Cat and Mouse of Unhooking

EDR vendors respond to unhooking by detecting the unhooking itself. They monitor for their hooks being removed, for processes mapping fresh copies of ntdll, and for syscall instruction execution from non-ntdll addresses. Attackers respond with more subtle techniques. This cycle drives both sides to increasingly sophisticated implementations.

AMSI Bypass

AMSI (Antimalware Scan Interface) scans script content before execution. Bypasses typically patch the AMSI scanning function in memory to return "clean" for all content:

The AmsiScanBuffer function in amsi.dll is the entry point for content scanning. A common bypass overwrites the first bytes of this function to immediately return S_OK (indicating no malware detected), effectively disabling AMSI for the current process.

Detection: Monitor for modifications to amsi.dll's code sections, unusual return values from AMSI functions, and processes that load amsi.dll but never generate scan events.

ETW Patching

Event Tracing for Windows (ETW) is used by EDR products to receive kernel-level events. Attackers patch the ETW event writing function (EtwEventWrite or NtTraceEvent) to suppress event generation:

Similar to AMSI bypass, the first bytes of the ETW function are overwritten to return immediately without generating events. This blinds any ETW-based monitoring in the process.

Detection: the patch only silences events written from inside that process. Providers raised in the kernel, the Threat-Intelligence provider among them, carry on regardless, and kernel-mode ETW consumers receive them as usual. Integrity monitoring of ETW functions detects patching. Comparing expected event volumes with actual events reveals suppression.

Sleep Obfuscation

During sleep intervals, the beacon sits in memory with readable strings, configurations, and code. Sleep obfuscation encrypts the beacon's memory during sleep:

  1. Before sleeping, the beacon encrypts its own memory (data sections, heap allocations)
  2. A timer callback or threadpool callback is set to wake the beacon
  3. The callback decrypts the memory and resumes normal operation
  4. Memory scanners that run during sleep find only encrypted data

Detection: Look for the encryption/decryption routines themselves, unusual timer callbacks, threads with encrypted stack data, or periodic memory content changes.

Building Better Detection

Each evasion technique has invariants that cannot be hidden:

  • Unhooking requires reading ntdll from disk or KnownDlls: These file access patterns can be monitored
  • Direct syscalls execute the syscall instruction: The instruction's location and calling context can be checked
  • AMSI/ETW patching modifies code sections: Code integrity monitoring detects modifications
  • Process injection requires cross-process operations: Kernel callbacks capture all cross-process memory operations
  • Sleep encryption requires periodic encrypt/decrypt: The encryption routine itself is detectable

The principle for defenders: don't chase the specific technique — it'll change next week. Detect the invariant side effects that every evasion technique leaves behind. Cross-process memory writes, code section modifications, and syscalls originating from unusual regions are structural invariants; they survive technique variation because the underlying OS mechanics don't.

Assume Breach

The most effective defensive posture assumes that some evasion will succeed. Instead of relying solely on prevention, build detection for post-exploitation behaviour: lateral movement, credential access, data exfiltration, and persistence. Even the most evasive implant must eventually perform actions that leave detectable traces - network connections, file access, registry modifications, and authentication events.