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
| #include <windows.h> #include <stdio.h> #include<string.h> #pragma comment(lib, "OneCore.lib")
const char* dllPath = "F:\\code_py&C\\vs2017\\injecte\\Dll1\\Debug\\Dll1.dll";
DWORD pid = 37836;
int main() { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); if (!hProcess) { printf("目标进程打开失败. Error code: %d\n", GetLastError()); return -1; }
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, strlen(dllPath) + 1, NULL); if (!hMapping) { printf("创建失败. Error code: %d\n", GetLastError()); CloseHandle(hProcess); return -1; }
LPVOID lpMapAddress = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, strlen(dllPath) + 1); if (!lpMapAddress) { printf("映射失败. Error code: %d\n", GetLastError()); CloseHandle(hMapping); CloseHandle(hProcess); return -1; }
strcpy((char*)lpMapAddress, dllPath);
LPVOID pLoadLibraryA = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (!pLoadLibraryA) { printf("获取LoadLibraryA函数失败. Error code: %d\n", GetLastError()); UnmapViewOfFile(lpMapAddress); CloseHandle(hMapping); CloseHandle(hProcess); return -1; }
LPVOID lpRemoteMapAddress = MapViewOfFile2(hMapping, hProcess, 0, NULL, 0, 0, PAGE_READWRITE); if (!lpRemoteMapAddress) { printf("Failed to map view of file in the target process. Error code: %d\n", GetLastError()); UnmapViewOfFile(lpMapAddress); CloseHandle(hMapping); CloseHandle(hProcess); return -1; }
HANDLE hRemoteThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pLoadLibraryA, lpRemoteMapAddress, 0, NULL); if (!hRemoteThread) { printf("Failed to create a remote thread in the target process. Error code: %d\n", GetLastError()); UnmapViewOfFile(lpRemoteMapAddress); UnmapViewOfFile(lpMapAddress); CloseHandle(hMapping); CloseHandle(hProcess); return -1; }
WaitForSingleObject(hRemoteThread, INFINITE);
CloseHandle(hRemoteThread); UnmapViewOfFile(lpRemoteMapAddress); UnmapViewOfFile(lpMapAddress); CloseHandle(hMapping); CloseHandle(hProcess);
return 0; }
|