前些时候因为人机识别和反作弊业务的需求调研了浏览器指纹和追踪的一些方法,那么当我们把检测代码上线后,怎么保护它,不被攻击者迅速分析破解呢?

常见的编码(如 Base62)、压缩(如 UglifyJS)、复杂化表达式(如填充无用代码,拆分字符串)就不细说了。
至于将 JavaScript 代码隐藏在图片中类似隐写术的方法,一般是恶意程序为了逃避杀毒软件检测所用,正常业务很少用到。

通常用各种编码“加密”的代码,无论怎样变形,其最终都要调用一次 eval 等函数执行。
只需劫持关键函数调用的行为,改为文本输出(如 console.log)即可得到载体中隐藏的代码。

  1. eval = function() {
  2. console.log('eval', JSON.stringify(arguments));
  3. };
  4. eval('console.log("Hello world!")');

去除空格、换行,缩短函数、变量名之类的压缩代码可以直接用浏览器的开发者工具格式化,或是使用 jsbeautifier 等在线工具美化。
复杂化表达式会增加代码复杂度,极大地降低可读性,但有经验和耐心的研究者依然能慢慢调试还原出功能来。

(下面代码在 Chrome 59 上测试通过)

Console

检测到浏览器 Console 打开(Detect all browser console open or not)时阻塞 Javascript 执行:

  1. var checkStatus;
  2.  
  3. var element = new Image();
  4. // var element = document.createElement('any');
  5. element.__defineGetter__('id', function() {
  6. checkStatus = 'on';
  7. });
  8.  
  9. setInterval(function() {
  10. checkStatus = 'off';
  11. console.log(element);
  12. console.clear();
  13. if(checkStatus == 'on') {
  14. alert('Prohibit the use of console!');
  15. }
  16. }, 1000)
  17.  

Debugger

Console 调试时会自动停在断点处,借此可以插入随机的 debugger 干扰正常调试。

  1. !function test() {
  2. // 捕获异常,递归次数过多调试工具会抛出异常。
  3. try{
  4. !function cir(i)
  5. {
  6. // 当打开调试工具后,抛出异常,setTimeout执行test无参数,此时i == NaN,("" + i / i).length == 3
  7. // debugger设置断点
  8. ( 1 !== ( "" + i / i).length || 0===i ) && function({}.constructor("debugger")(),cir(++i);
  9. } (0)
  10. } catch(e) {
  11. setTimeout(test,500)
  12. }
  13. }()

demo:https://jsfiddle.net/ftpgxm/t4ux8xp4/2/

AST

通过修改 AST(Abstract Syntax Tree) 生成一个新的 AST,混淆规则有拆分字符串、拆分数组,增加废代码等。
如在同构语法的基础上提取出所有 key 值到闭包的参数中,破坏代码的可读性:

  1. var a = document.getElementById('a');
  2. a.innerHTML = 'test';

混淆之后是:

  1. (function(a,b,c,d,e,f){
  2. var g=a[b][c](d);
  3. g[e]=f
  4. })(window,'document', 'getElementById', 'a', 'innerHTML', 'test');

WebAssembly

WebAssembly 是可用于浏览器的字节码格式,比 JS 更高效,能从 C/C++ 编译。
如一个简单的 add 和 square

  1. WebAssembly.compile(new Uint8Array(`
  2. 00 61 73 6d 01 00 00 00 01 0c 02 60 02 7f 7f 01
  3. 7f 60 01 7f 01 7f 03 03 02 00 01 07 10 02 03 61
  4. 64 64 00 00 06 73 71 75 61 72 65 00 01 0a 13 02
  5. 08 00 20 00 20 01 6a 0f 0b 08 00 20 00 20 00 6c
  6. 0f 0b`.trim().split(/[\s\r\n]+/g).map(str => parseInt(str, 16))
  7. )).then(module => {
  8. const instance = new WebAssembly.Instance(module)
  9. const { add, square } = instance.exports
  10.  
  11. console.log('2 + 4 =', add(2, 4))
  12. console.log('3^2 =', square(3))
  13. console.log('(2 + 5)^2 =', square(add(2 + 5)))
  14.  
  15. })

它可能是终极的解决办法,因为作为二进制编码它自带“混淆”,还可以进一步加壳或虚拟机保护。

绕过方法

对于大部分的 JS 代码混淆加密,其实都可以用 Partial evaluation 解决(参见讨论:https://www.v2ex.com/t/367641)。
如 Google 的 Closure Compiler 和 FaceBook 的 Prepack,虽然是用于 JS 代码优化的工具,
但它们都会在编译期重构 AST、计算函数、初始化对象等,最终还原出正常的可读的代码。

对于干扰 Console 调试的方法,可以用 Fiddler 或 Burp Suite 抓包,拦截页面请求,删除或注释掉干扰代码。

参考