forked from hasherezade/tiny_tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvasionWatch.h
110 lines (87 loc) · 3.21 KB
/
EvasionWatch.h
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
#pragma once
#include "pin.H"
#include "FuncWatch.h"
#include <map>
//---
typedef enum {
WATCH_DISABLED = 0, // Evasion detection is disabled
WATCH_STANDARD = 1, // Track "standard" and easily identifiable techniques
WATCH_DEEP = 2, // Track more techniques, may lead to false positives
WATCH_OPTIONS_COUNT
} t_watch_level;
inline t_watch_level ConvertWatchLevel(int value)
{
if (value >= WATCH_OPTIONS_COUNT) {
// choose the last option:
return t_watch_level(WATCH_OPTIONS_COUNT - 1);
}
return (t_watch_level)value;
}
//---
struct FuncData
{
public:
FuncData() : name(""), argsNum(0) { }
FuncData(const std::string& _name, size_t _argsNum) : name(_name), argsNum(_argsNum)
{
::memset(args, 0, sizeof(args));
}
FuncData(const FuncData& other)
{
name = other.name;
argsNum = other.argsNum;
::memcpy(args, other.args, sizeof(args));
}
std::string name;
size_t argsNum;
VOID* args[5];
};
//---
inline VOID storeData(std::map<THREADID, FuncData>& funcDataStorage, THREADID tid, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
FuncData data(name, argCount);
data.args[0] = arg1;
data.args[1] = arg2;
data.args[2] = arg3;
data.args[3] = arg4;
data.args[4] = arg5;
funcDataStorage[tid] = data;
}
inline BOOL retrieveData(std::map<THREADID, FuncData>& funcDataStorage, THREADID tid, const CHAR* name, FuncData& data)
{
FuncData& _data = funcDataStorage[tid];
if (_data.name != name) {
return FALSE;
}
data = _data;
return TRUE;
}
//---
typedef VOID EvasionWatchBeforeCallBack(const ADDRINT Address, const THREADID tid, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5);
typedef VOID EvasionWatchAfterCallBack(const ADDRINT Address, const THREADID tid, const CHAR* name, ADDRINT result);
struct EvasionFuncInfo : public WFuncInfo
{
EvasionFuncInfo(const std::string& _dllName, const std::string& _funcName, const size_t _paramCount, EvasionWatchBeforeCallBack* _callbackB = nullptr, EvasionWatchAfterCallBack* _callbackA = nullptr, t_watch_level _type = WATCH_STANDARD)
: WFuncInfo(_dllName, _funcName, _paramCount),
callbackBefore(_callbackB), callbackAfter(_callbackA), type(_type)
{
}
EvasionWatchBeforeCallBack* callbackBefore;
EvasionWatchAfterCallBack* callbackAfter;
t_watch_level type;
};
//---
class EvasionWatch
{
public:
static bool EvasionAddCallbackBefore(IMG Image, const char* fName, uint32_t argNum, EvasionWatchBeforeCallBack callback);
static bool EvasionAddCallbackAfter(IMG Image, const char* fName, EvasionWatchAfterCallBack callback);
EvasionWatch() : isInit(FALSE) { }
virtual BOOL Init() = 0;
EvasionFuncInfo* fetchFunctionInfo(const std::string& dllName, const std::string& funcName, t_watch_level maxLevel);
EvasionFuncInfo* fetchSyscallFuncInfo(const std::string& funcName, t_watch_level maxLevel);
size_t installCallbacks(IMG Image, EvasionWatchBeforeCallBack defaultCallbackBefore, t_watch_level maxLevel);
FuncList<EvasionFuncInfo> watchedFuncs;
protected:
BOOL isInit;
};