Skip to content

Commit 0cfba8f

Browse files
committed
add option "breakout". Export Variants & Function
1 parent 0722a2b commit 0cfba8f

File tree

12 files changed

+198
-28
lines changed

12 files changed

+198
-28
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jfogs",
3-
"version": "0.0.13",
3+
"version": "0.0.15",
44
"description": "Javascript code obfuscator",
55
"main": "./lib/jfogs.js",
66
"authors": {

cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ var argv = optimist
2424
.describe('t', 'Encryption type. (e.g. "zero" | "reverse")')
2525
.string('t')
2626

27+
.alias('b', 'breakout')
28+
.describe('t', 'Export Variants & Function')
29+
.boolean('b')
30+
2731
.alias('v', 'version')
2832
.describe('v', 'Print version number and exit.')
2933

@@ -49,6 +53,7 @@ Options:
4953
#{-o, --output,cyan} Output file (default STDOUT)
5054
#{-t, --type,cyan} Encryption type (default "") e.g. "zero" | "reverse"
5155
#{-v, --version,cyan} Output jfogs version
56+
#{-b, --breakout,cyan} Export Variants & Function
5257
*/
5358
})
5459
.replace(/[^]*\/\*!?\s*|\s*\*\/[^]*/g, '')

index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/jfogs.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Javascript code obfuscator
66
* @author
77
* zswang (http://weibo.com/zswang)
8-
* @version 0.0.13
9-
* @date 2016-01-20
8+
* @version 0.0.15
9+
* @date 2016-02-19
1010
*/
1111
var esprimaInstance;
1212
if (typeof require === 'function') { // in node
@@ -89,10 +89,11 @@
8989
/**
9090
* 混淆 JS 代码
9191
*
92-
* @param {String} code JS 代码字符串
92+
* @param {string} code JS 代码字符串
9393
* @param {Object} options 配置项
94-
* @param {Object} options.type 混淆类型 'zero': 零宽字符, 'reverse': 颠掉字符
95-
* @return {String} 返回混淆后的代码
94+
* @param {string} options.type 混淆类型 'zero': 零宽字符, 'reverse': 颠掉字符
95+
* @param {string} options.export 是否导出函数
96+
* @return {string} 返回混淆后的代码
9697
*/
9798
function obfuscate(code, options) {
9899
if (!code) {
@@ -109,6 +110,18 @@
109110
range: true,
110111
loc: false
111112
});
113+
var breakoutVariants = [];
114+
if (options.breakout) { // 是否自动导出
115+
syntax.body.forEach(function(item) {
116+
if (item.type === 'VariableDeclaration') { // 变量定义
117+
item.declarations.forEach(function(sub) {
118+
breakoutVariants.push(sub.id.name);
119+
});
120+
} else if (item.type === 'FunctionDeclaration') { // 函数定义
121+
breakoutVariants.push(item.id.name);
122+
}
123+
});
124+
}
112125
var guid = 0;
113126
var memberExpressions = [];
114127
var propertys = {};
@@ -385,6 +398,30 @@
385398
decryption += format( "\nif (#{u202e} !== #{rightToLeft}) {\n return;\n}\n ", params);
386399
break;
387400
}
401+
if (options.breakout && breakoutVariants.length) {
402+
var breakoutIdent = identFrom(guid++);
403+
var breakoutInside = breakoutVariants.map(function (name) {
404+
return format('#{ident}.#{name} = #{name};', {
405+
ident: breakoutIdent,
406+
name: name
407+
});
408+
}).join('\n');
409+
var breakoutOutside = breakoutVariants.map(function (name) {
410+
return format('var #{name} = #{ident}.#{name};', {
411+
ident: breakoutIdent,
412+
name: name
413+
});
414+
}).join('\n');
415+
return format( "\nvar #{breakoutIdent} = {};\n(function (#{names}) {\n #{decryption}\n #{code}\n #{breakoutInside}\n})(#{expressions});\n#{breakoutOutside}\n ", {
416+
breakoutIdent: breakoutIdent,
417+
breakoutInside: breakoutInside,
418+
breakoutOutside: breakoutOutside,
419+
names: names.join(', '),
420+
decryption: decryption,
421+
code: code,
422+
expressions: expressions.join(', ')
423+
});
424+
}
388425
return format( "\n(function (#{names}) {\n #{decryption}\n #{code}\n})(#{expressions});\n ", {
389426
names: names.join(', '),
390427
decryption: decryption,

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "jfogs",
3-
"version": "0.0.13",
3+
"version": "0.0.15",
44
"description": "Javascript code obfuscator",
5-
"main": "index.js",
5+
"main": "lib/jfogs.js",
66
"bin": {
77
"jfogs": "cli.js"
88
},
@@ -35,8 +35,7 @@
3535
"esprima": "^2.5.0",
3636
"mkdirp": "^0.5.0",
3737
"colors": "^1.0.3",
38-
"optimist": "^0.6.1",
39-
"jhtmls": "^0.1.9"
38+
"optimist": "^0.6.1"
4039
},
4140
"scripts": {
4241
"_update_version": "node version.js",

src/jfogs.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@
4545
/**
4646
* 混淆 JS 代码
4747
*
48-
* @param {String} code JS 代码字符串
48+
* @param {string} code JS 代码字符串
4949
* @param {Object} options 配置项
50-
* @param {Object} options.type 混淆类型 'zero': 零宽字符, 'reverse': 颠掉字符
51-
* @return {String} 返回混淆后的代码
50+
* @param {string} options.type 混淆类型 'zero': 零宽字符, 'reverse': 颠掉字符
51+
* @param {string} options.export 是否导出函数
52+
* @return {string} 返回混淆后的代码
5253
*/
5354
function obfuscate(code, options) {
5455
if (!code) {
@@ -68,6 +69,19 @@
6869
loc: false
6970
});
7071

72+
var breakoutVariants = [];
73+
if (options.breakout) { // 是否自动导出
74+
syntax.body.forEach(function(item) {
75+
if (item.type === 'VariableDeclaration') { // 变量定义
76+
item.declarations.forEach(function(sub) {
77+
breakoutVariants.push(sub.id.name);
78+
});
79+
} else if (item.type === 'FunctionDeclaration') { // 函数定义
80+
breakoutVariants.push(item.id.name);
81+
}
82+
});
83+
}
84+
7185
var guid = 0;
7286
var memberExpressions = [];
7387
var propertys = {};
@@ -474,6 +488,44 @@ if (#{u202e} !== #{rightToLeft}) {
474488
}, params);
475489
break;
476490
}
491+
if (options.breakout && breakoutVariants.length) {
492+
493+
var breakoutIdent = identFrom(guid++);
494+
var breakoutInside = breakoutVariants.map(function (name) {
495+
return format('#{ident}.#{name} = #{name};', {
496+
ident: breakoutIdent,
497+
name: name
498+
});
499+
}).join('\n');
500+
var breakoutOutside = breakoutVariants.map(function (name) {
501+
return format('var #{name} = #{ident}.#{name};', {
502+
ident: breakoutIdent,
503+
name: name
504+
});
505+
}).join('\n');
506+
507+
return format( /*#*/ function() {
508+
/*!
509+
var #{breakoutIdent} = {};
510+
(function (#{names}) {
511+
#{decryption}
512+
#{code}
513+
#{breakoutInside}
514+
})(#{expressions});
515+
#{breakoutOutside}
516+
*/
517+
}, {
518+
breakoutIdent: breakoutIdent,
519+
breakoutInside: breakoutInside,
520+
breakoutOutside: breakoutOutside,
521+
522+
names: names.join(', '),
523+
decryption: decryption,
524+
code: code,
525+
expressions: expressions.join(', ')
526+
});
527+
}
528+
477529
return format( /*#*/ function() {
478530
/*!
479531
(function (#{names}) {

test/fixtures/breakout.input.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var tt, gloab_variant = 2;
2+
3+
function calc(x) {
4+
return x * gloab_variant + x * (gloab_variant + 1);
5+
}
6+
7+
tt = 6;

test/fixtures/breakout.output.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
var $fog$4 = {};
3+
(function ($fog$3, $fog$0, $fog$1, $fog$2) {
4+
5+
if ("‮" !== $fog$3) {
6+
return;
7+
}
8+
9+
var tt, gloab_variant = $fog$0;
10+
11+
function calc(x) {
12+
return x * gloab_variant + x * (gloab_variant + $fog$1);
13+
}
14+
15+
tt = $fog$2;
16+
$fog$4.tt = tt;
17+
$fog$4.gloab_variant = gloab_variant;
18+
$fog$4.calc = calc;
19+
})("‮", 2, 1, 6);
20+
var tt = $fog$4.tt;
21+
var gloab_variant = $fog$4.gloab_variant;
22+
var calc = $fog$4.calc;
23+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
var $fog$16 = {};
3+
(function ($fog$0, $fog$1, $fog$2, $fog$15, $fog$6, $fog$5, $fog$11, $fog$12, $fog$13) {
4+
5+
if ("‮" !== $fog$15) {
6+
return;
7+
}
8+
var $fog$3 = arguments;
9+
var $fog$4;
10+
11+
for ($fog$4 = $fog$11; $fog$4 < $fog$6 / $fog$13; $fog$4++) {
12+
var $fog$7 = $fog$3[$fog$4];
13+
$fog$3[$fog$4] = $fog$3[$fog$6 - $fog$4 - $fog$12];
14+
$fog$3[$fog$6 - $fog$4 - $fog$12] = $fog$7;
15+
}
16+
17+
var tt, gloab_variant = $fog$2;
18+
19+
function calc(x) {
20+
return x * gloab_variant + x * (gloab_variant + $fog$1);
21+
}
22+
23+
tt = $fog$0;
24+
$fog$16.tt = tt;
25+
$fog$16.gloab_variant = gloab_variant;
26+
$fog$16.calc = calc;
27+
})(2, 1, 6, "‮", 3, "", 0, 1, 2);
28+
var tt = $fog$16.tt;
29+
var gloab_variant = $fog$16.gloab_variant;
30+
var calc = $fog$16.calc;
31+

test/fixtures/breakout.zero.output.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
var $fog$3 = {};
3+
(function ($fog$0, $fog$1, $fog$2) {
4+
5+
var tt, gloab_variant = $fog$0;
6+
7+
function calc(x) {
8+
return x * gloab_variant + x * (gloab_variant + $fog$1);
9+
}
10+
11+
tt = $fog$2;
12+
$fog$3.tt = tt;
13+
$fog$3.gloab_variant = gloab_variant;
14+
$fog$3.calc = calc;
15+
})(2, 1, 6);
16+
var tt = $fog$3.tt;
17+
var gloab_variant = $fog$3.gloab_variant;
18+
var calc = $fog$3.calc;
19+

test/test.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,36 @@ describe('fixtures', function () {
3636
var output = input.replace(/\.input\.(\w+)$/, '.output.$1');
3737
var reverseOutput = input.replace(/\.input\.(\w+)$/, '.reverse.output.$1');
3838
var zeroOutput = input.replace(/\.input\.(\w+)$/, '.zero.output.$1');
39-
40-
// var options = {};
41-
// if (input.indexOf('zero') >= 0) {
42-
// options.type = 'zero';
43-
// }
44-
// if (input.indexOf('reverse') >= 0) {
45-
// options.type = 'reverse';
46-
// }
39+
var breakout = /breakout/.test(input);
4740
if (fs.existsSync(output)) {
48-
it(input, function () {
41+
it(input + (breakout ? ' -b' : ''), function () {
4942
assert.equal(
50-
jfogs.obfuscate(fs.readFileSync(input), {}),
43+
jfogs.obfuscate(fs.readFileSync(input), {
44+
breakout: breakout
45+
}),
5146
cleanCRLF(fs.readFileSync(output))
5247
);
5348
});
5449
}
5550

5651
if (fs.existsSync(reverseOutput)) {
57-
it(input + ' -t reverse', function () {
52+
it(input + ' -t reverse' + (breakout ? ' -b' : ''), function () {
5853
assert.equal(
5954
jfogs.obfuscate(fs.readFileSync(input), {
60-
type: 'reverse'
55+
type: 'reverse',
56+
breakout: breakout
6157
}),
6258
cleanCRLF(fs.readFileSync(reverseOutput))
6359
);
6460
});
6561
}
6662

6763
if (fs.existsSync(zeroOutput)) {
68-
it(input + ' -t zero', function () {
64+
it(input + ' -t zero' + (breakout ? ' -b' : ''), function () {
6965
assert.equal(
7066
jfogs.obfuscate(fs.readFileSync(input), {
71-
type: 'zero'
67+
type: 'zero',
68+
breakout: breakout
7269
}),
7370
cleanCRLF(fs.readFileSync(zeroOutput))
7471
);

version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var package = JSON.parse(fs.readFileSync(filename));
66
package.version = package.version.replace(/-?\d+$/, function(value) {
77
return parseInt(value) + 1;
88
});
9+
package.main = 'lib/' + package.name + '.js',
910
fs.writeFileSync(filename, JSON.stringify(package, null, ' '));
1011

1112
var bower_filename = path.join(__dirname, 'bower.json');

0 commit comments

Comments
 (0)