forked from liudaox/ClangObfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjcObfuscator.cpp
172 lines (150 loc) · 5.5 KB
/
ObjcObfuscator.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
//===- ObjcObfuscator.cpp ---------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Example clang plugin which simply prints the names of all the top-level decls
// in the input file.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Sema/Sema.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/AST/Type.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <random>
#include <time.h>
#include "./Strings.h"
//Setup Random Name Array
std::string arrayString[] = ObfuscateStringList;
//End
using namespace clang;
using namespace std;
class ObjcObfuscatorVisitor
: public RecursiveASTVisitor<ObjcObfuscatorVisitor> {
public:
ObjcObfuscatorVisitor(ASTContext *Context,SourceManager* Manager)
: Context(Context),Manager(Manager),_rewriter(*Manager,Context->getLangOpts ()){
}
void HandleTranslationUnit(clang::ASTContext &context)
{
TraverseDecl(context.getTranslationUnitDecl());
}
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *declaration)
{
if(Manager->isInMainFile(declaration->getSourceRange ().getBegin () )){
llvm::errs()<<"Objc Interface:"<<declaration->getObjCRuntimeNameAsString ()<<"\n";
std::cout<<"#define "<<declaration->getNameAsString ()<<" "<<RandomString()<<"\n";
}
return true;
}
bool VisitObjCMethodDecl(ObjCMethodDecl *declaration)
{
if(Manager->isInMainFile(declaration->getSourceRange ().getBegin () )){
//Need To Break Down The Selector Into Parts
llvm::errs()<<"ObjcMethod:"<<declaration->getNameAsString ()<<"\n";
SplitSelector(declaration->getNameAsString ());
}
return true;
}
bool VisitObjCIvarDecl(ObjCIvarDecl *declaration)
{
if(Manager->isInMainFile(declaration->getSourceRange ().getBegin () )){
llvm::errs()<<"ObjcIvar:"<<declaration->getNameAsString ()<<"\n";
std::cout<<"#define "<<declaration->getNameAsString ()<<" "<<RandomString()<<"\n";
}
return true;
}
bool VisitObjCPropertyDecl(ObjCPropertyDecl *declaration)
{
if(Manager->isInMainFile(declaration->getSourceRange ().getBegin () )){
llvm::errs()<<"ObjCPropertyDecl:"<<declaration->getNameAsString ()<<"\n";
std::cout<<"#define "<<declaration->getNameAsString ()<<" "<<RandomString()<<"\n";
}
return true;
}
bool VisitObjCCategoryDecl(ObjCCategoryDecl *declaration)
{
if(Manager->isInMainFile(declaration->getSourceRange ().getBegin () )){
llvm::errs()<<"ObjCCategoryDecl:"<<declaration->getNameAsString ()<<"\n";
std::cout<<"#define "<<declaration->getNameAsString ()<<" "<<RandomString()<<"\n";
}
return true;
}
static std::string RandomString(){
std::string ReturnString;
for(int i=0;i<72;i++){
std::string string=arrayString[rand() % ObfuscateListSize];
ReturnString+=string;
}
return ReturnString;
}
static void SplitSelector(std::string str)
{
for (int i=0; i<str.length(); i++)
{
if (str[i] == ':')
str[i] = ' ';
}
vector<std::string> array;
stringstream ss(str);
std::string temp;
while (ss >> temp)
array.push_back(temp);
for(int i=0; i<array.size(); i++){
std::cout<<"#define "<<array[i]<<" "<<RandomString()<<"\n";
}
}
private:
ASTContext *Context;
SourceManager* Manager;
typedef clang::RecursiveASTVisitor<ObjcObfuscatorVisitor> Base;
clang::Rewriter _rewriter;
};
class ObjcObfuscatorConsumer : public clang::ASTConsumer {
public:
explicit ObjcObfuscatorConsumer(ASTContext *Context,SourceManager* Manager): Visitor(Context,Manager) {}
virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
// a DeclGroupRef may have multiple Decls, so we iterate through each one
for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; i++) {
Decl *D = *i;
Visitor.TraverseDecl(D); // recursively visit each AST node in Decl "D"
}
return true;
}
private:
SourceManager *Manager;
ObjcObfuscatorVisitor Visitor;
};
class ObjcObfuscatorAction : public PluginASTAction {
protected:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
llvm::StringRef) override {
llvm::errs()<<"Loading ObjcClassGuard\n";
return llvm::make_unique<ObjcObfuscatorConsumer>(&CI.getASTContext(),&CI.getSourceManager ());
}
bool ParseArgs(const CompilerInstance &CI,
const std::vector<std::string> &args) override {
return true;
}
void PrintHelp(llvm::raw_ostream& ros) {
ros << "Add -ObjcClassGuard Obfuscate Objc Class & Method Names";
}
};
static FrontendPluginRegistry::Add<ObjcObfuscatorAction> X("-ObjcClassGuard", "Obfuscate Objc Class & Method Names");