MicrosoftScriptDecoder.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @author bmwhitn [brian.m.whitney@outlook.com]
  3. * @copyright Crown Copyright 2017
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. /**
  8. * Microsoft Script Decoder operation
  9. */
  10. class MicrosoftScriptDecoder extends Operation {
  11. /**
  12. * MicrosoftScriptDecoder constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Microsoft Script Decoder";
  17. this.module = "Default";
  18. this.description = "Decodes Microsoft Encoded Script files that have been encoded with Microsoft's custom encoding. These are often VBS (Visual Basic Script) files that are encoded and renamed with a '.vbe' extention or JS (JScript) files renamed with a '.jse' extention.<br><br><b>Sample</b><br><br>Encoded:<br><code>#@~^RQAAAA==-mD~sX|:/TP{~J:+dYbxL~@!F@*@!+@*@!&amp;@*eEI@#@&amp;@#@&amp;.jm.raY 214Wv:zms/obI0xEAAA==^#~@</code><br><br>Decoded:<br><code>var my_msg = &#34;Testing <1><2><3>!&#34;;\n\nVScript.Echo(my_msg);</code>";
  19. this.inputType = "string";
  20. this.outputType = "string";
  21. this.args = [];
  22. }
  23. /**
  24. * @param {string} input
  25. * @param {Object[]} args
  26. * @returns {string}
  27. */
  28. run(input, args) {
  29. const matcher = /#@~\^.{6}==(.+).{6}==\^#~@/;
  30. const encodedData = matcher.exec(input);
  31. if (encodedData){
  32. return MicrosoftScriptDecoder._decode(encodedData[1]);
  33. } else {
  34. return "";
  35. }
  36. }
  37. /**
  38. * Decodes Microsoft Encoded Script files that can be read and executed by cscript.exe/wscript.exe.
  39. * This is a conversion of a Python script that was originally created by Didier Stevens
  40. * (https://DidierStevens.com).
  41. *
  42. * @private
  43. * @param {string} data
  44. * @returns {string}
  45. */
  46. static _decode(data) {
  47. const result = [];
  48. let index = -1;
  49. data = data.replace(/@&/g, String.fromCharCode(10))
  50. .replace(/@#/g, String.fromCharCode(13))
  51. .replace(/@\*/g, ">")
  52. .replace(/@!/g, "<")
  53. .replace(/@\$/g, "@");
  54. for (let i = 0; i < data.length; i++) {
  55. const byte = data.charCodeAt(i);
  56. let char = data.charAt(i);
  57. if (byte < 128) {
  58. index++;
  59. }
  60. if ((byte === 9 || byte > 31 && byte < 128) &&
  61. byte !== 60 &&
  62. byte !== 62 &&
  63. byte !== 64) {
  64. char = D_DECODE[byte].charAt(D_COMBINATION[index % 64]);
  65. }
  66. result.push(char);
  67. }
  68. return result.join("");
  69. }
  70. }
  71. const D_DECODE = [
  72. "",
  73. "",
  74. "",
  75. "",
  76. "",
  77. "",
  78. "",
  79. "",
  80. "",
  81. "\x57\x6E\x7B",
  82. "\x4A\x4C\x41",
  83. "\x0B\x0B\x0B",
  84. "\x0C\x0C\x0C",
  85. "\x4A\x4C\x41",
  86. "\x0E\x0E\x0E",
  87. "\x0F\x0F\x0F",
  88. "\x10\x10\x10",
  89. "\x11\x11\x11",
  90. "\x12\x12\x12",
  91. "\x13\x13\x13",
  92. "\x14\x14\x14",
  93. "\x15\x15\x15",
  94. "\x16\x16\x16",
  95. "\x17\x17\x17",
  96. "\x18\x18\x18",
  97. "\x19\x19\x19",
  98. "\x1A\x1A\x1A",
  99. "\x1B\x1B\x1B",
  100. "\x1C\x1C\x1C",
  101. "\x1D\x1D\x1D",
  102. "\x1E\x1E\x1E",
  103. "\x1F\x1F\x1F",
  104. "\x2E\x2D\x32",
  105. "\x47\x75\x30",
  106. "\x7A\x52\x21",
  107. "\x56\x60\x29",
  108. "\x42\x71\x5B",
  109. "\x6A\x5E\x38",
  110. "\x2F\x49\x33",
  111. "\x26\x5C\x3D",
  112. "\x49\x62\x58",
  113. "\x41\x7D\x3A",
  114. "\x34\x29\x35",
  115. "\x32\x36\x65",
  116. "\x5B\x20\x39",
  117. "\x76\x7C\x5C",
  118. "\x72\x7A\x56",
  119. "\x43\x7F\x73",
  120. "\x38\x6B\x66",
  121. "\x39\x63\x4E",
  122. "\x70\x33\x45",
  123. "\x45\x2B\x6B",
  124. "\x68\x68\x62",
  125. "\x71\x51\x59",
  126. "\x4F\x66\x78",
  127. "\x09\x76\x5E",
  128. "\x62\x31\x7D",
  129. "\x44\x64\x4A",
  130. "\x23\x54\x6D",
  131. "\x75\x43\x71",
  132. "\x4A\x4C\x41",
  133. "\x7E\x3A\x60",
  134. "\x4A\x4C\x41",
  135. "\x5E\x7E\x53",
  136. "\x40\x4C\x40",
  137. "\x77\x45\x42",
  138. "\x4A\x2C\x27",
  139. "\x61\x2A\x48",
  140. "\x5D\x74\x72",
  141. "\x22\x27\x75",
  142. "\x4B\x37\x31",
  143. "\x6F\x44\x37",
  144. "\x4E\x79\x4D",
  145. "\x3B\x59\x52",
  146. "\x4C\x2F\x22",
  147. "\x50\x6F\x54",
  148. "\x67\x26\x6A",
  149. "\x2A\x72\x47",
  150. "\x7D\x6A\x64",
  151. "\x74\x39\x2D",
  152. "\x54\x7B\x20",
  153. "\x2B\x3F\x7F",
  154. "\x2D\x38\x2E",
  155. "\x2C\x77\x4C",
  156. "\x30\x67\x5D",
  157. "\x6E\x53\x7E",
  158. "\x6B\x47\x6C",
  159. "\x66\x34\x6F",
  160. "\x35\x78\x79",
  161. "\x25\x5D\x74",
  162. "\x21\x30\x43",
  163. "\x64\x23\x26",
  164. "\x4D\x5A\x76",
  165. "\x52\x5B\x25",
  166. "\x63\x6C\x24",
  167. "\x3F\x48\x2B",
  168. "\x7B\x55\x28",
  169. "\x78\x70\x23",
  170. "\x29\x69\x41",
  171. "\x28\x2E\x34",
  172. "\x73\x4C\x09",
  173. "\x59\x21\x2A",
  174. "\x33\x24\x44",
  175. "\x7F\x4E\x3F",
  176. "\x6D\x50\x77",
  177. "\x55\x09\x3B",
  178. "\x53\x56\x55",
  179. "\x7C\x73\x69",
  180. "\x3A\x35\x61",
  181. "\x5F\x61\x63",
  182. "\x65\x4B\x50",
  183. "\x46\x58\x67",
  184. "\x58\x3B\x51",
  185. "\x31\x57\x49",
  186. "\x69\x22\x4F",
  187. "\x6C\x6D\x46",
  188. "\x5A\x4D\x68",
  189. "\x48\x25\x7C",
  190. "\x27\x28\x36",
  191. "\x5C\x46\x70",
  192. "\x3D\x4A\x6E",
  193. "\x24\x32\x7A",
  194. "\x79\x41\x2F",
  195. "\x37\x3D\x5F",
  196. "\x60\x5F\x4B",
  197. "\x51\x4F\x5A",
  198. "\x20\x42\x2C",
  199. "\x36\x65\x57"
  200. ];
  201. const D_COMBINATION = [
  202. 0, 1, 2, 0, 1, 2, 1, 2, 2, 1, 2, 1, 0, 2, 1, 2, 0, 2, 1, 2, 0, 0, 1, 2, 2, 1, 0, 2, 1, 2, 2, 1,
  203. 0, 0, 2, 1, 2, 1, 2, 0, 2, 0, 0, 1, 2, 0, 2, 1, 0, 2, 1, 2, 0, 0, 1, 2, 2, 0, 0, 1, 2, 0, 2, 1
  204. ];
  205. export default MicrosoftScriptDecoder;