Package: donut-shellcode
Current version:v1Table of contentsIntroductionHow It WorksBuildingUsageSubprojectsDeveloping with DonutQuestions and DiscussionsDisclaimer1. IntroductionDonutis a position-independent code that enables in-memory execution of VBScript, JScript, EXE, DLL files and dotNET assemblies. A module created by Donut can either be staged from a HTTP server or embedded directly in the loader itself. The module is optionally encrypted using theChaskeyblock cipher and a 128-bit randomly generated key. After the file is loaded and executed in memory, the original reference is erased to deter memory scanners. The generator and loader support the following features:Compression of input files with aPLib and LZNT1, Xpress, Xpress Huffman via RtlCompressBuffer.Using entropy for API hashes and generation of strings.128-bit symmetric encryption of files.Overwriting native PE headers.Storing native PEs in MEM_IMAGE memory.Patching Antimalware Scan Interface (AMSI) and Windows Lockdown Policy (WLDP).Patching Event Tracing for Windows (ETW).Patching command line for EXE files.Patching exit-related API to avoid termination of host process.Multiple output formats: C, Ruby, Python, PowerShell, Base64, C#, Hexadecimal, and UUID string.There are dynamic and static libraries for both Linux and Windows that can be integrated into your own projects. There's also a python module which you can read more about inBuilding and using the Python extension.2. How It WorksDonut contains individual loaders for each supported file type. For dotNET EXE/DLL assemblies, Donut uses the Unmanaged CLR Hosting API to load the Common Language Runtime. Once the CLR is loaded into the host process, a new Application Domain is created to allow for running Assemblies in disposable AppDomains. When the AppDomain is ready, the dotNET Assembly is loaded via the AppDomain.Load_3 method. Finally, the Entry Point for EXEs or public method for DLLs specified by the user is invoked with any additional parameters. Refer to MSDN for documentation on theUnmanaged CLR Hosting API.For a standalone example of a CLR Host, refer tocode here.VBScript and JScript files are executed using the IActiveScript interface. There's also minimal support for some of the methods provided by the Windows Script Host (wscript/cscript). For a standalone example, refer tocode here.For a more detailed description, read:In-Memory Execution of JavaScript, VBScript, JScript and XSLUnmanaged or native EXE/DLL files are executed using a custom PE loader with support for Delayed Imports, TLS and patching the command line. Only files with relocation information are supported. ReadIn-Memory Execution of DLLfor more information.The loader can disable AMSI and WLDP to help evade detection of malicious files executed in-memory. For more information, readHow Red Teams Bypass AMSI and WLDP for .NET Dynamic Code. It also supports decompression of files in memory using aPLib or the RtlDecompressBuffer API. ReadData Compressionfor more information.As of v1.0, ETW is also bypassed. Like with AMSI/WLDP, this a modular system that allows you to swap out the default bypass with your own. The default bypass is derived from research by XPN. ReadHiding your .NET - ETWfor more information.By default, the loader will overwrite the PE headers of unmanaged PEs (from the base address to `IMAGE_OPTIONAL_HEADER.SizeOfHeaders`). If no decoy module is used (module overloading), then the PE headers will be zeroed. If a decoy module is used, the PE headers of the decoy module will be used to overwrite those of the payload module. This is to deter detection by comparing the PE headers of modules in memory with the file backing them on disk. The user may request that all PE headers be preserved in their original state. This is helpful for scenarios when the payload module needs to access its PE headers, such as when looking up embedded PE resources.For a detailed walkthrough using the generator and how Donut affects tradecraft, readDonut - Injecting .NET Assemblies as Shellcode. For more information about the loader, readLoading .NET Assemblies From Memory.Those who wish to know more about the internals should refer toDeveloper notes.3. BuildingThere are two types of build. If you want to debug Donut, please refer todocumentation here. If not, continue reading for the release build.CloneFrom a Windows command prompt or Linux terminal, clone the repository.git clone http://github.com/thewover/donut.gitThe next step depends on your operating system and what compiler you decide to use. Currently, the generator and loader template for Donut can be compiled successfully with both Microsoft Visual Studio 2019 and MingGW-64. To use the libraries in your own C/C++ project, please refer to theexamples provided here.WindowsTo generate the loader template, dynamic library donut.dll, the static library donut.lib and the generator donut.exe. Start an x64 Microsoft Visual Studio Developer Command Prompt, change to the directory where you cloned the Donut repository and enter the following:nmake -f Makefile.msvcTo do the same, except using MinGW-64 on Windows or Linux, change to the directory where you cloned the Donut repository and enter the following:make -f Makefile.mingwLinuxTo generate the dynamic library donut.so, the static library donut.a and the generator donut. Change to the directory where you cloned the Donut repository and simply type make.Python ModuleDonut can be installed and used as a Python module. To install from source requires pip for Python3. First, ensure older versions of donut-shellcode are not installed by issuing the following command on Linux terminal or Microsoft Visual Studio command prompt.pip3 uninstall donut-shellcodeAfter you confirm older versions are no longer installed, issue the following command.pip3 install .You may also install Donut as a Python module by grabbing it from the PyPi repository.pip3 install donut-shellcodeFor more information, please refer toBuilding and using the Python extension.DockerBuilding the docker container.docker build -t donut .Running donut.docker run -it --rm -v "${PWD}:/workdir" donut -hSupport ToolsDonut includes several other executables that may be built separately. This include "hash.exe", "encrypt.exe","inject.exe", and "inject_local.exe". The first two are used in shellcode generation. The latter two are provided to assist with testing donut shellcode. "inject.exe" will inject a raw binary file (loader.bin) into a process by its PID or process name. "inject_local.exe" will inject a raw binary file into its own process.To build these support executables separately you may use the MSVC makefile. For example, to build "inject_local.exe" to test your donut shellcode, you may run.nmake inject_local -f Makefile.msvcReleasesTags have been provided for each release version of Donut that contain the compiled executables.v0.9.3, TBDv0.9.2, Bear Clawv0.9.1, Apple Fritterv0.9.0, Initial ReleaseCurrently, there are two other generators available.C# generator by n1xbyteGo generator by awgh4. UsageThe following table lists switches supported by the command line version of the generator.SwitchArgumentDescription-aarchTarget architecture for loader : 1=x86, 2=amd64, 3=x86+amd64(default).-blevelBehavior for bypassing AMSI/WLDP : 1=None, 2=Abort on fail, 3=Continue on fail.(default)-kheadersPreserve PE headers. 1=Overwrite (default), 2=Keep all-jdecoyOptional path of decoy module for Module Overloading.-cclassOptional class name. (required for .NET DLL) Can also include namespace: e.gnamespace.class-dnameAppDomain name to create for .NET. If entropy is enabled, one will be generated randomly.-elevelEntropy level. 1=None, 2=Generate random names, 3=Generate random names + use symmetric encryption (default)-fformatThe output format of loader saved to file. 1=Binary (default), 2=Base64, 3=C, 4=Ruby, 5=Python, 6=PowerShell, 7=C#, 8=Hexadecimal-mnameOptional method or function for DLL. (a method is required for .NET DLL)-nnameModule name for HTTP staging. If entropy is enabled, one is generated randomly.-opathSpecifies where Donut should save the loader. Default is "loader.bin" in the current directory.-pparametersOptional parameters/command line inside quotations for DLL method/function or EXE.-rversionCLR runtime version. MetaHeader used by default or v4.0.30319 if none available.-sserverURL for the HTTP server that will host a Donut module. Credentials may be provided in the following format:https://username:password@192.168.0.1/-tRun the entrypoint of an unmanaged/native EXE as a thread and wait for thread to end.-wCommand line is passed to unmanaged DLL function in UNICODE format. (default is ANSI)-xoptionDetermines how the loader should exit. 1=exit thread (default), 2=exit process, 3=Do not exit or cleanup and block indefinitely-yaddrCreates a new thread for the loader and continues execution at an address that is an offset relative to the host process's executable. The value provided is the offset. This option supports loaders that wish to resume execution of the host process after donut completes execution.-zenginePack/Compress the input file. 1=None, 2=aPLib, 3=LZNT1, 4=Xpress, 5=Xpress Huffman. Currently, the last three are only supported on Windows.Payload RequirementsThere are some specific requirements that your payload must meet in order for Donut to successfully load it..NET AssembliesThe entry point method must only take strings as arguments, or take no arguments.The entry point method must be marked as public and static.The class containing the entry point method must be marked as public.The Assembly must NOT be a Mixed Assembly (contain both managed and native code).As such, the Assembly must NOT contain any Unmanaged Exports.Native EXE/DLLBinaries built with Cygwin are unsupported.Cygwin executables use initialization routines that expect the host process to be running from disk. If executing from memory, the host process will likely crash.Unmanaged DLLsA user-specified entry point method must only take a string as an argument, or take no arguments. We have provided anexample.5. SubprojectsThere are four companion projects provided with donut:ToolDescriptionDemoCreateProcessA sample .NET Assembly to use in testing. Takes two command-line parameters that each specify a program to execute.DonutTestA simple C# shellcode injector to use in testing donut. The shellcode must be base64 encoded and copied in as a string.ModuleMonitorA proof-of-concept tool that detects CLR injection as it is done by tools such as Donut and Cobalt Strike's execute-assembly.ProcessManagerA Process Discovery tool that offensive operators may use to determine what to inject into and defensive operators may use to determine what is running, what properties those processes have, and whether or not they have the CLR loaded.6. Developing with DonutYou may want to add support for more types of payloads, change our feature set, or integrate Donut into your existing tooling. We have provideddeveloper documentation. Additional features are left as exercises to the reader. Our suggestions:Add environmental keying.Make Donut polymorphic by obfuscating the loader every time shellcode is generated.Integrate Donut as a module into your favorite RAT/C2 Framework.7. Questions and DiscussionIf you have any questions or comments about Donut. Join the #Donut channel in theBloodHound Gang Slack8. DisclaimerWe are not responsible for any misuse of this software or technique. Donut is provided as a demonstration of CLR Injection and in-memory loading through shellcode in order to provide red teamers a way to emulate adversaries and defenders a frame of reference for building analytics and mitigations. This inevitably runs the risk of malware authors and threat actors misusing it. However, we believe that the net benefit outweighs the risk. Hopefully that is correct. In the event EDR or AV products are capable of detecting Donut via signatures or behavioral patterns, we will not update Donut to counter signatures or detection methods. To avoid being offended, please do not ask.