-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHWBRK.cpp
195 lines (160 loc) · 4.09 KB
/
HWBRK.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
#include "main.h"
LONG __stdcall hwbrkHandler(PEXCEPTION_POINTERS ExceptionInfo)
{
if(ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP || ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
{
for each(HWBRKEntry entry in *HWBRK->Hooks())
{
if(entry.targetFunction == ExceptionInfo->ContextRecord->Eip)
{
ExceptionInfo->ContextRecord->Eip = static_cast<DWORD>(entry.hook);
return EXCEPTION_CONTINUE_EXECUTION;
}
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
void *__fastcall hkBaseThreadInitThunk(ULONG Unknown, PVOID StartAddress, PVOID ThreadParameter)
{
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, GetCurrentThreadId());
if(hThread)
{
HWBRK->RefreshHooks(hThread);
CloseHandle(hThread);
}
return HWBRK->BaseThreadInitThunk() (Unknown, StartAddress, ThreadParameter);
}
int CHWBRK::GetFreeIndex(size_t regValue)
{
if(!(regValue & 1))
return 0;
else if(!(regValue & 4))
return 1;
else if(!(regValue & 16))
return 2;
else if(!(regValue & 64))
return 3;
return -1;
}
std::vector< HWBRKEntry > *CHWBRK::Hooks()
{
return &this->m_hooks;
}
tBaseThreadInitThunk CHWBRK::BaseThreadInitThunk()
{
return this->m_oBaseThreadInitThunk;
}
bool CHWBRK::HookThread(HANDLE thread, HWBRKEntry entry)
{
CONTEXT context = { 0 };
context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
if(!GetThreadContext(thread, &context))
{
CloseHandle(thread);
return false;
}
for(int i = 0; i < 4; i++)
{
if(*((size_t*)&context.Dr0 + i) == (size_t)entry.targetFunction)
{
return false; //function already hooked
}
}
int index = this->GetFreeIndex(context.Dr7);
if(index < 0)
{
return false;
}
context.Dr7 |= 1 << (2 * index) | 0x100; // enable corresponding HWBP and local BP flag
*((size_t*)&context.Dr0 + index) = (size_t)entry.targetFunction; // write address to DR0-DR3
// Suspend thread
DWORD suspendTime = 0x1337;
bool ret = true;
if(GetThreadId(thread) != GetCurrentThreadId())
{
suspendTime = SuspendThread(thread);
}
// Write values to registers
if(!SetThreadContext(thread, &context))
{
ret = false;
}
if(suspendTime <= 0)
{
ResumeThread(thread);
}
return ret;
}
void CHWBRK::RefreshHooks(HANDLE hThread)
{
//add every hook to debug register
for each(HWBRKEntry entry in this->m_hooks)
{
this->HookThread(hThread, entry);
}
}
void CHWBRK::RefreshHooks()
{
DWORD processId = GetCurrentProcessId();
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if(h != INVALID_HANDLE_VALUE)
{
THREADENTRY32 te;
te.dwSize = sizeof(te);
if(Thread32First(h, &te))
{
do
{
if(te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID) && te.th32OwnerProcessID == processId)
{
//Thread belongs to our process
if(te.th32ThreadID == GetCurrentThreadId())
{
continue;
}
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
if(hThread)
{
this->RefreshHooks(hThread);
CloseHandle(hThread);
}
}
te.dwSize = sizeof(te);
} while(Thread32Next(h, &te));
}
CloseHandle(h);
}
}
void CHWBRK::AddHook(DWORD_PTR targetFunction, DWORD_PTR hook)
{
HWBRKEntry newEntry;
newEntry.hook = hook;
newEntry.targetFunction = targetFunction;
this->m_hooks.push_back(newEntry);
if(this->m_hooks.size() > 4)
{
throw new std::runtime_error("Too many hooks registered");
}
this->RefreshHooks();
}
CHWBRK::CHWBRK()
{
this->m_hooks = std::vector<HWBRKEntry>();
this->m_handlerHandle = AddVectoredExceptionHandler(TRUE, hwbrkHandler);
byte *pBaseThreadInitThunk = reinterpret_cast<byte*>(GetProcAddress(GetModuleHandleA("kernel32.dll"), "BaseThreadInitThunk"));
this->m_oBaseThreadInitThunk = reinterpret_cast<tBaseThreadInitThunk>(DetourFunction(pBaseThreadInitThunk, reinterpret_cast<byte*>(hkBaseThreadInitThunk)));
}
CHWBRK::~CHWBRK()
{
this->m_hooks.clear();
this->RefreshHooks();
if(this->m_oBaseThreadInitThunk)
{
//DetourRemove ( )
}
if(this->m_handlerHandle)
{
RemoveVectoredExceptionHandler(this->m_handlerHandle);
}
}
CHWBRK *HWBRK = nullptr;