1 of 175

My first

and Last

Shellcode Loader

Dobin Rutishauser

Red Team Lead, Raiffeisen Schweiz

Commsec Track

29 AUG

Slides: https://bit.ly/4dGhBXl

2 of 175

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

3 of 175

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

4 of 175

Intro

5 of 175

Intro

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

Create C2

Implant

???

Send .exe

to victim

Pack in

.exe

Profit

Loader

6 of 175

Motivation: Initial Access with C2

Loader

7 of 175

Motivation: Initial Access with C2

Loader

8 of 175

Why

“EDR bypass this”

“EDR bypass that”

“New EDR bypass technique”

“How i bypassed EDR”

“Usermode unhooking to bypass EDR”

  • People dont understand EDR
  • People dont know what they are bypassing
  • People develop super advanced low level Anti-EDR techniques which create more telemetry than they solve

Loader

9 of 175

Processes

10 of 175

Program vs. Process

Code

Header

Data

Program.exe

Process

Windows Loader

Code

Data

Harddisk

RAM

Loader

11 of 175

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

12 of 175

Memory Region Permissions

Code

Header

Data

Program.exe

Process

Code

Data

Read, Execute

Read, Write

Loader

13 of 175

Memory Region Backed vs. Unbacked

Code

Header

Data

Program.exe

Process

Code

Data

Backed

Backed

VirtualAlloc’d

Unbacked

Loader

14 of 175

Loader

15 of 175

Process Memory Regions

Loader

16 of 175

Shellcode Loader Example

17 of 175

Shellcode: Calc

Loader

18 of 175

Shellcode: Calc

Loader

19 of 175

Shellcode: Loader

Need:

  • Shellcode (payload)
  • VirtualAlloc memory
  • Copy shellcode to memory
  • Exec memory

Loader

20 of 175

Shellcode Loader: 1/3 VirtualAlloc

Code

Data

RWX Region

VirtualAlloc(RWX)

Create new region in process

Payload

Loader

21 of 175

Shellcode Loader: 2/3 Copy

Code

Data

RWX Region

Copy Payload to RWX Region

Payload

Loader

22 of 175

Shellcode Loader: 3/3 Exec

Code

Data

RWX Region

execute payload

(shellcode / memory region)

Payload

Loader

23 of 175

Shellcode Loader Structure

  • The payload / shellcode to execute
    • In .data, .rdata, .text, from a file
    • Encoded, encrypted, base64, xor’d…
  • The writeable/executable memory
    • VirtualAlloc()
    • NtAllocateVirtualMemory()
    • HeapAlloc()
  • The copy
    • for() loop
    • memcpy() / memmove()
    • RtlCopyMemory(), CopyMemory(), MoveMemory()
  • The execution
    • Just jmp to it: ((void(*)())exec)();
    • CreateThread(), QueueUserWorkItem()
    • QueueUserApc()
    • Windows functions which use a callback
  • Shellcode can be a reflective DLL

Alloc RWX

Decode

Copy

RWX

Shellcode

Exec

Loader

24 of 175

Shellcode Loader

In other languages

25 of 175

Shellcode Loader: .NET / C#

Loader

26 of 175

Shellcode Loader: Powershell

Loader

27 of 175

Shellcode Loader: VBA

Loader

28 of 175

Shellcode Loader: Remote Process Injection

Code

Data

RWX

Teams.exe

Process

Code

Data

Shellcode

Loader.exe

Process

OpenProcess()

VirtualAllocEx()

WriteProcessMemory()

Shellcode

Loader

29 of 175

Shellcode Loader: Remote Process Injection

Loader

30 of 175

Anti Virus Detection

31 of 175

Loader: Unencrypted Payload

Alloc RWX

Copy

RWX

Payload

Exec

Loader

32 of 175

Loader: Unencrypted Payload

Code

Data

Payload

Scan File

Signature Scan

loader.exe

AV

Write-File Event

OS

Loader

33 of 175

DEMO 1

DEMO: Show AV finds unencrypted metasploit

Loader

34 of 175

AntiVirus - Encrypted Payload

35 of 175

Loader: Unencrypted Payload

Alloc RWX

Copy

RWX

Payload

Encrypted

Exec

Loader

36 of 175

Loader: Encrypted payload

“Encryption” can be anything

  • XOR
  • ROT13
  • ADD 1
  • ZIP
  • Base64

Theres no need to:

  • AES, RC4 etc.
  • Low entropy / steganography
  • Hide it / steganogrphy / low entropy (like SVG, CSS, UUID, CSV)

Code

Data

Payload

Encrypted

Signature Scan

Loader

37 of 175

DEMO 2

DEMO: Show AV with encrypted metasploit

Loader

38 of 175

AntiVirus

AV Emulator

39 of 175

AV Emulator

AV Emulator:

  • “Interpret” PE file
  • Virtual CPU, Windows

It is not:

  • Virtualization
  • Sandbox
  • Full Emulation (Bochs)
  • Wine

Loader

40 of 175

AV Emulator

Emulate binary until condition is met

Signature Memory Scan after that

Cut-off condition:

  • Time
  • Number of instructions
  • Number of API Calls
  • Amount of memory used

Emulating

EXE

Cut-Off reached?

Memory Scan

Loader

41 of 175

Anti AV Emulator

Process

Anti

Emulation

Payload

Alloc

Copy

Exec

AV Emulation

Payload

Encrypted

Loader

42 of 175

Anti AV Emulator

Process

Anti

Emulation

Payload

Alloc

Copy

Exec

AV Emulation

Payload

Encrypted

Static Code Analysis

Loader

43 of 175

DEMO 3

DEMO: AV does NOT find encrypted metasploit with Anti-Emulation

  • Show Anti-Emulation

Loader

44 of 175

Detection in Middleboxes

Dynamic Analysis

45 of 175

Middleboxes

Client

Email

Gateway

Web

Proxy

Teams

Sharepoint

Malware

AV

AV

AV

AV

Sandbox

Loader

46 of 175

Execution Guardrails

  • AD Domain
  • Username
  • Installed Software
  • IP Address
  • Vmtools installed
  • # CPUs, RAM
  • Vmware Drivers

Execution guardrails:

  • Environment check
  • Environmental keying
  • Sandbox / VM detection

Loader

47 of 175

Anti AV Emulator

Process

Execution

Guardrails

Payload

Alloc

Copy

Exec

Sandbox

Payload

Encrypted

Loader

48 of 175

Loader Design

Conclusion

49 of 175

Loader Summary

Process

Execution

Guardrails

Payload

Alloc

Copy

Exec

Middleboxes (off target)

Payload

Encrypted

Anti

Emulation

AV Emulator

Static Analysis

Loader

50 of 175

Loader Problem

Process

Execution

Guardrails

Payload

Alloc

Copy

Exec

Payload

Encrypted

Anti

Emulation

EDR

Telemetry

Memory Scan

Loader

51 of 175

EDR Fundamentals

52 of 175

EDR

EDR:

  • Agent on each System
  • Find malicious processes

Loader

53 of 175

EDR

EDR is blackbox

Many different EDR

Rapid development

Therefore:

  • Focus on what the EDR sees
  • Not the detections itself
  • Whats the input?
  • Create a framework to reason about EDR

EDR

Blackbox

Input

Alerts

Loader

54 of 175

EDR - Bubbles of Bane

File Scan

AV

Mem Scan

EDR

Behaviour

Telemetry

EDR

Signatures

Loader

55 of 175

EDR Input: Usermode-Hooks

56 of 175

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

57 of 175

Usermode Hooks

EDR

Process

Ntdll.dll

Hooked

Windows

Kernel

Syscall

Usermode Hooks

Hook

DLL

Loader

58 of 175

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

59 of 175

Usermode Hooks

Typically hooked functions:

  • VirtualAlloc, VirtualProtect
  • MapViewOfFile, MapViewOfFile2
  • VirtualAllocEx, VirtualProtectEx
  • QueueUserAPC
  • SetThreadContext
  • WriteProcessMemory, ReadProcessMemory

Loader

60 of 175

EDR Input List

61 of 175

EDR Inputs

OS

Process

ntdll.dll

amsi.dll

EtwWrite()

syscall

pipe

Kernel Callbacks

ETW

ETW-TI

EDR

Usermode Hooks

Loader

62 of 175

EDR Input

Kernel Callbacks

63 of 175

Kernel Callbacks

void CreateProcessNotifyRoutine(parent_process, pid, createInfo)

void CreateThreadNotifyRoutine(ProcessId, ThreadId, Create);

void LoadImageNotifyRoutine(FullImageName, ProcessId, ImageInfo);

void ObCallback(RegistrationContext, PreInfo);

Loader

64 of 175

Kernel Callbacks

Loader

65 of 175

EDR Input

ETW

66 of 175

ETW

Loader

67 of 175

ETW Providers

Loader

68 of 175

ETW Providers, Loader relevant

ETW Provider

Info

Microsoft-Windows-Kernel-Process

  • Process Start/Stop
  • Thread Start/Stop
  • Image Loads

Microsoft-Windows-Security-Auditing

  • Process Start/Stop
  • Security Operations

Microsoft-Antimalware-*

  • Defender Internals

<tbd>

Loader

69 of 175

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

Basically same as Kernel Callbacks

Loader

70 of 175

ETW Provider: Microsoft-Windows-Security-Auditing

Loader

71 of 175

Two Sides of ETW

OS

Process

(Etw)EventWrite()

ETW

EDR

ETW

ETW

Loader

72 of 175

EDR Input

ETW-TI

73 of 175

ETW-TI

ETW-Threat Intelligence

The good shit

Few consumers (Defender?)

Req PPL’d and signed process

Loader

74 of 175

EDR Input

Query Process

75 of 175

Query Process Information

Most events only have very little information

  • PID
  • ThreadID
  • What happened (Image allocation at address x)

Loader

76 of 175

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

77 of 175

EDR: Query Process Information

Query Process Information:

  • Parent Process Id
  • Image filename (source exe)
  • Command line parameters
  • Loaded DLL’s

Note:

  • PPID Spoofing
  • Command line argument Spoofing

NtQueryInformationProcess()

Process

PEB

EPROCESS

Loader

78 of 175

EDR: Memory Scanning

Signature scan (like in files)

Performance intensive - only on trigger

Process

Code

Data

Loader

79 of 175

EDR: Callstack Analysis

Callstack:

  • On NtApi Call (AMSI or syscall)
  • List of addresses of all previous parent functions

Loader

80 of 175

EDR: Callstack Analysis

Process

ntdll.dll

amsi.dll

syscall

EDR

AMSI

Process

Callstack

OS

Stack

.text

Loader

81 of 175

Callstack analysis - Elastic

Elastic has callstack analysis rules for:

  • Direct syscalls
  • Callback-based evasion
  • Module Stomping
  • Library loading from unbacked region
  • Process created from unbacked region

Callstack analysis for:

  • VirtualAlloc, VirtualProtect
  • MapViewOfFile, MapViewOfFile2
  • VirtualAllocEx, VirtualProtectEx
  • QueueUserAPC
  • SetThreadContext
  • WriteProcessMemory, ReadProcessMemory

Loader

82 of 175

EDR Performance

83 of 175

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

84 of 175

Time in Event Processing

EDR

Input Events

Query Process Info (QPI)

time

Loader

85 of 175

Sysmon

  • MD5 hashes of images
  • Callstack (ProcessAccess)
  • Current Working Directoy

Process

Kernel Callbacks

ETW

Process Info

Memory Scan

OS

ETW

Sysmon

Loader

86 of 175

EDR Example Attacks

87 of 175

Usermode-hook patch

88 of 175

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

89 of 175

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

90 of 175

“EDR bypass”

91 of 175

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

92 of 175

Callstack Spoofing

93 of 175

Callstack Spoofing

Callstack:

  • List of addresses of all previous parent functions

Loader

94 of 175

Callstack Spoofing

Callstack patch: Modify process/thread stack return addresses

Loader

95 of 175

Callstack Spoofing

EDR

Stack

Process

Query Callstack

.text

OS

NtApi

Unbacked shellcode

Patch

Stack

Loader

96 of 175

Image Spoofing

97 of 175

Image Spoofing

.text

Start Suspended

Overwrite Memory

Resume Process

notepad.exe

C2

Loader

98 of 175

Module Stomping

99 of 175

Module Stomping

C2

openssl.dll

.text

LoadLibrary(“openssl.dll”)

Overwrite Memory

Start Thread

notepad.exe

Loader

100 of 175

Memory Encryption

101 of 175

Memory Encryption

.text

.data

Active

.text

encrypted

.data

encrypted

Sleep

EDR

Sleep()

Memory Scan

Loader

102 of 175

EDR Attacks Summary

103 of 175

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

104 of 175

SuperMega Loader

Cordyceps Technique

105 of 175

Loader injection

Payload

encoded

Carrier

Loader

=

Loader

Putty, 7zip, ...

PIC, Shellcode

program.exe

Loader

106 of 175

Code Similarity Scanning

Malware Detection:

Code Similary Scanning

Compare code in EXE files with known bad

  • Find new versions of malware
  • Find code of existing malware in new files
  • “Are QBot and PikaBot related?”
  • “This looks like QBot”

Loader

107 of 175

Machine Learning

Machine Learning

  1. Train Neural Network on malware files
  2. ???
  3. Profit?

But, what is the similarity in the following malware?

  • Mimikatz
  • CobaltStrike
  • Nmap
  • Metasploit
  • Qbot
  • Rubeus
  • Psexec

Loader

108 of 175

Why file injection?

File injection:

  • Harder to find the malicious code
    • Lots of “code”
    • Code similarity searches fail
    • No “Good code stuffing”
  • Existing Meta information in the PE
    • 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?

.text

7zip.exe

Loader

Shellcode

Loader

109 of 175

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

110 of 175

RedBackdoorer

https://github.com/mgeeky/ProtectMyTooling/blob/master/RedBackdoorer.py

Loader

111 of 175

Disassembled PE Entry Point (main)

Loader

112 of 175

SuperMega

Shellcode generation

113 of 175

SuperMega: Shellcode Creation

C

Shellcode

ASM Text

Loader

114 of 175

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

115 of 175

SuperMega: Shellcode Creation

.c

�Template

.c

Rendered

.asm

Compiled

.asm

Cleaned

.exe

Compiled

.bin

Shellcode

jinja2

cl.exe

masm_shc

ml64.exe

pefile

Loader

116 of 175

Demo

Demo SuperMega UI

  • C -> ASM
  • Phases
  • Options

Loader

117 of 175

Cordyceps

118 of 175

Cordyceps Motivation

Improve “From C project, through assembly, to shellcode”

Goal:

  • Less signaturable
  • Less obviously malware

Make it look as genuine as possible

Loader

119 of 175

Cordyceps

Original Loader PEB Walk

120 of 175

PEB Walk

Calling functions in shellcode:

  • Locate the PEB
  • Access Ldr data structure: PEB->Ldr
    • Traverse module list (find “ntdll.dll”)
      • Get export table of module
      • Resolve function address

Loader

121 of 175

PEB Walk

NtApi

kernel32.dll

VirtualAllocEx

Ntdll.dll

NtAllocateVirtualMemory

Kernel

NtAllocateVirtualMemory

syscall

WinApi

NO

PEB Walk

Find this

Loader

122 of 175

PEB Walk

Loader

123 of 175

PEB Walk

Loader

124 of 175

PEB Walk

  • Why cant we call functions like the program itself?
    • Avoiding the PEB walk

Loader

125 of 175

IAT calls

The normal way

126 of 175

IAT Call

Loader

127 of 175

IAT Call

.text

IAT

User32.dll

MessageBoxW()

Call iat:

MessageBoxW

Call User32.dll:

MessageBoxW()

Loader

128 of 175

IAT Call

Call IAT:

IAT:

Loader

129 of 175

IAT Call

0x140001017 + 0x1063 - 6 = 0x140002080

0x140002080

6 bytes

Loader

130 of 175

Cordyceps

IAT Reuse

131 of 175

Cordyceps: IAT reuse

IAT reuse:

  • Goal: Get rid of PEB_WALK
  • Solution: Relative call to IAT

Problem:

  • MASM doesnt support relative call’s
  • Solution: Patch shellcode in the infected binary

Loader

132 of 175

Cordyceps: IAT reuse

Loader

133 of 175

Cordyceps: IAT reuse

Loader

134 of 175

Cordyceps: IAT reuse

Loader

135 of 175

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

136 of 175

Cordyceps: IAT reuse

  • Find RVA of placeholder (\xd8\x4a\xcc\x09\x26\x9e)
  • Find RVA of IAT entry (GetEnvironmentVariableW())
  • Create relative “call” instruction
  • Replace placeholder with “call” instruction

Note: This is not IAT hooking, its normal IAT usage

Loader

137 of 175

Cordyceps: IAT reuse

Replaced

RVA of call address + RVA IAT = call with offset

Loader

138 of 175

Demo

Demo SuperMega UI

  • Templates

Loader

139 of 175

Cordyceps

.rdata Reuse

140 of 175

Problem: Shellcode Data Reference

Shellcode is code only

How to handle data? (function call arguments)

Loader

141 of 175

Problem: Shellcode Data Reference

Instruct compiler to push data on stack

Loader

142 of 175

Problem: Shellcode Data Reference

Or, alternatively:

  • Interleave data in code
  • Jump over it

Loader

143 of 175

Cordyceps: .rdata reuse

Both solutions look suspicious

Solution similar to IAT-reuse:

  • Inject data into .rdata section
  • Patch shellcode in exe to reference it
    • Relative load

.rdata

.text

shellcode

ref

Inject code

Inject data

Shellcode data

Loader

144 of 175

Cordyceps: .rdata reuse

Loader

145 of 175

Cordyceps Technique

146 of 175

Cordyceps Technique

Cordyceps:

Inject shellcode into executable .text

Patch injected shellcode:

  • IAT reuse
  • .rdata reuse

Result: Cant differentiate from genuine program

  • No IOC’s
  • No shellcode detection possible

The restrictions of shellcode dont apply when EXE injections is performed

Like in “The last of us”

Loader

147 of 175

Demo 4

Demo: Demo 3 Metasploit Meterpreter execution

  • Defender: No detection
  • MDE: Detection

Loader

148 of 175

Anti EDR

149 of 175

Goal: Avoid Memory Scan Trigger

File Scan

AV

Mem Scan

EDR

Behaviour

Telemetry

EDR

File Carrier / Loader

With Encrypted Payload

Unencrypted Payload

Loader

150 of 175

EDR Design

  • High performance required
  • Little information available
  • A lot of noise in the system

  • Focus: Unbacked memory
    • Unbacked RWX memory
    • Threads starting in unbacked memory
    • Calls into kernel from unbacked memory
    • Unbacked RX memory (going RW)
  • Backed = already AV Scanned

.code

VirtualAlloc

.code

Backed

Unbacked

Loader

151 of 175

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

152 of 175

Cordyceps

EDR deconditioning

153 of 175

EDR Deconditioning

Make EDR tired of scanning our memory

Copy carrier functionality

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

154 of 175

EDR Deconditioning

Like pavlov’s dogs

Ring the bell a lot

Loader

155 of 175

Demo 5

Demo with sirallocalot MDE

Loader

156 of 175

Conclusion

157 of 175

Basic Assumption

  • It seems there is not enough information to identify loader based on telemetry
    • Only Process / Thread / Image loads
    • Loader doesnt use networking, file or registry access
  • Telemetry may be there for loader mischief
    • unbacked RW -> RX changes
    • Modifying backed regions
  • But not used

Loader

158 of 175

Self-Stomping

Loader is integrated in backed image section

  • Makes it trustworthy

.text

SuperMega

Loader

Payload

Shellcode

Unbacked

C2 doing its thing

Loader

159 of 175

Bubbles of Bane

Supermega:

  • No signature
    • Or easy changeable
  • Very little telemetry
    • All look normal
    • From backed memory
  • Will not trigger mem scan
    • But susceptible to on-demand mem scan
    • pe-sieve, moneta

File Scan

AV

Mem Scan

EDR

Behaviour

Telemetry

EDR

Loader

160 of 175

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

161 of 175

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

162 of 175

Things to avoid in payload

Payload should not do fancy memory things

  • No Stagers
  • No Reflective DLL

Staged:

windows/meterpreter/reverse_http

Stageless:

windows/meterpreter_reverse_http

Loader

163 of 175

Loader vs. Payload

Loader

Payload

Loader loads the payload

  • CobaltStrike, Sliver, Brute ratel, havoc…
  • Give the payload best possible changes

C2 should protect itself

  • Leave it to the experts
    • Memory encryption
    • Callstacks

Loader

164 of 175

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

165 of 175

Loader Design

EXE

Loader

Execution

Guardrails

Payload

Alloc

Decode

Exec

Anti

Emulation

Payload

Encrypted

EDR

Deconditioning

When doing your own loader:

  • EDR bypass really necessary? (usermode hook patching)
  • Strong encryption / entropy really important?
  • Focus on:
    • Backed memory
    • No RWX
    • No RX -> RW
    • Clean Callstacks
  • Careful with process injection

Alternatives:

  • DLL Sideloading

Loader

166 of 175

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

  • On Win10/Win11 Defender with no alerts
  • On Win11 MDE with low-rated alerts

As of August 2024

Loader

167 of 175

Outlook

  • Execution Guardrails are very powerful
    • Do them early
  • Injecting shellcode into .exe’s is… nice
    • Looks genuine. Can thwart automated analysis
    • Makes manual analysis maybe a bit harder
    • Different than creating your own malicious exe’s
    • Different than shellcode inject through some other means
  • Injecting shellcode into .dll’s is cool
  • SuperMega loader is… ok
    • Writing C to inject as shellcode into an .exe is a nice workflow to have
    • Good against file based scanning
    • Not a super special new anti EDR or memory scanning
    • But difficult of being AV sig’ed
  • RWX reuse maybe better against memory analysis tools
  • Need framework for loader-chaining

Loader

168 of 175

My First and Last Shellcode Loader

My First Shellcode Loader

  • Using Linux exploit development know-how
  • Learning a lot about Windows

My Last Shellcode Loader

  • Works forever
  • Debugging sucks

Loader

169 of 175

Stuff

Loader

170 of 175

References

Loader

171 of 175

Additoinal Loader Tricks

172 of 175

Self Stomping

  • Inject dll in .text (pre-loaded, encrypted)
  • Fixup:
    • RW it (part of .text)
    • Decrypt, apply reloc’s etc.
    • RX it again
  • Result: DLL in modified .text
    • Backed memory region

.text

SuperMega

Loader

Payload DLL

Encrypted

.text

SuperMega

Loader

Payload DLL

Loader

173 of 175

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

174 of 175

UPX as EXE

  • UPX has RWX sections
    • Obfuscate payload with Shikata ga nai obfuscator

Loader

175 of 175

Advanced C2

Loader

CobaltStrike

“Stub”

CobaltStrike

Backend

CobaltStrike

Caller

CobaltStrike 4.10

Proposal

Loader