关于 node.js 混淆,有没有办法?

2014-06-17 09:06:28 +08:00
 kurtis
请教各位牛人,一个问题 有没有较好的方案可以混淆 node.js程序。

条件:
1. 不是单个js文件,而是互相require引用的若干个js组成的一个package
2. 混淆或者大幅降低可读性即可,不需要加密(估计也无法加密)
3. 不接受 “没有办法” 或者 “为什么要混淆”之类的回答。

查阅了很多资料,国外也有人在讨论,方法千奇百怪,但几乎没有一个很靠谱的。

感谢先!!!
14609 次点击
所在节点    Node.js
27 条回复
liuyanghejerry
2014-06-22 22:52:12 +08:00
// require_x.js

var fs = require('fs');

module.exports = {
require_x: function (name) {
var mod = fs.readFileSync(name);
mod = decode(mod);
var real_name = 'hack here as you wish';
var mod_path = save_file_into_one_dir(real_name, mod);
// now you have the original js module
// but be sure all deps are installed in that path.
return require(mod_path);
},
transform_x: function (file) {
// use async read if you want to
var mod = fs.readFileSync(file);
mod = encode(mod);
var transformed_name = 'hack here as you wish';
var mod_path = save_file_into_one_dir(transformed_name, mod);
// now you have your transformed js module file
return true;
}
}

function encode (content) {
// do your hack here
return content;
}

function decode (content) {
// do your hack here
return content;
}

function save_file_into_one_dir(name, content) {
// do your hack here
return real_path;
}


// app.js

var require_x = require('./require_x.js');
var my_mod = require_x('./my_mod_x.js');
var other_mod = require('other_mod');

my_mod.use.what.you.have();
kurtis
2014-06-23 07:00:49 +08:00
@liuyanghejerry

var a= require_x("./lib/A.xxx");

// insert hack code here
console.log(a.toString()); // source will be shown here without any encryption
liuyanghejerry
2014-06-23 17:09:56 +08:00
@kurtis var a= require_x("./lib/A.xxx", priviate_key);
kurtis
2014-06-23 17:26:21 +08:00
@liuyanghejerry
首先感谢已发送,很好的集思广益的设想,

但是我困惑的是加不加privatekey有区别吗?
反正,都是要decode以后再require。
任何人只要 加一句console.log 就能看到源码明文。

var a= require_x("./lib/A.xxx", priviate_key);
console.log(a.toString());
liuyanghejerry
2014-06-24 22:18:10 +08:00
@kurtis 这只是个框架啊,之后的细节你可以自己根据你实际情况改动。我这个方案的意图是告诉你,可以在require之前做手脚,node拿到的代码是decode过的这一点毫无疑问,因为node的源码没有改动。

比如decode之后的源码你可以从磁盘上删去,priviate_key你也可以删掉,这些都可以根据你自己的环境做适应啊
kurtis
2014-06-24 23:53:39 +08:00
@liuyanghejerry
让我想起了google group 里的讨论,里面有个更高级的办法,decode部分都不是用node写的,而是C写的node扩展。 可是破解也是一样的简单,如上所述。因为无论你怎么加密,最终node运行的都是明文,node的机制就是会保留 source 供toString() 使用。任何人都可以自己加上.toString()看到明文。

关于这一点,@juzai 提到的jxcore 就充分注意到了这个问题,并进行了内核上的改进。
idoldog
2016-01-22 03:22:41 +08:00
@kurtis 可以尝试一下

```
Function.prototype.toString = function () { return "[protect code]"; }
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/117914

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX