-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsample_int3Trick_hook.cpp
39 lines (30 loc) · 1.35 KB
/
sample_int3Trick_hook.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
/*
ïåðâàÿ èíñòðóêöèÿ ïåðåõâàòûâàåìîé ôóíêöèè çàìåíÿåòñÿ èíñòðóêöèåé ïðåðûâàíèÿ INT 3.
Äàëåå ïðîöåäóðà îáðàáîòêè íåîáðàáîòàííûõ èñêëþ÷åíèé (unhandled exception handler) ïîäìåíÿåò ðåãèñòð EIP íà àäðåñ íàøåé ôóíêöèè-ïåðåõâàò÷èêà.
*/
static DWORD_PTR m_dwFunction;
static LONG WINAPI MyUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
if (pExceptionInfo->ContextRecord->Eip != m_dwFunction)
return EXCEPTION_CONTINUE_SEARCH;
// Continue execution from MyMessageBeep
pExceptionInfo->ContextRecord->Eip = (DWORD_PTR)MyMessageBeep;
return EXCEPTION_CONTINUE_EXECUTION;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
m_dwFunction = (DWORD_PTR)::GetProcAddress(::GetModuleHandle("USER32.dll"), "MessageBeep");
BYTE nSavedByte = *(LPBYTE)m_dwFunction;
LPTOP_LEVEL_EXCEPTION_FILTER pOldFilter = ::SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
const BYTE nInt3 = 0xCC;
// Inject int 3
HRESULT hr = WriteProtectedMemory(LPVOID(m_dwFunction), &nInt3, sizeof(const BYTE));
if (SUCCEEDED(hr))
{
::MessageBeep(m_uType);
// Restore function
hr = WriteProtectedMemory(LPVOID(m_dwFunction), &nSavedByte, sizeof(BYTE));
}
::SetUnhandledExceptionFilter(pOldFilter);
return 0;
}