5 uintptr_t
injectDLL(HANDLE hProcess,
const char* dllPath) {
8 if (hProcess == NULL) {
9 std::cerr <<
"OpenProcess failed: " << GetLastError() << std::endl;
14 size_t pathLen = strlen(dllPath) + 1;
17 LPVOID pDllPath = VirtualAllocEx(hProcess, NULL, pathLen, MEM_COMMIT, PAGE_READWRITE);
18 if (pDllPath == NULL) {
19 std::cerr <<
"VirtualAllocEx failed: " << GetLastError() << std::endl;
20 CloseHandle(hProcess);
23 std::cerr <<
"DLL Base: " << std::hex << pDllPath << std::endl;
25 if (!WriteProcessMemory(hProcess, pDllPath, (LPVOID)dllPath, pathLen, NULL)) {
26 std::cerr <<
"WriteProcessMemory failed: " << GetLastError() << std::endl;
27 VirtualFreeEx(hProcess, pDllPath, 0, MEM_RELEASE);
28 CloseHandle(hProcess);
33 LPVOID pLoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle(
"kernel32.dll"),
"LoadLibraryA");
34 if (pLoadLibrary == NULL) {
35 std::cerr <<
"GetProcAddress failed: " << GetLastError() << std::endl;
36 VirtualFreeEx(hProcess, pDllPath, 0, MEM_RELEASE);
37 CloseHandle(hProcess);
40 std::cerr <<
"Loaded!" << std::endl;
42 HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
43 (LPTHREAD_START_ROUTINE)pLoadLibrary,
45 if (hThread == NULL) {
46 std::cerr <<
"CreateRemoteThread failed: " << GetLastError() << std::endl;
47 VirtualFreeEx(hProcess, pDllPath, 0, MEM_RELEASE);
48 CloseHandle(hProcess);
51 std::cerr <<
"Thread created!" << std::endl;
52 return reinterpret_cast<uintptr_t
>(pDllPath);
55 std::string
getDllName(HANDLE hProcess, LPVOID lpImageName, BOOL isUnicode) {
56 if (!lpImageName)
return "<unknown>";
57 LPVOID actualStringPtr =
nullptr;
60 if (!ReadProcessMemory(hProcess, lpImageName, &actualStringPtr,
sizeof(LPVOID), &bytesRead)) {
61 return "<read error>";
64 if (!actualStringPtr) {
65 return "<null pointer>";
71 wchar_t wbuffer[MAX_PATH] = {};
72 if (ReadProcessMemory(hProcess, actualStringPtr, &wbuffer,
sizeof(wbuffer), &bytesRead)) {
73 char buffer[MAX_PATH];
74 WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, MAX_PATH,
nullptr,
nullptr);
77 name =
"<unicode read error>";
80 char buffer[MAX_PATH] = {};
81 if (ReadProcessMemory(hProcess, actualStringPtr, &buffer,
sizeof(buffer), &bytesRead)) {
84 name =
"<ansi read error>";
93 bool executeRemote(HANDLE hProcessGlobal,
const std::vector<BYTE>& shellcode) {
94 if (shellcode.empty())
return false;
96 SIZE_T codeSize = shellcode.size();
99 LPVOID remoteMem = VirtualAllocEx(hProcessGlobal,
nullptr, codeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
100 if (!remoteMem)
return false;
103 if (!WriteProcessMemory(hProcessGlobal, remoteMem, shellcode.data(), codeSize,
nullptr)) {
104 VirtualFreeEx(hProcessGlobal, remoteMem, 0, MEM_RELEASE);
109 HANDLE hThread = CreateRemoteThread(hProcessGlobal,
nullptr, 0,
110 (LPTHREAD_START_ROUTINE)remoteMem,
111 nullptr, 0,
nullptr);
113 VirtualFreeEx(hProcessGlobal, remoteMem, 0, MEM_RELEASE);
123 PROCESSENTRY32 processInfo;
124 processInfo.dwSize =
sizeof(processInfo);
125 HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
126 if (processesSnapshot == INVALID_HANDLE_VALUE) {
127 DWORD error = GetLastError();
128 std::cerr <<
"CreateToolhelp32Snapshot failed with error: " << error << std::endl;
132 Process32First(processesSnapshot, &processInfo);
133 if (!processName.compare(processInfo.szExeFile)) {
134 CloseHandle(processesSnapshot);
135 return processInfo.th32ProcessID;
138 while (Process32Next(processesSnapshot, &processInfo)) {
139 if (!processName.compare(processInfo.szExeFile)) {
140 CloseHandle(processesSnapshot);
141 return processInfo.th32ProcessID;
145 CloseHandle(processesSnapshot);
155 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
156 std::cerr <<
"[-] OpenProcessToken failed: " << GetLastError() <<
"\n";
161 if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
162 std::cerr <<
"[-] LookupPrivilegeValue failed: " << GetLastError() <<
"\n";
168 tp.PrivilegeCount = 1;
169 tp.Privileges[0].Luid = luid;
170 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
173 if (!AdjustTokenPrivileges(hToken, FALSE, &tp,
sizeof(TOKEN_PRIVILEGES),
nullptr,
nullptr)) {
174 std::cerr <<
"[-] AdjustTokenPrivileges failed: " << GetLastError() <<
"\n";
179 if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
180 std::cerr <<
"[-] The token does not have the specified privilege.\n";
191 IMAGE_DOS_HEADER dosHeader;
192 IMAGE_NT_HEADERS ntHeaders;
197 if (!ReadProcessMemory(hProcess, baseAddress, &dosHeader,
sizeof(dosHeader), &bytesRead) ||
198 bytesRead !=
sizeof(dosHeader) || dosHeader.e_magic != IMAGE_DOS_SIGNATURE) {
203 LPVOID ntHeadersAddr = (LPBYTE)baseAddress + dosHeader.e_lfanew;
204 if (!ReadProcessMemory(hProcess, ntHeadersAddr, &ntHeaders,
sizeof(ntHeaders), &bytesRead) ||
205 bytesRead !=
sizeof(ntHeaders) || ntHeaders.Signature != IMAGE_NT_SIGNATURE) {
210 DWORD_PTR entryPoint = (DWORD_PTR)baseAddress + ntHeaders.OptionalHeader.AddressOfEntryPoint;