aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbloodstalker <thabogre@gmail.com>2017-12-16 13:31:54 +0000
committerbloodstalker <thabogre@gmail.com>2017-12-16 13:31:54 +0000
commit453ccbcb228b7f39adf087bfe955c770e4640f64 (patch)
tree730b6b5fbcf8e7aaf65df822ac2c8c70417f3dd2
parentwip, added methods for dealing with filename changes to obfuscator (diff)
downloadmutator-453ccbcb228b7f39adf087bfe955c770e4640f64.tar.gz
mutator-453ccbcb228b7f39adf087bfe955c770e4640f64.zip
obfuscator should handle macro expansions just fine now, also there was no rewriter for macro expansion so added one and added a test folder for obfuscator, also added a new method for getting the spellingloc to aux methods
Diffstat (limited to '')
-rw-r--r--mutator_aux.cpp6
-rw-r--r--mutator_aux.h2
-rw-r--r--obfuscator/obfuscator.cpp58
-rw-r--r--obfuscator/test/compile_commands.json7
-rw-r--r--obfuscator/test/dir1/dir12/dir122/dir1221/header.hpp0
-rw-r--r--obfuscator/test/dir1/dir12/dir122/header.h0
-rw-r--r--obfuscator/test/dir1/dir12/source.cpp0
-rw-r--r--obfuscator/test/dir1/dir12/source.cxx0
-rw-r--r--obfuscator/test/dir1/source.cc0
-rw-r--r--obfuscator/test/makefile24
-rw-r--r--obfuscator/test/obfuscator-tee68
-rw-r--r--obfuscator/test/test.cpp68
12 files changed, 213 insertions, 20 deletions
diff --git a/mutator_aux.cpp b/mutator_aux.cpp
index 9eac0dd..d6400f2 100644
--- a/mutator_aux.cpp
+++ b/mutator_aux.cpp
@@ -77,6 +77,12 @@ SourceLocation SourceLocationHasMacro(SourceLocation __sl, Rewriter &__rewrite)
return __sl;
}
}
+
+SourceLocation getSLSpellingLoc(SourceLocation __sl, Rewriter &__rewrite)
+{
+ if (__sl.isMacroID()) {return __rewrite.getSourceMgr().getSpellingLoc(__sl);}
+ else {return __sl;}
+}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
/*********************************************************************************************************************/
diff --git a/mutator_aux.h b/mutator_aux.h
index c6baa8e..53ea0ad 100644
--- a/mutator_aux.h
+++ b/mutator_aux.h
@@ -49,6 +49,8 @@ enum class FunctionDeclKind {NoValue, Definition, Declaration};
SourceLocation SourceLocationHasMacro(SourceLocation SL, Rewriter &Rewrite, std::string Kind);
SourceLocation SourceLocationHasMacro(SourceLocation __sl, Rewriter &__rewrite);
+
+SourceLocation getSLSpellingLoc(SourceLocation __sl, Rewriter &__rewrite);
/*********************************************************************************************************************/
bool IsTheMatchInSysHeader(bool SysHeaderFlag, const ast_matchers::MatchFinder::MatchResult &MR, SourceLocation SL);
diff --git a/obfuscator/obfuscator.cpp b/obfuscator/obfuscator.cpp
index 51e26b4..0f9b03d 100644
--- a/obfuscator/obfuscator.cpp
+++ b/obfuscator/obfuscator.cpp
@@ -73,14 +73,14 @@ class CryptoSponge {
~CryptoSponge() {}
};
/**********************************************************************************************************************/
-std::vector<std::string> listEverything(std::string _path) {
+std::vector<std::string> listDirs(std::string _path) {
std::vector<std::string> dummy_;
- DIR *dir_;
+ DIR* dir_;
struct dirent* ent_;
if ((dir_ = opendir(_path.c_str())) != nullptr) {
while((ent_ = readdir(dir_)) != nullptr) {
- std::cout << "name: " << ent_->d_name << "\ttype:" << ent_->d_type << "\n";
- if (ent_->d_type == DT_DIR) {std::cout << "ZZZ\n";}
+ std::cout << "name: " << ent_->d_name << "\ttype:" << int(ent_->d_type) << "\n";
+ if (ent_->d_type == DT_DIR) {}
dummy_.push_back(ent_->d_name);
}
}
@@ -90,6 +90,11 @@ std::vector<std::string> listEverything(std::string _path) {
return dummy_;
}
+void dumpDirList(std::vector<std::string> _dirs) {
+ for (auto &iter : _dirs) {std::cout << iter << "\t";}
+ std::cout << "\n";
+}
+
std::tuple<std::string, std::string, std::string> getNameFromPath(std::string _path) {
size_t pos = _path.rfind("/");
size_t pos2 = _path.rfind(".");
@@ -146,7 +151,9 @@ class CalledFunc : public MatchFinder::MatchCallback {
auto dummy = Rewrite.getRewrittenText(SourceRange(CE->getLocStart(), CE->getRParenLoc()));
auto LParenOffset = dummy.find("(");
- dummy = Rewrite.getRewrittenText(SourceRange(CE->getLocStart(), CE->getLocStart().getLocWithOffset(LParenOffset - 1U)));
+ SourceLocation SL = Devi::getSLSpellingLoc(CE->getLocStart(), Rewrite);
+ SourceLocation SLE = Devi::getSLSpellingLoc(CE->getLocStart(), Rewrite).getLocWithOffset(LParenOffset - 0U);
+ dummy = Rewrite.getRewrittenText(SourceRange(SL, SLE));
Rewrite.ReplaceText(SourceRange(CE->getLocStart(), CE->getLocStart().getLocWithOffset(LParenOffset - 1U)), StringRef(newname));
}
}
@@ -168,8 +175,8 @@ class CalledVar : public MatchFinder::MatchCallback {
#ifdef DBG
std::cout << "DeclRefExpr name: " << name << " Hash: " << hash << " New ID: " << newname << "\n";
#endif
- SourceLocation SL = DRE->getNameInfo().getBeginLoc();
- SourceLocation SLE = DRE->getNameInfo().getEndLoc();
+ SourceLocation SL = Devi::getSLSpellingLoc(DRE->getNameInfo().getBeginLoc(), Rewrite);
+ SourceLocation SLE = Devi::getSLSpellingLoc(DRE->getNameInfo().getEndLoc(), Rewrite);
Rewrite.ReplaceText(SourceRange(SL, SLE), StringRef(newname));
}
@@ -196,8 +203,8 @@ public:
std::cout << "Function name: " << funcname << " Hash: " << hash << " New ID: " << newname << "\n";
#endif
- SourceLocation SL = FD->getNameInfo().getBeginLoc();
- SourceLocation SLE = FD->getNameInfo().getEndLoc();
+ SourceLocation SL = Devi::getSLSpellingLoc(FD->getNameInfo().getBeginLoc(), Rewrite);
+ SourceLocation SLE = Devi::getSLSpellingLoc(FD->getNameInfo().getEndLoc(), Rewrite);
Rewrite.ReplaceText(SourceRange(SL, SLE), StringRef(newname));
}
@@ -222,15 +229,15 @@ public:
#ifdef DBG
std::cout << "Var name: " << varname << " Hash: " << hash << " New ID: " << newname << "\n";
#endif
- SourceLocation SL = VD->getLocation();
+ SourceLocation SL = Devi::getSLSpellingLoc(VD->getLocation(), Rewrite);
SourceLocation SLE;
const clang::Expr* EXP = nullptr;
if (MR.Nodes.getNodeAs<clang::Expr>("expr") !=nullptr) {
EXP = MR.Nodes.getNodeAs<clang::Expr>("expr");
- SLE = EXP->getExprLoc();
+ SLE = Devi::getSLSpellingLoc(EXP->getExprLoc(), Rewrite);
} else {
- SLE = VD->getLocEnd();
+ SLE = Devi::getSLSpellingLoc(VD->getLocEnd(), Rewrite);
}
//@devi-FIXME-cluncky
@@ -260,8 +267,8 @@ class ClassDecl : public MatchFinder::MatchCallback {
std::cout << "Record name: " << varname << " Hash: " << hash << " New ID: " << newname << "\n";
#endif
- SourceLocation SL = RD->getLocation();
- SourceLocation SLE = RD->getLocEnd();
+ SourceLocation SL = Devi::getSLSpellingLoc(RD->getLocation(), Rewrite);
+ SourceLocation SLE = Devi::getSLSpellingLoc(RD->getLocEnd(), Rewrite);
std::string dummy = Rewrite.getRewrittenText(SourceRange(SL, SLE));
Rewrite.ReplaceText(SourceRange(SL, SLE), StringRef(newname));
@@ -278,8 +285,7 @@ public:
explicit PPInclusion (SourceManager *SM, Rewriter *Rewrite) : SM(*SM), Rewrite(*Rewrite) {}
virtual void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) {
-
- SourceLocation SL = MacroNameTok.getLocation();
+ SourceLocation SL = Devi::getSLSpellingLoc(MacroNameTok.getLocation(), Rewrite);
if (!SM.isInMainFile(SL)) return void();
if (!SM.isWrittenInMainFile(SL)) return void();
CheckSLValidity(SL);
@@ -294,7 +300,19 @@ public:
std::string dummy = Rewrite.getRewrittenText(SourceRange(MacroNameTok.getLocation(), MacroNameTok.getLocation().getLocWithOffset(MacroNameTok.getLength())));
std::cout << dummy << "\n";
#endif
- Rewrite.ReplaceText(SourceRange(MacroNameTok.getLocation(), MacroNameTok.getLocation().getLocWithOffset(MacroNameTok.getLength())), newname);
+ Rewrite.ReplaceText(SourceRange(SL, SL.getLocWithOffset(MacroNameTok.getLength() - 1)), newname);
+ }
+
+ virtual void MacroExpands (const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) {
+ SourceLocation SL = Devi::getSLSpellingLoc(MacroNameTok.getLocation(), Rewrite);
+ if (!SM.isInMainFile(SL)) return void();
+ if (!SM.isWrittenInMainFile(SL)) return void();
+ CheckSLValidity(SL);
+ std::string macroname = MacroNameTok.getIdentifierInfo()->getName().str();
+ std::size_t hash = std::hash<std::string>{}(macroname);
+ std::string newname = "ID" + std::to_string(hash);
+
+ Rewrite.ReplaceText(SourceRange(SL, SL.getLocWithOffset(MacroNameTok.getLength() - 1)), newname);
}
virtual void InclusionDirective (SourceLocation HashLoc, const Token &IncludeTok,
@@ -375,7 +393,7 @@ class CommentWiper {
int run(void) {
for (auto &filepath : sourcelist) {
std::ifstream sourcefile;
- sourcefile.open("../test/bruisertest/obfuscator-tee");
+ sourcefile.open("./test/obfuscator-tee");
std::ofstream dupe;
auto filename_ = getNameFromPath(filepath);
dupe.open(nameMaker(getHashedName(std::get<0>(filename_)), std::get<1>(filename_), ""));
@@ -489,8 +507,8 @@ int main(int argc, const char **argv) {
//WW.run();
CommentWiper CW(SourcePathList);
CW.run();
- dumpHashFilenames(hashFilenames(SourcePathList));
- listEverything("./");
+ //dumpHashFilenames(hashFilenames(SourcePathList));
+ //dumpDirList(listDirs("./test"));
#if 0
for (auto &iter : SourcePathList) {
std::cout << "name: " << std::get<0>(getNameFromPath(iter)) << "\t" << "extension: " << std::get<1>(getNameFromPath(iter)) << "\tpath: " << std::get<2>(getNameFromPath(iter)) << "\n";
diff --git a/obfuscator/test/compile_commands.json b/obfuscator/test/compile_commands.json
new file mode 100644
index 0000000..ea76446
--- /dev/null
+++ b/obfuscator/test/compile_commands.json
@@ -0,0 +1,7 @@
+[
+ {
+ "command": "c++ -c -I/usr/lib/gcc/x86_64-redhat-linux/6.3.1/include -o test.o test.cpp",
+ "directory": "/home/bloodstalker/devi/hell2/obfuscator/test",
+ "file": "/home/bloodstalker/devi/hell2/obfuscator/test/test.cpp"
+ }
+] \ No newline at end of file
diff --git a/obfuscator/test/dir1/dir12/dir122/dir1221/header.hpp b/obfuscator/test/dir1/dir12/dir122/dir1221/header.hpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/obfuscator/test/dir1/dir12/dir122/dir1221/header.hpp
diff --git a/obfuscator/test/dir1/dir12/dir122/header.h b/obfuscator/test/dir1/dir12/dir122/header.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/obfuscator/test/dir1/dir12/dir122/header.h
diff --git a/obfuscator/test/dir1/dir12/source.cpp b/obfuscator/test/dir1/dir12/source.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/obfuscator/test/dir1/dir12/source.cpp
diff --git a/obfuscator/test/dir1/dir12/source.cxx b/obfuscator/test/dir1/dir12/source.cxx
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/obfuscator/test/dir1/dir12/source.cxx
diff --git a/obfuscator/test/dir1/source.cc b/obfuscator/test/dir1/source.cc
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/obfuscator/test/dir1/source.cc
diff --git a/obfuscator/test/makefile b/obfuscator/test/makefile
new file mode 100644
index 0000000..1b0859b
--- /dev/null
+++ b/obfuscator/test/makefile
@@ -0,0 +1,24 @@
+BRUISER_TEST=test
+CXX?=clang++
+# you should change the include path to one that is right for you
+CXX_FLAGS=-I/usr/lib/gcc/x86_64-redhat-linux/6.3.1/include
+
+.DEFAULT:all clean
+
+.PHONY:all clean $(BRUISER_TEST) help
+
+all:$(BRUISER_TEST)
+
+.cpp.o:
+ $(CXX) $(CXX_FLAGS) -c $< -o $@
+
+$(BRUISER_TEST): test.o
+ $(CXX) $^ $(LD_FLAGS) -o $@
+
+clean:
+ rm -f *.o *~ $(BRUISER_TEST)
+
+help:
+ @echo "the makfile for the bruiser test file."
+ @echo "there is the taget itself, all and clean."
+
diff --git a/obfuscator/test/obfuscator-tee b/obfuscator/test/obfuscator-tee
new file mode 100644
index 0000000..7fc775f
--- /dev/null
+++ b/obfuscator/test/obfuscator-tee
@@ -0,0 +1,68 @@
+
+#if 0
+#include <fstream>
+#include <iostream>
+#endif
+
+int ID7143433513913994171(void) {
+ return 123;
+}
+
+int ID806107362239807644(int ID6414370376350317282, int ID5596922551969966689) {
+ return ID6414370376350317282 + ID5596922551969966689;
+}
+
+#define ID17270913437327947952 1
+#define ID3990433166784345791 int
+
+#define ID13091842907427809651 3.14
+
+#define ID8553886278371731254 int ID6457201339852078238
+#define ID5893689362230717210 int ID17001300751847036766
+
+namespace devi
+{
+ class ID3351929323710182842
+ {
+ public:
+ ID3351929323710182842 () {}
+
+ void ID5560374959326430988 (void)
+ {
+ int ID4993892634952068459 = 1;
+ int ID10838281452030117757 = 2;
+ int ID10959529184379665549 = 3;
+ }
+ };
+}
+
+int main(int ID3701972582333163920, const char **ID14643508047410943861)
+{
+ /***hya**/
+ /*
+ *
+ * */
+ // /**/
+ int ID17697423301731741216; // this one
+ int ID3227026850925696272; /*
+ *
+ */
+ /* "//" \\ \\\\ // */
+ //std::cout << "//" << "/**/" << "\n";
+ int/***/ ID5827489293749552374;
+#if 0
+ std::ofstream myfile;
+ myfile.open("./touch");
+ myfile << "line one.\n";
+ myfile.close();
+#endif
+ int ID468926534229516570 = 100;
+ int ID1567269223287109631 = 28;
+
+ ID8553886278371731254;
+ ID5893689362230717210;
+ ID806107362239807644(ID6457201339852078238, ID17001300751847036766);
+ return ID806107362239807644(ID468926534229516570, ID1567269223287109631);
+
+ //return return123();
+}
diff --git a/obfuscator/test/test.cpp b/obfuscator/test/test.cpp
new file mode 100644
index 0000000..23c3f23
--- /dev/null
+++ b/obfuscator/test/test.cpp
@@ -0,0 +1,68 @@
+
+#if 0
+#include <fstream>
+#include <iostream>
+#endif
+
+int return123(void) {
+ return 123;
+}
+
+int retret(int s, int w) {
+ return s + w;
+}
+
+#define loco 1
+#define puta int
+
+#define PI 3.14
+
+#define INTMAC1 int mac1
+#define INTMAC2 int mac2
+
+namespace devi
+{
+ class LOCO
+ {
+ public:
+ LOCO () {}
+
+ void lupo (void)
+ {
+ int a = 1;
+ int b = 2;
+ int c = 3;
+ }
+ };
+}
+
+int main(int argc, const char **argv)
+{
+ /***hya**/
+ /*
+ *
+ * */
+ // /**/
+ int bubba; // this one
+ int hubba; /*
+ *
+ */
+ /* "//" \\ \\\\ // */
+ //std::cout << "//" << "/**/" << "\n";
+ int/***/ cucu;
+#if 0
+ std::ofstream myfile;
+ myfile.open("./touch");
+ myfile << "line one.\n";
+ myfile.close();
+#endif
+ int aa = 100;
+ int bb = 28;
+
+ INTMAC1;
+ INTMAC2;
+ retret(mac1, mac2);
+ return retret(aa, bb);
+
+ //return return123();
+}