-
Notifications
You must be signed in to change notification settings - Fork 178
/
main.cpp
220 lines (167 loc) · 8.88 KB
/
main.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
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
// LLVM includes
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include "Consumer.h"
#include "MatchHandler.h"
#include "ApiMatchHandler.h"
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <fstream>
namespace ClSetup {
static llvm::cl::OptionCategory ToolCategory("AVObfuscator");
llvm::cl::opt<bool> IsEditEnabled("edit",
llvm::cl::desc("Edit the file in place, or write a copy with .patch extension."),
llvm::cl::cat(ToolCategory));
llvm::cl::opt<bool> IsStringObfuscationEnabled("strings", llvm::cl::desc("Enable obfuscation of string literals."),
llvm::cl::cat(ToolCategory));
llvm::cl::opt<bool> IsApiObfuscationEnabled("api", llvm::cl::desc("Enable obfuscation of api calls."),
llvm::cl::cat(ToolCategory));
}
namespace AVObfuscator {
clang::Rewriter ASTRewriter;
class StringEncryptionConsumer : public clang::ASTConsumer {
public:
void HandleTranslationUnit(clang::ASTContext &Context) override {
using namespace clang::ast_matchers;
using namespace AVObfuscator;
llvm::outs() << "[StringEncryption] Registering ASTMatcher...\n";
MatchFinder Finder;
MatchHandler Handler(&ASTRewriter);
const auto Matcher = stringLiteral().bind("decl");
Finder.addMatcher(Matcher, &Handler);
Finder.matchAST(Context);
}
};
class ApiCallConsumer : public clang::ASTConsumer {
public:
ApiCallConsumer(std::string ApiName, std::string TypeDef, std::string Library)
: _ApiName(std::move(ApiName)), _TypeDef(std::move(TypeDef)), _Library(std::move(Library)) {}
ApiCallConsumer(std::string ApiName, std::string TypeDef, std::string NtName, bool ConvertToSyscall)
: _ApiName(std::move(ApiName)), _TypeDef(std::move(TypeDef)), _NtName(std::move(NtName)), _ConvertToSyscall(ConvertToSyscall) {}
void HandleTranslationUnit(clang::ASTContext &Context) override {
using namespace clang::ast_matchers;
using namespace AVObfuscator;
llvm::outs() << "[ApiCallObfuscation] Registering ASTMatcher for " << _ApiName << "\n";
MatchFinder Finder;
const auto Matcher = callExpr(callee(functionDecl(hasName(_ApiName)))).bind("callExpr");
ApiMatchHandler Handler;
if(_ConvertToSyscall) {
Handler = ApiMatchHandler(&ASTRewriter, _ApiName, _TypeDef, _NtName, true);
} else {
Handler = ApiMatchHandler(&ASTRewriter, _ApiName, _TypeDef, _Library);
}
Finder.addMatcher(Matcher, &Handler);
Finder.matchAST(Context);
}
private:
std::string _ApiName ="";
std::string _TypeDef ="";
std::string _Library = "";
std::string _NtName ="";
bool _ConvertToSyscall;
};
StringEncryptionConsumer StringConsumer = StringEncryptionConsumer();
class Action : public clang::ASTFrontendAction {
public:
using ASTConsumerPointer = std::unique_ptr<clang::ASTConsumer>;
ASTConsumerPointer CreateASTConsumer(clang::CompilerInstance &Compiler,
llvm::StringRef Filename) override {
ASTRewriter.setSourceMgr(Compiler.getSourceManager(), Compiler.getLangOpts());
std::vector<clang::ASTConsumer *> consumers;
if (ClSetup::IsStringObfuscationEnabled) {
consumers.push_back(&StringConsumer);
}
if (ClSetup::IsApiObfuscationEnabled) {
for(auto const& el: ApiToHide_samlib){
auto Cons = std::make_unique<ApiCallConsumer*>(new ApiCallConsumer(el.first, el.second,
"samlib.dll"));
consumers.push_back(*Cons);
}
std::string MessageBoxATypeDef = "typedef int (*_MessageBoxA)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);";
auto Cons = std::make_unique<ApiCallConsumer*>(new ApiCallConsumer("MessageBoxA", MessageBoxATypeDef,
"User32.dll"));
consumers.push_back(*Cons);
auto ZwWriteVirtualMemoryTypeDef = "typedef NTSTATUS(__stdcall *_ZwWriteVirtualMemory)(\n"
" HANDLE ProcessHandle,\n"
" PVOID BaseAddress,\n"
" PVOID Buffer,\n"
" ULONG NumberOfBytesToWrite,\n"
" PULONG NumberOfBytesWritten);\n\n";
auto Cons2 = std::make_unique<ApiCallConsumer*>(new ApiCallConsumer("WriteProcessMemory", ZwWriteVirtualMemoryTypeDef,
"ZwWriteVirtualMemory", true));
consumers.push_back(*Cons2);
auto ZwCreateThreadExTypeDef = "typedef NTSTATUS(__stdcall* _ZwCreateThreadEx)(HANDLE * pHandle,\n"
" ACCESS_MASK DesiredAccess,\n"
" void * pAttr, \n"
" HANDLE hProc,\n"
" void * pFunc,\n"
" void * pArg,\n"
" ULONG Flags,\n"
" SIZE_T ZeroBits,\n"
" SIZE_T StackSize,\n"
" SIZE_T MaxStackSize,\n"
" void * pAttrListOut);\n\n";
auto Cons3 = std::make_unique<ApiCallConsumer*>(new ApiCallConsumer("CreateRemoteThread", ZwCreateThreadExTypeDef,
"ZwCreateThreadEx", true));
consumers.push_back(*Cons3);
}
auto TheConsumer = std::make_unique<Consumer>();
TheConsumer->consumers = consumers;
return TheConsumer;
}
bool BeginSourceFileAction(clang::CompilerInstance &Compiler) override {
llvm::outs() << "Processing file " << Compiler.getSourceManager().getFileEntryForID(Compiler.getSourceManager().getMainFileID())->getName() << '\n';
return true;
}
void EndSourceFileAction() override {
clang::SourceManager &SM = ASTRewriter.getSourceMgr();
std::string FileName = SM.getFileEntryForID(SM.getMainFileID())->getName().data();
llvm::errs() << "** EndSourceFileAction for: " << FileName << "\n";
// Now emit the rewritten buffer.
std::string TypeS;
llvm::raw_string_ostream s(TypeS);
auto FileID = SM.getMainFileID();
auto ReWriteBuffer = ASTRewriter.getRewriteBufferFor(FileID);
if (ReWriteBuffer != nullptr)
ReWriteBuffer->write((s));
else {
llvm::errs() << "File was not modified\n";
return;
}
std::string result = s.str();
std::ofstream fo;
if (ClSetup::IsEditEnabled) {
fo.open(FileName);
} else {
fo.open(FileName + ".patch");
}
if (fo.is_open())
fo << result;
else
llvm::errs() << "[!] Error saving result to " << FileName << "\n";
}
};
}
auto main(int argc, const char *argv[]) -> int {
using namespace clang::tooling;
using namespace ClSetup;
auto option_parser = CommonOptionsParser::create(argc, argv, ToolCategory);
ClangTool Tool(option_parser->getCompilations(),
option_parser->getSourcePathList());
auto Action = newFrontendActionFactory<AVObfuscator::Action>();
return Tool.run(Action.get());
}