javascriptobfuscator_unpacker.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // simple unpacker/deobfuscator for scripts messed up with javascriptobfuscator.com
  3. // written by Einar Lielmanis <einar@jsbeautifier.org>
  4. //
  5. // usage:
  6. //
  7. // if (JavascriptObfuscator.detect(some_string)) {
  8. // var unpacked = JavascriptObfuscator.unpack(some_string);
  9. // }
  10. //
  11. //
  12. var JavascriptObfuscator = {
  13. detect: function (str) {
  14. return /^var _0x[a-f0-9]+ ?\= ?\[/.test(str);
  15. },
  16. unpack: function (str) {
  17. if (JavascriptObfuscator.detect(str)) {
  18. var matches = /var (_0x[a-f\d]+) ?\= ?\[(.*?)\];/.exec(str);
  19. if (matches) {
  20. var var_name = matches[1];
  21. var strings = JavascriptObfuscator._smart_split(matches[2]);
  22. str = str.substring(matches[0].length);
  23. for (var k in strings) {
  24. str = str.replace(new RegExp(var_name + '\\[' + k + '\\]', 'g'),
  25. JavascriptObfuscator._fix_quotes(JavascriptObfuscator._unescape(strings[k])));
  26. }
  27. }
  28. }
  29. return str;
  30. },
  31. _fix_quotes: function(str) {
  32. var matches = /^"(.*)"$/.exec(str);
  33. if (matches) {
  34. str = matches[1];
  35. str = "'" + str.replace(/'/g, "\\'") + "'";
  36. }
  37. return str;
  38. },
  39. _smart_split: function(str) {
  40. var strings = [];
  41. var pos = 0;
  42. while (pos < str.length) {
  43. if (str.charAt(pos) == '"') {
  44. // new word
  45. var word = '';
  46. pos += 1;
  47. while (pos < str.length) {
  48. if (str.charAt(pos) == '"') {
  49. break;
  50. }
  51. if (str.charAt(pos) == '\\') {
  52. word += '\\';
  53. pos++;
  54. }
  55. word += str.charAt(pos);
  56. pos++;
  57. }
  58. strings.push('"' + word + '"');
  59. }
  60. pos += 1;
  61. }
  62. return strings;
  63. },
  64. _unescape: function (str) {
  65. // inefficient if used repeatedly or on small strings, but wonderful on single large chunk of text
  66. for (var i = 32; i < 128; i++) {
  67. str = str.replace(new RegExp('\\\\x' + i.toString(16), 'ig'), String.fromCharCode(i));
  68. }
  69. str = str.replace(/\\x09/g, "\t");
  70. return str;
  71. },
  72. run_tests: function (sanity_test) {
  73. var t = sanity_test || new SanityTest();
  74. t.test_function(JavascriptObfuscator._smart_split, "JavascriptObfuscator._smart_split");
  75. t.expect('', []);
  76. t.expect('"a", "b"', ['"a"', '"b"']);
  77. t.expect('"aaa","bbbb"', ['"aaa"', '"bbbb"']);
  78. t.expect('"a", "b\\\""', ['"a"', '"b\\\""']);
  79. t.test_function(JavascriptObfuscator._unescape, 'JavascriptObfuscator._unescape');
  80. t.expect('\\x40', '@');
  81. t.expect('\\x10', '\\x10');
  82. t.expect('\\x1', '\\x1');
  83. t.expect("\\x61\\x62\\x22\\x63\\x64", 'ab"cd');
  84. t.test_function(JavascriptObfuscator.detect, 'JavascriptObfuscator.detect');
  85. t.expect('', false);
  86. t.expect('abcd', false);
  87. t.expect('var _0xaaaa', false);
  88. t.expect('var _0xaaaa = ["a", "b"]', true);
  89. t.expect('var _0xaaaa=["a", "b"]', true);
  90. t.expect('var _0x1234=["a","b"]', true);
  91. return t;
  92. }
  93. };