My first
and Last
Shellcode Loader
Dobin Rutishauser
Red Team Lead, Raiffeisen Schweiz
Commsec Track
29 AUG
Slides: https://bit.ly/4dGhBXl
About Me
Developer // TerreActive
Pentester // Compass Security
Developer // UZH
SOC Analyst // Infoguard
RedTeam Lead // Raiffeisen
SSL/TLS Recommendations�// OWASP Switzerland
Burp Sentinel - Semi Automated Web Scanner�// BSides Vienna
Automated WAF Testing and XSS Detection�// OWASP Switzerland Barcamp
Fuzzing For Worms - AFL For Network Servers�// Area 41
Develop your own RAT - EDR & AV Defense�// Area 41
Avred - Analyzing and Reverse Engineering AV Signatures�// HITB
Memory Corruption Exploits & Mitigations�// BFH - Bern University of Applied Sciences
Gaining Access�// OST - Eastern Switzerland University of Applied Sciences
2
Loader
Content
How loader works
Payload detection & bypass
Make Shellcode & EXE Injection
Antivirus, 10min
Intro to Loader, 5min
EDR, 20min
Supermega & Cordyceps, 20min
01
02
03
04
Analysis & Conclusion
Anti-EDR, 5min+
05
EDR Input & Attacks
3
Loader
Intro
Intro
Target Audience
Me:
Create C2
Implant
???
Send .exe
to victim
Pack in
.exe
Profit
Loader
Motivation: Initial Access with C2
Loader
Motivation: Initial Access with C2
Loader
Why
“EDR bypass this”
“EDR bypass that”
“New EDR bypass technique”
“How i bypassed EDR”
“Usermode unhooking to bypass EDR”
Loader
Processes
Program vs. Process
Code
Header
Data
Program.exe
Process
Windows Loader
Code
Data
Harddisk
RAM
Loader
File vs. Process Analysis
Code
Header
Data
Program.exe
Process
Code
Data
Antivirus
Signatures
Yara
File Hash
Imports
Disassembler
Decompiler
Memory scanning
Sandbox
EDR
Debugger
Static Analysis
Dynamic Analysis
Behaviour Analysis
Loader
Memory Region Permissions
Code
Header
Data
Program.exe
Process
Code
Data
Read, Execute
Read, Write
Loader
Memory Region Backed vs. Unbacked
Code
Header
Data
Program.exe
Process
Code
Data
Backed
Backed
VirtualAlloc’d
Unbacked
Loader
Loader
Process Memory Regions
Loader
Shellcode Loader Example
Shellcode: Calc
Loader
Shellcode: Calc
Loader
Shellcode: Loader
Need:
Loader
Shellcode Loader: 1/3 VirtualAlloc
Code
Data
RWX Region
VirtualAlloc(RWX)
Create new region in process
Payload
Loader
Shellcode Loader: 2/3 Copy
Code
Data
RWX Region
Copy Payload to RWX Region
Payload
Loader
Shellcode Loader: 3/3 Exec
Code
Data
RWX Region
execute payload
(shellcode / memory region)
Payload
Loader
Shellcode Loader Structure
Alloc RWX
Decode
Copy
RWX
Shellcode
Exec
Loader
Shellcode Loader
In other languages
Shellcode Loader: .NET / C#
Loader
Shellcode Loader: Powershell
Loader
Shellcode Loader: VBA
Loader
Shellcode Loader: Remote Process Injection
Code
Data
RWX
Teams.exe
Process
Code
Data
Shellcode
Loader.exe
Process
OpenProcess()
VirtualAllocEx()
WriteProcessMemory()
Shellcode
Loader
Shellcode Loader: Remote Process Injection
Loader
Anti Virus Detection
Loader: Unencrypted Payload
Alloc RWX
Copy
RWX
Payload
Exec
Loader
Loader: Unencrypted Payload
Code
Data
Payload
Scan File
Signature Scan
loader.exe
AV
Write-File Event
OS
Loader
DEMO 1
DEMO: Show AV finds unencrypted metasploit
Loader
AntiVirus - Encrypted Payload
Loader: Unencrypted Payload
Alloc RWX
Copy
RWX
Payload
Encrypted
Exec
Loader
Loader: Encrypted payload
“Encryption” can be anything
Theres no need to:
Code
Data
Payload
Encrypted
Signature Scan
Loader
DEMO 2
DEMO: Show AV with encrypted metasploit
Loader
AntiVirus
AV Emulator
AV Emulator
AV Emulator:
It is not:
Loader
AV Emulator
Emulate binary until condition is met
Signature Memory Scan after that
Cut-off condition:
Emulating
EXE
Cut-Off reached?
Memory Scan
Loader
Anti AV Emulator
Process
Anti
Emulation
Payload
Alloc
Copy
Exec
AV Emulation
Payload
Encrypted
Loader
Anti AV Emulator
Process
Anti
Emulation
Payload
Alloc
Copy
Exec
AV Emulation
Payload
Encrypted
Static Code Analysis
Loader
DEMO 3
DEMO: AV does NOT find encrypted metasploit with Anti-Emulation
Loader
Detection in Middleboxes
Dynamic Analysis
Middleboxes
Client
Gateway
Web
Proxy
Teams
Sharepoint
Malware
AV
AV
AV
AV
Sandbox
Loader
Execution Guardrails
Execution guardrails:
Loader
Anti AV Emulator
Process
Execution
Guardrails
Payload
Alloc
Copy
Exec
Sandbox
Payload
Encrypted
Loader
Loader Design
Conclusion
Loader Summary
Process
Execution
Guardrails
Payload
Alloc
Copy
Exec
Middleboxes (off target)
Payload
Encrypted
Anti
Emulation
AV Emulator
Static Analysis
Loader
Loader Problem
Process
Execution
Guardrails
Payload
Alloc
Copy
Exec
Payload
Encrypted
Anti
Emulation
EDR
Telemetry
Memory Scan
Loader
EDR Fundamentals
EDR
EDR:
Loader
EDR
EDR is blackbox
Many different EDR
Rapid development
Therefore:
EDR
Blackbox
Input
Alerts
Loader
EDR - Bubbles of Bane
File Scan
AV
Mem Scan
EDR
Behaviour
Telemetry
EDR
Signatures
Loader
EDR Input: Usermode-Hooks
Usermode Hooks
NtApi
kernel32.dll
OpenProcess
Ntdll.dll
NtOpenProcess
Kernel
NtOpenProcess
syscall
kernel32.dll
VirtualAllocEx
Ntdll.dll
NtAllocateVirtualMemory
Kernel
NtAllocateVirtualMemory
syscall
WinApi
Usermode Hook
Usermode Hook
Kernel
Loader
Usermode Hooks
EDR
Process
Ntdll.dll
Hooked
Windows
Kernel
Syscall
Usermode Hooks
Hook
DLL
Loader
Usermode Hooks: Patching ntdll.dll
App.exe
Kernel32.dll::
OpenProcess()
Ntdll.dll::
NtOpenProcess()
OS
Kernel
jmp callback
syscall
Amsi.dll
NtCreateFileTrampoline()
syscall
EDR
notify
Loader
Usermode Hooks
Typically hooked functions:
Loader
EDR Input List
EDR Inputs
OS
Process
ntdll.dll
amsi.dll
EtwWrite()
syscall
pipe
Kernel Callbacks
ETW
ETW-TI
EDR
Usermode Hooks
Loader
EDR Input
Kernel Callbacks
Kernel Callbacks
void CreateProcessNotifyRoutine(parent_process, pid, createInfo)
void CreateThreadNotifyRoutine(ProcessId, ThreadId, Create);
void LoadImageNotifyRoutine(FullImageName, ProcessId, ImageInfo);
void ObCallback(RegistrationContext, PreInfo);
Loader
Kernel Callbacks
Loader
EDR Input
ETW
ETW
Loader
ETW Providers
Loader
ETW Providers, Loader relevant
ETW Provider | Info |
Microsoft-Windows-Kernel-Process |
|
Microsoft-Windows-Security-Auditing |
|
Microsoft-Antimalware-* |
|
<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.
ProcessStart data:
Basically same as Kernel Callbacks
Loader
ETW Provider: Microsoft-Windows-Security-Auditing
Loader
Two Sides of ETW
OS
Process
(Etw)EventWrite()
ETW
EDR
ETW
ETW
Loader
EDR Input
ETW-TI
ETW-TI
ETW-Threat Intelligence
The good shit
Few consumers (Defender?)
Req PPL’d and signed process
Loader
EDR Input
Query Process
Query Process Information
Most events only have very little information
Loader
EDR: Query Overview
OS
Process
ntdll.dll
amsi.dll
EtwWrite()
syscall
Kernel Callbacks
ETW
ETW-TI
AMSI
PEB
EPROCESS
File
Process Info
Memory Scan
File Scan
Process
Callstack
EDR
Loader
EDR: Query Process Information
Query Process Information:
Note:
NtQueryInformationProcess()
Process
PEB
EPROCESS
Loader
EDR: Memory Scanning
Signature scan (like in files)
Performance intensive - only on trigger
Process
Code
Data
Loader
EDR: Callstack Analysis
Callstack:
Loader
EDR: Callstack Analysis
Process
ntdll.dll
amsi.dll
syscall
EDR
AMSI
Process
Callstack
OS
Stack
.text
Loader
Callstack analysis - Elastic
Elastic has callstack analysis rules for:
Callstack analysis for:
Loader
EDR Performance
EDR Performance
If EDR is slow dev’s go to Mac. Cant let this happen.
Perf Impact | What |
1 | Event |
3 | Events Correlation |
10 | Process Query |
100 | Memory Scan |
1000 | File Scan |
Loader
Time in Event Processing
EDR
Input Events
Query Process Info (QPI)
time
Loader
Sysmon
Process
Kernel Callbacks
ETW
Process Info
Memory Scan
OS
ETW
Sysmon
Loader
EDR Example Attacks
Usermode-hook patch
Usermode Hooks
NtApi
kernel32.dll
OpenProcess
Ntdll.dll
NtOpenProcess
Kernel
NtOpenProcess
syscall
kernel32.dll
VirtualAllocEx
Ntdll.dll
NtAllocateVirtualMemory
Kernel
NtAllocateVirtualMemory
syscall
WinApi
Usermode Hook
Loader
Usermode-hook patch
Remove Userspace-Hooks by patching ntdll.dll
.text
ntdll.dll
EDR
sus?
VirtualProtect(ntdll.dll, RX->RW)
memcpy(ntdll.dll, …)
VirtualProtect(ntdll.dll, RW->RX)
Loader
“EDR bypass”
Usermode Hooks: Patching ntdll.dll
App.exe
Kernel32.dll::
OpenProcess()
Ntdll.dll::
NtOpenProcess()
OS
Kernel
jmp callback
syscall
Amsi.dll
NtCreateFileTrampoline()
syscall
EDR
Indirect Syscall
Direct Syscall
syscall
:-(
Loader
Callstack Spoofing
Callstack Spoofing
Callstack:
Loader
Callstack Spoofing
Callstack patch: Modify process/thread stack return addresses
Loader
Callstack Spoofing
EDR
Stack
Process
Query Callstack
.text
OS
NtApi
Unbacked shellcode
Patch
Stack
Loader
Image Spoofing
Image Spoofing
.text
Start Suspended
Overwrite Memory
Resume Process
notepad.exe
C2
Loader
Module Stomping
Module Stomping
C2
openssl.dll
.text
LoadLibrary(“openssl.dll”)
Overwrite Memory
Start Thread
notepad.exe
Loader
Memory Encryption
Memory Encryption
.text
.data
Active
.text
encrypted
.data
encrypted
Sleep
EDR
Sleep()
Memory Scan
Loader
EDR Attacks Summary
EDR Attacks Overview
Userspace-hook patch | Modifying backed RX memory region |
ETW patch | Modifying backed RX memory region |
Image Spoofing | Modifying backed RX memory region |
Module Stomping | Modifying backed RX memory region |
Memory Encryption | Modifying unbacked RX memory region |
Callstack spoofing | Modify process/thread stack |
Commandline spoofing | Overwrite commandline in PEB |
PPID spoofing | PROCINFO on ProcessCreate(), in EPROCESS |
Loader
SuperMega Loader
Cordyceps Technique
Loader injection
Payload
encoded
Carrier
Loader
=
Loader
Putty, 7zip, ...
PIC, Shellcode
program.exe
Loader
Code Similarity Scanning
Malware Detection:
Code Similary Scanning
Compare code in EXE files with known bad
Loader
Machine Learning
Machine Learning
But, what is the similarity in the following malware?
Loader
Why file injection?
File injection:
.text
7zip.exe
Loader
Shellcode
Loader
Basic File Injection
EXE
Header
.text
EXE
Header
.text
Loader
EXE
Header
.text
Loader
EXE
Header
.text
Loader
Plain
Overwrite main()
Middle of .text
Patch entry point
Middle of .text
Patch call
Mode = 1,1
Mode = 2,1
Loader
RedBackdoorer
https://github.com/mgeeky/ProtectMyTooling/blob/master/RedBackdoorer.py
Loader
Disassembled PE Entry Point (main)
Loader
SuperMega
Shellcode generation
SuperMega: Shellcode Creation
C
Shellcode
ASM Text
Loader
SuperMega: Shellcode Creation
char *dest = VirtualAlloc(
NULL, 202844, 0x3000, RW);
for (int n=0; n<202844; n++) {
dest[n] = supermega_payload[n];
}
if (MyVirtualProtect(
dest, 202844, RX, &res) == 0) {
return 7;
}
(*(void(*)())(dest))();
Loader
SuperMega: Shellcode Creation
.c
�Template
.c
Rendered
.asm
Compiled
.asm
Cleaned
.exe
Compiled
.bin
Shellcode
jinja2
cl.exe
masm_shc
ml64.exe
pefile
Loader
Demo
Demo SuperMega UI
Loader
Cordyceps
Cordyceps Motivation
Improve “From C project, through assembly, to shellcode”
Goal:
Make it look as genuine as possible
Loader
Cordyceps
Original Loader PEB Walk
PEB Walk
Calling functions in shellcode:
Loader
PEB Walk
NtApi
kernel32.dll
VirtualAllocEx
Ntdll.dll
NtAllocateVirtualMemory
Kernel
NtAllocateVirtualMemory
syscall
WinApi
NO
PEB Walk
Find this
Loader
PEB Walk
Loader
PEB Walk
Loader
PEB Walk
Loader
IAT calls
The normal way
IAT Call
Loader
IAT Call
.text
IAT
User32.dll
MessageBoxW()
Call iat:
MessageBoxW
Call User32.dll:
MessageBoxW()
Loader
IAT Call
Call IAT:
IAT:
Loader
IAT Call
0x140001017 + 0x1063 - 6 = 0x140002080
0x140002080
6 bytes
Loader
Cordyceps
IAT Reuse
Cordyceps: IAT reuse
IAT reuse:
Problem:
Loader
Cordyceps: IAT reuse
Loader
Cordyceps: IAT reuse
Loader
Cordyceps: IAT reuse
Loader
Cordyceps: IAT reuse
ASM Text
ASM Text
With Placeholder
loader.exe
Shellcode
With Placeholder
inject
Replace
placeholder
Shellcode
With Placeholder
C Loader
loader.exe
Shellcode
Fixed
IAT
Loader
Cordyceps: IAT reuse
Note: This is not IAT hooking, its normal IAT usage
Loader
Cordyceps: IAT reuse
Replaced
RVA of call address + RVA IAT = call with offset
Loader
Demo
Demo SuperMega UI
Loader
Cordyceps
.rdata Reuse
Problem: Shellcode Data Reference
Shellcode is code only
How to handle data? (function call arguments)
Loader
Problem: Shellcode Data Reference
Instruct compiler to push data on stack
Loader
Problem: Shellcode Data Reference
Or, alternatively:
Loader
Cordyceps: .rdata reuse
Both solutions look suspicious
Solution similar to IAT-reuse:
.rdata
.text
shellcode
ref
Inject code
Inject data
Shellcode data
Loader
Cordyceps: .rdata reuse
Loader
Cordyceps Technique
Cordyceps Technique
Cordyceps:
Inject shellcode into executable .text
Patch injected shellcode:
Result: Cant differentiate from genuine program
The restrictions of shellcode dont apply when EXE injections is performed
Like in “The last of us”
Loader
Demo 4
Demo: Demo 3 Metasploit Meterpreter execution
Loader
Anti EDR
Goal: Avoid Memory Scan Trigger
File Scan
AV
Mem Scan
EDR
Behaviour
Telemetry
EDR
File Carrier / Loader
With Encrypted Payload
Unencrypted Payload
Loader
EDR Design
.code
VirtualAlloc
.code
Backed
Unbacked
Loader
EDR Deconditioning
What will trigger a Memory Scan?
1 VirtualAlloc RW |
2 memcpy |
3 VirtualProtect RX |
4 CreateNewThread() |
1 VirtualAlloc(RW) |
2 memcpy |
3 VirtualProtect RX |
4 jmp |
Loader
Cordyceps
EDR deconditioning
EDR Deconditioning
Make EDR tired of scanning our memory
Copy carrier functionality
Sirallocalot:
Loader
EDR Deconditioning
Like pavlov’s dogs
Ring the bell a lot
Loader
Demo 5
Demo with sirallocalot MDE
Loader
Conclusion
Basic Assumption
Loader
Self-Stomping
Loader is integrated in backed image section
.text
SuperMega
Loader
Payload
Shellcode
Unbacked
C2 doing its thing
Loader
Bubbles of Bane
Supermega:
File Scan
AV
Mem Scan
EDR
Behaviour
Telemetry
EDR
Loader
Anti EDR Techniques used for SuperMega Loader
RedTeam Technique | Applied? | Aka | Examples |
ETW patch? | No | ETW bypass | |
Usermode-hook patch? | No | AMSI patch, EDR Unhooking | RefleXXion, ScareCrow |
Module stomping? | No | DLL stomping | |
Image spoofing? | No | Process Hollowing | |
Memory encryption? | No | Sleepmask | Ekko, Gargoyle, Foliage |
direct/indirect syscalls? | No | EDR bypass | SysWhisper 1/2/3, Hells Gate, Halos gate |
Callstack spoofing? | No | | |
Mess with other process? | No | Process injection | |
PPID or Argument spoofing? | No | | |
Loader
EDR Checkboxes for SuperMega Loader
Carrier code signatured? | No |
Windows API Calls coming from unbacked memory? | No |
Windows API Calls have a suspicious callstack? | No |
Change memory region from RX to RW? | No |
Hardware / Software breakpoints? | No |
APC calls? | No |
Unbacked RWX memory? | No |
Unbacked RX memory? | Yes |
Suspicious sleep state? | No |
Reflective DLL used? | No |
Loader
Things to avoid in payload
Payload should not do fancy memory things
Staged:
windows/meterpreter/reverse_http
Stageless:
windows/meterpreter_reverse_http
Loader
Loader vs. Payload
Loader
Payload
Loader loads the payload
C2 should protect itself
Loader
EDR: Query Overview
OS
Process
ntdll.dll
amsi.dll
EtwWrite()
syscall
pipe
Kernel Callbacks
ETW
ETW-TI
EDR
AMSI
PEB
EPROCESS
File
Process Info
Memory Scan
File Scan
Process
Callstack
Loader
Loader Design
EXE
Loader
Execution
Guardrails
Payload
Alloc
Decode
Exec
Anti
Emulation
Payload
Encrypted
EDR
Deconditioning
When doing your own loader:
Alternatives:
Loader
Correct Anti-EDR
SuperMega & Cordyceps
With Anti-Emulator, and sirallocalot EDR deconditioner
Is able to load:
Nonstaged Winhttp Metasploit with disabled stdapi, and CobaltStrike 4.9 default config
As of August 2024
Loader
Outlook
Loader
My First and Last Shellcode Loader
My First Shellcode Loader
My Last Shellcode Loader
Loader
Stuff
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
Matt Hand - Evading EDR
https://github.com/hasherezade/masm_shc
From a C project through assembly, to shellcode
https://www.elastic.co/security-labs
https://github.com/mgeeky/ProtectMyTooling/blob/master/RedBackdoorer.py
Loader
Additoinal Loader Tricks
Self Stomping
.text
SuperMega
Loader
Payload DLL
Encrypted
.text
SuperMega
Loader
Payload DLL
Loader
Undersized alloc trick
VirtualProtect sets the permission of the page(s) (4kb)
Use size=1, get the other 4095 bytes for free
EDR will only scan 1 byte?
// Use size 1, still change all the page
VirtualProtect(shellcode_rw, 1, RX)
Loader
UPX as EXE
Loader
Advanced C2
Loader
CobaltStrike
“Stub”
CobaltStrike
Backend
CobaltStrike
Caller
CobaltStrike 4.10
Proposal
Loader