• 在NodeJS中使用npm包实现JS代码混淆加密


    使用npm包,在NodeJS中实现JS代码混淆加密

    在前后端JS开发过程中,JS代码保护(JS代码混淆加密)是非常重要的一环。

    JShaman是一个云端的代码保护Saas平台,可以对JS代码进行混淆、加密、压缩等操作,从而增强JS代码的安全性。同时,JShaman还有更方便易用的npm包,方便开发人员通过调用接口的方式,快速完成JS代码混淆加密。

    npm网站,可以找到名为jshaman-javascript-obfuscator的包,如下图所示:

    这里有对它的使用说明,如在Nodejs环境中的安装方法,调用例程,等。

    安装

    npm install jshaman-javascript-obfuscator

    使用

    NodeJS例程代码如下所示。

    1. //JShaman JavaScript Obfuscator Web API Interface
    2. var jshaman_javascript_obfuscator = require("jshaman-javascript-obfuscator");
    3. //JavaScript Code to be obfuscated
    4. var javascript_code = `
    5. function NewObject(prefix)
    6. {
    7. var count=0;
    8. this.SayHello=function(msg)
    9. {
    10. count++;
    11. alert(prefix+msg);
    12. }
    13. this.GetCount=function()
    14. {
    15. return count;
    16. }
    17. }
    18. var obj=new NewObject("Message : ");
    19. obj.SayHello("You are welcome.");
    20. `;
    21. //Options.
    22. //Please refer to the official website of JShaman in English for relevant instructions.
    23. //https://www.jshaman.com/en/
    24. var options = {
    25. "part_variable_identifier_obfuscate": 1,
    26. "global_variable_identifier_obfuscate": 0,
    27. "part_function_identifier_obfuscate":0,
    28. "global_function_identifier_obfuscate": 0,
    29. "member_expression_encode": 1,
    30. "numberic_literal_encode": 1,
    31. "binary_express_obfuscate": 1,
    32. "boolean_encode": 1,
    33. "json_encode":1,
    34. "regexp_encode":1,
    35. "string_unicode_encode": 1,
    36. "assignment_junk_code":1,
    37. "zombie_code": 1,
    38. "eval_encode": 1,
    39. "control_flow": 1,
    40. "string_reverse": 1,
    41. "comma_operator": 1,
    42. "string_array": 0,
    43. "string_array_encode": 0,
    44. "vm_execute": 0,
    45. "ast_execute": 0,
    46. "no_beautifier": 0,
    47. "tamper_proof": 0,
    48. "comments": 0,
    49. "compress": 1,
    50. "reserved_word": ["jshaman","w2sfot"]
    51. }
    52. //Secret key,Obtained from the JShaman official website.
    53. //If not yet obtained, it can be set to free
    54. var secret_key = "free";
    55. var obfuscated_result = jshaman_javascript_obfuscator(javascript_code, options, secret_key);
    56. //Obfuscation result,
    57. //if "state" is 0 it means successful and the "content" is the obfuscated JavaScript code.
    58. //Otherwise,if there is an error,the "message" will contain an error prompt message.
    59. console.log(obfuscated_result.state, obfuscated_result.message, obfuscated_result.content);

    代码说明

    调用JShaman接口,传入js代码、配置即可,非常简单。

    上面的代码中,javascript_code变量是要保护的JS代码,options 变量是参数,参数中各项目的含义,可以参考JShaman官网的说明,值设为1表示启用、设为0表示不启用,secret_key是接口密钥,设为free是免费使用,商业的密钥可以从JShaman官网获得。

    加密效果

    上面例程中的JS代码,保护后生成的加密JS代码如下所示。

    1. //Obfuscted javascript code
    2. /*
    3. var _0xce7d8a = ["117.", "92.103.98.103.102.126.103.41.72.90.93.41.125.112.121.108.51.", "117.", "117."];
    4. function _0x57d18d(_4, _5) {
    5. _5 = 9;
    6. var _,
    7. _2,
    8. _3 = "";
    9. _2 = _4.split(".");
    10. for (_ = 0; _ < _2.length - 1; _++) {
    11. _3 += String.fromCharCode(_2[_] ^ _5);
    12. }
    13. return _3;
    14. }
    15. var visitors = {
    16. File(node, scope) {
    17. ast_excute(node['\x70\x72\x6f\x67\x72\x61\x6d'], scope);
    18. },
    19. Program(program, scope) {
    20. for (i = function () {
    21. return eval(String.fromCharCode(57, 48, 53, 49, 49, 53, 32, 94, 32, 57, 48, 53, 49, 49, 53));
    22. }(); eval(String.fromCharCode(105, 32, 60, 32, 112, 114, 111, 103, 114, 97, 109, 91, 39, 92, 120, 54, 50, 92, 120, 54, 102, 92, 120, 54, 52, 92, 120, 55, 57, 39, 93, 91, 39, 92, 120, 54, 99, 92, 120, 54, 53, 92, 120, 54, 101, 92, 120, 54, 55, 92, 120, 55, 52, 92, 120, 54, 56, 39, 93)); eval(String.fromCharCode(105, 43, 43))) {
    23. ast_excute(program['\x62\x6f\x64\x79'][i], scope);
    24. }
    25. },
    26. ExpressionStatement(node, scope) {
    27. return ast_excute(node['\x65\x78\x70\x72\x65\x73\x73\x69\x6f\x6e'], scope);
    28. },
    29. CallExpression(node, scope) {
    30. var func = ast_excute(node['\x63\x61\x6c\x6c\x65\x65'], scope);
    31. var args = node['\x61\x72\x67\x75\x6d\x65\x6e\x74\x73']['\x6d\x61\x70'](function (arg) {
    32. return ast_excute(arg, scope);
    33. });
    34. var value;
    35. if (eval(String.fromCharCode(110, 111, 100, 101, 91, 39, 92, 120, 54, 51, 92, 120, 54, 49, 92, 120, 54, 99, 92, 120, 54, 99, 92, 120, 54, 53, 92, 120, 54, 53, 39, 93, 91, 39, 92, 120, 55, 52, 92, 120, 55, 57, 92, 120, 55, 48, 92, 120, 54, 53, 39, 93, 32, 61, 61, 61, 32, 39, 77, 101, 109, 98, 101, 114, 69, 120, 112, 114, 101, 115, 115, 105, 111, 110, 39))) {
    36. value = ast_excute(node['\x63\x61\x6c\x6c\x65\x65']['\x6f\x62\x6a\x65\x63\x74'], scope);
    37. }
    38. return func['\x61\x70\x70\x6c\x79'](value, args);
    39. },
    40. MemberExpression(node, scope) {
    41. var obj = ast_excute(node['\x6f\x62\x6a\x65\x63\x74'], scope);
    42. var name = node['\x70\x72\x6f\x70\x65\x72\x74\x79']['\x6e\x61\x6d\x65'];
    43. return obj[name];
    44. },
    45. Identifier(node, scope) {
    46. return scope[node['\x6e\x61\x6d\x65']];
    47. },
    48. StringLiteral(node) {
    49. return node['\x76\x61\x6c\x75\x65'];
    50. },
    51. NumericLiteral(node) {
    52. return node['\x76\x61\x6c\x75\x65'];
    53. }
    54. };
    55. function ast_excute(node, scope) {
    56. var _0x51e = "2|1|0".split(_0x57d18d(_0xce7d8a[0])),
    57. _0x6ebgc = 0;
    58. while (!![]) {
    59. switch (+_0x51e[_0x6ebgc++]) {
    60. case 0:
    61. return evalute(node, scope);
    62. continue;
    63. case 1:
    64. if (!evalute) {
    65. throw new Error(_0x57d18d(_0xce7d8a[1]), node['\x74\x79\x70\x65']);
    66. }
    67. continue;
    68. case 2:
    69. var evalute = visitors[node['\x74\x79\x70\x65']];
    70. continue;
    71. }
    72. break;
    73. }
    74. }
    75. function _0x2dd6b(prefix) {
    76. var _0xcf9e = "4|2|0|3|1".split(_0x57d18d(_0xce7d8a[2])),
    77. _0xef765g = 0;
    78. while (!![]) {
    79. switch (+_0xcf9e[_0xef765g++]) {
    80. case 0:
    81. _0x38e = function () {
    82. return eval(String.fromCharCode(56, 54, 57, 53, 54, 52, 32, 94, 32, 56, 54, 57, 53, 53, 54));
    83. }();
    84. continue;
    85. case 1:
    86. this['\x47\x65\x74\x43\x6f\x75\x6e\x74'] = function () {
    87. return _0xa1b;
    88. };
    89. continue;
    90. case 2:
    91. var _0xa1b = function (s, h) {
    92. return eval(String.fromCharCode(115, 32, 94, 32, 104));
    93. }(693721, 693721);
    94. continue;
    95. case 3:
    96. this['\x53\x61\x79\x48\x65\x6c\x6c\x6f'] = function (msg) {
    97. var _0xag624c = "1|0".split(_0x57d18d(_0xce7d8a[3])),
    98. _0xc1411b = 0;
    99. while (!![]) {
    100. switch (+_0xag624c[_0xc1411b++]) {
    101. case 0:
    102. alert(eval(String.fromCharCode(112, 114, 101, 102, 105, 120, 32, 43, 32, 109, 115, 103)));
    103. continue;
    104. case 1:
    105. eval(String.fromCharCode(95, 48, 120, 97, 49, 98, 43, 43));
    106. continue;
    107. }
    108. break;
    109. }
    110. };
    111. continue;
    112. case 4:
    113. var _0x38e;
    114. continue;
    115. }
    116. break;
    117. }
    118. }
    119. var _0xecf = new _0x2dd6b(" : egasseM"['\x73\x70\x6c\x69\x74']("")['\x72\x65\x76\x65\x72\x73\x65']()['\x6a\x6f\x69\x6e'](""));
    120. _0xecf['\x53\x61\x79\x48\x65\x6c\x6c\x6f'](".emoclew era uoY"['\x73\x70\x6c\x69\x74']("")['\x72\x65\x76\x65\x72\x73\x65']()['\x6a\x6f\x69\x6e'](""));
    121. */

    做为颇具知名度的JS代码混淆加密平台,JShaman的加密效果还是很不错的。

    扩展使用

    把上述例程代码稍加改造,嵌入到自己的项目或产品中,就可以进行自动化的JS代码混淆加密了。

    混淆加密JS代码、提高JS代码安全性,防止他人随意查看、复制,就是如此简单。

  • 相关阅读:
    Java练习题-输出斐波那契(Fibonacci)数列
    当下、百年之计与蝼蚁一生——读《原则2 :应对变化中的世界秩序》(下)...
    36.骑士周游算法及其基于贪心算法的优化
    一次简单易懂的多态重构实践,让你理解条件逻辑
    图片转表格怎么转?看完这篇你就会了
    堆外内存和堆内内存及虚引用的应用
    mysql查看binlog内容,dump 恢复单表
    壳聚糖-聚乙二醇-吲哚菁绿,Indocyaninegreen-PEG-Chitosan
    学习架设传奇入门必读的好文章
    夜神模拟器微信扫码
  • 原文地址:https://blog.csdn.net/w2sft/article/details/134021719