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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
| #include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#define TARGET_PROCESS_NAME "contest.exe"
#define WRITE_FILENAME "wjh.txt"
#define INJECT_ADDRESS_OFFSET 0xC761
#define FILENAME_ADDRESS_OFFSET 0x772FA
BYTE shellcode[] = { 0x49, 0x8B, 0xF8, 0x48, 0x8B, 0xF1, 0x6A, 0x10, 0x59, 0xFC, 0xF3, 0xA4 };
// Helper function to get the base address of a module in a process
DWORD_PTR GetModuleBaseAddress(HANDLE hProcess, const char* moduleName)
{
DWORD_PTR lpBaseAddress = 0;
HMODULE hMods[1024];
DWORD cbNeeded;
if (!EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) {
return 0;
}
for (int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) {
char szModName[MAX_PATH];
if (GetModuleBaseNameA(hProcess, hMods[i], szModName, sizeof(szModName)) == 0) {
continue;
}
if (_stricmp(szModName, moduleName) != 0) {
continue;
}
MODULEINFO modInfo;
if (!GetModuleInformation(hProcess, hMods[i], &modInfo, sizeof(modInfo))) {
continue;
}
lpBaseAddress = (DWORD_PTR)modInfo.lpBaseOfDll;
break;
}
return lpBaseAddress;
}
// Helper function to suspend/resume all threads in a process
int SuspendResumeThreads(DWORD pid, BOOL suspend)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pid);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
printf("Failed to create thread snapshot. Error code: %d\n", GetLastError());
return 1;
}
THREADENTRY32 te;
te.dwSize = sizeof(THREADENTRY32);
while (Thread32Next(hSnapshot, &te))
{
if (te.th32OwnerProcessID != pid)
continue;
HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID);
if (hThread == NULL)
continue;
if (suspend)
SuspendThread(hThread);
else
ResumeThread(hThread);
CloseHandle(hThread);
}
CloseHandle(hSnapshot);
return 0;
}
// Helper function to modify memory in a process
int ModifyMemory(HANDLE hProcess, LPVOID lpAddress, LPVOID lpBuffer, SIZE_T nSize)
{
DWORD dwOldProtect;
if (!VirtualProtectEx(hProcess, lpAddress, nSize, PAGE_EXECUTE_READWRITE, &dwOldProtect)) {
printf("Failed to modify process memory protection. Error code: %d\n", GetLastError());
return 1;
}
SIZE_T bytesWritten;
if (!WriteProcessMemory(hProcess, lpAddress, lpBuffer, nSize, &bytesWritten)) {
printf("Failed to write to process memory. Error code: %d\n", GetLastError());
}
if (!VirtualProtectEx(hProcess, lpAddress, nSize, dwOldProtect, &dwOldProtect)) {
printf("Failed to restore process memory protection. Error code: %d\n", GetLastError());
return 1;
}
return 0;
}
// Helper function to find process by name
int FindProcessByName(const char* name, DWORD& pid)
{
DWORD processes[1024];
DWORD bytesReturned;
if (!EnumProcesses(processes, sizeof(processes), &bytesReturned))
{
printf("Failed to enumerate processes. Error code: %lu\n", GetLastError());
return 1;
}
DWORD count = bytesReturned / sizeof(DWORD);
for (DWORD i = 0; i < count; i++)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processes[i]);
if (hProcess)
{
char filename[MAX_PATH];
if (GetModuleFileNameExA(hProcess, NULL, filename, MAX_PATH) > 0)
{
if (strstr(filename, name) != NULL)
{
pid = processes[i];
CloseHandle(hProcess);
return 0;
}
}
}
CloseHandle(hProcess);
}
printf("Target process %s not found\n", name);
return 1;
}
int main()
{
DWORD pid = 0;
HANDLE hProcess = NULL;
char* pBaseAddress = NULL;
if (FindProcessByName(TARGET_PROCESS_NAME, pid))
{
return 1;
}
// Get handle to the target process
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (hProcess == NULL)
{
printf("OpenProcess failed. Error code: %lu\n", GetLastError());
return 1;
}
// Get the address to modify in the target process
pBaseAddress = (char*)GetModuleBaseAddress(hProcess, TARGET_PROCESS_NAME);
// Suspend all threads of the target process
if (SuspendResumeThreads(pid, true))
{
printf("Failed to suspend process threads. Error code: %lu\n", GetLastError());
goto cleanup;
}
// Modify the memory of the target process
if (ModifyMemory(hProcess, pBaseAddress + INJECT_ADDRESS_OFFSET, shellcode, sizeof(shellcode)))
{
printf("Failed to modify memory. Error code: %lu\n", GetLastError());
goto cleanup;
}
// Modify the memory of the target process
if (ModifyMemory(hProcess, pBaseAddress + FILENAME_ADDRESS_OFFSET, (LPVOID)WRITE_FILENAME, sizeof(WRITE_FILENAME)))
{
printf("Failed to modify memory. Error code: %lu\n", GetLastError());
goto cleanup;
}
// Resume all threads of the target process
if (SuspendResumeThreads(pid, false))
{
printf("Failed to resume process threads. Error code: %lu\n", GetLastError());
goto cleanup;
}
printf("Memory modification successful\n");
cleanup:
CloseHandle(hProcess);
return 0;
}
|