myobfuscate_unpacker.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation files
  6. (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge,
  8. publish, distribute, sublicense, and/or sell copies of the Software,
  9. and to permit persons to whom the Software is furnished to do so,
  10. subject to the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. //
  23. // simple unpacker/deobfuscator for scripts messed up with myobfuscate.com
  24. // You really don't want to obfuscate your scripts there: they're tracking
  25. // your unpackings, your script gets turned into something like this,
  26. // as of 2011-04-25:
  27. /*
  28. var _escape = 'your_script_escaped';
  29. var _111 = document.createElement('script');
  30. _111.src = 'http://api.www.myobfuscate.com/?getsrc=ok' +
  31. '&ref=' + encodeURIComponent(document.referrer) +
  32. '&url=' + encodeURIComponent(document.URL);
  33. var 000 = document.getElementsByTagName('head')[0];
  34. 000.appendChild(_111);
  35. document.write(unescape(_escape));
  36. */
  37. //
  38. // written by Einar Lielmanis <einar@beautifier.io>
  39. //
  40. // usage:
  41. //
  42. // if (MyObfuscate.detect(some_string)) {
  43. // var unpacked = MyObfuscate.unpack(some_string);
  44. // }
  45. //
  46. //
  47. /*jshint strict:false */
  48. var MyObfuscate = {
  49. detect: function(str) {
  50. if (/^var _?[0O1lI]{3}\=('|\[).*\)\)\);/.test(str)) {
  51. return true;
  52. }
  53. if (/^function _?[0O1lI]{3}\(_/.test(str) && /eval\(/.test(str)) {
  54. return true;
  55. }
  56. return false;
  57. },
  58. unpack: function(str) {
  59. if (MyObfuscate.detect(str)) {
  60. var __eval = eval;
  61. try {
  62. eval = function(unpacked) { // jshint ignore:line
  63. if (MyObfuscate.starts_with(unpacked, 'var _escape')) {
  64. // fetch the urlencoded stuff from the script,
  65. var matches = /'([^']*)'/.exec(unpacked);
  66. var unescaped = unescape(matches[1]);
  67. if (MyObfuscate.starts_with(unescaped, '<script>')) {
  68. unescaped = unescaped.substr(8, unescaped.length - 8);
  69. }
  70. if (MyObfuscate.ends_with(unescaped, '</script>')) {
  71. unescaped = unescaped.substr(0, unescaped.length - 9);
  72. }
  73. unpacked = unescaped;
  74. }
  75. // throw to terminate the script
  76. unpacked = "// Unpacker warning: be careful when using myobfuscate.com for your projects:\n" +
  77. "// scripts obfuscated by the free online version may call back home.\n" +
  78. "\n//\n" + unpacked;
  79. throw unpacked;
  80. }; // jshint ignore:line
  81. __eval(str); // should throw
  82. } catch (e) {
  83. // well, it failed. we'll just return the original, instead of crashing on user.
  84. if (typeof e === "string") {
  85. str = e;
  86. }
  87. }
  88. eval = __eval; // jshint ignore:line
  89. }
  90. return str;
  91. },
  92. starts_with: function(str, what) {
  93. return str.substr(0, what.length) === what;
  94. },
  95. ends_with: function(str, what) {
  96. return str.substr(str.length - what.length, what.length) === what;
  97. },
  98. run_tests: function(sanity_test) {
  99. var t = sanity_test || new SanityTest();
  100. return t;
  101. }
  102. };