forked from F8LEFT/FAInHook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFAInHook.cpp
245 lines (209 loc) · 6.38 KB
/
FAInHook.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//===------------------------------------------------------------*- C++ -*-===//
//
// Created by F8LEFT on 2017/6/19.
// Copyright (c) 2017. All rights reserved.
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
#include "FAInHook.h"
#include "MemHelper.h"
#include "HookInfo.h"
#include "instruction/Instruction.h"
#if defined(__arm__)
#include "instruction/ArmInstruction.h"
#include "instruction/ThumbInstruction.h"
#elif defined(__aarch64__)
#include "instruction/Arm64Instruction.h"
#elif defined(__i386__) || defined(__x86_64__)
#include "instruction/IntelInstruction.h"
#elif defined(__mips64__) /* mips64el-* toolchain defines __mips__ too */
#include "instruction/Mips64Instruction.h"
#elif defined(__mips__)
#include "instruction/MipsInstruction.h"
#endif
FAInHook *FAInHook::instance() {
static FAInHook* mIns = nullptr;
if(mIns == nullptr) {
mIns = new FAInHook();
}
return mIns;
}
FAInHook::FAInHook() {
}
FAInHook::~FAInHook() {
}
FAInHook::HOOK_STATUS FAInHook::registerHook(
Elf_Addr orginalFunAddr, Elf_Addr newFunAddr, Elf_Addr *callOrigin) {
// register hook information, calc hook stub at the same time.
if(!FAHook::MemHelper::isFunctionAddr((void *) orginalFunAddr)
|| !FAHook::MemHelper::isFunctionAddr((void *) newFunAddr)) {
return FERROR_NOT_EXECUTABLE;
}
auto info = getHookInfo(orginalFunAddr);
if(nullptr != info) {
auto hookStatus = info->getHookStatus();
if(FAHook::HOOKED == hookStatus) {
return FERROR_ALREADY_HOOKED;
} else if(FAHook::REGISTERED == hookStatus) {
delHookInfo(info);
}
}
// check for FunctionType
auto type = FAHook::Instruction::getFunctionType(orginalFunAddr);
if(FAHook::ERRTYPE == type) {
return FERROR_UNKNOWN;
}
info = new FAHook::HookInfo((void *) orginalFunAddr, (void *) newFunAddr);
info->setOriginalFunctionType(type);
FAHook::Instruction* instruction = nullptr;
switch(type) {
#if defined(__arm__)
case FAHook::ARM:
instruction = new FAHook::ArmInstruction();
break;
case FAHook::THUMB:
instruction = new FAHook::ThumbInstruction();
break;
#elif defined(__aarch64__)
case FAHook::ARM64:
instruction = new FAHook::Arm64Instruction();
break;
#elif defined(__i386__) || defined(__x86_64__)
case FAHook::X86:
case FAHook::X64:
instruction = new FAHook::IntelInstruction();
break;
#elif defined(__mips64__)
case FAHook::MIPS64:
instruction = new FAHook::Mips64Instruction();
break;
#elif defined(__mips__)
case FAHook::MIPS:
instruction = new FAHook::MipsInstruction();
break;
#endif
default:
assert(false && "not support abi");
return FERROR_UNKNOWN;
break;
}
if(!instruction->createStub(info)
|| !instruction->createBackStub(info)
|| (callOrigin != nullptr) ?
!instruction->createCallOriginalStub(info) : false // want a callback
) {
delete instruction;
delete info;
return FERROR_MEMORY;
}
addHookInfo(info);
info->setHookStatus(FAHook::REGISTERED);
if(callOrigin != nullptr) {
*callOrigin = (Elf_Addr) info->getCallOriginalIns();
}
delete instruction;
return FERROR_SUCCESS;
}
bool FAInHook::hook(Elf_Addr originalFunAddr) {
auto info = getHookInfo(originalFunAddr);
if(info == nullptr) {
return false;
}
if(info->getHookStatus() == FAHook::HOOKED) {
return true;
} else if(info->getHookStatus() != FAHook::REGISTERED) {
return false;
}
return Hook(info);
}
bool FAInHook::unhook(Elf_Addr originalFuncAddr) {
auto info = getHookInfo(originalFuncAddr);
if(info == nullptr) {
return false;
}
if(info->getHookStatus() == FAHook::REGISTERED) {
return true;
} else if(info->getHookStatus() != FAHook::HOOKED) {
return false;
}
return UnHook(info);
}
void FAInHook::hookAll() {
for(auto it: hook_map) {
if(it.second->getHookStatus() == FAHook::REGISTERED) {
Hook(it.second);
}
}
}
void FAInHook::unhookAll() {
for(auto it: hook_map) {
if(it.second->getHookStatus() == FAHook::HOOKED) {
UnHook(it.second);
}
}
}
bool FAInHook::isAlreadyHooked(Elf_Addr originalFunAddr) {
auto it = hook_map.find(originalFunAddr);
if(it == hook_map.end()) {
return false;
}
auto info = it->second;
return info->getHookStatus() == FAHook::HOOKED;
}
Elf_Addr FAInHook::getCallOriginFuncAddr(Elf_Addr originalFunAddr) {
auto info = getHookInfo(originalFunAddr);
if(info == nullptr) {
return 0;
}
return (Elf_Addr) info->getCallOriginalIns();
}
Elf_Addr FAInHook::getNewFunAddr(Elf_Addr originalFunAddr) {
auto info = getHookInfo(originalFunAddr);
if(info == nullptr) {
return 0;
}
return (Elf_Addr) info->getHookAddr();
}
int FAInHook::getHookedCount() {
return hook_map.size();
}
FAHook::HookStatus FAInHook::getFunctionStatus(Elf_Addr functionAddr) {
auto it = hook_map.find(functionAddr);
if(it == hook_map.end()) {
return FAHook::ERRSTATUS;
}
return it->second->getHookStatus();
}
FAHook::HookInfo *FAInHook::getHookInfo(Elf_Addr origFunAddr) {
auto it = hook_map.find(origFunAddr);
if(it == hook_map.end()) {
return nullptr;
}
return it->second;
}
void FAInHook::addHookInfo(FAHook::HookInfo *info) {
hook_map[(Elf_Addr)info->getOriginalAddr()] = info;
}
bool FAInHook::delHookInfo(FAHook::HookInfo *info) {
auto it = hook_map.find((Elf_Addr)info->getOriginalAddr());
if(it != hook_map.end()) {
delete it->second;
hook_map.erase(it);
return true;
}
return false;
}
bool FAInHook::Hook(FAHook::HookInfo *info) {
if(!FAHook::Instruction::enableJumpStub(info)) {
return false;
}
info->setHookStatus(FAHook::HOOKED);
return true;
}
bool FAInHook::UnHook(FAHook::HookInfo *info) {
if(!FAHook::Instruction::disableJumpStub(info)) {
return false;
}
info->setHookStatus(FAHook::REGISTERED);
return true;
}