-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobfuscato2.js
225 lines (207 loc) · 5.58 KB
/
obfuscato2.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
function JS() {
this.vars = {};
this.funcs = {};
this.statements = [];
}
JS.prototype = {
toString: function() {
var res = "",
v = [],
v_arr = [];
for (var n in this.vars) {
v_arr.push("var " + n + "=" + this.vars[n] + ";\n");
}
res += v_arr.sort().join("");
for (var n in this.funcs) {
v.push(n);
}
if (v.length > 0) res += "var " + v.join(",") + ";\n";
var f_arr = [];
for (var n in this.funcs) {
f_arr.push(n);
}
f_arr.sort();
for (var i = 0; i < f_arr.length; i++) {
var n = f_arr[i];
res += n + "=function () {\n" + this.funcs[n].toString() +
"\n" + "};\n";
}
this.statements.forEach(function(statement) {
res += statement;
});
return res;
},
Obfuscate: function() {
var res = new JS,
v = [];
for (var n in this.vars) {
res.vars[n] = this.vars[n];
}
for (var n in this.funcs) {
res.vars[n] = "null";
}
for (var n in this.funcs) {
res.statements.push(n + "=function () {\n" + this.funcs[
n].toString() + "\n" + "};\n");
}
this.statements.forEach(function(statement) {
res.statements.push(statement);
});
return res;
},
encrypt: function($$) {
var ns = this.statements.splice(0, this.statements.length);
$$.cur = this;
encryptSts($$, ns);
for (var n in this.funcs) {
this.funcs[n].encrypt($$);
}
},
gen_v: function() {
var res = "";
do {
for (var i = 0; i < 3; i++) {
res += String.fromCharCode(Math.floor((Math.random() <
0.5 ? 65 : 97) + Math.random() *
26));
}
} while (res in this.vars);
this.vars[res] = "null";
return res;
}
};
var F = "function";
function Exp(expr) {
return "(" + expr + ")";
}
function Arr() {
var a = [];
for (var i = 0; i < arguments.length; i++) a.push(arguments[i]);
return a.join(",");
}
function Eval(s) {
return "eval" + P(s);
}
function sep(s) {
var i = Math.floor(Math.random() * (s.length - 1)) + 1;
var pre = s.substring(0, i);
var post = s.substring(i);
return [pre, post];
}
function sep2(s) {
var i = 0,
n, res = [];
while (i < s.length) {
n = i + Math.floor(Math.random() * 20 + 5);
if (n > s.length) n = s.length;
res.push(s.substring(i, n));
i = n;
}
return res;
}
function sep_a(statements) {
var i = Math.floor(Math.random() * (statements.length - 1)) + 1;
var pre = statements.splice(0, i);
var post = statements;
return [pre, post];
}
function gen_v($$) {
if (!$$.count) $$.count = 0;
$$.count++;
return "_" + $$.count;
}
function compress(code) {
var comped= "";
var rep = ["eval(", "\\\\\\\\", "var "];
rep.forEach(function(rep) {
var f = gen_sym(code);
code = code.replace(regx(rep), f);
comped += ".replace(" + regex(f) + ", " + Q(rep) +
")";
});
return E(Q(code) + comped);
}
function gen_sym(code) {
while (true) {
var st1 = String.fromCharCode(Math.floor(Math.random() *
32 + 32));
var st2 = String.fromCharCode(Math.floor(Math.random() *
32 + 64));
if (code.indexOf(st1 + st2) < 0) {
return st1 + st2;
}
}
}
function regx(s) {
return eval(rege(s));
}
function regex(s) {
return "/" + s.replace(/\W/g, function(s) {
return "\\" + s;
}) + "/g";
}
function Q(s, quote) {
if (!quote) quote = "'";
s = s.replace(/\\/g, "\\\\");
s = s.replace(/\r/g, "\\r");
s = s.replace(/\n/g, "\\n");
if (quote == "'") s = s.replace(/'/g, "\\'");
else s = s.replace(/"/g, '\\"');
return quote + s + quote;
}
function encryptSts($$, statements) {
statements.forEach(function(statement) {
encryptSt($$, statement);
});
}
function encryptSt($$, statement) {
var s = sep2(statement);
if (s.length <= 1) {
$$.cur.statements.push(statement);
return;
}
var evs = [];
var genfn = $$.top.gen_v($$);
var genf = new JS();
s.forEach(function(e, i) {
var v = $$.top.gen_v($$);
$$.top.vars[v] = true;
var rnd = Math.floor(Math.random() * 4);
if (rnd == 0) $$.top.vars[v] = Q(e);
else if (rnd == 1) $$.cur.statements.push(v + "=" + Q(
e) + ";\n");
else if (rnd == 2) $$.top.statements.unshift(v + "=" +
Q(e) + ";\n");
else genf.statements.push(v + "=" + Q(e) + ";\n");
evs.push(v);
});
$$.top.funcs[genfn] = genf;
genf.statements.push("eval(" + evs.join("+") + ");\n");
$$.cur.statements.push("setTimeout(" + genfn + ",0);\n");
$$.cur = genf;
}
$(function() {
$("#btn_obf").click(start);
$("#btn_eval").click(function() {
eval($("#obf").val());
});
$("#btn_clr").click(function(){
$("#obf").val('');
});
});
function Func(s) {
return "(" + F + "(){" + s + "})()";
}
function start() {
var top = new JS(),
cur = top;
var $$ = {
top: top,
cur: cur,
lim: 24
};
encryptSts($$, [$("#orig").val()]);
top.encrypt($$);
var fcomp = Func(top.toString());
$("#obf").val(fcomp);
}