22#include < tlhelp32.h>
33#include < cstdio>
44
5- const BYTE shellcode [] = {
5+ BYTE MonikaPayload [] = {
66 0x50 , // push rax
77 0x53 , // push rbx
88 0x51 , // push rcx
@@ -19,7 +19,7 @@ const BYTE shellcode[] = {
1919 0x41 , 0x57 , // push r15
2020 0x55 , // push rbp
2121 0x48 , 0x8B , 0xEC , // mov rbp, rsp
22- 0x48 , 0x83 , 0xEC , 0x28 , // sub rsp, 0x28 (MessageBoxA Strick Call Convention )
22+ 0x48 , 0x83 , 0xEC , 0x28 , // sub rsp, 0x28 (MessageBoxA Strictly requires 32-byte aligned stack )
2323 0xE8 , 0x00 , 0x00 , 0x00 , 0x00 , // call $+5 (self-relative)
2424 0x5A , // pop rdx
2525 0x48 , 0x83 , 0xC2 , 0x3C , // add rdx, 0x3C (adjust rdx to point to "JUST Monika!")
@@ -52,16 +52,45 @@ const BYTE shellcode[] = {
5252 ' A' , ' L' , ' E' , ' R' , ' T' , 0x00 , // "ALERT"
5353};
5454
55+ BYTE Gidget_Shellcode[] = {
56+ 0x55 , // push rbp
57+ 0x48 , 0x8B , 0xEC , // mov rbp, rsp
58+ 0x48 , 0x83 , 0xEC , 0x20 , // sub rsp, 32
59+ 0xE8 , 0x00 , 0x00 , 0x00 , 0x00 , // call $+5 (self-relative)
60+ 0x59 , // pop rcx
61+ 0x48 , 0x83 , 0xC1 , 0x3F , // add rcx, 0x3F
62+ 0x48 , 0xB8 , 0xC0 , 0x04 , 0x10 , 0x1B , 0xFC , 0x7F , 0x00 , 0x00 , // mov rax, 0x7FFC1B1004C0
63+ 0xFF , 0xD0 , // call rax
64+ 0x48 , 0x8B , 0xC8 , // mov rcx, rax
65+ 0xE8 , 0x00 , 0x00 , 0x00 , 0x00 , // call $+5 (self-relative)
66+ 0x5A , // pop rdx
67+ 0x48 , 0x83 , 0xC2 , 0x31 , // add rdx, 0x31
68+ 0x48 , 0xB8 , 0x50 , 0xAA , 0x0F , 0x1B , 0xFC , 0x7F , 0x00 , 0x00 , // mov rax, 0x7FFC1B0FAA50
69+ 0xFF , 0xD0 , // call rax
70+ 0xE8 , 0x00 , 0x00 , 0x00 , 0x00 , // call $+5 (self-relative)
71+ 0x5B , // pop rbx
72+ 0x48 , 0x83 , 0xC3 , 0x27 , // add rbx, 0x27
73+ 0x48 , 0x89 , 0x03 , // mov [rbx], rax
74+ 0x48 , 0x83 , 0xC4 , 0x20 , // add rsp, 32
75+ 0x5D , // pop rbp
76+ 0xC3 , // ret
77+ 0x90 , 0x90 , // nop, nop (padding)
78+ 0x75 , 0x73 , 0x65 , 0x72 , 0x33 , 0x32 , 0x2E , 0x64 , 0x6C , 0x6C , 0x00 , // "user32.dll"
79+ 0x4D , 0x65 , 0x73 , 0x73 , 0x61 , 0x67 , 0x65 , 0x42 , 0x6F , 0x78 , 0x41 , 0x00 , // "MessageBoxA"
80+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 // reserved for result
81+ };
82+
5583typedef struct InjectInfo
5684{
5785 DWORD processId;
5886 HANDLE hProcess;
5987 DWORD mainThreadId;
6088 HANDLE hThread;
61- LPVOID remoteMemory;
89+ LPVOID remoteGadgetMemory;
90+ LPVOID remotePayloadMemory;
6291} InjectInfo;
6392
64- InjectInfo targetGalgame = { 0 , 0 , 0 , 0 , 0 };
93+ InjectInfo targetGalgame = { 0 , 0 , 0 , 0 , 0 , 0 };
6594
6695// Function to get the PID of the target process by name
6796void GetProcessIdByName (const char * processName)
@@ -131,33 +160,30 @@ void GetMainThreadId()
131160 return ;
132161}
133162
134- // Function to inject shellcode into the target process and return the address of the remote memory
163+ // Function to inject MonikaPayload into the target process and return the address of the remote memory
135164void InjectShellcode ()
136165{
137- targetGalgame.remoteMemory = NULL ;
138- targetGalgame.hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE , targetGalgame.processId );
166+ targetGalgame.remotePayloadMemory = NULL ;
139167 if (!targetGalgame.hProcess )
140168 {
141- printf (" Failed to open process with PID %lu \n " , targetGalgame. processId );
169+ printf (" Invalid process handle \n " );
142170 return ;
143171 }
144172 // Allocate memory in the target process
145- targetGalgame.remoteMemory = VirtualAllocEx (targetGalgame.hProcess , NULL , sizeof (shellcode ), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
146- if (!targetGalgame.remoteMemory )
173+ targetGalgame.remotePayloadMemory = VirtualAllocEx (targetGalgame.hProcess , NULL , sizeof (MonikaPayload ), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
174+ if (!targetGalgame.remotePayloadMemory )
147175 {
148176 printf (" Failed to allocate memory in the target process\n " );
149- CloseHandle (targetGalgame.hProcess );
150- targetGalgame.hProcess = NULL ;
151177 return ;
152178 }
153- printf (" Allocated RWX memory at address: 0x%p\n " , targetGalgame.remoteMemory );
154- // Write the shellcode to the allocated memory
155- WriteProcessMemory (targetGalgame.hProcess , targetGalgame.remoteMemory , shellcode , sizeof (shellcode ), NULL );
179+ printf (" Allocated RWX memory at address: 0x%p\n " , targetGalgame.remotePayloadMemory );
180+ // Write the MonikaPayload to the allocated memory
181+ WriteProcessMemory (targetGalgame.hProcess , targetGalgame.remotePayloadMemory , MonikaPayload , sizeof (MonikaPayload ), NULL );
156182 printf (" Shellcode written to remote memory successfully\n " );
157183 return ;
158184}
159185
160- // Function to hijack the main thread and set its RIP to the injected shellcode
186+ // Function to hijack the main thread and set its RIP to the injected MonikaPayload
161187void HijackMainThread ()
162188{
163189 targetGalgame.hThread = OpenThread (THREAD_ALL_ACCESS, FALSE , targetGalgame.mainThreadId );
@@ -182,9 +208,9 @@ void HijackMainThread()
182208 WriteProcessMemory (targetGalgame.hProcess , (LPVOID)ctx.Rsp , &ctx.Rip , sizeof (LPVOID), NULL );
183209 printf (" Original RIP Pushed to Stack: 0x%p\n " , (LPVOID)ctx.Rsp );
184210
185- // Set RIP to the shellcode address
186- ctx.Rip = (DWORD64)targetGalgame.remoteMemory ;
187- printf (" Hijacking RIP to address: 0x%p\n " , targetGalgame.remoteMemory );
211+ // Set RIP to the MonikaPayload address
212+ ctx.Rip = (DWORD64)targetGalgame.remotePayloadMemory ;
213+ printf (" Hijacking RIP to address: 0x%p\n " , targetGalgame.remotePayloadMemory );
188214
189215 // Update the thread context
190216 SetThreadContext (targetGalgame.hThread , &ctx);
@@ -205,18 +231,56 @@ void HijackMainThread()
205231 return ;
206232}
207233
234+ void GetTargetMsgBoxA_Routine ()
235+ {
236+ // Allocate RWX memory in the target process, size is gadget_shellcode size
237+ targetGalgame.remoteGadgetMemory = VirtualAllocEx (targetGalgame.hProcess , NULL , sizeof (Gidget_Shellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
238+ if (!targetGalgame.remoteGadgetMemory )
239+ {
240+ printf (" Failed to allocate memory in the target process\n " );
241+ return ;
242+ }
243+ printf (" Allocated RWX memory for Gidget_Shellcode at address: 0x%p\n " , targetGalgame.remoteGadgetMemory );
244+ // Write the Gidget_Shellcode to the allocated memory
245+ WriteProcessMemory (targetGalgame.hProcess , targetGalgame.remoteGadgetMemory , Gidget_Shellcode, sizeof (Gidget_Shellcode), NULL );
246+ printf (" Gidget_Shellcode written to remote memory successfully\n " );
247+ // Create Remote Thread to execute Gidget_Shellcode
248+ HANDLE hRemoteThread = CreateRemoteThread (targetGalgame.hProcess , NULL , 0 , (LPTHREAD_START_ROUTINE)targetGalgame.remoteGadgetMemory , NULL , 0 , NULL );
249+ if (!hRemoteThread)
250+ {
251+ printf (" Failed to create remote thread\n " );
252+ return ;
253+ }
254+ WaitForSingleObject (hRemoteThread, INFINITE);
255+ CloseHandle (hRemoteThread);
256+ // Write Back last 8 bytes of Gidget_Shellcode to get MessageBoxA address
257+ ReadProcessMemory (targetGalgame.hProcess , (LPVOID)((UINT64)targetGalgame.remoteGadgetMemory + sizeof (Gidget_Shellcode) - 8 ), (LPVOID)((UINT64)Gidget_Shellcode + sizeof (Gidget_Shellcode) - 8 ), 8 , NULL );
258+ printf (" MessageBoxA Address in Target: 0x%p\n " , *(UINT64 *)((UINT64)Gidget_Shellcode + sizeof (Gidget_Shellcode) - 8 ));
259+ }
260+
208261int main ()
209262{
263+ *(UINT64 *)(Gidget_Shellcode + 20 ) = (UINT64)LoadLibraryA;
264+ *(UINT64 *)(Gidget_Shellcode + 45 ) = (UINT64)GetProcAddress;
265+
210266 const char * targetProcessName = " target.exe" ; // Replace with your target process name
211267 GetProcessIdByName (targetProcessName);
212268
213269 if (targetGalgame.processId )
214270 {
215271 printf (" Target process \" %s\" found with PID %lu\n " , targetProcessName, targetGalgame.processId );
216-
217- // Inject shellcode and get the remote memory address
272+ targetGalgame.hProcess = OpenProcess (PROCESS_ALL_ACCESS, FALSE , targetGalgame.processId );
273+ if (!targetGalgame.hProcess )
274+ {
275+ printf (" Failed to open process handle\n " );
276+ return 0 ;
277+ }
278+ GetTargetMsgBoxA_Routine ();
279+ // Update MonikaPayload with Target MessageBoxA address
280+ *(UINT64 *)(MonikaPayload + 55 ) = *(UINT64 *)(Gidget_Shellcode + sizeof (Gidget_Shellcode) - 8 );
281+ // Inject MonikaPayload and get the remote memory address
218282 InjectShellcode ();
219- if (targetGalgame.remoteMemory )
283+ if (targetGalgame.remotePayloadMemory )
220284 {
221285 printf (" Shellcode injected successfully.\n " );
222286 // Get the main thread ID
@@ -237,14 +301,14 @@ int main()
237301 printf (" Failed to find main thread.\n " );
238302 }
239303 // 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;
304+ // VirtualFreeEx(targetGalgame.hProcess, targetGalgame.remotePayloadMemory , 0, MEM_RELEASE);
305+ // targetGalgame.remotePayloadMemory = NULL;
242306 CloseHandle (targetGalgame.hProcess );
243307 targetGalgame.hProcess = NULL ;
244308 }
245309 else
246310 {
247- printf (" Failed to inject shellcode .\n " );
311+ printf (" Failed to inject MonikaPayload .\n " );
248312 }
249313 }
250314 else
0 commit comments