-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy patheaf.cpp
More file actions
181 lines (169 loc) · 5.15 KB
/
eaf.cpp
File metadata and controls
181 lines (169 loc) · 5.15 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
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
#include <Windows.h>
#include "util.h"
static int hw_break_count = 0;
#define HW_BREAK_COUNT_MAX 4
static int hw_break_table[HW_BREAK_COUNT_MAX];
extern HANDLE thread_lock;
void hook_eat(LPCWSTR lpModuleName)
{
HMODULE handle; // eax@2
int v2; // edx@3
int v3; // ecx@3
if ( hw_break_count != 4 )
{
HMODULE handle = GetModuleHandleW(lpModuleName);
if ( handle )
{
//what is this mean
v2 = *(DWORD *)((char *)handle + *((DWORD *)handle + 15) + 120);
v3 = hw_break_count++;
hw_break_table[v3] = (int)((char *)handle + v2 + 28);
}
}
}
void ClearHardwareBPS(HANDLE hThread)
{
CONTEXT lpcontext;
memset(&lpcontext, 0, sizeof(CONTEXT));
lpcontext.ContextFlags = 0x10010;
if ( GetThreadContext(GetCurrentThread(), (LPCONTEXT)&lpcontext) )
{
if ( lpcontext.Dr0 )
lpcontext.Dr7 &= 0xFFFFFFFE;
if ( lpcontext.Dr1 )
lpcontext.Dr7 &= 0xFFFFFFFB;
if ( lpcontext.Dr2 )
lpcontext.Dr7 &= 0xFFFFFFEF;
if ( lpcontext.Dr3 )
lpcontext.Dr7 &= 0xFFFFFFBF;
lpcontext.ContextFlags = 0x10010; // set ContextFlags
SetThreadContext(hThread, (const CONTEXT *)&lpcontext);
}
}
void SetHardwareBPS(HANDLE hThread)
{
CONTEXT lpcontext;
memset(&lpcontext, 0, sizeof(CONTEXT));
lpcontext.ContextFlags = 0x10010;
if ( GetThreadContext(GetCurrentThread(), (LPCONTEXT)&lpcontext) )
{
if ( lpcontext.Dr0 )
lpcontext.Dr7 |= 0x1;
if ( lpcontext.Dr1 )
lpcontext.Dr7 |= 0x4;
if ( lpcontext.Dr2 )
lpcontext.Dr7 |= 0x10;
if ( lpcontext.Dr3 )
lpcontext.Dr7 |= 0x40;
lpcontext.ContextFlags = 0x10010; // set ContextFlags
SetThreadContext(hThread, (const CONTEXT *)&lpcontext);
}
}
int ceh_handler(PEXCEPTION_POINTERS ExceptionInfo)
{
HMODULE phModule = NULL;
bool result;
if ( ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_SINGLE_STEP )
{
ClearHardwareBPS(GetCurrentThread());
PCONTEXT context = ExceptionInfo->ContextRecord;
if ( context->Dr6 & 0x11 ){
if ( !GetModuleHandleExW(6u, *(LPCWSTR *)(context->Eip), &phModule) )// check eip should in one of the module
find_invalid_execute(context, 0xC0000409u);// terminate execution
if ( !phModule )
find_invalid_execute(context, 0xC0000409u);
}
CONTEXT lpcontext;
memset(&lpcontext, 0, sizeof(CONTEXT));
lpcontext.ContextFlags = 0x10010;
GetThreadContext(GetCurrentThread(), (LPCONTEXT)&lpcontext);
lpcontext.Dr6 = 0;
lpcontext.ContextFlags = 0x10010;
SetThreadContext(GetCurrentThread(), (const CONTEXT *)&lpcontext);
SetHardwareBPS(GetCurrentThread());
result = -1;
} else {
result = 0;
}
return result;
}
PVOID set_ceh_handler()
{
return AddVectoredExceptionHandler(0x1, (PVECTORED_EXCEPTION_HANDLER)ceh_handler);
}
BOOL __stdcall set_bp2(void *handle, int index)
{
int bp_address;
bool result;
bp_address = hw_break_table[index];
CONTEXT lpcontext;
memset(&lpcontext, 0, sizeof(CONTEXT));
lpcontext.ContextFlags = 0x10010;
if ( GetThreadContext(handle, (LPCONTEXT)&lpcontext) )
{
unsigned int new_Dr7 = lpcontext.Dr7;
if ( index )
{
switch ( index )
{
case 1:
lpcontext.Dr1 = bp_address;
new_Dr7 = lpcontext.Dr7 & 0xFFFFFFF7 | 0xF00004;
break;
case 2:
lpcontext.Dr2 = bp_address;
new_Dr7 = lpcontext.Dr7 & 0xFFFFFFDF | 0xF000010;
break;
case 3:
lpcontext.Dr3 = bp_address;
new_Dr7 = lpcontext.Dr7 & 0xFFFFFF7F | 0xF0000040;
break;
}
}
else
{
lpcontext.Dr0 = bp_address;
new_Dr7 = lpcontext.Dr7 & 0xFFFFFFFD | 0xF0001;
}
lpcontext.Dr7 = new_Dr7 | 0x500; // set 4 bit length?
lpcontext.ContextFlags = 0x10010;
result = SetThreadContext(handle, (const CONTEXT *)&lpcontext);
}
return result;
}
void *set_bp(DWORD dwThreadId)
{
unsigned int index;
void *result;
void *threadhd;
bool suspended;
index = 0;
result = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_QUERY_INFORMATION, false, dwThreadId);
threadhd = result;
if ( result ){
suspended = SuspendThread(result) != -1;
if ( (unsigned int)hw_break_count > 0 ){
do
set_bp2(threadhd, index++);
while ( index < hw_break_count );
}
if ( suspended )
ResumeThread(threadhd);
result = (void *)CloseHandle(threadhd);
}
return result;
}
void StartAddress()
{
while ( 1 ){
Sleep(0x64);
WaitForSingleObject(thread_lock, 0xFFFFFFFF);
for (int i = thread_count; i; --i )
{
if ( threadid_list[i] != GetCurrentThreadId() )
set_bp(threadid_list[i]);
}
thread_count = 0;
ReleaseMutex(thread_lock);
}
}