Shellcode && EDR Bypass
Shellcode && EDR Bypass
and Last
Shellcode Loader
Dobin Rutishauser
Slides: https://bit.ly/4dGhBXl
SSL/TLS Recommendations
Developer // TerreActive // OWASP Switzerland
Developer // UZH
Automated WAF Testing and XSS Detection
// OWASP Switzerland Barcamp
SOC Analyst // Infoguard
Fuzzing For Worms - AFL For Network Servers
// Area 41
RedTeam Lead // Raiffeisen
Develop your own RAT - EDR & AV Defense
// Area 41
Memory Corruption Exploits & Mitigations Avred - Analyzing and Reverse Engineering AV
// BFH - Bern University of Applied Sciences Signatures
// HITB
Gaining Access
// OST - Eastern Switzerland University of Applied Sciences
3
Loader Content
Target Audience
● RedTeamers
● Doing initial access with their C2 (CobaltStrike, Sliver, Havoc…)
● Have some EDR knowhow, but confused
Me:
● Not much interest in specific (detectable) anti-EDR techniques
● Interest in how stuff overall works
https://github.com/sevagas/Advanced_Initial_access_in_2024_OffensiveX/blob/main/breach_the_gates_extended.pdf
Loader Why
Program.exe Process
Header
Code
Code
Data
Data
Harddisk RAM
Windows Loader
Loader File vs. Process Analysis
Program.exe Process
Antivirus Header Memory scanning
Signatures Code Sandbox
Yara Code EDR
File Hash Debugger
Imports
Disassembler Data
Decompiler Data
Program.exe Process
Header
Code Read, Execute
Code
Data
Data Read, Write
Loader Memory Region Backed vs. Unbacked
Program.exe Process
Header
Code Backed
Code
Data
Data Backed
VirtualAlloc’d Unbacked
Loader
Loader Process Memory Regions
Shellcode Loader Example
Loader Shellcode: Calc
Loader Shellcode: Calc
Loader Shellcode: Loader
Need:
● Shellcode (payload)
● VirtualAlloc memory
● Copy shellcode to memory
● Exec memory
Loader Shellcode Loader: 1/3 VirtualAlloc
Code
VirtualAlloc(RWX)
Data Create new region in process
Payload
RWX Region
Loader Shellcode Loader: 2/3 Copy
Code
Data
Payload
Copy Payload to RWX Region
RWX Region
Loader Shellcode Loader: 3/3 Exec
Code
Data
execute payload
Payload (shellcode / memory region)
RWX Region
Loader Shellcode Loader Structure
Loader.exe Teams.exe
Process Process
OpenProcess()
Code Code
Copy Payload
Exec
Loader Loader: Unencrypted Payload
loader.exe
OS
Code
Write-File Event
Data
Scan File
Payload AV
Signature Scan
Loader DEMO 1
Payload
Copy
Encrypted
Exec
Loader Loader: Encrypted payload
AV Emulator:
● “Interpret” PE file
● Virtual CPU, Windows
It is not:
● Virtualization
● Sandbox
● Full Emulation (Bochs)
● Wine
Loader AV Emulator
Cut-off condition:
● Time Cut-Off
● Number of instructions reached?
● Number of API Calls
● Amount of memory used
Memory Scan
Loader Anti AV Emulator
Payload
Encrypted
Alloc
Anti
Process Copy Payload
Emulation
Exec
AV Emulation
Loader Anti AV Emulator
Payload
Encrypted
Alloc
Anti
Process Copy Payload
Emulation
Exec
AV Emulation
● Show Anti-Emulation
Detection in Middleboxes
Dynamic Analysis
Loader Middleboxes
Sandbox
AV
Email
Gateway
AV AV
Web
Client Malware
Proxy
AV
Teams
Sharepoint
Loader Execution Guardrails
● AD Domain
Execution guardrails: ● Username
● Installed Software
● Environment check ● IP Address
● Environmental keying
● Sandbox / VM detection
● Vmtools installed
● # CPUs, RAM
● Vmware Drivers
Loader Anti AV Emulator
Payload
Encrypted
Alloc
Execution
Process Copy Payload
Guardrails
Exec
Sandbox
Loader Design
Conclusion
Loader Loader Summary
Payload
Encrypted
Alloc
Execution Anti
Process Copy Payload
Guardrails Emulation
Exec
AV Emulator
Static Analysis
Loader Loader Problem
Payload
Encrypted
Alloc
Execution Anti
Process Copy Payload
Guardrails Emulation
Exec
Telemetry
Memory Scan EDR
EDR Fundamentals
Loader EDR
EDR:
● Agent on each System
● Find malicious processes
Loader EDR
EDR is blackbox
Many different EDR
Rapid development EDR
Input Alerts
Therefore: Blackbox
● Focus on what the EDR sees
● Not the detections itself
● Whats the input?
● Create a framework to reason
about EDR
Loader EDR - Bubbles of Bane
Signatures
File Mem
Scan Scan
AV EDR
Behaviour
Telemetry
EDR
EDR Input: Usermode-Hooks
Loader Usermode Hooks
kernel32.dll Ntdll.dll
syscall Kernel
VirtualAllocEx NtAllocateVirtualMemory NtAllocateVirtualMemory
WinApi NtApi
Usermode Usermode
Hook Hook Kernel
Loader Usermode Hooks
Process
Syscall
Ntdll.dll
Hooked Windows
Kernel
Hook EDR
DLL
Usermode Hooks
Loader Usermode Hooks: Patching ntdll.dll
Kernel32.dll:: Ntdll.dll::
App.exe
OpenProcess() NtOpenProcess()
jmp callback
syscall
syscall OS
Kernel
Amsi.dll
NtCreateFileTrampoline()
notify
EDR
Loader Usermode Hooks
Process EtwWrite()
Kernel Callbacks
ETW
syscall OS
ntdll.dll
ETW-TI
EDR
amsi.dll pipe Usermode Hooks
EDR Input
Kernel Callbacks
Loader Kernel Callbacks
<tbd>
Loader ETW Provider: Microsoft-Windows-Kernel-Process
Microsoft-Windows-Kernel-Process: Provides events related to process creation and termination. It can help
detect suspicious processes being spawned.
● Process Start/Stop
● Thread Start/Stop
● Image Load/Unload
● Some more
ProcessStart data:
● ProcessID
● CreateTime
● ParentProcessID
● ImageName
Process (Etw)EventWrite()
ETW
OS
ETW
EDR
ETW
EDR Input
ETW-TI
Loader ETW-TI
ETW-Threat Intelligence
The good shit
syscall ETW
ntdll.dll OS
ETW-TI
amsi.dll AMSI
PEB EDR
Process Info
EPROCESS
Callstack
Memory Scan
File Scan
File
Loader EDR: Query Process Information
Note:
● PPID Spoofing
● Command line argument Spoofing PEB EPROCESS
NtQueryInformationProcess()
Loader EDR: Memory Scanning
Process
Data
Loader EDR: Callstack Analysis
Callstack:
● On NtApi Call (AMSI or syscall)
● List of addresses of all previous parent functions
Loader EDR: Callstack Analysis
Process Process
.text
syscall OS
ntdll.dll
AMSI
EDR
amsi.dll
Callstack
Stack
Loader Callstack analysis - Elastic
Input Events
EDR
time
Process
Kernel Callbacks
ETW
OS
ETW
kernel32.dll Ntdll.dll
syscall Kernel
VirtualAllocEx NtAllocateVirtualMemory NtAllocateVirtualMemory
WinApi NtApi
Usermode
Hook
Loader Usermode-hook patch
VirtualProtect(ntdll.dll, RX->RW)
memcpy(ntdll.dll, …)
VirtualProtect(ntdll.dll, RW->RX)
.text
EDR
sus?
ntdll.dll
“EDR bypass”
Loader Usermode Hooks: Patching ntdll.dll
Kernel32.dll:: Ntdll.dll::
App.exe
OpenProcess() NtOpenProcess()
jmp callback
Indirect Syscall syscall
syscall OS
Kernel
Direct Syscall syscall
Amsi.dll
:-(
NtCreateFileTrampoline()
EDR
Callstack Spoofing
Loader Callstack Spoofing
Callstack:
● List of addresses of all previous parent functions
Loader Callstack Spoofing
Process
.text NtApi
OS
Unbacked
shellcode
Patch
Stack EDR
Query Callstack
Stack
Image Spoofing
Loader Image Spoofing
notepad.exe
Start Suspended
Resume Process
Module Stomping
Loader Module Stomping
notepad.exe
LoadLibrary(“openssl.dll”)
Active Sleep
.text
.text EDR
encrypted
Sleep()
Memory Scan
.data
.data
encrypted
EDR Attacks Summary
Loader EDR Attacks Overview
program.exe
Carrier
= Loader Loader
Payload
encoded
Malware Detection:
Code Similary Scanning
Machine Learning
1) Train Neural Network on malware files
2) ???
3) Profit?
7zip.exe
File injection:
● Harder to find the malicious code
○ Lots of “code” .text
○ Code similarity searches fail
○ No “Good code stuffing” Loader
● Existing Meta information in the PE Shellcode
○ Metadata like Company, Issuer
○ Imports / IAT
● Whats the alternative?
○ Write your own loader which results in a
5kb file?
○ EXES generated from C2 frameworks?
○ Burned Public loaders?
Loader Basic File Injection Mode = 1,1 Mode = 2,1
Loader
Loader Loader
https://github.com/mgeeky/ProtectMyTooling/blob/master/RedBackdoorer.py
Loader Disassembled PE Entry Point (main)
SuperMega
Shellcode generation
Loader SuperMega: Shellcode Creation
ASM
C Shellcode
Text
Loader SuperMega: Shellcode Creation
if (MyVirtualProtect(
dest, 202844, RX, &res) == 0) {
return 7;
}
(*(void(*)())(dest))();
Loader SuperMega: Shellcode Creation
Demo SuperMega UI
● C -> ASM
● Phases
● Options
Cordyceps
Loader Cordyceps Motivation
PEB Walk
NO
Find this
WinApi NtApi
Loader PEB Walk
Loader PEB Walk
Loader PEB Walk
.text
Call iat:
MessageBoxW
IAT
Call User32.dll:
MessageBoxW()
User32.dll
MessageBoxW()
Loader IAT Call
Call IAT:
IAT:
Loader IAT Call
6 bytes
0x140002080
Cordyceps
IAT Reuse
Loader Cordyceps: IAT reuse
Problem:
IAT reuse:
● MASM doesnt support relative call’s
● Goal: Get rid of PEB_WALK
● Solution: Patch shellcode in the infected
● Solution: Relative call to IAT
binary
Loader Cordyceps: IAT reuse
Loader Cordyceps: IAT reuse
Loader Cordyceps: IAT reuse
Loader Cordyceps: IAT reuse
C Loader loader.exe
loader.exe
ASM Text
Shellcode Shellcode
ASM Text
With Placeholder Fixed
With Placeholder Replace
placeholder
Shellcode IAT
With Placeholder inject
Loader Cordyceps: IAT reuse
Replaced
Demo SuperMega UI
● Templates
Cordyceps
.rdata Reuse
Loader Problem: Shellcode Data Reference
Or, alternatively:
● Interleave data in code
● Jump over it
Loader Cordyceps: .rdata reuse
Cordyceps:
Inject shellcode into executable .text
Behaviour
Telemetry
EDR
Loader EDR Design
1 VirtualAlloc RW 1 VirtualAlloc(RW)
2 memcpy 2 memcpy
3 VirtualProtect RX 3 VirtualProtect RX
4 CreateNewThread() 4 jmp
Cordyceps
EDR deconditioning
Loader EDR Deconditioning
Sirallocalot:
● Do 10 times:
○ Do 100 times:
■ Alloc memory RW with shellcode_len
■ Copy fake data into memory
■ Change to RX
■ Leave it for a bit
○ Free 100
Loader EDR Deconditioning
.text
SuperMega
Loader
Payload
Shellcode C2 doing its thing
Unbacked
Loader Bubbles of Bane
Supermega:
● No signature
○ Or easy changeable
File Scan Mem Scan ● Very little telemetry
AV EDR ○ All look normal
○ From backed memory
● Will not trigger mem scan
○ But susceptible to
on-demand mem scan
Behaviour
○ pe-sieve, moneta
Telemetry
EDR
Loader Anti EDR Techniques used for SuperMega Loader
Callstack spoofing? No
APC calls? No
Stageless:
windows/meterpreter_reverse_tcp
Loader Loader vs. Payload
Loader Payload
syscall ETW
ntdll.dll OS
ETW-TI
amsi.dll AMSI
pipe
EDR
PEB Process Info
EPROCESS
Callstack
Memory Scan
File Scan
File
Loader Loader Design
Alloc
EXE Execution Anti EDR
Decode Payload
Loader Guardrails Emulation Deconditioning
Exec
Is able to load:
Nonstaged Winhttp Metasploit with disabled stdapi, and CobaltStrike 4.9 default
config
● On Win10/Win11 Defender with no alerts
● On Win11 MDE with low-rated alerts
As of August 2024
Loader Outlook
More details:
https://blog.deeb.ch/posts/how-edr-works
https://blog.deeb.ch/posts/exe-injection
https://blog.deeb.ch/posts/supermega
SuperMega Loader:
https://github.com/dobin/SuperMega
Soon:
https://github.com/dobin/RedEdr
Loader References
Proposal
CobaltStrike 4.10