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
|
#include "dll.h"
#include <stdlib.h>
CContextHook GContextHook;
LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS* e)
{
if (e->ExceptionRecord->ExceptionCode != EXCEPTION_SINGLE_STEP)
{
return EXCEPTION_CONTINUE_SEARCH;
}
Context_t* Context = GContextHook.GetContextInfo();
if (Context)
{
if (e->ExceptionRecord->ExceptionAddress == (PVOID)(intptr_t)Context->Hook1 ||
e->ExceptionRecord->ExceptionAddress == (PVOID)(intptr_t)Context->Hook2 ||
e->ExceptionRecord->ExceptionAddress == (PVOID)(intptr_t)Context->Hook3 ||
e->ExceptionRecord->ExceptionAddress == (PVOID)(intptr_t)Context->Hook4)
{
Handler_t Handler = GContextHook.GetHandlerInfo();
if (Handler)
{
Handler(Context, e);
}
return EXCEPTION_CONTINUE_EXECUTION;
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
bool CContextHook::InitiateContext(Handler_t ContextHandler, Context_t* C)
{
if (C == NULL || ContextHandler == NULL)
return false;
m_Handler = ContextHandler;
memcpy(&m_Context, C, sizeof(Context_t));
if (IsReady(&C->Hook1) == false)
return false;
HANDLE hMainThread = GetMainThread();
if (hMainThread == INVALID_HANDLE_VALUE)
return false;
srand(GetTickCount());
PVOID pHandler = AddVectoredExceptionHandler(1, ExceptionHandler);
if (pHandler == NULL)
return false;
m_pHandler = pHandler;
CONTEXT c;
c.ContextFlags = CONTEXT_DEBUG_REGISTERS;
SuspendThread(hMainThread);
GetThreadContext(hMainThread, &c);
c.Dr0 = C->Hook1;
int SevenFlags = (1 << 0);
if (IsReady(&C->Hook2))
{
SevenFlags |= (1 << 2);
c.Dr1 = C->Hook2;
}
if (IsReady(&C->Hook3))
{
SevenFlags |= (1 << 4);
c.Dr2 = C->Hook3;
}
if (IsReady(&C->Hook4))
{
SevenFlags |= (1 << 6);
c.Dr3 = C->Hook4;
}
c.Dr6 = 0x00000000;
c.Dr7 = SevenFlags;
SetThreadContext(hMainThread, &c);
ResumeThread(hMainThread);
return true;
}
Context_t* CContextHook::GetContextInfo(void)
{
return &m_Context;
}
Handler_t CContextHook::GetHandlerInfo(void)
{
return m_Handler;
}
bool CContextHook::ClearContext(void)
{
HANDLE hMainThread = GetMainThread();
if (hMainThread == INVALID_HANDLE_VALUE)
return false;
CONTEXT c;
c.ContextFlags = CONTEXT_DEBUG_REGISTERS;
SuspendThread(hMainThread);
GetThreadContext(hMainThread, &c);
c.Dr0 = 0;
c.Dr1 = 0;
c.Dr2 = 0;
c.Dr3 = 0;
c.Dr6 = 0;
c.Dr7 = 0;
SetThreadContext(hMainThread, &c);
ResumeThread(hMainThread);
return true;
}
bool CContextHook::IsReady(unsigned long* H)
{
if (!H)
return false;
return (*H != NULL);
}
DWORD CContextHook::GetProcessThreadId(unsigned long ProcessId)
{
HANDLE hSnapThread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, ProcessId);
if (hSnapThread == INVALID_HANDLE_VALUE)
{
return NULL;
}
THREADENTRY32 te;
te.dwSize = sizeof(THREADENTRY32);
if (Thread32First(hSnapThread, &te))
{
do
{
if (te.th32OwnerProcessID == ProcessId)
{
CloseHandle(hSnapThread);
return te.th32ThreadID;
}
} while (Thread32Next(hSnapThread, &te));
}
CloseHandle(hSnapThread);
return 0;
}
#define ADDRESSTOHOOK 0x7FF73FB32B7B
void ContextHandler(Context_t* C, EXCEPTION_POINTERS* E)
{
MessageBoxA(0, "BPHIT", "BPHIT BPHIT", 0);
if (!C || !E)
return;
switch (E->ContextRecord->Rip)
{
case ADDRESSTOHOOK:
{
MessageBoxA(0, "BPHIT", "BPHIT BPHIT", 0);
E->ContextRecord->Rax = 1;
//GLogging.AddToLogFileA("hook.log", "BP HIT");
break;
}
}
}
void Initialize3Context()
{
Context_t C;
C.Hook1 = ADDRESSTOHOOK;
if (GContextHook.InitiateContext(ContextHandler, &C))
{
MessageBoxA(0, "", "HOOKED SUCCESS", 0);
}
else
{
MessageBoxA(0, "", "FAILURE ", 0);
}
}
HANDLE CContextHook::GetMainThread(void)
{
DWORD ProcessId = GetCurrentProcessId();
DWORD ProcessThreadId = GetProcessThreadId(ProcessId);
return OpenThread(THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME, TRUE, ProcessThreadId);
}
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Initialize3Context, 0, 0, 0);
break;
}
return TRUE;
} |
Partager