Skip to content

Commit 3132fc5

Browse files
author
Liam
committed
Refactoring (cleaner code)
1 parent c448d62 commit 3132fc5

File tree

4 files changed

+175
-6
lines changed

4 files changed

+175
-6
lines changed

Lua Obfusactor/Lua Obfusactor.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
</Link>
155155
</ItemDefinitionGroup>
156156
<ItemGroup>
157-
<ClCompile Include="Lua Obfusactor.cpp" />
157+
<ClCompile Include="obfuscator.cpp" />
158158
</ItemGroup>
159159
<ItemGroup>
160160
<CopyFileToFolders Include="obf.lua">
@@ -167,6 +167,7 @@
167167
<Image Include="icon.ico" />
168168
</ItemGroup>
169169
<ItemGroup>
170+
<ClInclude Include="obfuscator.h" />
170171
<ClInclude Include="resource.h" />
171172
</ItemGroup>
172173
<ItemGroup>

Lua Obfusactor/Lua Obfusactor.vcxproj.filters

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
1515
</Filter>
1616
</ItemGroup>
17-
<ItemGroup>
18-
<ClCompile Include="Lua Obfusactor.cpp">
19-
<Filter>Source Files</Filter>
20-
</ClCompile>
21-
</ItemGroup>
2217
<ItemGroup>
2318
<None Include="packages.config" />
2419
<None Include="lua.dll" />
@@ -35,10 +30,18 @@
3530
<ClInclude Include="resource.h">
3631
<Filter>Header Files</Filter>
3732
</ClInclude>
33+
<ClInclude Include="obfuscator.h">
34+
<Filter>Header Files</Filter>
35+
</ClInclude>
3836
</ItemGroup>
3937
<ItemGroup>
4038
<ResourceCompile Include="Resource.rc">
4139
<Filter>Resource Files</Filter>
4240
</ResourceCompile>
4341
</ItemGroup>
42+
<ItemGroup>
43+
<ClCompile Include="obfuscator.cpp">
44+
<Filter>Source Files</Filter>
45+
</ClCompile>
46+
</ItemGroup>
4447
</Project>

Lua Obfusactor/obfuscator.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include "obfuscator.h"
2+
3+
int main()
4+
{
5+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
6+
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
7+
std::cout << "Input directory:" << std::endl;
8+
std::string dir{};
9+
try {
10+
std::getline(std::cin, dir);
11+
system("CLS");
12+
originalDir = dir;
13+
startMS = std::chrono::duration_cast<std::chrono::milliseconds>(
14+
std::chrono::system_clock::now().time_since_epoch()
15+
);
16+
count_files(dir);
17+
18+
if (allFiles <= 0) {
19+
std::cout << "There are no files to obfuscate in that directory!" << std::endl;
20+
try_exit();
21+
return 0;
22+
}
23+
24+
L = luaL_newstate();
25+
luaL_openlibs(L);
26+
luaopen_base(L);
27+
luaL_dostring(L, o_54b23f86700cdd0d671bbeaab0542ce5);
28+
file_loop(dir);
29+
lua_close(L);
30+
std::chrono::milliseconds newMS = std::chrono::duration_cast<std::chrono::milliseconds>(
31+
std::chrono::system_clock::now().time_since_epoch()
32+
);
33+
std::cout << "Finished obfuscating " << allFiles << " file(s) in " << (newMS - startMS).count() << "ms." << std::endl;
34+
try_exit();
35+
}
36+
catch (const std::exception& e) {
37+
std::cout << "ERROR: " << e.what() << std::endl;
38+
std::cout << "Error opening the directory \"" + dir + "\"!" << std::endl;
39+
std::cout << "Trying again..." << std::endl;
40+
return main();
41+
}
42+
}
43+
44+
void count_files(std::string dir) {
45+
fs::directory_iterator it = fs::directory_iterator(dir);
46+
std::string lastFolder = dir.substr(dir.find_last_of("\\") + 1, dir.size());
47+
for (const auto& entry : it) {
48+
if (fs::is_directory(entry.path())) {
49+
count_files(entry.path().string());
50+
continue;
51+
}
52+
if (entry.path().extension() != ".lua" || entry.path().filename().string().find("__resource") != std::string::npos || entry.path().filename().string().find("fxmanifest") != std::string::npos) {
53+
continue;
54+
}
55+
allFiles++;
56+
}
57+
}
58+
59+
void file_loop(std::string dir) {
60+
fs::directory_iterator it = fs::directory_iterator(dir);
61+
std::string lastFolder = dir.substr(dir.find_last_of("\\") + 1, dir.size());
62+
for (const auto& entry : it) {
63+
if (fs::is_directory(entry.path())) {
64+
file_loop(entry.path().string());
65+
continue;
66+
}
67+
if (entry.path().extension() != ".lua" || entry.path().filename().string().find("__resource") != std::string::npos || entry.path().filename().string().find("fxmanifest") != std::string::npos) {
68+
continue;
69+
}
70+
std::string line;
71+
std::ifstream myfile(entry.path());
72+
if (myfile.is_open())
73+
{
74+
std::string newLine = "";
75+
while (getline(myfile, line))
76+
{
77+
if (line.empty()) {
78+
continue;
79+
}
80+
newLine += line + "\n";
81+
}
82+
83+
myfile.close();
84+
85+
lua_pushstring(L, newLine.c_str());
86+
lua_setglobal(L, "codeTable");
87+
88+
lua_getglobal(L, "obf");
89+
lua_pcall(L, 0, 1, 0);
90+
91+
std::string obfCode = lua_tostring(L, -1);
92+
93+
std::ofstream obfFile(entry.path());
94+
95+
obfFile << "load(\"" << obfCode << "\")()";
96+
97+
obfFile.close();
98+
completedFiles++;
99+
std::string folderPath = entry.path().string();
100+
replace(folderPath, originalDir, "");
101+
if (originalDir._Starts_with("C:\\") || originalDir._Starts_with("C:/")) {
102+
folderPath.replace(0, 1, "");
103+
}
104+
std::cout << "(" << floor(completedFiles / allFiles * 100) << "%) Successfully obfusacted " << folderPath << std::endl;
105+
}
106+
}
107+
}
108+
109+
void try_exit() {
110+
std::cout << "Press any key to exit..." << std::endl;
111+
getchar();
112+
exit(0);
113+
}
114+
115+
bool replace(std::string& str, const std::string& from, const std::string& to) {
116+
size_t start_pos = str.find(from);
117+
if (start_pos == std::string::npos)
118+
return false;
119+
str.replace(start_pos, from.length(), to);
120+
return true;
121+
}
122+
123+
bool ends_with(const std::string& mainStr, const std::string& toMatch)
124+
{
125+
if (mainStr.size() >= toMatch.size() &&
126+
mainStr.compare(mainStr.size() - toMatch.size(), toMatch.size(), toMatch) == 0)
127+
return true;
128+
else
129+
return false;
130+
}

Lua Obfusactor/obfuscator.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <filesystem>
4+
#include <fstream>
5+
#include <math.h>
6+
#include <windows.h>
7+
#include <chrono>
8+
#include <cstring>
9+
#include <algorithm>
10+
11+
extern "C" {
12+
#include <lua.h>
13+
#include <lauxlib.h>
14+
#include <lualib.h>
15+
#include <lua.hpp>
16+
}
17+
18+
namespace fs = std::filesystem;
19+
lua_State* L;
20+
std::string originalDir{};
21+
double allFiles = 0;
22+
double completedFiles = 0;
23+
24+
char* o_54b23f86700cdd0d671bbeaab0542ce5 = "\x66""u\156c\x74""i\157n\x20""o\142f\x28"")\040l\x6F""c\141l\x20""a\040=\x20""[\133]\x5D"";\040a\x20""=\040a\x20"".\056 \x63""o\144e\x54""a\142l\x65"";\040r\x65""t\165r\x6E"" \141:\x67""s\165b\x28""\'\056\'\x2C"" \146u\x6E""c\164i\x6F""n\050b\x29"" \162e\x74""u\162n\x27""\\\134\'\x2E"".\142:\x62""y\164e\x28"")\040e\x6E""d\051 \x6F""r\040a\x2E"".\047\\\x22""\'\040e\x6E""d";
25+
26+
std::chrono::milliseconds startMS;
27+
28+
bool replace(std::string& str, const std::string& from, const std::string& to);
29+
bool ends_with(const std::string& mainStr, const std::string& toMatch);
30+
31+
void file_loop(std::string dir);
32+
void count_files(std::string dir);
33+
void try_exit();
34+
35+
int main();

0 commit comments

Comments
 (0)