-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObfuscador.cpp
More file actions
188 lines (176 loc) · 4.86 KB
/
Obfuscador.cpp
File metadata and controls
188 lines (176 loc) · 4.86 KB
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
// Zach J. Elko
// 2010
// skater.cpp
//
// I've wanted to make one of these for a while now. I got bored and whipped
// this up in about 3 hours. There are a lot of improvements that can/should
// be made, but it's not bad for the short amount of time put into it.
//
// Basic C/C++ code obfuscator.
// Could be improved by using regular expressions.
// Could be expanded by also renaming variables as part of the obfuscation process.
//
// Features:
// Strips single/multi line comments
// Removing leading whitespace
// Removing line breaks
// When removing line breaks, it properly handles all preprocessor directives,
// 'using' declarations and single-line 'else' clauses where the braces have been
// omitted. Does not yet support all cases in which braces have been omitted in
// single-line conditional expressions.
//
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
void stripLeadingWhite(std::string& line);
void stripSingleLineComment(std::string& line);
bool containsOnlyWhite(const std::string& line);
void stripMultiLineComment(char* inFile);
const std::string TEMP_FILE = ".skater-temp-file.tmp";
int main(int argc, char *argv[])
{
if (argc != 3)
{
std::cerr << "Usage: ./skater infile outfile\n";
exit(1);
}
// TODO: Do this on the same pass as the rest of the obfuscation
// to save the extra file I/O
stripMultiLineComment(argv[1]);
std::ifstream inFileStream(TEMP_FILE.c_str());
std::ofstream outFileStream(argv[2]);
std::string line;
bool addReturns;
while (std::getline(inFileStream, line))
{
addReturns = false;
if (!containsOnlyWhite(line))
{
stripLeadingWhite(line);
stripSingleLineComment(line);
if (line.size() > 0)
{
// TODO: Put this into a function to properly
// handle the exceptional cases
if (line.find("#") != std::string::npos ||
line.find("using") != std::string::npos ||
line == "else")
{
addReturns = true;
}
if (addReturns)
{
outFileStream << std::endl << line << std::endl;
}
else
{
outFileStream << line;
}
}
}
}
inFileStream.close();
outFileStream.close();
remove(TEMP_FILE.c_str());
return 0;
}
bool containsOnlyWhite(const std::string& line)
{
std::string temp = line;
temp.erase(remove_if(temp.begin(), temp.end(), isspace), temp.end());
return temp.size() <= 0;
}
void stripLeadingWhite(std::string& line)
{
if (line.size() > 0)
{
int nonWhiteIndex = 0;
for (int i = 0; i < line.size(); ++i)
{
// TODO: remove whitespace in-place to avoid the call to erase()
if (isspace(line.at(i)))
{
++nonWhiteIndex;
}
else
{
break;
}
}
line.erase(0, nonWhiteIndex);
}
}
void stripSingleLineComment(std::string& line)
{
if (line.size() >= 2)
{
int loc = line.find("//");
if (loc != std::string::npos)
{
line.erase(loc);
}
}
}
// TODO: Do this on the same pass as the rest of the obfuscation
// to save the extra file I/O
void stripMultiLineComment(char* inFile)
{
std::ifstream inFileStream(inFile);
std::ofstream outFileStream(TEMP_FILE.c_str());
std::string line;
int startPos;
int endPos;
bool multiLine = false;
while (std::getline(inFileStream, line))
{
// TODO: Refactor this! Both sections do the same thing, differently!
if (multiLine)
{
endPos = line.find("*/");
// If we find the closing comment, erase up through it
if (endPos != std::string::npos)
{
line.erase(0, endPos + 2);
multiLine = false;
}
// Otherwise, erase the entire line
else
{
line.erase();
multiLine = true;
}
}
else
{
startPos = line.find("/*");
if (startPos != std::string::npos)
{
endPos = line.find("*/");
if (endPos != std::string::npos)
{
// We add 2 to the endPos for the */ because find() returns the start
// of the string, and we need to remove through the end of the string.
int commentLength = (endPos + 2) - startPos;
line.erase(startPos, commentLength);
multiLine = false;
}
else
{
multiLine = true;
line.erase();
}
}
}
if (line.size() > 0)
{
outFileStream << line << std::endl;
}
}
inFileStream.close();
outFileStream.close();
}