• Stars
    star
    294
  • Rank 135,932 (Top 3 %)
  • Language
    C
  • Created 8 months ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Encrypted shellcode Injection to avoid Kernel triggered memory scans

Caro Kann

Caro Kann defense

Encrypted shellcode Injection to avoid memory scans triggered from Kernel (ETWti / Kernel Callbacks). Specific combinations of Windows APIs, e.g. for injection into a remote process can lead to a memory scan:

ScanTrigger

Typically, the scan can be triggered from Userland via hooks on the execute primitive such as NtCreateThreadEx. But more and more EDR vendors also tend to trigger scans from Kernel, for example after the Kernel Callback PsSetCreateThreadNotifyRoutine() a scan could be triggered. But what if there is no executable memory section with known malicious code? Well, no alert for an detection I guess.

The idea is as follows:

  • Inject encrypted known malicious payload into an RW section
  • Inject custom non known malicious shellcode into an RX section
  • Create a remote Thread on the second shellcode

Inject

The custom shellcode will than:

  • Sleep for an amount x (to avoid memory scans triggered by the execute primitive of Thread creation)
  • Decrypt the first known malicious shellcode
  • Protect the section from RW to RX
  • Make a direct JMP to the known malicious shellcode

Shellcode

Setup

On linux, the PIC-Code was found to be compiled correctly with mingw-w64 version version 10-win32 20220324 (GCC). With that version installed, the shellcode can be compiled with a simple make and extracted from the .text section via bash extract.sh.

If you'd like to compile from Windows, you can use the following commands:

as -o adjuststack.o adjuststack_as.asm
gcc ApiResolve.c -Wall -m64 -ffunction-sections -fno-asynchronous-unwind-tables -nostdlib -fno-ident -O2 -c -o ApiResolve.o -Wl,--no-seh
gcc DecryptProtect.c -Wall -m64 -masm=intel -ffunction-sections -fno-asynchronous-unwind-tables -nostdlib -fno-ident -O2 -c -o decryptprotect.o -Wl,--no-seh
ld -s adjuststack.o ApiResolve.o decryptprotect.o -o decryptprotect.exe
gcc extract.c -o extract.exe
extract.exe

You also need to have Nim installed for this PoC.

After installation, the dependencies can be installed via the following oneliner:

nimble install winim ptr_math

The PoC can than be compiled with:

nim c -d:release -d=mingw -d:noRes CaroKann.nim # Cross compile
nim c -d:release CaroKann.nim # Windows

Any payload can be XOR encrypted with the given encrypt.cpp code:

Usage: encrypter.exe input_file output_file

The encrypted payload can than be embedded in the PoC via the following line:

const shellcode = slurp"<encrypted.bin>"

OPSec improvement ideas

  • Bypass Userland-Hooks for Injection (although not really needed, but for fun)
  • Back Payload(s) by legitimate DLL (Module Stomping)
  • Load C2-Dlls via the first Shellcode - which can avoid memory scans triggered by module loads
  • Use ThreadlessInject or DLLNotificationInjection instead of Remote Thread Creation

OPSec considerations for C2-Payloads

  • Should use Sleep encryption, otherwise the payload will get flagged later
  • Should use Unhooking first or (in)direct Syscalls
  • Should use Proxy module loading

More Repositories

1

WinPwn

Automation for internal Windows Penetrationtest / AD-Security
PowerShell
3,153
star
2

Pentest-Tools

2,063
star
3

Amsi-Bypass-Powershell

This repo contains some Amsi Bypass methods i found on different Blog Posts.
1,465
star
4

PowerSharpPack

PowerShell
1,380
star
5

OffensiveVBA

This repo covers some code execution and AV Evasion methods for Macros in Office documents
VBA
1,131
star
6

Creds

Some usefull Scripts and Executables for Pentest & Forensics
PowerShell
1,007
star
7

MultiPotato

C++
493
star
8

SharpImpersonation

A User Impersonation tool - via Token or Shellcode injection
C#
391
star
9

Invoke-SharpLoader

PowerShell
333
star
10

Ruy-Lopez

C
288
star
11

SharpNamedPipePTH

Pass the Hash to a named pipe for token Impersonation
C#
286
star
12

Nim-RunPE

A Nim implementation of reflective PE-Loading from memory
Nim
253
star
13

NimGetSyscallStub

Get fresh Syscalls from a fresh ntdll.dll copy
Nim
215
star
14

SharpVeeamDecryptor

Decrypt Veeam database passwords
C#
150
star
15

NamedPipePTH

Pass the Hash to a named pipe for token Impersonation
PowerShell
139
star
16

SyscallAmsiScanBufferBypass

AmsiScanBufferBypass using D/Invoke
C#
129
star
17

Nim_DInvoke

D/Invoke implementation in Nim
Nim
97
star
18

Excel-Phish

Phish password protected Excel-Files
VBA
93
star
19

Sharp-HackBrowserData

C# binary with embeded golang hack-browser-data
C#
93
star
20

Get-System-Techniques

PowerShell
85
star
21

NimShellcodeFluctuation

ShellcodeFluctuation PoC ported to Nim
Nim
74
star
22

RDPThiefInject

RDPThief donut shellcode inject into mstsc
C#
70
star
23

Invoke-Sharpcradle

Load C# Code straight to memory
PowerShell
54
star
24

Nim_CBT_Shellcode

CallBack-Techniques for Shellcode execution ported to Nim
Nim
53
star
25

LDAP-Signing-Scanner

A little scanner to check the LDAP Signing state
46
star
26

BitwardenDecryptBrute

Wordlist attacks on Bitwarden data.json files
Python
44
star
27

SharpOxidResolver

IOXIDResolver from AirBus Security/PingCastle
C#
40
star
28

SharpPolarBear

Privesc through import of Sheduled tasks + Hardlinks - CVE-2019-1069
C#
36
star
29

SharpByeBear

AppXSVC Service race condition - privilege escalation
C#
26
star
30

S3cur3Th1sSh1t

23
star
31

TeamViewerDecrypt

PowerShell
17
star
32

SharpLigolo

C# wrapper for ligolo
C#
16
star
33

Invoke-WMI-Information

Straight forward script for WMI information gathering (local or remote)
13
star
34

EmpEISDecrypt

Decrypt Matrix42 Empirum /EIS Passwords
C#
11
star
35

NimWinstaEveryoneAccess

Nim
10
star
36

darkamour_clone

Objective-C
9
star
37

WinFor

Powershell script to execute different forensic Powershell functions / tools on a compromised host
PowerShell
7
star
38

ssdp-poisoning

Python
7
star
39

MimiMisc

C
6
star
40

SSJI---JSGen

Just a copy from here: https://gitlab.com/0x4ndr3/blog/blob/master/JSgen/JSgen.py
Python
5
star
41

Hosts-File---AD-Tracking-Blocker

Hosts File for Blocking Advertising & Tracking Domains
3
star