exeHandler.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. Copyright 2018-2022 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*xjslint node: true */
  14. /*xjslint plusplus: true */
  15. /*xjslint maxlen: 256 */
  16. /*jshint node: true */
  17. /*jshint strict: false */
  18. /*jshint esversion: 6 */
  19. "use strict";
  20. const exeJavaScriptGuid = 'B996015880544A19B7F7E9BE44914C18';
  21. const exeMeshPolicyGuid = 'B996015880544A19B7F7E9BE44914C19';
  22. const exeNullPolicyGuid = 'B996015880544A19B7F7E9BE44914C20';
  23. // Changes a Windows Executable to add JavaScript inside of it.
  24. // This method will write to destination stream and close it.
  25. //
  26. // options = {
  27. // platform: 'win32' or 'linux',
  28. // sourceFileName: 'pathToBinary',
  29. // destinationStream: 'outputStream'
  30. // js: 'jsContent',
  31. // peinfo {} // Optional, if PE header already parsed place it here.
  32. // }
  33. //
  34. module.exports.streamExeWithJavaScript = function (options) {
  35. // Check all inputs
  36. if (!options.platform) { throw ('platform not specified'); }
  37. if (!options.destinationStream) { throw ('destination stream was not specified'); }
  38. if (!options.sourceFileName) { throw ('source file not specified'); }
  39. if (!options.js) { throw ('js content not specified'); }
  40. // If a Windows binary, parse it if not already parsed
  41. if ((options.platform == 'win32') && (!options.peinfo)) { options.peinfo = module.exports.parseWindowsExecutable(options.sourceFileName); }
  42. // If unsigned Windows or Linux, we merge at the end with the GUID and no padding.
  43. if (((options.platform == 'win32') && (options.peinfo.CertificateTableAddress == 0)) || (options.platform != 'win32')) {
  44. // This is not a signed binary, so we can just send over the EXE then the MSH
  45. options.destinationStream.sourceStream = require('fs').createReadStream(options.sourceFileName, { flags: 'r' });
  46. options.destinationStream.sourceStream.options = options;
  47. options.destinationStream.sourceStream.on('end', function () {
  48. // Once the binary is streamed, write the msh + length + guid in that order.
  49. this.options.destinationStream.write(this.options.js); // JS content
  50. var sz = Buffer.alloc(4);
  51. sz.writeUInt32BE(this.options.js.length, 0);
  52. this.options.destinationStream.write(sz); // Length in small endian
  53. this.options.destinationStream.end(Buffer.from(exeJavaScriptGuid, 'hex')); // GUID
  54. });
  55. // Pipe the entire source binary without ending the stream.
  56. options.destinationStream.sourceStream.pipe(options.destinationStream, { end: false });
  57. } else {
  58. throw ('streamExeWithJavaScript(): Cannot stream JavaScript with signed executable.');
  59. }
  60. };
  61. // Changes a Windows Executable to add the MSH inside of it.
  62. // This method will write to destination stream and close it.
  63. //
  64. // options = {
  65. // platform: 'win32' or 'linux',
  66. // sourceFileName: 'pathToBinary',
  67. // destinationStream: 'outputStream'
  68. // msh: 'mshContent',
  69. // randomPolicy: true, // Set is the MSH contains random data
  70. // peinfo {} // Optional, if PE header already parsed place it here.
  71. // }
  72. //
  73. module.exports.streamExeWithMeshPolicy = function (options) {
  74. // Check all inputs
  75. if (!options.platform) { throw ('platform not specified'); }
  76. if (!options.destinationStream) { throw ('destination stream was not specified'); }
  77. if (!options.sourceFileName) { throw ('source file not specified'); }
  78. if (!options.msh) { throw ('msh content not specified'); }
  79. options.mshbuf = Buffer.from(options.msh, 'utf8');
  80. // If a Windows binary, parse it if not already parsed
  81. if ((options.platform == 'win32') && (!options.peinfo)) { options.peinfo = module.exports.parseWindowsExecutable(options.sourceFileName); }
  82. // If unsigned Windows or Linux, we merge at the end with the GUID and no padding.
  83. if ((options.platform == 'win32' && options.peinfo.CertificateTableAddress == 0) || options.platform != 'win32') {
  84. // This is not a signed binary, so we can just send over the EXE then the MSH
  85. options.destinationStream.sourceStream = require('fs').createReadStream(options.sourceFileName, { flags: 'r' });
  86. options.destinationStream.sourceStream.options = options;
  87. options.destinationStream.sourceStream.on('end', function () {
  88. // Once the binary is streamed, write the msh + length + guid in that order.
  89. this.options.destinationStream.write(this.options.mshbuf); // MSH
  90. var sz = Buffer.alloc(4);
  91. sz.writeUInt32BE(this.options.mshbuf.length, 0);
  92. this.options.destinationStream.write(sz); // Length in small endian
  93. this.options.destinationStream.end(Buffer.from((this.options.randomPolicy === true) ? exeNullPolicyGuid : exeMeshPolicyGuid, 'hex')); // Guid
  94. });
  95. // Pipe the entire source binary without ending the stream.
  96. options.destinationStream.sourceStream.pipe(options.destinationStream, { end: false });
  97. } else if (options.platform == 'win32' && options.peinfo.CertificateTableAddress != 0) {
  98. // Read up to the certificate table size and stream that out
  99. options.destinationStream.sourceStream = require('fs').createReadStream(options.sourceFileName, { flags: 'r', start: 0, end: options.peinfo.CertificateTableSizePos - 1 });
  100. options.destinationStream.sourceStream.mshPadding = (8 - ((options.peinfo.certificateDwLength + options.mshbuf.length + 20) % 8)) % 8; // Compute the padding with quad-align
  101. options.destinationStream.sourceStream.CertificateTableSize = (options.peinfo.CertificateTableSize + options.mshbuf.length + 20 + options.destinationStream.sourceStream.mshPadding); // Add to the certificate table size
  102. options.destinationStream.sourceStream.certificateDwLength = (options.peinfo.certificateDwLength + options.mshbuf.length + 20 + options.destinationStream.sourceStream.mshPadding); // Add to the certificate size
  103. options.destinationStream.sourceStream.options = options;
  104. options.destinationStream.sourceStream.on('end', function () {
  105. // We sent up to the CertificateTableSize, now we need to send the updated certificate table size
  106. var sz = Buffer.alloc(4);
  107. sz.writeUInt32LE(this.CertificateTableSize, 0);
  108. this.options.destinationStream.write(sz); // New cert table size
  109. // Stream everything up to the start of the certificate table entry
  110. var source2 = require('fs').createReadStream(options.sourceFileName, { flags: 'r', start: this.options.peinfo.CertificateTableSizePos + 4, end: this.options.peinfo.CertificateTableAddress - 1 });
  111. source2.options = this.options;
  112. source2.mshPadding = this.mshPadding;
  113. source2.certificateDwLength = this.certificateDwLength;
  114. source2.on('end', function () {
  115. // We've sent up to the Certificate DWLength, which we need to update
  116. var sz = Buffer.alloc(4);
  117. sz.writeUInt32LE(this.certificateDwLength, 0);
  118. this.options.destinationStream.write(sz); // New certificate length
  119. // Stream the entire binary until the end
  120. var source3 = require('fs').createReadStream(options.sourceFileName, { flags: 'r', start: this.options.peinfo.CertificateTableAddress + 4 });
  121. source3.options = this.options;
  122. source3.mshPadding = this.mshPadding;
  123. source3.on('end', function () {
  124. // We've sent the entire binary... Now send: Padding + MSH + MSHLength + GUID
  125. if (this.mshPadding > 0) { this.options.destinationStream.write(Buffer.alloc(this.mshPadding)); } // Padding
  126. this.options.destinationStream.write(this.options.mshbuf); // MSH content
  127. var sz = Buffer.alloc(4);
  128. sz.writeUInt32BE(this.options.mshbuf.length, 0);
  129. this.options.destinationStream.write(sz); // MSH Length, small-endian
  130. this.options.destinationStream.end(Buffer.from((this.options.randomPolicy === true) ? exeNullPolicyGuid : exeMeshPolicyGuid, 'hex')); // Guid
  131. });
  132. source3.pipe(this.options.destinationStream, { end: false });
  133. this.options.sourceStream = source3;
  134. });
  135. source2.pipe(this.options.destinationStream, { end: false });
  136. this.options.destinationStream.sourceStream = source2;
  137. });
  138. options.destinationStream.sourceStream.pipe(options.destinationStream, { end: false });
  139. }
  140. };
  141. // Return information about this executable
  142. // This works only on Windows binaries
  143. module.exports.parseWindowsExecutable = function (exePath) {
  144. var retVal = {};
  145. var fs = require('fs');
  146. var fd = fs.openSync(exePath, 'r');
  147. var bytesRead;
  148. var dosHeader = Buffer.alloc(64);
  149. var ntHeader = Buffer.alloc(24);
  150. var optHeader;
  151. var numRVA;
  152. // Read the DOS header
  153. bytesRead = fs.readSync(fd, dosHeader, 0, 64, 0);
  154. if (dosHeader.readUInt16LE(0).toString(16).toUpperCase() != '5A4D') { throw ('unrecognized binary format'); }
  155. // Read the NT header
  156. bytesRead = fs.readSync(fd, ntHeader, 0, ntHeader.length, dosHeader.readUInt32LE(60));
  157. if (ntHeader.slice(0, 4).toString('hex') != '50450000') {
  158. throw ('not a PE file');
  159. }
  160. switch (ntHeader.readUInt16LE(4).toString(16)) {
  161. case '14c': // 32 bit
  162. retVal.format = 'x86';
  163. break;
  164. case '8664': // 64 bit
  165. retVal.format = 'x64';
  166. break;
  167. default: // Unknown
  168. retVal.format = undefined;
  169. break;
  170. }
  171. retVal.optionalHeaderSize = ntHeader.readUInt16LE(20);
  172. retVal.optionalHeaderSizeAddress = dosHeader.readUInt32LE(60) + 20;
  173. // Read the optional header
  174. optHeader = Buffer.alloc(ntHeader.readUInt16LE(20));
  175. bytesRead = fs.readSync(fd, optHeader, 0, optHeader.length, dosHeader.readUInt32LE(60) + 24);
  176. retVal.CheckSumPos = dosHeader.readUInt32LE(60) + 24 + 64;
  177. retVal.SizeOfCode = optHeader.readUInt32LE(4);
  178. retVal.SizeOfInitializedData = optHeader.readUInt32LE(8);
  179. retVal.SizeOfUnInitializedData = optHeader.readUInt32LE(12);
  180. switch (optHeader.readUInt16LE(0).toString(16).toUpperCase()) {
  181. case '10B': // 32 bit binary
  182. numRVA = optHeader.readUInt32LE(92);
  183. retVal.CertificateTableAddress = optHeader.readUInt32LE(128);
  184. retVal.CertificateTableSize = optHeader.readUInt32LE(132);
  185. retVal.CertificateTableSizePos = dosHeader.readUInt32LE(60) + 24 + 132;
  186. retVal.rvaStartAddress = dosHeader.readUInt32LE(60) + 24 + 96;
  187. break;
  188. case '20B': // 64 bit binary
  189. numRVA = optHeader.readUInt32LE(108);
  190. retVal.CertificateTableAddress = optHeader.readUInt32LE(144);
  191. retVal.CertificateTableSize = optHeader.readUInt32LE(148);
  192. retVal.CertificateTableSizePos = dosHeader.readUInt32LE(60) + 24 + 148;
  193. retVal.rvaStartAddress = dosHeader.readUInt32LE(60) + 24 + 112;
  194. break;
  195. default:
  196. throw ('Unknown Value found for Optional Magic: ' + ntHeader.readUInt16LE(24).toString(16).toUpperCase());
  197. }
  198. retVal.rvaCount = numRVA;
  199. if (retVal.CertificateTableAddress) {
  200. // Read the authenticode certificate, only one cert (only the first entry)
  201. var hdr = Buffer.alloc(8);
  202. fs.readSync(fd, hdr, 0, hdr.length, retVal.CertificateTableAddress);
  203. retVal.certificate = Buffer.alloc(hdr.readUInt32LE(0));
  204. fs.readSync(fd, retVal.certificate, 0, retVal.certificate.length, retVal.CertificateTableAddress + hdr.length);
  205. retVal.certificate = retVal.certificate.toString('base64');
  206. retVal.certificateDwLength = hdr.readUInt32LE(0);
  207. }
  208. fs.closeSync(fd);
  209. return (retVal);
  210. };
  211. //
  212. // Hash a executable file. Works on both Windows and Linux.
  213. // On Windows, will hash so that signature or .msh addition will not change the hash. Adding a .js on un-signed executable will change the hash.
  214. //
  215. // options = {
  216. // sourcePath: <string> Executable Path
  217. // targetStream: <stream.writeable> Hashing Stream
  218. // platform: <string> Optional. Same value as process.platform ('win32' | 'linux' | 'darwin')
  219. // }
  220. //
  221. module.exports.hashExecutableFile = function (options) {
  222. if (!options.sourcePath || !options.targetStream) { throw ('Please specify sourcePath and targetStream'); }
  223. var fs = require('fs');
  224. // If not specified, try to determine platform type
  225. if (!options.platform) {
  226. try {
  227. // If we can parse the executable, we know it's windows.
  228. options.peinfo = module.exports.parseWindowsExecutable(options.sourcePath);
  229. options.platform = 'win32';
  230. } catch (e) {
  231. options.platform = 'other';
  232. }
  233. }
  234. // Setup initial state
  235. options.state = { endIndex: 0, checkSumIndex: 0, tableIndex: 0, stats: fs.statSync(options.sourcePath) };
  236. if (options.platform == 'win32') {
  237. if (options.peinfo.CertificateTableAddress != 0) { options.state.endIndex = options.peinfo.CertificateTableAddress; }
  238. options.state.tableIndex = options.peinfo.CertificateTableSizePos - 4;
  239. options.state.checkSumIndex = options.peinfo.CheckSumPos;
  240. }
  241. if (options.state.endIndex == 0) {
  242. // We just need to check for Embedded MSH file
  243. var fd = fs.openSync(options.sourcePath, 'r');
  244. var guid = Buffer.alloc(16);
  245. var bytesRead;
  246. bytesRead = fs.readSync(fd, guid, 0, guid.length, options.state.stats.size - 16);
  247. if (guid.toString('hex') == exeMeshPolicyGuid) {
  248. bytesRead = fs.readSync(fd, guid, 0, 4, options.state.stats.size - 20);
  249. options.state.endIndex = options.state.stats.size - 20 - guid.readUInt32LE(0);
  250. } else {
  251. options.state.endIndex = options.state.stats.size;
  252. }
  253. fs.closeSync(fd);
  254. }
  255. // Linux does not have a checksum
  256. if (options.state.checkSumIndex != 0) {
  257. // Windows
  258. options.state.source = fs.createReadStream(options.sourcePath, { flags: 'r', start: 0, end: options.state.checkSumIndex - 1 });
  259. options.state.source.on('end', function () {
  260. options.targetStream.write(Buffer.alloc(4));
  261. var source = fs.createReadStream(options.sourcePath, { flags: 'r', start: options.state.checkSumIndex + 4, end: options.state.tableIndex - 1 });
  262. source.on('end', function () {
  263. options.targetStream.write(Buffer.alloc(8));
  264. var source = fs.createReadStream(options.sourcePath, { flags: 'r', start: options.state.tableIndex + 8, end: options.state.endIndex - 1 });
  265. options.state.source = source;
  266. options.state.source.pipe(options.targetStream);
  267. });
  268. options.state.source = source;
  269. options.state.source.pipe(options.targetStream, { end: false });
  270. });
  271. options.state.source.pipe(options.targetStream, { end: false });
  272. } else {
  273. // Linux
  274. options.state.source = fs.createReadStream(options.sourcePath, { flags: 'r', start: 0, end: options.state.endIndex - 1 });
  275. options.state.source.pipe(options.targetStream);
  276. }
  277. };