1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
| #include "clr.h" #include <winhttp.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "winhttp.lib") #pragma comment(lib, "user32.lib") #pragma comment(lib, "oleaut32.lib")
#define SAFE_CLOSE(h) { if (h && h != INVALID_HANDLE_VALUE) { CloseHandle(h); h = NULL; } } #define SAFE_RELEASE(p) { if (p) { (p)->lpVtbl->Release(p); (p) = NULL; } }
HANDLE g_OrigninalStdOut = INVALID_HANDLE_VALUE; HANDLE g_OrigninalStdErr = INVALID_HANDLE_VALUE;
const GUID xCLSID_CLRMetaHost = { 0x9280188d, 0xe8e, 0x4867, { 0xb3, 0xc, 0x7f, 0xa8, 0x38, 0x84, 0xe8, 0xde } }; const GUID xCLSID_CorRuntimeHost = { 0xcb2f6723, 0xab3a, 0x11d2, { 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e } }; const GUID xIID_AppDomain = { 0x05F696DC, 0x2B29, 0x3663, { 0xAD, 0x8B, 0xC4, 0x38, 0x9C, 0xF2, 0xA7, 0x13 } }; const GUID xIID_ICLRMetaHost = { 0xD332DB9E, 0xB9B3, 0x4125, { 0x82, 0x07, 0xA1, 0x48, 0x84, 0xF5, 0x32, 0x16 } }; const GUID xIID_ICLRRuntimeInfo = { 0xBD39D1D2, 0xBA2F, 0x486a, { 0x89, 0xB0, 0xB4, 0xB0, 0xCB, 0x46, 0x68, 0x91 } }; const GUID xIID_ICorRuntimeHost = { 0xcb2f6722, 0xab3a, 0x11d2, { 0x9c, 0x40, 0x00, 0xc0, 0x4f, 0xa3, 0x0a, 0x3e } };
DWORD WINAPI PipeReaderThread(LPVOID lpParam) { PipeContext* ctx = (PipeContext*)lpParam;
const int BUFSIZE = 4096; char buffer[4096]; DWORD bytesRead = 0; BOOL bSuccess = FALSE;
ctx->outputBuffer = NULL; ctx->totalBytes = 0;
while (TRUE) { bSuccess = ReadFile(ctx->hPipeRead, buffer, BUFSIZE, &bytesRead, NULL);
if (!bSuccess || bytesRead == 0) break;
char* newPtr = (char*)realloc(ctx->outputBuffer, ctx->totalBytes + bytesRead + 1);
if (newPtr == NULL) { break; }
ctx->outputBuffer = newPtr;
memcpy(ctx->outputBuffer + ctx->totalBytes, buffer, bytesRead);
ctx->totalBytes += bytesRead;
ctx->outputBuffer[ctx->totalBytes] = '\0'; }
return 0; }
BOOL DotnetExecute(BUFFER Assembly, BUFFER Arguments) { PICLRMetaHost pMetaHost = NULL; PICLRRuntimeInfo pRuntimeInfo = NULL; PICorRuntimeHost pCorRuntimeHost = NULL; IUnknown* pAppDomainThunk = NULL; IAppDomain* pAppDomain = NULL; IAssembly* pAssembly = NULL; IMethodInfo* pMethodInfo = NULL;
SAFEARRAY* pSafeArray = NULL; SAFEARRAY* pMethodArgs = NULL; SAFEARRAYBOUND RgsBound[1] = { 0 }; void* pData = NULL;
VARIANT vObj; VARIANT vRet; VARIANT vPsa;
VariantInit(&vObj); VariantInit(&vRet); VariantInit(&vPsa);
HMODULE hMscoree = NULL; CLRCreateInstanceFnPtr pfnCLRCreateInstance = NULL;
if (!Assembly.Buffer || !Assembly.Length) { printf("[-] Invalid Assembly buffer.\n"); return FALSE; }
printf("[*] Starting DotnetExecute (Pure C Mode)...\n");
SECURITY_ATTRIBUTES saAttr; HANDLE hPipeRead = NULL; HANDLE hPipeWrite = NULL; saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); saAttr.bInheritHandle = TRUE; saAttr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0)) { return "Error: CreatePipe failed\n"; }
g_OrigninalStdOut = GetStdHandle(STD_OUTPUT_HANDLE); g_OrigninalStdErr = GetStdHandle(STD_ERROR_HANDLE);
if (!SetStdHandle(STD_OUTPUT_HANDLE, hPipeWrite) || !SetStdHandle(STD_ERROR_HANDLE, hPipeWrite)) { CloseHandle(hPipeRead); CloseHandle(hPipeWrite); return "Error: SetStdHandle failed\n"; }
PipeContext ctx; ctx.hPipeRead = hPipeRead; ctx.outputBuffer = NULL; ctx.totalBytes = 0; HANDLE hThread = CreateThread( NULL, 0, PipeReaderThread, &ctx, 0, NULL );
HRESULT hr = S_OK; hMscoree = LoadLibraryA("mscoree.dll"); if (!hMscoree) return FALSE;
pfnCLRCreateInstance = (CLRCreateInstanceFnPtr)GetProcAddress(hMscoree, "CLRCreateInstance"); if (!pfnCLRCreateInstance) return FALSE;
hr = pfnCLRCreateInstance(&xCLSID_CLRMetaHost, &xIID_ICLRMetaHost, (void**)&pMetaHost); if (FAILED(hr)) return FALSE;
hr = pMetaHost->lpVtbl->GetRuntime(pMetaHost, L"v4.0.30319", &xIID_ICLRRuntimeInfo, (void**)&pRuntimeInfo); if (FAILED(hr)) return FALSE;
hr = pRuntimeInfo->lpVtbl->GetInterface(pRuntimeInfo, &xCLSID_CorRuntimeHost, &xIID_ICorRuntimeHost, (void**)&pCorRuntimeHost); if (FAILED(hr)) return FALSE;
pCorRuntimeHost->lpVtbl->Start(pCorRuntimeHost);
pCorRuntimeHost->lpVtbl->GetDefaultDomain(pCorRuntimeHost, (IUnknown**)&pAppDomainThunk);
pAppDomainThunk->lpVtbl->QueryInterface(pAppDomainThunk, &xIID_AppDomain, (void**)&pAppDomain);
RgsBound[0].cElements = Assembly.Length; RgsBound[0].lLbound = 0; pSafeArray = SafeArrayCreate(VT_UI1, 1, RgsBound);
SafeArrayAccessData(pSafeArray, &pData); memcpy(pData, Assembly.Buffer, Assembly.Length); SafeArrayUnaccessData(pSafeArray);
hr = pAppDomain->lpVtbl->Load_3(pAppDomain, pSafeArray, &pAssembly); if (FAILED(hr)) return FALSE;
hr = pAssembly->lpVtbl->EntryPoint(pAssembly, &pMethodInfo); if (FAILED(hr)) return FALSE;
long idx = 0; pMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 1);
vPsa.vt = (VT_ARRAY | VT_BSTR); vPsa.parray = SafeArrayCreateVector(VT_BSTR, 0, 0);
SafeArrayPutElement(pMethodArgs, idx, &vPsa);
pMethodInfo->lpVtbl->Invoke_3(pMethodInfo, vObj, pMethodArgs, &vRet);
SetStdHandle(STD_OUTPUT_HANDLE, g_OrigninalStdOut); SetStdHandle(STD_ERROR_HANDLE, g_OrigninalStdErr);
if (g_OrigninalStdOut != INVALID_HANDLE_VALUE) SetStdHandle(STD_OUTPUT_HANDLE, g_OrigninalStdOut); if (g_OrigninalStdErr != INVALID_HANDLE_VALUE) SetStdHandle(STD_ERROR_HANDLE, g_OrigninalStdErr);
SAFE_CLOSE(hPipeWrite); SAFE_CLOSE(hThread);
if (ctx.outputBuffer) { printf("\n[+] Captured Output:\n%s\n", ctx.outputBuffer); free(ctx.outputBuffer); } else { printf("[-] No output captured or execution failed.\n"); }
if (pSafeArray) SafeArrayDestroy(pSafeArray); if (pMethodArgs) SafeArrayDestroy(pMethodArgs);
SAFE_RELEASE(pMethodInfo); SAFE_RELEASE(pAssembly); SAFE_RELEASE(pAppDomain); SAFE_RELEASE(pAppDomainThunk); SAFE_RELEASE(pCorRuntimeHost); SAFE_RELEASE(pRuntimeInfo); SAFE_RELEASE(pMetaHost);
return TRUE; }
int main() { const char* targetAssembly = "D:\\代码\\Vs2022\\TestForC#\\DotnetNoVirtualProtectShellcodeLoader\\bin\\x64\\Debug\\DotnetNoVirtualProtectShellcodeLoader.exe";
BUFFER assemblyBuffer = { 0 }; BUFFER argumentsBuffer = { 0 };
printf("[*] Reading assembly from: %s\n", targetAssembly);
HANDLE hFile = CreateFileA(targetAssembly, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("[-] Error: Could not open file. Error Code: %d\n", GetLastError()); return 1; }
assemblyBuffer.Length = GetFileSize(hFile, NULL); assemblyBuffer.Buffer = (unsigned char*)malloc(assemblyBuffer.Length); DWORD lpNumberOfBytesRead = 0; if (!ReadFile(hFile, assemblyBuffer.Buffer, assemblyBuffer.Length, &lpNumberOfBytesRead, NULL)) { printf("[-] Error: Could not read file.\n"); CloseHandle(hFile); return 1; } printf("[+] Assembly read into memory (%d bytes).\n", assemblyBuffer.Length); CloseHandle(hFile);
if (!DotnetExecute(assemblyBuffer, argumentsBuffer)) { printf("[-] DotnetExecute failed.\n"); } return 0; }
|