forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.cpp
More file actions
403 lines (331 loc) · 10.3 KB
/
Copy pathdllmain.cpp
File metadata and controls
403 lines (331 loc) · 10.3 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#define EXTERN_DLL_EXPORT
#include <windows.h>
#include <Psapi.h>
#include <tlhelp32.h>
#include <vector>
#include <algorithm>
#include <beaengine/BeaEngine.h>
const int PATH_MAXIMUM_LENGTH = 260;
enum class RequestFunction
{
IsProcessValid,
OpenRemoteProcess,
CloseRemoteProcess,
ReadRemoteMemory,
WriteRemoteMemory,
EnumerateProcesses,
EnumerateRemoteSectionsAndModules,
DisassembleRemoteCode,
ControlRemoteProcess
};
typedef LPVOID(__stdcall *RequestFunctionPtrCallback)(RequestFunction request);
RequestFunctionPtrCallback requestFunction;
EXTERN_DLL_EXPORT VOID __stdcall Initialize(RequestFunctionPtrCallback requestCallback)
{
requestFunction = requestCallback;
}
DWORD lastError = 0;
EXTERN_DLL_EXPORT DWORD __stdcall GetLastErrorCode()
{
return lastError;
}
EXTERN_DLL_EXPORT BOOL __stdcall IsProcessValid(HANDLE process)
{
if (!process)
{
return FALSE;
}
auto retn = WaitForSingleObject(process, 0);
if (retn == WAIT_FAILED)
{
return FALSE;
}
return retn == WAIT_TIMEOUT;
}
EXTERN_DLL_EXPORT LPVOID __stdcall OpenRemoteProcess(DWORD pid, DWORD desiredAccess)
{
return OpenProcess(desiredAccess, FALSE, pid);
}
EXTERN_DLL_EXPORT VOID __stdcall CloseRemoteProcess(HANDLE process)
{
CloseHandle(process);
}
EXTERN_DLL_EXPORT BOOL __stdcall ReadRemoteMemory(HANDLE process, LPCVOID address, LPVOID buffer, SIZE_T size)
{
if (ReadProcessMemory(process, address, buffer, size, nullptr))
{
lastError = 0;
return TRUE;
}
lastError = GetLastError();
return FALSE;
}
EXTERN_DLL_EXPORT BOOL __stdcall WriteRemoteMemory(HANDLE process, LPVOID address, LPCVOID buffer, SIZE_T size)
{
DWORD oldProtect;
if (VirtualProtectEx(process, address, size, PAGE_EXECUTE_READWRITE, &oldProtect))
{
if (WriteProcessMemory(process, address, buffer, size, nullptr))
{
VirtualProtectEx(process, address, size, oldProtect, nullptr);
lastError = 0;
return TRUE;
}
}
lastError = GetLastError();
return FALSE;
}
enum class Platform
{
Unknown,
X86,
X64
};
Platform GetProcessPlatform(HANDLE process)
{
auto GetProcessorArchitecture = []()
{
static USHORT processorArchitecture = PROCESSOR_ARCHITECTURE_UNKNOWN;
if (processorArchitecture == PROCESSOR_ARCHITECTURE_UNKNOWN)
{
SYSTEM_INFO info = {};
GetNativeSystemInfo(&info);
processorArchitecture = info.wProcessorArchitecture;
}
return processorArchitecture;
};
switch (GetProcessorArchitecture())
{
case PROCESSOR_ARCHITECTURE_INTEL:
return Platform::X86;
case PROCESSOR_ARCHITECTURE_AMD64:
BOOL isWow64 = FALSE;
if (IsWow64Process(process, &isWow64))
{
return isWow64 ? Platform::X86 : Platform::X64;
}
#ifdef _WIN64
return Platform::X64;
#else
return Platform::X86;
#endif
}
return Platform::Unknown;
}
typedef VOID(__stdcall EnumerateProcessCallback)(DWORD pid, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
EXTERN_DLL_EXPORT VOID __stdcall EnumerateProcesses(EnumerateProcessCallback callbackProcess)
{
if (callbackProcess == nullptr)
{
return;
}
auto handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (handle != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32W pe32 = {};
pe32.dwSize = sizeof(PROCESSENTRY32W);
if (Process32FirstW(handle, &pe32))
{
auto openRemoteProcess = reinterpret_cast<decltype(OpenRemoteProcess)*>(requestFunction(RequestFunction::OpenRemoteProcess));
auto closeRemoteProcess = reinterpret_cast<decltype(CloseRemoteProcess)*>(requestFunction(RequestFunction::CloseRemoteProcess));
do
{
auto process = openRemoteProcess(pe32.th32ProcessID, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ);
if (process != nullptr && process != INVALID_HANDLE_VALUE)
{
auto platform = GetProcessPlatform(process);
#ifdef _WIN64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
WCHAR process_path[MAX_PATH] = { };
GetModuleFileNameExW(process, NULL, process_path, MAX_PATH);
callbackProcess(pe32.th32ProcessID, process_path);
}
closeRemoteProcess(process);
}
} while (Process32NextW(handle, &pe32));
}
CloseHandle(handle);
lastError = 0;
return;
}
lastError = GetLastError();
}
typedef VOID(__stdcall EnumerateRemoteSectionsCallback)(LPVOID baseAddress, SIZE_T regionSize, WCHAR name[IMAGE_SIZEOF_SHORT_NAME + 1], DWORD state, DWORD protection, DWORD type, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
typedef VOID(__stdcall EnumerateRemoteModulesCallback)(LPVOID baseAddress, SIZE_T regionSize, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
EXTERN_DLL_EXPORT VOID __stdcall EnumerateRemoteSectionsAndModules(HANDLE process, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule)
{
if (callbackSection == nullptr && callbackModule == nullptr)
{
return;
}
struct SectionInfo
{
LPVOID BaseAddress;
SIZE_T RegionSize;
WCHAR Name[IMAGE_SIZEOF_SHORT_NAME + 1];
DWORD State;
DWORD Protection;
DWORD Type;
WCHAR ModulePath[PATH_MAXIMUM_LENGTH];
};
std::vector<SectionInfo> sections;
MEMORY_BASIC_INFORMATION memInfo = { 0 };
memInfo.RegionSize = 0x1000;
size_t address = 0;
while (VirtualQueryEx(process, (LPCVOID)address, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) != 0 && address + memInfo.RegionSize > address)
{
if (memInfo.State == MEM_COMMIT)
{
SectionInfo section = {};
section.BaseAddress = memInfo.BaseAddress;
section.RegionSize = memInfo.RegionSize;
section.State = memInfo.State;
section.Protection = memInfo.Protect;
section.Type = memInfo.Type;
sections.push_back(std::move(section));
}
address = (size_t)memInfo.BaseAddress + memInfo.RegionSize;
}
auto handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetProcessId(process));
if (handle != INVALID_HANDLE_VALUE)
{
MODULEENTRY32W me32 = {};
me32.dwSize = sizeof(MODULEENTRY32W);
if (Module32FirstW(handle, &me32))
{
auto readRemoteMemory = reinterpret_cast<decltype(ReadRemoteMemory)*>(requestFunction(RequestFunction::ReadRemoteMemory));
do
{
if (callbackModule != nullptr)
{
callbackModule(me32.modBaseAddr, me32.modBaseSize, me32.szExePath);
}
if (callbackSection != nullptr)
{
auto it = std::lower_bound(std::begin(sections), std::end(sections), (LPVOID)me32.modBaseAddr, [§ions](const SectionInfo& lhs, const LPVOID& rhs)
{
return lhs.BaseAddress < rhs;
});
IMAGE_DOS_HEADER DosHdr = {};
IMAGE_NT_HEADERS NtHdr = {};
readRemoteMemory(process, me32.modBaseAddr, &DosHdr, sizeof(IMAGE_DOS_HEADER));
readRemoteMemory(process, me32.modBaseAddr + DosHdr.e_lfanew, &NtHdr, sizeof(IMAGE_NT_HEADERS));
std::vector<IMAGE_SECTION_HEADER> sectionHeaders(NtHdr.FileHeader.NumberOfSections);
readRemoteMemory(process, me32.modBaseAddr + DosHdr.e_lfanew + sizeof(IMAGE_NT_HEADERS), sectionHeaders.data(), NtHdr.FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER));
for (int i = 0; i < NtHdr.FileHeader.NumberOfSections; ++i)
{
auto&& sectionHeader = sectionHeaders[i];
auto sectionAddress = (size_t)me32.modBaseAddr + sectionHeader.VirtualAddress;
for (auto j = it; j != std::end(sections); ++j)
{
if (sectionAddress >= (size_t)j->BaseAddress && sectionAddress < (size_t)j->BaseAddress + (size_t)j->RegionSize)
{
// Copy the name because it is not null padded.
char buffer[IMAGE_SIZEOF_SHORT_NAME + 1] = { 0 };
std::memcpy(buffer, sectionHeader.Name, IMAGE_SIZEOF_SHORT_NAME);
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, j->Name, IMAGE_SIZEOF_SHORT_NAME, buffer, _TRUNCATE);
std::memcpy(j->ModulePath, me32.szExePath, sizeof(SectionInfo::ModulePath));
break;
}
}
}
}
} while (Module32NextW(handle, &me32));
}
CloseHandle(handle);
if (callbackSection != nullptr)
{
for (auto&& section : sections)
{
callbackSection(section.BaseAddress, section.RegionSize, section.Name, section.State, section.Protection, section.Type, section.ModulePath);
}
}
lastError = 0;
return;
}
lastError = GetLastError();
}
typedef VOID(__stdcall DisassembleRemoteCodeCallback)(LPVOID address, DWORD length, CHAR instruction[64]);
EXTERN_DLL_EXPORT VOID __stdcall DisassembleRemoteCode(HANDLE process, LPVOID address, int length, DisassembleRemoteCodeCallback callbackDisassembledCode)
{
if (callbackDisassembledCode == nullptr)
{
return;
}
UIntPtr start = (UIntPtr)address;
DISASM disasm = { };
disasm.Options = NasmSyntax + PrefixedNumeral;
#ifdef _WIN64
disasm.Archi = 64;
#endif
auto readRemoteMemory = reinterpret_cast<decltype(ReadRemoteMemory)*>(requestFunction(RequestFunction::ReadRemoteMemory));
std::vector<uint8_t> buffer(length);
readRemoteMemory(process, address, buffer.data(), buffer.size());
UIntPtr end = (UIntPtr)buffer.data() + length;
disasm.EIP = (UIntPtr)buffer.data();
disasm.VirtualAddr = start;
while (true)
{
disasm.SecurityBlock = (UInt32)(end - disasm.EIP);
auto disamLength = Disasm(&disasm);
if (disamLength == OUT_OF_BLOCK || disamLength == UNKNOWN_OPCODE)
{
break;
}
callbackDisassembledCode((LPVOID)disasm.VirtualAddr, disamLength, disasm.CompleteInstr);
disasm.EIP += disamLength;
if (disasm.EIP >= end || buffer[disasm.EIP - (UIntPtr)buffer.data()] == 0xCC)
{
break;
}
disasm.VirtualAddr += disamLength;
}
}
enum class ControlRemoteProcessAction
{
Suspend,
Resume,
Terminate
};
EXTERN_DLL_EXPORT VOID __stdcall ControlRemoteProcess(HANDLE process, ControlRemoteProcessAction action)
{
if (action == ControlRemoteProcessAction::Suspend || action == ControlRemoteProcessAction::Resume)
{
auto processId = GetProcessId(process);
if (processId != 0)
{
auto handle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (handle != INVALID_HANDLE_VALUE)
{
auto fn = action == ControlRemoteProcessAction::Suspend ? SuspendThread : ResumeThread;
THREADENTRY32 te32 = {};
te32.dwSize = sizeof(THREADENTRY32);
if (Thread32First(handle, &te32))
{
do
{
if (te32.th32OwnerProcessID == processId)
{
auto threadHandle = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID);
if (threadHandle)
{
fn(threadHandle);
CloseHandle(threadHandle);
}
}
} while (Thread32Next(handle, &te32));
}
CloseHandle(handle);
}
}
}
else if (action == ControlRemoteProcessAction::Terminate)
{
TerminateProcess(process, 0);
}
}