[C++] 纯文本查看 复制代码
#include <Windows.h>
#include <iostream>
int main(int argc, char* argv[])
{
char* ExePath; // exe路径
char* DllPath; // dll路径
int dll_mem_len = 0; // dll路径长度(包括结尾'\0')
LPVOID Remote_DllStringPtr; // 写入被注入进程的dll路径
LPVOID Remote_ShellCodePtr; // 写入被注入进程的ShellCode
CONTEXT ctx = {};
PROCESS_INFORMATION pi = {};
STARTUPINFOA si = {};
if (argc < 3) {
printf("Usage: CreateSuspendInject.exe <exe> <dll>\n");
return 1;
}
ExePath = argv[1];
DllPath = argv[2];
dll_mem_len = strlen(DllPath) + 1;
if (CreateProcessA(ExePath, NULL, NULL, NULL, NULL, CREATE_SUSPENDED, NULL, NULL, &si, &pi) == 0) {
printf("Can't create process\n");
return 1;
}
Remote_DllStringPtr = VirtualAllocEx(pi.hProcess, NULL, dll_mem_len, MEM_COMMIT, PAGE_READWRITE);
Remote_ShellCodePtr = VirtualAllocEx(pi.hProcess, NULL, 64, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
ctx.ContextFlags = CONTEXT_CONTROL;
GetThreadContext(pi.hThread, &ctx); // 获取线程上下文
unsigned char ShellCode[] = {
0x48, 0xb8, 0,0,0,0,0,0,0,0, // mov rax, original_rip
0x50, // push rax
0x51, // push rcx
0x52, // push rdx
0x41, 0x50, // push r8
0x41, 0x51, // push r9
0x41, 0x52, // push r10
0x41, 0x53, // push r11
0x48, 0xb9, 0,0,0,0,0,0,0,0, // mov rcx, str
0x48, 0xb8, 0,0,0,0,0,0,0,0, // mov rax, LoadLibraryA
0xff, 0xd0, // call rax
0x41, 0x5b, // pop r11
0x41, 0x5a, // pop r10
0x41, 0x59, // pop r9
0x41, 0x58, // pop r8
0x5a, // pop rdx
0x59, // pop rcx
0xc3, // ret
};
*(void**)&ShellCode[2] = (void*)ctx.Rip;
*(void**)&ShellCode[23] = Remote_DllStringPtr;
*(void**)&ShellCode[33] = (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");
WriteProcessMemory(pi.hProcess, Remote_DllStringPtr, DllPath, dll_mem_len, NULL);
WriteProcessMemory(pi.hProcess, Remote_ShellCodePtr, ShellCode, sizeof(ShellCode), NULL);
ctx.Rip = (DWORD64)Remote_ShellCodePtr; // 把Rip指向ShellCode
ctx.ContextFlags = CONTEXT_CONTROL;
SetThreadContext(pi.hThread, &ctx); // 设置线程上下文
ResumeThread(pi.hThread); // 恢复线程,执行ShellCode
Sleep(1000);
VirtualFreeEx(pi.hProcess, Remote_DllStringPtr, dll_mem_len, MEM_DECOMMIT);
VirtualFreeEx(pi.hProcess, Remote_ShellCodePtr, sizeof(ShellCode), MEM_DECOMMIT);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}