-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMain.cpp
More file actions
97 lines (79 loc) · 2.52 KB
/
Main.cpp
File metadata and controls
97 lines (79 loc) · 2.52 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
#include "HardwareBreakpoint.hpp"
void(*_printf)(const char* szFmt, ...);
void __cdecl printf_hook(const char* szFmt, ...);
int main()
{
//! Test BreakpointHandlerType::Notify on execution
{
HardwareBreakpoint breakpoint;
//
// Change MessageBoxA text
BreakpointHandler handler{}; // Note: handlers are optional
handler.m_type = BreakpointHandlerType::Notify; // No hooking, just notify when the breakpoint is hit
#if defined(HWBP_X64)
handler.m_var = [](EXCEPTION_POINTERS* p)
{
printf("$ Exception caught - modifying RDX\n");
//
// Second argument will hold the text, so RDX
p->ContextRecord->Rdx = reinterpret_cast<std::uintptr_t>("Changed the text!");
};
#else
handler.m_var = [](EXCEPTION_POINTERS* p)
{
printf("$ Exception caught - modifying [esp+8]\n");
//
// Second argument will hold the text, so [esp+8]
*reinterpret_cast<const char**>(p->ContextRecord->Esp + 8) = "Changed the text!";
};
#endif
breakpoint.Create(MessageBoxA, BreakpointLength::OneByte, BreakpointCondition::Execute, handler);
//
// Call to test
MessageBoxA(NULL, "Change this text!", "Test", MB_OK);
}
//! Test BreakpointHandlerType::Hook
{
HardwareBreakpoint breakpoint;
BreakpointHandler handler{};
handler.m_type = BreakpointHandlerType::Hook; // Redirect printf
handler.m_var = printf_hook;
breakpoint.Create(printf, BreakpointLength::OneByte, BreakpointCondition::Execute, handler);
_printf = ((decltype(_printf))breakpoint.GetBuffer());
//
// Test printf (this will print "$ Inside printf_hook.")
printf("This is a test.\n");
}
//! Test a dword/qword read/write
{
#if defined(HWBP_X64)
BreakpointLength bplen = BreakpointLength::EightByte;
#else
BreakpointLength bplen = BreakpointLength::FourByte;
#endif
std::uintptr_t dummy{};
HardwareBreakpoint breakpoint;
BreakpointHandler handler{};
handler.m_type = BreakpointHandlerType::Notify;
handler.m_var = [](EXCEPTION_POINTERS* p)
{
#if defined(HWBP_X64)
printf("$ Dummy was read/written - current RIP: 0x%p\n", p->ContextRecord->Rip);
#else
printf("$ Dummy was read/written - current EIP: 0x%p\n", p->ContextRecord->Eip);
#endif
};
breakpoint.Create(&dummy, bplen, BreakpointCondition::ReadWrite, handler);
//
// Invoke the breakpoint
dummy = 1337;
}
//
// Clean up by calling HwbpTerminate
HwbpTerminate();
return getchar();
}
void __cdecl printf_hook(const char* szFmt, ...)
{
_printf("$ Inside printf_hook.\n");
}