-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
353 lines (309 loc) · 10.2 KB
/
main.cpp
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
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <TlHelp32.h>
#include <signal.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Error.hpp"
typedef BOOL Error;
class Debugger
{
private:
HANDLE hProcess = nullptr;
DWORD pid = 0;
BOOL active = FALSE;
BOOL first_breakpoint = TRUE;
HANDLE hThread = nullptr;
DWORD exception = 0;
void* exception_addr = nullptr;
CONTEXT ctx = { 0 };
DWORD page_size;
std::map<uintptr_t, std::vector<uintptr_t>> software_breakpoints;
std::vector<uintptr_t> gaurd_pages;
std::map<uintptr_t, std::vector<DWORD64>> memory_breakpoints;
public:
Debugger()
{
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
this->page_size = sysInfo.dwPageSize;
}
void load(std::string path_to_exe)
{
BOOL bCreated = FALSE;
PROCESS_INFORMATION pinfo;
STARTUPINFOA stinfo;
stinfo.cb = sizeof(STARTUPINFO);
stinfo.dwFlags = DEBUG_PROCESS;
stinfo.wShowWindow = (WORD)CREATE_NO_WINDOW;
bCreated = CreateProcessA(path_to_exe.c_str(),
NULL, NULL, NULL,
FALSE, DEBUG_PROCESS,
NULL, NULL, &stinfo,
&pinfo);
if (bCreated)
{
WriteOutput("We have successfully launched the process!\n");
WriteOutputFormatted("PID: %d\n", pinfo.dwProcessId);
this->hProcess = this->open_process(pinfo.dwProcessId);
}
else
WriteOutputFormatted("Error: 0x%08x.\n", GetLastError());
}
HANDLE open_process(int pid)
{
HANDLE h_process = OpenProcess(PROCESS_ALL_ACCESS, pid, FALSE);
return h_process;
}
void attach(int pid)
{
this->hProcess = this->open_process(pid);
if (DebugActiveProcess(pid))
{
this->active = TRUE;
this->pid = pid;
//this->run();
}
}
void run()
{
while (this->active)
{
this->get_debug_event();
}
}
void get_debug_event()
{
DEBUG_EVENT Event;
DWORD continue_status = DBG_CONTINUE;
if (WaitForDebugEvent(&Event, INFINITE))
{
this->hThread = this->open_thread(Event.dwThreadId);
this->ctx = this->get_thread_context(Event.dwThreadId);
printf("Event Code: %lu Thread ID: %lu\n", Event.dwDebugEventCode, Event.dwThreadId);
/* ContinueDebugEvent(Event.dwProcessId, Event.dwThreadId, continue_status);*/
if (Event.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
{
this->exception = Event.u.Exception.ExceptionRecord.ExceptionCode;
this->exception_addr = Event.u.Exception.ExceptionRecord.ExceptionAddress;
WriteOutputFormatted("Exception code: %lu\n", this->exception);
}
if (this->exception == EXCEPTION_ACCESS_VIOLATION)
{
WriteOutput("Access Violation Detected");
}
else if (this->exception == EXCEPTION_BREAKPOINT)
{
continue_status = this->exception_handler_breakpoint();
}
else if (this->exception == EXCEPTION_GUARD_PAGE)
{
WriteOutput("Guard Page Access Detected");
}
else if (this->exception == EXCEPTION_SINGLE_STEP)
{
this->exception_handler_single_step();
}
ContinueDebugEvent(Event.dwProcessId, Event.dwThreadId, continue_status);
}
}
BOOL exception_handler_single_step()
{
/*int slot = -1;
if (this->ctx.Dr6 & 0x1 && this->hardware_breakpoints.count(0))
slot = 1;*/
return TRUE;
}
DWORD exception_handler_breakpoint()
{
WriteOutput("Inside the breakpoint handler.\n");
WriteOutputFormatted("Exception Address: 0x%16p\n",this->exception_addr);
return DBG_CONTINUE;
}
BOOL detach()
{
if (DebugActiveProcessStop(this->pid))
{
WriteOutput("Finished debugging. Exiting ...");
return TRUE;
}
else
{
WriteError("There was an error");
return FALSE;
}
}
HANDLE open_thread(int threadId)
{
HANDLE hThread = OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT |
THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, FALSE, threadId);
if (hThread != NULL)
{
return hThread;
}
else
{
WriteError("Could not obtain a valid thread handle");
return NULL;
}
}
std::vector<DWORD> enumerate_threads()
{
BOOL bSuccess = TRUE;
THREADENTRY32 entry;
static std::vector<DWORD> thread_list;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, this->pid);
WriteOutput("Got handle to snapshot object ...");
if (hSnapshot != NULL)
{
WriteOutput("Snapshot not null");
entry.dwSize = sizeof(THREADENTRY32);
bSuccess = Thread32First(hSnapshot, &entry);
WriteOutput("Trying to get first thread Object");
while (bSuccess)
{
bSuccess = Thread32Next(hSnapshot, &entry);
if (entry.th32OwnerProcessID == this->pid)
{
WriteOutputFormatted("Owner PID: %d. ThreadId: %d\n", entry.th32OwnerProcessID, entry.th32ThreadID);
thread_list.push_back(entry.th32ThreadID);
}
}
CloseHandle(hSnapshot);
return thread_list;
}
else
{
thread_list.push_back((DWORD)-1);
return thread_list;
}
}
CONTEXT get_thread_context(int threadId)
{
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
HANDLE hThread = this->open_thread(threadId);
Sleep(100);
if (SuspendThread(hThread) == -1)
{
WriteOutputFormatted("Error suspending thread.. 0x%08x\n", GetLastError());
CloseHandle(hThread);
}
if (GetThreadContext(hThread, &ctx))
{
ResumeThread(hThread);
return ctx;
}
else
{
ResumeThread(hThread);
CloseHandle(hThread);
ctx.ContextFlags = (DWORD)-1;
return ctx;
}
}
char* read_process_memory(uintptr_t addr, size_t length)
{
char* buff = new char[length];
size_t count = 0;
if (ReadProcessMemory(this->hProcess, (void*)addr,buff,length,&count));
{
return buff;
}
delete[] buff;
return NULL;
}
BOOL write_process_memory(uintptr_t addr, char* data,size_t length)
{
size_t count = 0;
DWORD dwOldProtect = 0;
BOOL bPermChanged = FALSE;
bPermChanged = VirtualProtect((void*)addr, length, PAGE_EXECUTE_READWRITE, &dwOldProtect);
if (bPermChanged && WriteProcessMemory(this->hProcess, (void*)addr, data, length, &count))
{
VirtualProtect((void*)addr, length,dwOldProtect, &dwOldProtect);
return TRUE;
}
return FALSE;
}
BOOL soft_bp_set(uintptr_t addr)
{
if (!this->software_breakpoints.count(addr))
{
char int3[] = "\xCC";
try
{
char *original_byte = this->read_process_memory(addr, 1);
WriteOutputFormatted("original byte %s\n", original_byte);
BOOL bWritten = this->write_process_memory(addr,int3, 1);
if (!bWritten)
throw FALSE;
std::vector<uintptr_t> info = {addr,(uintptr_t)original_byte};
this->software_breakpoints[addr] = info;
}
catch(Error e){
return e;
}
}
return TRUE;
}
BOOL mem_bp_set(uintptr_t addr,DWORD size)
{
MEMORY_BASIC_INFORMATION* MemBasicInfo = new MEMORY_BASIC_INFORMATION;
BOOL bSuccess = VirtualQueryEx(this->hProcess, (void*)addr, MemBasicInfo, sizeof(MemBasicInfo)) < sizeof(MemBasicInfo);
if (!bSuccess)
return FALSE;
uintptr_t current_page = (uintptr_t)MemBasicInfo->BaseAddress; /* Current page base address */
while (current_page <= (addr + size))
{
this->gaurd_pages.push_back(current_page);
DWORD dwOldProtect = 0;
if (!VirtualProtectEx(this->hProcess,
(LPVOID)current_page, size, MemBasicInfo->Protect | PAGE_GUARD, &dwOldProtect))
{
return FALSE;
}
current_page += this->page_size;
}
this->memory_breakpoints[(addr)] = std::vector<uintptr_t>{addr,size,(uintptr_t)MemBasicInfo};
return TRUE;
}
uintptr_t func_resolve(char* dllname, char* function)
{
HMODULE DllHandle = LoadLibraryA(dllname);
if (DllHandle == NULL)
{
puts("DllHandle is NULL");
return NULL;
}
FARPROC addr = GetProcAddress(DllHandle,function);
if (addr == NULL)
return (uintptr_t)-1;
FreeLibrary(DllHandle);
return (uintptr_t)addr;
}
};
//void sighandler(int signum)
//{
// dbg.detach();
//}
int main()
{
//signal(SIGINT, sighandler);
Debugger dbg;
int pid;
printf("Enter the PID of the process to attach to: ");
scanf_s("%d", &pid);
puts("Attaching to process ...");
dbg.attach(pid);
char dllname[] = "kernel32.dll";
char function[] = "CreateFile";
uintptr_t addr = dbg.func_resolve(dllname,function);
if (addr == (uintptr_t)-1)
WriteError("Could not resolve function");
printf("Addr: %16p\n", (void*)addr);
dbg.soft_bp_set(addr);
dbg.run();
}