|
9#
楼主 |
发表于 2024-4-24 20:00:21
|
只看该作者
- #include <windows.h>
- #include <iostream>
- // 获取PE文件头
- PIMAGE_DOS_HEADER GetDosHeader(LPVOID lpBaseAddress) {
- return reinterpret_cast<PIMAGE_DOS_HEADER>(lpBaseAddress);
- }
- // 获取NT头
- PIMAGE_NT_HEADERS GetNtHeaders(LPVOID lpBaseAddress) {
- PIMAGE_DOS_HEADER pDosHeader = GetDosHeader(lpBaseAddress);
- return reinterpret_cast<PIMAGE_NT_HEADERS>(
- reinterpret_cast<BYTE*>(pDosHeader) + pDosHeader->e_lfanew);
- }
- // 执行远程进程入口点
- void ExecuteRemoteProcess(HANDLE hProcess, LPVOID remoteBaseAddress) {
- PIMAGE_NT_HEADERS pNtHeaders = GetNtHeaders(remoteBaseAddress);
- LPVOID entryPoint = (LPVOID)((DWORD_PTR)remoteBaseAddress + pNtHeaders->OptionalHeader.AddressOfEntryPoint);
- HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
- (LPTHREAD_START_ROUTINE)entryPoint, NULL, 0, NULL);
- if (hThread == NULL) {
- std::cerr << "CreateRemoteThread failed: " << GetLastError() << std::endl;
- return;
- }
- WaitForSingleObject(hThread, INFINITE);
- CloseHandle(hThread);
- }
- int main() {
- // 要注入的exe文件路径
- TCHAR szFilePath[] = L"C:\\Users\\HZW\\Desktop\\32.exe";
- //被注入的exe文件
- TCHAR FilePath111[] = L"C:\\Windows\\System32\\notepad.exe";
- // 打开exe文件
- HANDLE hFile = CreateFile(szFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- std::cerr << "CreateFile failed: " << GetLastError() << std::endl;
- return -1;
- }
- // 获取exe文件大小
- LARGE_INTEGER fileSize;
- if (!GetFileSizeEx(hFile, &fileSize)) {
- std::cerr << "GetFileSizeEx failed: " << GetLastError() << std::endl;
- CloseHandle(hFile);
- return -1;
- }
- // 创建新进程
- PROCESS_INFORMATION pi;
- STARTUPINFO si = { sizeof(STARTUPINFO) };
- if (!CreateProcess(NULL, FilePath111, NULL, NULL, FALSE,
- CREATE_SUSPENDED, NULL, NULL, &si, &pi)) {
- std::cerr << "CreateProcess failed: " << GetLastError() << std::endl;
- CloseHandle(hFile);
- return -1;
- }
- // 映射exe文件到内存
- HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
- if (hMapFile == NULL) {
- std::cerr << "CreateFileMapping failed: " << GetLastError() << std::endl;
- TerminateProcess(pi.hProcess, -1);
- CloseHandle(hFile);
- return -1;
- }
- LPVOID pFileBase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
- if (pFileBase == NULL) {
- std::cerr << "MapViewOfFile failed: " << GetLastError() << std::endl;
- CloseHandle(hMapFile);
- TerminateProcess(pi.hProcess, -1);
- CloseHandle(hFile);
- return -1;
- }
- // 将exe文件内容写入新进程
- LPVOID remoteBaseAddress = VirtualAllocEx(pi.hProcess, NULL, fileSize.LowPart, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
- if (remoteBaseAddress == NULL) {
- std::cerr << "VirtualAllocEx failed: " << GetLastError() << std::endl;
- UnmapViewOfFile(pFileBase);
- CloseHandle(hMapFile);
- TerminateProcess(pi.hProcess, -1);
- CloseHandle(hFile);
- return -1;
- }
- SIZE_T bytesWritten;
- if (!WriteProcessMemory(pi.hProcess, remoteBaseAddress, pFileBase, fileSize.LowPart, &bytesWritten)) {
- std::cerr << "WriteProcessMemory failed: " << GetLastError() << std::endl;
- VirtualFreeEx(pi.hProcess, remoteBaseAddress, 0, MEM_RELEASE);
- UnmapViewOfFile(pFileBase);
- CloseHandle(hMapFile);
- TerminateProcess(pi.hProcess, -1);
- CloseHandle(hFile);
- return -1;
- }
- // 执行注入的exe
- ExecuteRemoteProcess(pi.hProcess, remoteBaseAddress);
- // 清理
- VirtualFreeEx(pi.hProcess, remoteBaseAddress, 0, MEM_RELEASE);
- UnmapViewOfFile(pFileBase);
- CloseHandle(hMapFile);
- CloseHandle(hFile);
- ResumeThread(pi.hThread);
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- return 0;
- }
复制代码 你看看,你试下 |
|