forked from scrt/avcleaner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApiMatchHandler.cpp
412 lines (304 loc) · 15.2 KB
/
ApiMatchHandler.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//
// Created by Vladimir on 20.06.20.
//
#include "ApiMatchHandler.h"
#include <string>
#include <sstream>
#include <climits>
#include <fstream>
#include "clang/Lex/Preprocessor.h"
#include "clang/Tooling/Inclusions/IncludeStyle.h"
#include "Globals.h"
#include "Utils.h"
#include <filesystem>
using namespace clang;
std::string ApiMatchHandler::getFunctionIdentifier(const CallExpr *CallExpression) {
const FunctionDecl *FnDeclaration = CallExpression->getDirectCallee();
//abort if invalid call
if (FnDeclaration == nullptr)
return nullptr;
IdentifierInfo *II = FnDeclaration->getIdentifier();
if (II == nullptr) {
return nullptr;
}
return II->getName().data();
}
bool ApiMatchHandler::replaceIdentifier(const CallExpr *CallExpression, const std::string &ApiName,
const std::string &NewIdentifier) {
return this->ASTRewriter->ReplaceText(CallExpression->getBeginLoc(), ApiName.length(), NewIdentifier);
}
bool ApiMatchHandler::handleCallExpr(const CallExpr *CallExpression, clang::ASTContext *const pContext) {
std::string Identifier = getFunctionIdentifier(CallExpression);
if (shouldReplaceWithSyscall(Identifier)) {
rewriteApiToSyscall(CallExpression, pContext, Identifier);
return true;
}
std::string Replacement = Utils::translateStringToIdentifier(Identifier);
if (!addGetProcAddress(CallExpression, pContext, Replacement, Identifier))
return false;
return replaceIdentifier(CallExpression, Identifier, Replacement);
}
void ApiMatchHandler::run(const MatchResult &Result) {
llvm::outs() << "Found " << _ApiName << "\n";
llvm::outs() << "Found " << _TypeDef << "\n";
const auto *CallExpression = Result.Nodes.getNodeAs<clang::CallExpr>("callExpr");
handleCallExpr(CallExpression, Result.Context);
}
bool ApiMatchHandler::addGetProcAddress(const clang::CallExpr *pCallExpression, clang::ASTContext *const pContext,
const std::string &NewIdentifier, std::string &ApiName) {
SourceRange EnclosingFunctionRange = findInjectionSpot(pContext, clang::DynTypedNode(),
*pCallExpression, 0);
std::stringstream Result;
// add function prototype if not already added
//if(std::find(TypedefAdded.begin(), TypedefAdded.end(), pCallExpression->getDirectCallee()) == TypedefAdded.end()) {
Result << "\t" << _TypeDef << "\n";
//}
// add LoadLibrary with obfuscated strings
std::string LoadLibraryVariable = Utils::translateStringToIdentifier(_Library);
std::string LoadLibraryString = Utils::generateVariableDeclaration(LoadLibraryVariable, _Library);
std::string LoadLibraryHandleIdentifier = Utils::translateStringToIdentifier("hHandle_" + _Library);
Result << "\t" << LoadLibraryString << std::endl;
Result << "\tHANDLE " << LoadLibraryHandleIdentifier << " = LoadLibrary(" << LoadLibraryVariable << ");\n";
// add GetProcAddress with obfuscated string: TypeDef NewIdentifier = (TypeDef) GetProcAddress(handleIdentifier, ApiName)
std::string ApiNameIdentifier = Utils::translateStringToIdentifier(ApiName);
std::string ApiNameDecl = Utils::generateVariableDeclaration(ApiNameIdentifier, ApiName);
Result << "\t" << ApiNameDecl << "\n";
Result << "\t_" << ApiName << " " << NewIdentifier << " = (_" << ApiName << ") GetProcAddress("
<< LoadLibraryHandleIdentifier << ", " << ApiNameIdentifier << ");\n";
TypedefAdded.push_back(pCallExpression->getDirectCallee());
// add everything at the beginning of the function.
return !(ASTRewriter->InsertText(EnclosingFunctionRange.getBegin(), Result.str()));
}
SourceRange
ApiMatchHandler::findInjectionSpot(clang::ASTContext *const Context, clang::DynTypedNode Parent,
const clang::CallExpr &Literal, uint64_t Iterations) {
if (Iterations > Globs::CLIMB_PARENTS_MAX_ITER)
throw std::runtime_error("Reached max iterations when trying to find a function declaration");
clang::DynTypedNodeList parents = Context->getParents(Literal);;
if (Iterations > 0) {
parents = Context->getParents(Parent);
}
for (const auto &parent : parents) {
StringRef ParentNodeKind = parent.getNodeKind().asStringRef();
if (ParentNodeKind.find("FunctionDecl") != std::string::npos) {
auto FunDecl = parent.get<clang::FunctionDecl>();
auto *Statement = FunDecl->getBody();
auto *FirstChild = *Statement->child_begin();
return {FirstChild->getBeginLoc(), FunDecl->getEndLoc()};
}
return findInjectionSpot(Context, parent, Literal, ++Iterations);
}
}
static std::vector<std::string> GetArgs(const CallExpr *CallExpression) {
std::vector<std::string> Args;
clang::LangOptions LangOpts;
LangOpts.CPlusPlus = true;
clang::PrintingPolicy Policy(LangOpts);
for (unsigned int i = 0; i < CallExpression->getNumArgs(); i++) {
std::string TypeS;
llvm::raw_string_ostream s(TypeS);
CallExpression->getArg(i)->printPretty(s, 0, Policy);
Args.push_back(s.str());
}
return Args;
}
bool ApiMatchHandler::shouldReplaceWithSyscall(std::string ApiName) {
return std::find(Syscalls.begin(), Syscalls.end(), ApiName) != Syscalls.end();
}
clang::SourceLocation
ApiMatchHandler::findFirstFunctionDecl(const Expr &pExpr, clang::DynTypedNode Node,
clang::ASTContext *const Context, SourceLocation Loc,
uint64_t Iterations) {
if (Iterations > Globs::CLIMB_PARENTS_MAX_ITER) {
return Loc;
}
clang::DynTypedNodeList parents = Context->getParents(pExpr);
if (Iterations > 0) {
parents = Context->getParents(Node);
}
for (const auto &parent : parents) {
StringRef ParentNodeKind = parent.getNodeKind().asStringRef();
if (ParentNodeKind.find("TranslationUnitDecl") != std::string::npos) {
llvm::outs() << "Found TranslationUnitDecl\n";
bool invalid;
auto TmpLineNumber = INT_MAX;
llvm::outs() << "TmpLineNumber : " << TmpLineNumber << "\n";
for (auto it = parent.get<clang::TranslationUnitDecl>()->decls_begin();
it != parent.get<clang::TranslationUnitDecl>()->decls_end(); it++) {
int toto = ASTRewriter->getSourceMgr().getLineNumber(ASTRewriter->getSourceMgr().getMainFileID(),
it->getBeginLoc().getRawEncoding(), &invalid);
//llvm::outs() << "Decl of " << it->getDeclKindName() << " @ " << toto << "\n";
if (std::string(it->getDeclKindName()) == "Function" && toto < TmpLineNumber) {
Loc = it->getBeginLoc();
TmpLineNumber = toto;
}
}
llvm::outs() << "First function @ " << TmpLineNumber << "\n";
Globs::FirstFunctionDeclLoc = Loc;
}
return findFirstFunctionDecl(pExpr, parent, Context, Loc, ++Iterations);
}
return Loc;
}
/*
* 1. Rename API to random identifier
* 2. Adapt parameters
* 3. Handle If conditions since the return value is different
* 4.
*/
void ApiMatchHandler::rewriteApiToSyscall(const clang::CallExpr *pExpr, clang::ASTContext *const pContext,
std::string ApiName) {
std::string Replacement, Prefix, Suffix = "";
std::ostringstream params(std::ostringstream::out);
SourceRange Range;
llvm::outs() << _TypeDef << "\n";
if (ApiName == "WriteProcessMemory") {
llvm::outs() << "[*] Found WriteProcessMemory\n";
std::vector<std::string> FunctionArgs = GetArgs(pExpr);
// reussite = ((fZwWriteVirtualMemory(handleProcess, (VOID*)adresseBase, adresseSource, longueur, &dwBytesWrite) != 0) && (dwBytesWrite == longueur));
params << "("
<< FunctionArgs.at(0) << ", "
<< FunctionArgs.at(1) << ", (PVOID)("
<< FunctionArgs.at(2) << "), (ULONG)("
<< FunctionArgs.at(3) << "), (PULONG)("
<< FunctionArgs.at(4) << "))";
Replacement = params.str();
Range = clang::SourceRange(pExpr->getBeginLoc(), pExpr->getEndLoc());
} else if (ApiName == "CreateRemoteThread") {
/*
* Variable cible d'assignation hThread devient premier paramètre
* 1er paramètre devient 4ème.
* 4ème et 5ème paramètres deviennent 5 et 6ème.
*/
std::vector<std::string> FunctionArgs = GetArgs(pExpr);
std::ostringstream params(std::ostringstream::out);
std::string VarName = getCallExprAssignmentVarName(pExpr, pContext);
params << "(&" << VarName << ", THREAD_ALL_ACCESS, NULL, "
<< FunctionArgs.at(0) << ", "
<< FunctionArgs.at(3) << ", "
<< FunctionArgs.at(4) << ", 0x00000001 | 0x00000004, 0, 0, 0, NULL)";
Replacement = params.str();
auto ParentNode = pContext->getParents(*pExpr).begin();
Range = clang::SourceRange(ParentNode->getSourceRange().getBegin(), pExpr->getEndLoc());
if(ParentNode->getNodeKind().asStringRef() == "VarDecl") {
Prefix = ParentNode->get<clang::VarDecl>()->getType().getAsString() + " " + VarName + ";\n\t";
}
//SourceLocation NewStart = !CallInfos._VarName.empty() ? CallInfos._StartOfLHS : CallExpression->getBeginLoc();
}
if (isInsideIfCondition(pExpr, pContext)) {
llvm::outs() << "CompountStmt > IfStmt\n";
Suffix = "==ERROR_SUCCESS";
}
llvm::outs() << "Replacing with " << Prefix + Replacement + Suffix << "\n";
SourceRange EnclosingFunctionRange = findInjectionSpot(pContext, clang::DynTypedNode(),
*pExpr, 0);
// remember line number + function name
bool invalid;
auto toto = ASTRewriter->getSourceMgr().getLineNumber(ASTRewriter->getSourceMgr().getMainFileID(),
EnclosingFunctionRange.getBegin().getRawEncoding(), &invalid);
auto index = std::pair<int, std::string>(toto, _ApiName);
bool AlreadyInitialized = Globs::TypeDefsInserted.count(index) != 0;
auto FunctionPointerIdentifier = std::string();
if (AlreadyInitialized) {
FunctionPointerIdentifier = Globs::TypeDefsInserted.at(index);
} else {
FunctionPointerIdentifier = Utils::translateStringToIdentifier(_ApiName);
}
ASTRewriter->ReplaceText(Range, Prefix + FunctionPointerIdentifier + Replacement + Suffix);
if (AlreadyInitialized) {
return;
}
// remember that the fn pointer was already initialised
Globs::TypeDefsInserted.insert({index, FunctionPointerIdentifier});
/*
*
void *memWriteVirtualMemory = get_shellcode_buffer("ZwWriteVirtualMemory");
_ZwWriteVirtualMemory fZwWriteVirtualMemory =(_ZwWriteVirtualMemory)(memWriteVirtualMemory);
*/
std::ostringstream InitStream(std::ostringstream::out);
auto MemoryBufferIdentifier = Utils::translateStringToIdentifier(_ApiName);
InitStream << _TypeDef << "\n";
InitStream << "\tvoid *" << MemoryBufferIdentifier << " = get_shellcode_buffer(\"" << _NtName
<< "\");\n";
InitStream << "\t_" << _NtName << " " << FunctionPointerIdentifier << " =(_"
<< _NtName << ")("
<< MemoryBufferIdentifier << ");\n\n";
ASTRewriter->InsertText(EnclosingFunctionRange.getBegin(), InitStream.str(), false, true);
if (Globs::SyscallInserted) {
return;
}
Globs::SyscallInserted = true;
auto FirstFunctionDeclLoc = findFirstFunctionDecl(*pExpr, clang::DynTypedNode(), pContext,
clang::SourceLocation(), 0);
// insert some code to dynamically get syscalls IDs from ntdll
std::ifstream fd("/tmp/patch_enum_syscalls.txt");
std::stringstream buffer;
// Verify that the file was open successfully
if (fd) {
buffer << fd.rdbuf();
//insert some declaration at the beginning of the translation unit
ASTRewriter->InsertText(FirstFunctionDeclLoc, buffer.str(), false, true);
} else {
llvm::errs() << "File could not be opened in " << std::filesystem::current_path(); // Report error
llvm::errs() << "Error code: " << strerror(errno); // Get some info as to why
}
}
std::vector<std::string>
ApiMatchHandler::getParents(const Expr &pExpr, clang::DynTypedNode Node,
clang::ASTContext *const Context, std::vector<std::string> &CurrentParents,
uint64_t Iterations) {
if (Iterations > Globs::CLIMB_PARENTS_MAX_ITER) {
return CurrentParents;
}
clang::DynTypedNodeList parents = Context->getParents(pExpr);
if (Iterations > 0) {
parents = Context->getParents(Node);
}
for (const auto &parent : parents) {
StringRef ParentNodeKind = parent.getNodeKind().asStringRef();
if (ParentNodeKind.find("Cast") != std::string::npos) {
return getParents(pExpr, parent, Context, CurrentParents, ++Iterations);
}
CurrentParents.push_back(ParentNodeKind.data());
return getParents(pExpr, parent, Context, CurrentParents, ++Iterations);
}
return CurrentParents;
}
bool ApiMatchHandler::isInsideIfCondition(const clang::CallExpr *pExpr, clang::ASTContext *const pContext) {
std::vector<std::string> Parents;
getParents(*pExpr, clang::DynTypedNode(), pContext, Parents, 0);
for (auto &parent : Parents) {
llvm::outs() << "Parent is : " << parent << "\n";
}
auto it = std::find(Parents.begin(), Parents.end(), "IfStmt");
if (it != Parents.end()) {
// WriteProcessMemory call is located within an If statement. Now we should check if it's the
// in the If condition or the If Body.
auto CompoundStmtIt = std::find(Parents.begin(), Parents.end(), "CompoundStmt");
return (CompoundStmtIt == Parents.end() || CompoundStmtIt > it);
}
}
std::string
ApiMatchHandler::getCallExprAssignmentVarName(const clang::CallExpr *pExpr, clang::ASTContext *const pContext) {
auto VarName = std::string();
auto ParentNodes = pContext->getParents(*pExpr);
for (auto &Parent : ParentNodes) {
if (Parent.getNodeKind().asStringRef() == "VarDecl") {
auto Node = Parent.get<clang::VarDecl>();
VarName = Node->getNameAsString();
} else if (Parent.getNodeKind().asStringRef() == "BinaryOperator") {
llvm::outs() << "Parent (BinOp) : " << Parent.getNodeKind().asStringRef() << "\n";
auto Node = Parent.get<clang::BinaryOperator>();
auto Child = Node->getLHS();
if (auto *DRE = dyn_cast<DeclRefExpr>(Child)) {
// It's a reference to a declaration...
if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// It's a reference to a variable (a local, function parameter, global, or static data member).
VarName = VD->getQualifiedNameAsString();
}
}
llvm::outs() << "Var name is " << VarName << "\n";
}
}
return VarName;
}