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
| BOOL CreateSafeThread(IN LPVOID lpFunction, INJECTPARAM Injectparam,SIZE_T TargetPID, TCHAR* ProcessName) { BOOL bRet = FALSE; HANDLE hThread = NULL; HANDLE hFakeThread = NULL; CONTEXT ThreadCtx = { 0 };
HMODULE advapi32_dll = GetModuleHandle(TEXT("advapi32.dll"));
typedef NTSTATUS(WINAPI* FN_NtQueryInformationThread)(HANDLE, THREAD_INFORMATION_CLASS, PVOID, ULONG, PULONG); FN_NtQueryInformationThread p_NtQueryInformationThread = (FN_NtQueryInformationThread)GetProcAddress(advapi32_dll, "NtQueryInformationThread");
HMODULE hModule = NULL; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, TargetPID);
if (hSnapshot == INVALID_HANDLE_VALUE) { return NULL; }
MODULEENTRY32W me32; me32.dwSize = sizeof(MODULEENTRY32W);
if (Module32First(hSnapshot, &me32)) { do { if (_tcsicmp(me32.szModule, ProcessName) == 0) { hModule = me32.hModule; break; } } while (Module32Next(hSnapshot, &me32)); }
CloseHandle(hSnapshot);
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, TargetPID);
if (hSnapShot == INVALID_HANDLE_VALUE) { return FALSE; }
THREADENTRY32 ThreadEntry32 = { 0 }; ThreadEntry32.dwSize = sizeof(ThreadEntry32);
if (!Thread32First(hSnapShot, &ThreadEntry32)) { CloseHandle(hSnapShot); return FALSE; }
do { hThread = OpenThread(THREAD_ALL_ACCESS, false, ThreadEntry32.th32ThreadID);
if (hThread) { ULONG_PTR dwStartAddress = 0; if (NT_SUCCESS(NtQueryInformationThread(hThread, static_cast<THREADINFOCLASS>(9), &dwStartAddress, sizeof(ULONG_PTR), NULL))) {
if(dwStartAddress) { MEMORY_BASIC_INFORMATION mbi = { 0 }; SIZE_T Old = 0;
HANDLE TargetProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, TargetPID);
VirtualQueryEx(TargetProcess, (LPCVOID)dwStartAddress, &mbi, sizeof(mbi));
DWORD oldProtect; VirtualProtectEx(TargetProcess, (LPVOID)dwStartAddress, 0x1000, PAGE_EXECUTE_READWRITE, (PDWORD)&oldProtect);
VirtualProtectEx(TargetProcess, (LPVOID)pRemoteMemory, pNtHeader->OptionalHeader.SizeOfImage, PAGE_EXECUTE_READWRITE, (PDWORD)&oldProtect);
hFakeThread = CreateRemoteThread(TargetProcess, 0, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(dwStartAddress), 0, CREATE_SUSPENDED, 0);
if (hFakeThread == NULL || hFakeThread == INVALID_HANDLE_VALUE) { break; }
ThreadCtx.ContextFlags = CONTEXT_ALL;
if (!GetThreadContext(hFakeThread, &ThreadCtx)) { break; }
#ifdef _WIN64 ThreadCtx.Rcx = reinterpret_cast<DWORD64>(lpFunction); #else ThreadCtx.Eax = reinterpret_cast<DWORD>(lpFunction); #endif if (!SetThreadContext(hFakeThread, &ThreadCtx)) { break; }
ResumeThread(hFakeThread);
bRet = TRUE; break; } } }
} while (Thread32Next(hSnapShot, &ThreadEntry32));
if (hFakeThread) { CloseHandle(hFakeThread); }
if (hThread) { CloseHandle(hThread); }
if (hSnapShot) { CloseHandle(hSnapShot); }
return bRet; }
|