Coder Social home page Coder Social logo

primody / caro-kann Goto Github PK

View Code? Open in Web Editor NEW

This project forked from s3cur3th1ssh1t/caro-kann

0.0 0.0 0.0 203 KB

Encrypted shellcode Injection to avoid Kernel triggered memory scans

Shell 0.31% C++ 4.90% C 72.90% Assembly 2.18% Nim 18.31% Makefile 1.41%

caro-kann's Introduction

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

caro-kann's People

Contributors

s3cur3th1ssh1t avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.