forked from mojtabafalleh/emudbg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemudbg.cpp
More file actions
298 lines (248 loc) · 11.7 KB
/
emudbg.cpp
File metadata and controls
298 lines (248 loc) · 11.7 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
#include "cpu.hpp"
using namespace std;
std::unordered_map<DWORD, CPU> cpuThreads;
int wmain(int argc, wchar_t* argv[]) {
if (argc < 2) {
wprintf(L"Usage: %s <exe_path> [-m target.dll] [-b software|hardware]\n", argv[0]);
return 1;
}
std::wstring exePath;
std::wstring targetModuleName;
bool waitForModule = false;
for (int i = 1; i < argc; ++i) {
std::wstring arg = argv[i];
if (arg == L"-m" && i + 1 < argc) {
targetModuleName = argv[++i];
waitForModule = true;
}
else if (arg == L"-b" && i + 1 < argc) {
std::wstring type = argv[++i];
std::transform(type.begin(), type.end(), type.begin(), ::towlower);
if (type == L"hardware") bpType = BreakpointType::Hardware;
else if (type == L"software") bpType = BreakpointType::Software;
else {
wprintf(L"[-] Invalid breakpoint type: %s\n", type.c_str());
return 1;
}
}
else {
exePath = arg;
}
}
if (exePath.empty()) {
wprintf(L"Usage: %s <exe_path> [-m target.dll] [-b software|hardware]\n", argv[0]);
return 1;
}
STARTUPINFOW si = { sizeof(si) };
uint32_t entryRVA = GetEntryPointRVA(exePath);
std::vector<uint32_t> tlsRVAs = GetTLSCallbackRVAs(exePath);
if (!CreateProcessW(exePath.c_str(), NULL, NULL, NULL, FALSE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi)) return 1;
DEBUG_EVENT dbgEvent = {};
uint64_t baseAddress = 0;
uint64_t moduleBase = 0;
std::unordered_map<uint64_t, BreakpointInfo> breakpoints;
while (true) {
if (!WaitForDebugEvent(&dbgEvent, INFINITE)) break;
DWORD continueStatus = DBG_CONTINUE;
switch (dbgEvent.dwDebugEventCode) {
case LOAD_DLL_DEBUG_EVENT: {
auto& ld = dbgEvent.u.LoadDll;
if (ld.lpImageName && ld.fUnicode) {
ULONGLONG ptr = 0;
wchar_t buffer[MAX_PATH] = {};
if (ReadProcessMemory(pi.hProcess, (LPCVOID)ld.lpImageName, &ptr, sizeof(ptr), nullptr) && ptr &&
ReadProcessMemory(pi.hProcess, (LPCVOID)ptr, buffer, sizeof(buffer) - sizeof(wchar_t), nullptr)) {
std::wstring loadedName(buffer);
std::wstring lowerLoaded = loadedName;
std::transform(lowerLoaded.begin(), lowerLoaded.end(), lowerLoaded.begin(), ::towlower);
std::wstring lowerTarget = targetModuleName;
std::transform(lowerTarget.begin(), lowerTarget.end(), lowerTarget.begin(), ::towlower);
#if analyze_ENABLED
LOG_analyze(GREEN,"DLL LOADED : "<< lowerLoaded.c_str());
if (lowerLoaded.find(L"ntdll.dll") != std::wstring::npos) {
ntdllBase = reinterpret_cast<uint64_t>(ld.lpBaseOfDll);
LOG(L"[+] ntdll.dll loaded at 0x" << std::hex << ntdllBase);
}
#endif
if (waitForModule && lowerLoaded.find(lowerTarget) != std::wstring::npos) {
moduleBase = (uint64_t)ld.lpBaseOfDll;
auto modEntryRVA = GetEntryPointRVA(buffer);
auto modTLSRVAs = GetTLSCallbackRVAs(buffer);
valid_ranges.emplace_back(moduleBase, moduleBase + optionalHeader.SizeOfImage);
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dbgEvent.dwThreadId);
if (modEntryRVA) modTLSRVAs.push_back(modEntryRVA);
for (auto &rva : modTLSRVAs) {
uint64_t addr = moduleBase + rva;
if (bpType == BreakpointType::Hardware)
SetHardwareBreakpointAuto(hThread, addr);
else {
BYTE orig;
if (SetBreakpoint(pi.hProcess, addr, orig)) breakpoints[addr] = { orig, 1 };
}
}
if (hThread) CloseHandle(hThread);
}
}
}
if (ld.hFile) CloseHandle(ld.hFile);
break;
}
case CREATE_THREAD_DEBUG_EVENT: {
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_FULL;
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dbgEvent.dwThreadId);
if (hThread && GetThreadContext(hThread, &ctx)) {
uint64_t pointer = ctx.Rdx, address = 0;
if (ReadProcessMemory(pi.hProcess, (LPCVOID)pointer, &address, sizeof(address), nullptr) && IsInEmulationRange(address)) {
#if analyze_ENABLED
LOG_analyze(GREEN, "New THREAD CREATED! Entry point : " << std::hex << address);
#endif
CPU cpu(hThread);
cpu.CPUThreadState = ThreadState::Unknown;
cpuThreads.emplace(dbgEvent.dwThreadId, std::move(cpu));
if (bpType == BreakpointType::Hardware)
SetHardwareBreakpointAuto(hThread, address);
else {
BYTE orig;
if (breakpoints.find(address) == breakpoints.end()) {
if (SetBreakpoint(pi.hProcess, address, orig)) breakpoints[address] = { orig, 1 };
}
}
}
}
if (hThread) CloseHandle(hThread);
break;
}
case CREATE_PROCESS_DEBUG_EVENT: {
auto& procInfo = dbgEvent.u.CreateProcessInfo;
baseAddress = reinterpret_cast<uint64_t>(procInfo.lpBaseOfImage);
valid_ranges.emplace_back(baseAddress, baseAddress + optionalHeader.SizeOfImage);
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dbgEvent.dwThreadId);
if (hThread) {
cpuThreads.emplace(dbgEvent.dwThreadId, CPU(hThread));
}
if (!waitForModule) {
if (entryRVA) tlsRVAs.push_back(entryRVA);
for (auto &rva : tlsRVAs) {
uint64_t addr = baseAddress + rva;
if (bpType == BreakpointType::Hardware)
SetHardwareBreakpointAuto(hThread, addr);
else {
BYTE orig;
if (SetBreakpoint(pi.hProcess, addr, orig)) breakpoints[addr] = { orig, 1 };
}
}
}
break;
}
case EXCEPTION_DEBUG_EVENT: {
auto& er = dbgEvent.u.Exception.ExceptionRecord;
DWORD exceptionCode = er.ExceptionCode;
uint64_t exAddr = reinterpret_cast<uint64_t>(er.ExceptionAddress);
switch (exceptionCode) {
case EXCEPTION_BREAKPOINT:
if (bpType == BreakpointType::Software && breakpoints.count(exAddr)) {
auto& bp = breakpoints[exAddr];
RemoveBreakpoint(pi.hProcess, exAddr, bp.originalByte);
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_FULL;
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dbgEvent.dwThreadId);
if (hThread && GetThreadContext(hThread, &ctx)) {
ctx.Rip -= 1;
SetThreadContext(hThread, &ctx);
}
RemoveAllBreakpoints(pi.hProcess,breakpoints);
auto it = cpuThreads.find(dbgEvent.dwThreadId);
if (it != cpuThreads.end()) {
CPU& cpu = it->second;
cpu.CPUThreadState = ThreadState::Running;
cpu.UpdateRegistersFromContext(ctx);
uint64_t addr = cpu.start_emulation();
LOG(L"[+] Emulation returned address: 0x" << std::hex << addr);
cpu.ApplyRegistersToContext(ctx);
bp.remainingHits--;
if (bp.remainingHits > 0) {
SetBreakpoint(pi.hProcess, exAddr, bp.originalByte);
}
else {
breakpoints.erase(exAddr);
LOG(L"[*] Breakpoint at 0x" << std::hex << exAddr << L" removed permanently");
}
if (breakpoints.find(addr) == breakpoints.end()) {
BYTE orig;
if (SetBreakpoint(pi.hProcess, addr, orig)) {
breakpoints[addr] = { orig, 1 };
LOG(L"[+] Breakpoint set at new address: 0x" << std::hex << addr);
}
}
else {
breakpoints[addr].remainingHits++;
}
}
if (hThread) CloseHandle(hThread);
}
break;
case EXCEPTION_SINGLE_STEP: {
CONTEXT ctx = {};
ctx.ContextFlags = CONTEXT_FULL;
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dbgEvent.dwThreadId);
RemoveHardwareBreakpointByAddress(hThread, exAddr);
GetThreadContext(hThread, &ctx);
auto it = cpuThreads.find(dbgEvent.dwThreadId);
if (it != cpuThreads.end()) {
CPU& cpu = it->second;
cpu.CPUThreadState = ThreadState::Running;
cpu.UpdateRegistersFromContext(ctx);
uint64_t addr = cpu.start_emulation();
LOG(L"[+] Emulation returned address: 0x" << std::hex << addr);
cpu.ApplyRegistersToContext(ctx);
if (SetHardwareBreakpointAuto(hThread, addr)) {
LOG(L"[+] Breakpoint set at new address: 0x" << std::hex << addr);
}
}
if (hThread) CloseHandle(hThread);
break;
}
case EXCEPTION_ACCESS_VIOLATION:
LOG(L"[!] Access Violation at 0x" << std::hex << exAddr);
exit(0);
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
LOG(L"[!] Illegal instruction at 0x" << std::hex << exAddr);
exit(0);
break;
case EXCEPTION_STACK_OVERFLOW:
LOG(L"[!] Stack overflow at 0x" << std::hex << exAddr);
exit(0);
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
LOG(L"[!] Divide by zero at 0x" << std::hex << exAddr);
exit(0);
break;
case EXCEPTION_PRIV_INSTRUCTION:
LOG(L"[!] Privileged instruction exception at 0x" << std::hex << exAddr);
break;
case 0x406d1388: // DBG_PRINTEXCEPTION_C
LOG(L"[i] Debug string output exception at 0x" << std::hex << exAddr);
break;
default:
LOG(L"[!] Unhandled exception 0x" << std::hex << exceptionCode << L" at 0x" << exAddr);
break;
}
break;
}
case EXIT_THREAD_DEBUG_EVENT:
cpuThreads.erase(dbgEvent.dwThreadId);
break;
case EXIT_PROCESS_DEBUG_EVENT:
goto cleanup;
default:
break;
}
ContinueDebugEvent(dbgEvent.dwProcessId, dbgEvent.dwThreadId, continueStatus);
}
cleanup:
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}