Skip to content

Instantly share code, notes, and snippets.

@OrbitZore
Last active October 24, 2022 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OrbitZore/e44d2066a49ca04c63761f445b38a845 to your computer and use it in GitHub Desktop.
Save OrbitZore/e44d2066a49ca04c63761f445b38a845 to your computer and use it in GitHub Desktop.
Obfuscation C++ code with emoji๐Ÿ˜Ž๐Ÿ˜Ž๐Ÿ˜Ž
/*
use any C++20 compiler to get executor
make sure /usr/bin/clang exist and run well
and POSIX support
this program only support single C/C++ Source!
*/
#include <sys/wait.h>
#include <unistd.h>
#include <array>
#include <cassert>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <vector>
string HELPSTRING=
"Please Input filename\n"
"usage: emoji [filename] [emojis] ...\n"
"Example:\n"
" $emoji a.cpp ๐Ÿ‡จ๐Ÿ‡ณ\n"
" $emoji a.cpp ๐Ÿฅต ๐Ÿ’ป\n"
;
using namespace std;
class TokenEngine {
public:
TokenEngine(vector<string> v) : v(v), i(0) {}
string operator++(int) {
auto n = v.size();
vector<string> a;
int x = i++;
if (n == 1)
do {
a.push_back(v[0]);
x--;
} while (x >= 0);
else
do {
a.push_back(v[x % n]);
x /= n;
} while (x > 0);
string b;
while (a.size()) {
b += a.back();
a.pop_back();
}
return b;
}
private:
vector<string> v;
int i;
};
struct channel {
int fd[2];
channel() {
if (pipe(fd) < 0)
exit(-1);
}
int out() { return fd[0]; }
int in() { return fd[1]; }
};
auto CLANG_ARGS =
(char* const[]){"clang++", "-Xclang", "-dump-tokens", "-E", "-", NULL};
map<string, string> tokenMap;
string pincludes;
struct Token {
string context, type;
bool StartOfLine, LeadingSpace;
Token(const string& clangLine) {
int l = clangLine.find_first_of('\''),
r = clangLine.find_first_of(' ', l);
type = clangLine.substr(0, clangLine.find_first_of(' '));
context = clangLine.substr(l + 1, r - l - 2);
StartOfLine = clangLine.find("[StartOfLine]") != string::npos;
LeadingSpace = clangLine.find("[LeadingSpace]") != string::npos;
}
};
vector<string> split(const string& str, const string& c) {
vector<string> a;
for (size_t i = 0, j = 0; i < str.size();) {
i = str.find(c, j);
if (i != j)
a.push_back(str.substr(j, i - j));
if (i < str.size())
i = j = i + c.size();
else
break;
}
return a;
}
int main(int argc, char** argv) {
if(argc < 2){
cout << HELPSTRING << endl;
exit(-1);
}
string file;
vector<string> bases = {"๐Ÿฅต", "๐Ÿฅฐ", "๐Ÿคค"};
if (argc >= 3)
bases = vector<string>(argv + 2, argc + argv);
ifstream f(argv[1]);
channel A, B;
auto pid = fork();
if (pid < 0)
exit(-1);
if (pid == 0) {
dup2(A.out(), STDIN_FILENO);
close(A.in());
dup2(B.in(), STDOUT_FILENO);
dup2(B.in(), STDERR_FILENO);
close(B.out());
execv("/usr/bin/clang++", CLANG_ARGS);
}
close(A.out());
close(B.in());
while (f) {
string buffer;
getline(f, buffer);
if (!buffer.starts_with("#include")) {
buffer += "\n";
int x = write(A.in(), buffer.c_str(), buffer.size());
assert(buffer.size() == x);
} else {
pincludes += buffer + "\n";
}
}
close(A.in());
waitpid(pid, NULL, 1);
string clangout;
{
array<char, 1024> buffer;
for (int len; (len = read(B.out(), &buffer, 1024)) > 0;)
clangout.append(&buffer[0], len);
}
close(B.out());
TokenEngine x(bases);
string xnow = x++;
string os;
map<string, string> tokenmap;
auto sp = split(clangout, "\n");
for (auto i = sp.begin(); i != sp.end(); i++) {
auto clangLine = *i;
while (clangLine.size() && clangLine.back() == '\\') {
clangLine.pop_back();
clangLine += *++i;
}
if (clangLine.size()) {
Token token(clangLine);
if (token.StartOfLine)
os += '\n';
if (token.LeadingSpace)
os += ' ';
if (token.context != ":") {
if (token.type != "identifier")
os += ' ';
auto [it, succ] = tokenmap.insert({token.context, xnow});
if (succ) {
xnow = x++;
}
os += it->second;
if (token.type != "identifier")
os += ' ';
} else {
os += token.context;
}
}
}
cout << pincludes << "\n";
for (auto [f, t] : tokenmap)
cout << "#define " << t << " " << f << "\n";
cout << os << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment