Sleep Obfuscation — Encrypting Beacons During Rest

The Problem of Persistent Memory Presence

Modern C2 frameworks such as Cobalt Strike, Havoc, Sliver, and Brute Ratel follow a well-defined cycle: the beacon "wakes up," checks in with the C2 server, executes pending tasks, then "sleeps" for a configured period. During that sleep window the beacon sits completely static in memory.

This dormant period is a critical detection vector. EDRs with periodic memory scanning capabilities (pe-sieve, Elastic Defend, SentinelOne) and dedicated tools like BeaconHunter and Hunt-Sleeping-Beacons were built specifically to find sleeping beacons by looking for:

  • Private executable memory regions (MEM_PRIVATE + PAGE_EXECUTE_*)

  • Byte patterns characteristic of known frameworks (Cobalt Strike, Meterpreter)

  • Suspicious call stacks in sleeping threads (SleepEx returning to a non-module region)

Sleep Obfuscation solves this by encrypting the beacon's own code and data while it sleeps, making it undetectable to memory scanners.

┌──────────────────────────────────────────────────────────────────────┐
│              Beacon Lifecycle with Sleep Obfuscation                 │
│                                                                      │
│  [Beacon wakes up]                                                   │
│       │                                                              │
│       ▼                                                              │
│  [Decrypts its own image in memory]                                  │
│       │                                                              │
│       ▼                                                              │
│  [Checks in with C2, executes tasks]                                 │
│       │                                                              │
│       ▼                                                              │
│  [Encrypts its own image: code, data, keys]                         │
│       │                                                              │
│  ─────┼──────────────────────────────────────────────────────────   │
│  Beacon sleeps. Memory contains only random bytes.                  │
│  No memory scanner finds recognizable patterns.                     │
│  ─────┼──────────────────────────────────────────────────────────   │
│       │                                                              │
│       ▼                                                              │
│  [Timer fires → wake up → decrypt → cycle restarts]                 │
└──────────────────────────────────────────────────────────────────────┘

Technique 1: Ekko — Timer-based Sleep Obfuscation

Ekko (published by C5pider/Cracked5pider) is an elegant sleep obfuscation implementation that uses Windows timer queue callbacks to perform encryption operations without spawning additional threads:

  1. Schedule a CreateTimerQueueTimer callback to fire after a short delay

  2. The callback XOR-encrypts the beacon's memory regions

  3. Block on a WaitForSingleObject that waits for the decrypt timer to fire

  4. A second timer decrypts memory before returning control to the beacon


Technique 2: Foliage — APC-based Sleep Obfuscation

Foliage uses APCs queued to the current thread to perform encryption, eliminating the need for timer queues:


Technique 3: Gargoyle — ROP-based Sleep Obfuscation

Gargoyle (Joshua Lospinoso, 2017) is a more advanced technique that uses ROP (Return Oriented Programming) to hide the beacon during sleep. While dormant:

  1. The shellcode is encrypted and execution permission is stripped

  2. A ROP gadget chain is set up to run when the timer fires

  3. The beacon is invisible to scanners: non-executable memory, encrypted bytes


Technique 4: Stack Spoofing During Sleep

Even with memory encrypted, the call stack of a sleeping thread can betray the beacon. A thread with a stack like ntdll!NtWaitForSingleObject → [code in heap] is immediately suspicious.

Stack spoofing replaces the thread's stack frames during sleep:


C2 Framework Support


Detecting Sleep Obfuscation

Detection of sleep obfuscation is an active area of defensive research:

  • Memory snapshot comparison: Compares region contents between two points in time. Regions whose content changes without a legitimate reason are suspicious.

  • Call stack analysis during sleep: Inspects stacks of sleeping threads. A stack with SleepEx returning to a non-module region indicates a beacon.

  • Timer callback inspection: Audits registered timer queue callbacks. A callback pointing into a private region is anomalous.

  • BeaconHunter (Elastic): Detects C2 behavioral patterns via ETW and syscall analysis.

  • Hunt-Sleeping-Beacons (thefLink): Purpose-built tool that finds dormant beacons by analyzing call stacks and memory region types.


References

  • C5pider, "Ekko: A Small Sleep Obfuscation Technique" — github.com/Cracked5pider/Ekko (2022)

  • Joshua Lospinoso, "Gargoyle: A Memory Scanning Evasion Technique" — jlospinoso.github.io (2017)

  • Kyle Avery, "VulpesFurtim: Obfuscating Cobalt Strike Beacons" — kyleleavery.com (2021)

  • thefLink, "Hunt-Sleeping-Beacons" — github.com/thefLink/Hunt-Sleeping-Beacons

  • Elastic Security Labs, "Detecting Sleeping Beacons" — elastic.co/security-labs (2022)

  • MDSec, "In-Process Shellcode Obfuscation" — mdsec.co.uk (2021)

  • Cobalt Strike, "Sleep Mask Kit Documentation" — hstechdocs.helpsystems.com

  • SEKTOR7, "Advanced Malware Development: Evasion" — sektor7.net

Last updated