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
| #include <iostream> #include <Windows.h> #include <TlHelp32.h>
using namespace std;
BOOL GetAllThreadIdByProcessId(DWORD dwPid, DWORD** ppThreadIdList, LPDWORD pThreadIdListLength) { DWORD dwThreadIdListLength = 0; DWORD dwThreadIdListMaxCount = 2000; LPDWORD pThreadIdList = NULL; pThreadIdList = (LPDWORD)VirtualAlloc(NULL, dwThreadIdListMaxCount * sizeof(DWORD), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (pThreadIdList == NULL) { printf("[*] Create Thread Id Space Error!\n"); return FALSE; } RtlZeroMemory(pThreadIdList, dwThreadIdListMaxCount * sizeof(DWORD)); THREADENTRY32 te32 = { 0 }; RtlZeroMemory(&te32, sizeof(te32)); te32.dwSize = sizeof(te32); HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (hThreadSnapshot == NULL) { printf("[*] Create Thread Snap Error!\n"); return FALSE; } BOOL bRet = Thread32First(hThreadSnapshot, &te32); while (bRet) { if (te32.th32OwnerProcessID == dwPid) { if (dwThreadIdListLength >= dwThreadIdListMaxCount) { break; } pThreadIdList[dwThreadIdListLength++] = te32.th32ThreadID; } bRet = Thread32Next(hThreadSnapshot, &te32); } *pThreadIdListLength = dwThreadIdListLength; *ppThreadIdList = pThreadIdList; return TRUE; }
int main() { char szDllPath[] = "F:\\code_py&C\\vs2017\\injecte\\Dll1\\Debug\\Dll1"; DWORD dwPid = 28496 ; BOOL bRet; LPDWORD pThreadIdList = NULL; DWORD dwThreadIdListLength = 0; bRet = GetAllThreadIdByProcessId(dwPid, &pThreadIdList, &dwThreadIdListLength); if (!bRet) { printf("[*] Get All Thread Id Error!\n"); return 0; } HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid); if (hProcess == NULL) { printf("[*] Open Process Error!\n"); return 0; } DWORD dwDllPathLen = strlen(szDllPath) + 1; LPVOID lpBaseAddress = VirtualAllocEx(hProcess, NULL, dwDllPathLen, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (lpBaseAddress == NULL) { printf("[*] VirtualAllocEx Error!\n"); return 0; } SIZE_T dwWriten = 0; WriteProcessMemory(hProcess, lpBaseAddress, szDllPath, dwDllPathLen, &dwWriten); if (dwWriten != dwDllPathLen) { printf("[*] Write Process Memory Error!\n"); return 0; } LPVOID pLoadLibraryFunc = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (pLoadLibraryFunc == NULL) { printf("[*] Get Func Address Error!\n"); return 0; } HANDLE hThread = NULL; for (int i = dwThreadIdListLength - 1; i >= 0; i--) { HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, pThreadIdList[i]); if (hThread) { QueueUserAPC((PAPCFUNC)pLoadLibraryFunc, hThread, (ULONG_PTR)lpBaseAddress); CloseHandle(hThread); hThread = NULL; } }
printf(" Success\n");
if (hProcess) { CloseHandle(hProcess); hProcess = NULL; } if (pThreadIdList) { VirtualFree(pThreadIdList, 0, MEM_RELEASE); pThreadIdList = NULL; } ExitProcess(0); return 0; }
|