-
Notifications
You must be signed in to change notification settings - Fork 142
/
EvasionWatch.cpp
127 lines (110 loc) · 3.81 KB
/
EvasionWatch.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
#include "EvasionWatch.h"
#include "Util.h"
#include "TinyTracer.h"
#include "ModuleInfo.h"
bool EvasionWatch::EvasionAddCallbackBefore(IMG Image, const char* fName, uint32_t argNum, EvasionWatchBeforeCallBack callback)
{
if (!callback) return false;
const size_t argMax = 5;
if (argNum > argMax) argNum = argMax;
RTN funcRtn = find_by_unmangled_name(Image, fName);
if (RTN_Valid(funcRtn)) {
RTN_Open(funcRtn);
RTN_InsertCall(funcRtn, IPOINT_BEFORE, AFUNPTR(callback),
IARG_RETURN_IP,
IARG_THREAD_ID,
IARG_ADDRINT, fName,
IARG_UINT32, argNum,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_FUNCARG_ENTRYPOINT_VALUE, 2,
IARG_FUNCARG_ENTRYPOINT_VALUE, 3,
IARG_FUNCARG_ENTRYPOINT_VALUE, 4,
IARG_END
);
RTN_Close(funcRtn);
return true;
}
return false;
}
bool EvasionWatch::EvasionAddCallbackAfter(IMG Image, const char* fName, EvasionWatchAfterCallBack callback)
{
if (!callback) return false;
RTN funcRtn = find_by_unmangled_name(Image, fName);
if (RTN_Valid(funcRtn)) {
RTN_Open(funcRtn);
RTN_InsertCall(funcRtn, IPOINT_AFTER, AFUNPTR(callback),
IARG_RETURN_IP,
IARG_THREAD_ID,
IARG_ADDRINT, fName,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END);
RTN_Close(funcRtn);
return true;
}
return false;
}
//
EvasionFuncInfo* EvasionWatch::fetchFunctionInfo(const std::string& dllName, const std::string& funcName, t_watch_level maxLevel)
{
for (size_t i = 0; i < watchedFuncs.funcs.size(); i++) {
if (util::iequals(dllName, watchedFuncs.funcs[i].dllName)) {
EvasionFuncInfo& wfunc = watchedFuncs.funcs[i];
if (wfunc.funcName != funcName) {
continue;
}
if (wfunc.type > maxLevel) {
continue;
}
return &wfunc;
}
}
return nullptr;
}
EvasionFuncInfo* EvasionWatch::fetchSyscallFuncInfo(const std::string& funcName, t_watch_level maxLevel)
{
EvasionFuncInfo* wfunc = fetchFunctionInfo("ntdll", funcName, maxLevel);
if (!wfunc) {
wfunc = fetchFunctionInfo("win32u", funcName, maxLevel);
}
return wfunc;
}
size_t EvasionWatch::installCallbacks(IMG Image, EvasionWatchBeforeCallBack defaultCallback, t_watch_level maxLevel)
{
if (!isInit) {
return 0;
}
// callbacks before:
size_t added = 0;
const std::string dllName = util::getDllName(IMG_Name(Image));
for (size_t i = 0; i < watchedFuncs.funcs.size(); i++) {
if (util::iequals(dllName, watchedFuncs.funcs[i].dllName)) {
EvasionFuncInfo& wfunc = watchedFuncs.funcs[i];
if (wfunc.type > maxLevel) {
continue;
}
EvasionWatchBeforeCallBack* callbackBefore = wfunc.callbackBefore;
if (!callbackBefore && !wfunc.callbackAfter) {
callbackBefore = defaultCallback;
}
if (EvasionAddCallbackBefore(Image, wfunc.funcName.c_str(), wfunc.paramCount, callbackBefore)) {
added++;
}
if (wfunc.callbackAfter)
if (EvasionAddCallbackAfter(Image, wfunc.funcName.c_str(), wfunc.callbackAfter)) {
added++;
}
}
}
// callbacks after: do not verify the DLL name, because the function can be proxied
for (size_t i = 0; i < watchedFuncs.funcs.size(); i++) {
EvasionFuncInfo& wfunc = watchedFuncs.funcs[i];
if (wfunc.type > maxLevel) {
continue;
}
if (EvasionAddCallbackAfter(Image, wfunc.funcName.c_str(), wfunc.callbackAfter)) {
added++;
}
}
return added;
}