authenticode.js 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /**
  2. * @description Authenticode parsing
  3. * @author Ylian Saint-Hilaire & Bryan Roe
  4. * @copyright Intel Corporation 2018-2022
  5. * @license Apache-2.0
  6. * @version v0.0.1
  7. */
  8. /*jslint node: true */
  9. /*jshint node: true */
  10. /*jshint strict:false */
  11. /*jshint -W097 */
  12. /*jshint esversion: 6 */
  13. "use strict";
  14. const fs = require('fs');
  15. const crypto = require('crypto');
  16. const forge = require('node-forge');
  17. const pki = forge.pki;
  18. const p7 = require('./pkcs7-modified');
  19. // Generate a test self-signed certificate with code signing extension
  20. function createSelfSignedCert(args) {
  21. var keys = pki.rsa.generateKeyPair(2048);
  22. var cert = pki.createCertificate();
  23. cert.publicKey = keys.publicKey;
  24. cert.serialNumber = (typeof args.serial == 'string')?args.serial:'012345'; // Serial number must always have a single leading '0', otherwise toPEM/fromPEM will not work right.
  25. cert.validity.notBefore = new Date();
  26. cert.validity.notAfter = new Date();
  27. cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 10);
  28. var attrs = [];
  29. if (typeof args.cn == 'string') { attrs.push({ name: 'commonName', value: args.cn }); }
  30. if (typeof args.country == 'string') { attrs.push({ name: 'countryName', value: args.country }); }
  31. if (typeof args.state == 'string') { attrs.push({ name: 'ST', value: args.state }); }
  32. if (typeof args.locality == 'string') { attrs.push({ name: 'localityName', value: args.locality }); }
  33. if (typeof args.org == 'string') { attrs.push({ name: 'organizationName', value: args.org }); }
  34. if (typeof args.orgunit == 'string') { attrs.push({ name: 'OU', value: args.orgunit }); }
  35. cert.setSubject(attrs);
  36. cert.setIssuer(attrs);
  37. cert.setExtensions([{ name: 'basicConstraints', cA: false }, { name: 'keyUsage', keyCertSign: false, digitalSignature: true, nonRepudiation: false, keyEncipherment: false, dataEncipherment: false }, { name: 'extKeyUsage', codeSigning: true }, { name: "subjectKeyIdentifier" }]);
  38. cert.sign(keys.privateKey, forge.md.sha384.create());
  39. return { cert: cert, key: keys.privateKey, extraCerts: [] };
  40. }
  41. // Create the output filename if not already specified
  42. function createOutFile(args, filename) {
  43. if (typeof args.out == 'string') return;
  44. var outputFileName = filename.split('.');
  45. outputFileName[outputFileName.length - 2] += '-out';
  46. args.out = outputFileName.join('.');
  47. }
  48. // Hash an object
  49. function hashObject(obj) {
  50. if (obj == null) { return null; }
  51. const hash = crypto.createHash('sha384');
  52. if (Buffer.isBuffer(obj)) { hash.update(obj); } else { hash.update(JSON.stringify(obj)); }
  53. return hash.digest().toString('hex');
  54. }
  55. // Load a .bmp file.
  56. function loadBitmap(bitmapFile) {
  57. var bitmapData = null;
  58. try { bitmapData = fs.readFileSync(bitmapFile); } catch (ex) { }
  59. if ((bitmapData == null) || (bitmapData.length < 14) || (bitmapData[0] != 0x42) || (bitmapData[1] != 0x4D)) return null;
  60. return bitmapData.slice(14);
  61. }
  62. // Load a .ico file. This will load all icons in the file into a icon group object
  63. function loadIcon(iconFile) {
  64. var iconData = null;
  65. try { iconData = fs.readFileSync(iconFile); } catch (ex) { }
  66. if ((iconData == null) || (iconData.length < 6) || (iconData[0] != 0) || (iconData[1] != 0)) return null;
  67. const r = { resType: iconData.readUInt16LE(2), resCount: iconData.readUInt16LE(4), icons: {} };
  68. if (r.resType != 1) return null;
  69. var ptr = 6;
  70. for (var i = 1; i <= r.resCount; i++) {
  71. var icon = {};
  72. icon.width = iconData[ptr + 0];
  73. icon.height = iconData[ptr + 1];
  74. icon.colorCount = iconData[ptr + 2];
  75. icon.planes = iconData.readUInt16LE(ptr + 4);
  76. icon.bitCount = iconData.readUInt16LE(ptr + 6);
  77. icon.bytesInRes = iconData.readUInt32LE(ptr + 8);
  78. icon.iconCursorId = i;
  79. const offset = iconData.readUInt32LE(ptr + 12);
  80. icon.icon = iconData.slice(offset, offset + icon.bytesInRes);
  81. r.icons[i] = icon;
  82. ptr += 16;
  83. }
  84. return r;
  85. }
  86. // Load certificates and private key from PEM files
  87. function loadCertificates(pemFileNames) {
  88. var certs = [], keys = [];
  89. if (pemFileNames == null) return;
  90. if (typeof pemFileNames == 'string') { pemFileNames = [pemFileNames]; }
  91. for (var i in pemFileNames) {
  92. try {
  93. // Read certificate
  94. var pem = fs.readFileSync(pemFileNames[i]).toString();
  95. var pemCerts = pem.split('-----BEGIN CERTIFICATE-----');
  96. for (var j in pemCerts) {
  97. var k = pemCerts[j].indexOf('-----END CERTIFICATE-----');
  98. if (k >= 0) { certs.push(pki.certificateFromPem('-----BEGIN CERTIFICATE-----' + pemCerts[j].substring(0, k) + '-----END CERTIFICATE-----')); }
  99. }
  100. var PemKeys = pem.split('-----BEGIN RSA PRIVATE KEY-----');
  101. for (var j in PemKeys) {
  102. var k = PemKeys[j].indexOf('-----END RSA PRIVATE KEY-----');
  103. if (k >= 0) { keys.push(pki.privateKeyFromPem('-----BEGIN RSA PRIVATE KEY-----' + PemKeys[j].substring(0, k) + '-----END RSA PRIVATE KEY-----')); }
  104. }
  105. PemKeys = pem.split('-----BEGIN PRIVATE KEY-----');
  106. for (var j in PemKeys) {
  107. var k = PemKeys[j].indexOf('-----END PRIVATE KEY-----');
  108. if (k >= 0) { keys.push(pki.privateKeyFromPem('-----BEGIN PRIVATE KEY-----' + PemKeys[j].substring(0, k) + '-----END PRIVATE KEY-----')); }
  109. }
  110. } catch (ex) { }
  111. }
  112. if ((certs.length == 0) || (keys.length != 1)) return; // No certificates or private keys
  113. var r = { cert: certs[0], key: keys[0], extraCerts: [] }
  114. if (certs.length > 1) { for (var i = 1; i < certs.length; i++) { r.extraCerts.push(certs[i]); } }
  115. return r;
  116. }
  117. function createAuthenticodeHandler(path) {
  118. const obj = {};
  119. obj.header = { path: path }
  120. // Read a file slice
  121. function readFileSlice(start, length) {
  122. var buffer = Buffer.alloc(length);
  123. var len = fs.readSync(obj.fd, buffer, 0, buffer.length, start);
  124. if (len < buffer.length) { buffer = buffer.slice(0, len); }
  125. return buffer;
  126. }
  127. // Close the file
  128. obj.close = function () {
  129. if (obj.fd == null) return;
  130. fs.closeSync(obj.fd);
  131. delete obj.fd;
  132. }
  133. // Private OIDS
  134. obj.Oids = {
  135. SPC_INDIRECT_DATA_OBJID: '1.3.6.1.4.1.311.2.1.4',
  136. SPC_STATEMENT_TYPE_OBJID: '1.3.6.1.4.1.311.2.1.11',
  137. SPC_SP_OPUS_INFO_OBJID: '1.3.6.1.4.1.311.2.1.12',
  138. SPC_INDIVIDUAL_SP_KEY_PURPOSE_OBJID: '1.3.6.1.4.1.311.2.1.21',
  139. SPC_COMMERCIAL_SP_KEY_PURPOSE_OBJID: '1.3.6.1.4.1.311.2.1.22',
  140. SPC_MS_JAVA_SOMETHING: '1.3.6.1.4.1.311.15.1',
  141. SPC_PE_IMAGE_DATA_OBJID: '1.3.6.1.4.1.311.2.1.15',
  142. SPC_CAB_DATA_OBJID: '1.3.6.1.4.1.311.2.1.25',
  143. SPC_TIME_STAMP_REQUEST_OBJID: '1.3.6.1.4.1.311.3.2.1',
  144. SPC_SIPINFO_OBJID: '1.3.6.1.4.1.311.2.1.30',
  145. SPC_PE_IMAGE_PAGE_HASHES_V1: '1.3.6.1.4.1.311.2.3.1',
  146. SPC_PE_IMAGE_PAGE_HASHES_V2: '1.3.6.1.4.1.311.2.3.2',
  147. SPC_NESTED_SIGNATURE_OBJID: '1.3.6.1.4.1.311.2.4.1',
  148. SPC_RFC3161_OBJID: '1.3.6.1.4.1.311.3.3.1'
  149. }
  150. // Open the file and read header information
  151. function openFile() {
  152. if (obj.fd != null) return true;
  153. // Open the file descriptor
  154. obj.path = path;
  155. try { obj.fd = fs.openSync(path, 'r'); } catch (ex) { return false; } // Unable to open file
  156. obj.stats = fs.fstatSync(obj.fd);
  157. obj.filesize = obj.stats.size;
  158. if (obj.filesize < 64) { obj.close(); return false; } // File too short.
  159. // Read the DOS header (64 bytes)
  160. var buf = readFileSlice(60, 4);
  161. obj.header.peHeaderLocation = buf.readUInt32LE(0); // The DOS header is 64 bytes long, the last 4 bytes are a pointer to the PE header.
  162. obj.header.peOptionalHeaderLocation = obj.header.peHeaderLocation + 24; // The PE optional header is located just after the PE header which is 24 bytes long.
  163. // Check file size and signature
  164. if (obj.filesize < (160 + obj.header.peHeaderLocation)) { obj.close(); return false; } // Invalid SizeOfHeaders.
  165. if (readFileSlice(obj.header.peHeaderLocation, 4).toString('hex') != '50450000') { obj.close(); return false; } // Invalid PE header, must start with "PE" (HEX: 50 45 00 00).
  166. // Read the COFF header
  167. // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#coff-file-header-object-and-image
  168. var coffHeader = readFileSlice(obj.header.peHeaderLocation + 4, 20)
  169. obj.header.coff = {};
  170. obj.header.coff.machine = coffHeader.readUInt16LE(0);
  171. obj.header.coff.numberOfSections = coffHeader.readUInt16LE(2);
  172. obj.header.coff.timeDateStamp = coffHeader.readUInt32LE(4);
  173. obj.header.coff.pointerToSymbolTable = coffHeader.readUInt32LE(8);
  174. obj.header.coff.numberOfSymbols = coffHeader.readUInt32LE(12);
  175. obj.header.coff.sizeOfOptionalHeader = coffHeader.readUInt16LE(16);
  176. obj.header.coff.characteristics = coffHeader.readUInt16LE(18);
  177. // Read the entire PE optional header
  178. var optinalHeader = readFileSlice(obj.header.peOptionalHeaderLocation, obj.header.coff.sizeOfOptionalHeader);
  179. // Decode the PE optional header standard fields
  180. // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#optional-header-standard-fields-image-only
  181. obj.header.peStandard = {};
  182. obj.header.peStandard.magic = optinalHeader.readUInt16LE(0);
  183. switch (obj.header.peStandard.magic) { // Check magic value
  184. case 0x020B: obj.header.pe32plus = 1; break;
  185. case 0x010B: obj.header.pe32plus = 0; break;
  186. default: { obj.close(); return false; } // Invalid Magic in PE
  187. }
  188. obj.header.peStandard.majorLinkerVersion = optinalHeader[2];
  189. obj.header.peStandard.minorLinkerVersion = optinalHeader[3];
  190. obj.header.peStandard.sizeOfCode = optinalHeader.readUInt32LE(4);
  191. obj.header.peStandard.sizeOfInitializedData = optinalHeader.readUInt32LE(8);
  192. obj.header.peStandard.sizeOfUninitializedData = optinalHeader.readUInt32LE(12);
  193. obj.header.peStandard.addressOfEntryPoint = optinalHeader.readUInt32LE(16);
  194. obj.header.peStandard.baseOfCode = optinalHeader.readUInt32LE(20);
  195. if (obj.header.pe32plus == 0) { obj.header.peStandard.baseOfData = optinalHeader.readUInt32LE(24); }
  196. // Decode the PE optional header windows fields
  197. // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#optional-header-windows-specific-fields-image-only
  198. obj.header.peWindows = {}
  199. if (obj.header.pe32plus == 0) {
  200. // 32bit header
  201. //obj.header.peWindows.imageBase = optinalHeader.readUInt32LE(28);
  202. obj.header.peWindows.sectionAlignment = optinalHeader.readUInt32LE(32);
  203. obj.header.peWindows.fileAlignment = optinalHeader.readUInt32LE(36);
  204. obj.header.peWindows.majorOperatingSystemVersion = optinalHeader.readUInt16LE(40);
  205. obj.header.peWindows.minorOperatingSystemVersion = optinalHeader.readUInt16LE(42);
  206. obj.header.peWindows.majorImageVersion = optinalHeader.readUInt16LE(44);
  207. obj.header.peWindows.minorImageVersion = optinalHeader.readUInt16LE(46);
  208. obj.header.peWindows.majorSubsystemVersion = optinalHeader.readUInt16LE(48);
  209. obj.header.peWindows.minorSubsystemVersion = optinalHeader.readUInt16LE(50);
  210. obj.header.peWindows.win32VersionValue = optinalHeader.readUInt32LE(52);
  211. obj.header.peWindows.sizeOfImage = optinalHeader.readUInt32LE(56);
  212. obj.header.peWindows.sizeOfHeaders = optinalHeader.readUInt32LE(60);
  213. obj.header.peWindows.checkSum = optinalHeader.readUInt32LE(64);
  214. obj.header.peWindows.subsystem = optinalHeader.readUInt16LE(68);
  215. obj.header.peWindows.dllCharacteristics = optinalHeader.readUInt16LE(70);
  216. //obj.header.peWindows.sizeOfStackReserve = optinalHeader.readUInt32LE(72);
  217. //obj.header.peWindows.sizeOfStackCommit = optinalHeader.readUInt32LE(76);
  218. //obj.header.peWindows.sizeOfHeapReserve = optinalHeader.readUInt32LE(80);
  219. //obj.header.peWindows.sizeOfHeapCommit = optinalHeader.readUInt32LE(84);
  220. obj.header.peWindows.loaderFlags = optinalHeader.readUInt32LE(88);
  221. obj.header.peWindows.numberOfRvaAndSizes = optinalHeader.readUInt32LE(92);
  222. } else {
  223. // 64bit header
  224. //obj.header.peWindows.imageBase = optinalHeader.readBigUInt64LE(24); // TODO: readBigUInt64LE is not supported in older NodeJS versions
  225. obj.header.peWindows.sectionAlignment = optinalHeader.readUInt32LE(32);
  226. obj.header.peWindows.fileAlignment = optinalHeader.readUInt32LE(36);
  227. obj.header.peWindows.majorOperatingSystemVersion = optinalHeader.readUInt16LE(40);
  228. obj.header.peWindows.minorOperatingSystemVersion = optinalHeader.readUInt16LE(42);
  229. obj.header.peWindows.majorImageVersion = optinalHeader.readUInt16LE(44);
  230. obj.header.peWindows.minorImageVersion = optinalHeader.readUInt16LE(46);
  231. obj.header.peWindows.majorSubsystemVersion = optinalHeader.readUInt16LE(48);
  232. obj.header.peWindows.minorSubsystemVersion = optinalHeader.readUInt16LE(50);
  233. obj.header.peWindows.win32VersionValue = optinalHeader.readUInt32LE(52);
  234. obj.header.peWindows.sizeOfImage = optinalHeader.readUInt32LE(56);
  235. obj.header.peWindows.sizeOfHeaders = optinalHeader.readUInt32LE(60);
  236. obj.header.peWindows.checkSum = optinalHeader.readUInt32LE(64);
  237. obj.header.peWindows.subsystem = optinalHeader.readUInt16LE(68);
  238. obj.header.peWindows.dllCharacteristics = optinalHeader.readUInt16LE(70);
  239. //obj.header.peWindows.sizeOfStackReserve = optinalHeader.readBigUInt64LE(72);
  240. //obj.header.peWindows.sizeOfStackCommit = optinalHeader.readBigUInt64LE(80);
  241. //obj.header.peWindows.sizeOfHeapReserve = optinalHeader.readBigUInt64LE(88);
  242. //obj.header.peWindows.sizeOfHeapCommit = optinalHeader.readBigUInt64LE(96);
  243. obj.header.peWindows.loaderFlags = optinalHeader.readUInt32LE(104);
  244. obj.header.peWindows.numberOfRvaAndSizes = optinalHeader.readUInt32LE(108);
  245. }
  246. // Decode the PE optional header data directories
  247. // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#optional-header-data-directories-image-only
  248. obj.header.dataDirectories = {}
  249. const pePlusOffset = (obj.header.pe32plus == 0) ? 0 : 16; // This header is the same for 32 and 64 bit, but 64bit is offset by 16 bytes.
  250. obj.header.dataDirectories.exportTable = { addr: optinalHeader.readUInt32LE(96 + pePlusOffset), size: optinalHeader.readUInt32LE(100 + pePlusOffset) };
  251. obj.header.dataDirectories.importTable = { addr: optinalHeader.readUInt32LE(104 + pePlusOffset), size: optinalHeader.readUInt32LE(108 + pePlusOffset) };
  252. obj.header.dataDirectories.resourceTable = { addr: optinalHeader.readUInt32LE(112 + pePlusOffset), size: optinalHeader.readUInt32LE(116 + pePlusOffset) }; // Same as .rsrc virtual address & size
  253. obj.header.dataDirectories.exceptionTableAddr = { addr: optinalHeader.readUInt32LE(120 + pePlusOffset), size: optinalHeader.readUInt32LE(124 + pePlusOffset) }; // Same as .pdata virtual address & size
  254. obj.header.dataDirectories.certificateTable = { addr: optinalHeader.readUInt32LE(128 + pePlusOffset), size: optinalHeader.readUInt32LE(132 + pePlusOffset) };
  255. obj.header.dataDirectories.baseRelocationTable = { addr: optinalHeader.readUInt32LE(136 + pePlusOffset), size: optinalHeader.readUInt32LE(140 + pePlusOffset) }; // Same as .reloc virtual address & size
  256. obj.header.dataDirectories.debug = { addr: optinalHeader.readUInt32LE(144 + pePlusOffset), size: optinalHeader.readUInt32LE(148 + pePlusOffset) };
  257. // obj.header.dataDirectories.architecture = optinalHeader.readBigUInt64LE(152 + pePlusOffset); // Must be zero
  258. obj.header.dataDirectories.globalPtr = { addr: optinalHeader.readUInt32LE(160 + pePlusOffset), size: optinalHeader.readUInt32LE(164 + pePlusOffset) };
  259. obj.header.dataDirectories.tLSTable = { addr: optinalHeader.readUInt32LE(168 + pePlusOffset), size: optinalHeader.readUInt32LE(172 + pePlusOffset) };
  260. obj.header.dataDirectories.loadConfigTable = { addr: optinalHeader.readUInt32LE(176 + pePlusOffset), size: optinalHeader.readUInt32LE(180 + pePlusOffset) };
  261. obj.header.dataDirectories.boundImport = { addr: optinalHeader.readUInt32LE(184 + pePlusOffset), size: optinalHeader.readUInt32LE(188 + pePlusOffset) };
  262. obj.header.dataDirectories.iAT = { addr: optinalHeader.readUInt32LE(192 + pePlusOffset), size: optinalHeader.readUInt32LE(196 + pePlusOffset) };
  263. obj.header.dataDirectories.delayImportDescriptor = { addr: optinalHeader.readUInt32LE(200 + pePlusOffset), size: optinalHeader.readUInt32LE(204 + pePlusOffset) };
  264. obj.header.dataDirectories.clrRuntimeHeader = { addr: optinalHeader.readUInt32LE(208 + pePlusOffset), size: optinalHeader.readUInt32LE(212 + pePlusOffset) };
  265. // obj.header.dataDirectories.reserved = optinalHeader.readBigUInt64LE(216 + pePlusOffset); // Must be zero
  266. // Get the certificate table location and size
  267. obj.header.sigpos = obj.header.dataDirectories.certificateTable.addr;
  268. obj.header.siglen = obj.header.dataDirectories.certificateTable.size
  269. obj.header.signed = ((obj.header.sigpos != 0) && (obj.header.siglen != 0));
  270. // The section headers are located after the optional PE header
  271. obj.header.SectionHeadersPtr = obj.header.peOptionalHeaderLocation + obj.header.coff.sizeOfOptionalHeader;
  272. // Read the sections
  273. obj.header.sections = {};
  274. for (var i = 0; i < obj.header.coff.numberOfSections; i++) {
  275. var section = {};
  276. buf = readFileSlice(obj.header.SectionHeadersPtr + (i * 40), 40);
  277. if ((buf[0] != 46) && (buf[0] != 95)) { obj.close(); return false; }; // Name of the section must start with a dot or underscore. If not, something is wrong.
  278. var sectionName = buf.slice(0, 8).toString().trim('\0');
  279. var j = sectionName.indexOf('\0');
  280. if (j >= 0) { sectionName = sectionName.substring(0, j); } // Trim any trailing zeroes
  281. section.ptr = obj.header.SectionHeadersPtr + (i * 40);
  282. section.virtualSize = buf.readUInt32LE(8);
  283. section.virtualAddr = buf.readUInt32LE(12);
  284. section.rawSize = buf.readUInt32LE(16);
  285. section.rawAddr = buf.readUInt32LE(20);
  286. section.relocAddr = buf.readUInt32LE(24);
  287. section.lineNumbers = buf.readUInt32LE(28);
  288. section.relocNumber = buf.readUInt16LE(32);
  289. section.lineNumbersNumber = buf.readUInt16LE(34);
  290. section.characteristics = buf.readUInt32LE(36);
  291. obj.header.sections[sectionName] = section;
  292. }
  293. // Compute the checkSum value for this file
  294. obj.header.peWindows.checkSumActual = runChecksum();
  295. // If there is a .rsrc section, read the resource information and locations
  296. if (obj.header.sections['.rsrc'] != null) {
  297. obj.resources = readResourceTable(obj.header.sections['.rsrc'].rawAddr, 0); // Read all resources recursively
  298. }
  299. if (obj.header.signed) {
  300. // Read signature block
  301. // Check if the file size allows for the signature block
  302. if (obj.filesize < (obj.header.sigpos + obj.header.siglen)) { obj.close(); return false; } // Executable file too short to contain the signature block.
  303. // Remove the padding if needed
  304. var i, pkcs7raw = readFileSlice(obj.header.sigpos + 8, obj.header.siglen - 8);
  305. var derlen = forge.asn1.getBerValueLength(forge.util.createBuffer(pkcs7raw.slice(1, 5))) + 4;
  306. if (derlen != pkcs7raw.length) { pkcs7raw = pkcs7raw.slice(0, derlen); }
  307. // Decode the signature block and check that it's valid
  308. var pkcs7der = null, valid = false;
  309. try { pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(pkcs7raw)); } catch (ex) { }
  310. try { valid = ((pkcs7der != null) && (forge.asn1.derToOid(pkcs7der.value[1].value[0].value[2].value[0].value) == "1.3.6.1.4.1.311.2.1.4")); } catch (ex) { }
  311. if (pkcs7der == null) {
  312. // Can't decode the signature
  313. obj.header.sigpos = 0;
  314. obj.header.siglen = 0;
  315. obj.header.signed = false;
  316. } else {
  317. // To work around ForgeJS PKCS#7 limitation, this may break PKCS7 verify if ForgeJS adds support for it in the future
  318. // Switch content type from "1.3.6.1.4.1.311.2.1.4" to "1.2.840.113549.1.7.1"
  319. pkcs7der.value[1].value[0].value[2].value[0].value = forge.asn1.oidToDer(forge.pki.oids.data).data;
  320. // Decode the PKCS7 message
  321. var pkcs7 = null, pkcs7content = null;
  322. try {
  323. pkcs7 = p7.messageFromAsn1(pkcs7der);
  324. pkcs7content = pkcs7.rawCapture.content.value[0];
  325. } catch (ex) { }
  326. if ((pkcs7 == null) || (pkcs7content == null)) {
  327. // Can't decode the signature
  328. obj.header.sigpos = 0;
  329. obj.header.siglen = 0;
  330. obj.header.signed = false;
  331. } else {
  332. // Verify a PKCS#7 signature
  333. // Verify is not currently supported in node-forge, but if implemented in the future, this code could work.
  334. //var caStore = forge.pki.createCaStore();
  335. //for (var i in obj.certificates) { caStore.addCertificate(obj.certificates[i]); }
  336. // Return is true if all signatures are valid and chain up to a provided CA
  337. //if (!pkcs7.verify(caStore)) { throw ('Executable file has an invalid signature.'); }
  338. // Get the signing attributes
  339. obj.signingAttribs = [];
  340. try {
  341. for (var i in pkcs7.rawCapture.authenticatedAttributes) {
  342. if (
  343. (pkcs7.rawCapture.authenticatedAttributes[i].value != null) &&
  344. (pkcs7.rawCapture.authenticatedAttributes[i].value[0] != null) &&
  345. (pkcs7.rawCapture.authenticatedAttributes[i].value[0].value != null) &&
  346. (pkcs7.rawCapture.authenticatedAttributes[i].value[1] != null) &&
  347. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value != null) &&
  348. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0] != null) &&
  349. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value != null) &&
  350. (forge.asn1.derToOid(pkcs7.rawCapture.authenticatedAttributes[i].value[0].value) == obj.Oids.SPC_SP_OPUS_INFO_OBJID)) {
  351. for (var j in pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value) {
  352. if (
  353. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value[j] != null) &&
  354. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value[j].value != null) &&
  355. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value[j].value[0] != null) &&
  356. (pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value[j].value[0].value != null)
  357. ) {
  358. var v = pkcs7.rawCapture.authenticatedAttributes[i].value[1].value[0].value[j].value[0].value;
  359. if (v.startsWith('http://') || v.startsWith('https://') || ((v.length % 2) == 1)) { obj.signingAttribs.push(v); } else {
  360. var r = ''; // This string value is in UCS2 format, convert it to a normal string.
  361. for (var k = 0; k < v.length; k += 2) { r += String.fromCharCode((v.charCodeAt(k + 8) << 8) + v.charCodeAt(k + 1)); }
  362. obj.signingAttribs.push(r);
  363. }
  364. }
  365. }
  366. }
  367. }
  368. } catch (ex) { }
  369. // Set the certificate chain
  370. obj.certificates = pkcs7.certificates;
  371. // Set the signature
  372. obj.signature = Buffer.from(pkcs7.rawCapture.signature, 'binary');
  373. // Get the file hashing algorithm
  374. var hashAlgoOid = forge.asn1.derToOid(pkcs7content.value[1].value[0].value[0].value);
  375. switch (hashAlgoOid) {
  376. case forge.pki.oids.sha256: { obj.fileHashAlgo = 'sha256'; break; }
  377. case forge.pki.oids.sha384: { obj.fileHashAlgo = 'sha384'; break; }
  378. case forge.pki.oids.sha512: { obj.fileHashAlgo = 'sha512'; break; }
  379. case forge.pki.oids.sha224: { obj.fileHashAlgo = 'sha224'; break; }
  380. case forge.pki.oids.md5: { obj.fileHashAlgo = 'md5'; break; }
  381. }
  382. // Get the signed file hash
  383. obj.fileHashSigned = Buffer.from(pkcs7content.value[1].value[1].value, 'binary')
  384. // Compute the actual file hash
  385. if (obj.fileHashAlgo != null) { obj.fileHashActual = obj.getHash(obj.fileHashAlgo); }
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. // Make a timestamp signature request
  392. obj.timeStampRequest = function (args, func) {
  393. // Create the timestamp request in DER format
  394. const asn1 = forge.asn1;
  395. const pkcs7dataOid = asn1.oidToDer('1.2.840.113549.1.7.1').data;
  396. const microsoftCodeSigningOid = asn1.oidToDer('1.3.6.1.4.1.311.3.2.1').data;
  397. const asn1obj =
  398. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  399. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, microsoftCodeSigningOid),
  400. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  401. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, pkcs7dataOid),
  402. asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
  403. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, obj.signature.toString('binary')) // Signature here
  404. ])
  405. ])
  406. ]);
  407. // Serialize an ASN.1 object to DER format in Base64
  408. const requestBody = Buffer.from(asn1.toDer(asn1obj).data, 'binary').toString('base64');
  409. // Make an HTTP request
  410. const options = { url: args.time, proxy: args.proxy };
  411. // Make a request to the time server
  412. httpRequest(options, requestBody, function (err, data) {
  413. if (err != null) { func(err); return; }
  414. // Decode the timestamp signature block
  415. var timepkcs7der = null;
  416. try { timepkcs7der = forge.asn1.fromDer(forge.util.createBuffer(Buffer.from(data, 'base64').toString('binary'))); } catch (ex) { func("Unable to parse time-stamp response: " + ex); return; }
  417. // Decode the executable signature block
  418. var pkcs7der = null;
  419. try {
  420. var pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(Buffer.from(obj.getRawSignatureBlock(), 'base64').toString('binary')));
  421. // Get the ASN1 certificates used to sign the timestamp and add them to the certs in the PKCS7 of the executable
  422. // TODO: We could look to see if the certificate is already present in the executable
  423. const timeasn1Certs = timepkcs7der.value[1].value[0].value[3].value;
  424. for (var i in timeasn1Certs) { pkcs7der.value[1].value[0].value[3].value.push(timeasn1Certs[i]); }
  425. // Remove any existing time stamp signatures
  426. var newValues = [];
  427. for (var i in pkcs7der.value[1].value[0].value[4].value[0].value) {
  428. const j = pkcs7der.value[1].value[0].value[4].value[0].value[i];
  429. if ((j.tagClass != 128) || (j.type != 1)) { newValues.push(j); } // If this is not a time stamp, add it to out new list.
  430. }
  431. pkcs7der.value[1].value[0].value[4].value[0].value = newValues; // Set the new list
  432. // Get the time signature and add it to the executables PKCS7
  433. const timeasn1Signature = timepkcs7der.value[1].value[0].value[4];
  434. const countersignatureOid = asn1.oidToDer('1.2.840.113549.1.9.6').data;
  435. const asn1obj2 =
  436. asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
  437. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  438. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, countersignatureOid),
  439. timeasn1Signature
  440. ])
  441. ]);
  442. pkcs7der.value[1].value[0].value[4].value[0].value.push(asn1obj2);
  443. // Re-encode the executable signature block
  444. const p7signature = Buffer.from(forge.asn1.toDer(pkcs7der).data, 'binary');
  445. // Open the output file
  446. var output = null;
  447. try { output = fs.openSync(args.out, 'w+'); } catch (ex) { }
  448. if (output == null) return false;
  449. var tmp, written = 0;
  450. var executableSize = obj.header.sigpos ? obj.header.sigpos : this.filesize;
  451. // Compute pre-header length and copy that to the new file
  452. var preHeaderLen = (obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  453. var tmp = readFileSlice(written, preHeaderLen);
  454. fs.writeSync(output, tmp);
  455. written += tmp.length;
  456. // Quad Align the results, adding padding if necessary
  457. var len = executableSize + p7signature.length;
  458. var padding = (8 - ((len) % 8)) % 8;
  459. // Write the signature header
  460. var addresstable = Buffer.alloc(8);
  461. addresstable.writeUInt32LE(executableSize);
  462. addresstable.writeUInt32LE(8 + p7signature.length + padding, 4);
  463. fs.writeSync(output, addresstable);
  464. written += addresstable.length;
  465. // Copy the rest of the file until the start of the signature block
  466. while ((executableSize - written) > 0) {
  467. tmp = readFileSlice(written, Math.min(executableSize - written, 65536));
  468. fs.writeSync(output, tmp);
  469. written += tmp.length;
  470. }
  471. // Write the signature block header and signature
  472. var win = Buffer.alloc(8); // WIN CERTIFICATE Structure
  473. win.writeUInt32LE(p7signature.length + padding + 8); // DWORD length
  474. win.writeUInt16LE(512, 4); // WORD revision
  475. win.writeUInt16LE(2, 6); // WORD type
  476. fs.writeSync(output, win);
  477. fs.writeSync(output, p7signature);
  478. if (padding > 0) { fs.writeSync(output, Buffer.alloc(padding, 0)); }
  479. written += (p7signature.length + padding + 8);
  480. // Compute the checksum and write it in the PE header checksum location
  481. var tmp = Buffer.alloc(4);
  482. tmp.writeUInt32LE(runChecksumOnFile(output, written, ((obj.header.peOptionalHeaderLocation + 64) / 4)));
  483. fs.writeSync(output, tmp, 0, 4, obj.header.peOptionalHeaderLocation + 64);
  484. // Close the file
  485. fs.closeSync(output);
  486. // Indicate we are done
  487. func(null);
  488. } catch (ex) { func('' + ex); return; }
  489. });
  490. }
  491. // Read a resource table.
  492. // ptr: The pointer to the start of the resource section
  493. // offset: The offset start of the resource table to read
  494. function readResourceTable(ptr, offset) {
  495. var buf = readFileSlice(ptr + offset, 16);
  496. var r = {};
  497. r.characteristics = buf.readUInt32LE(0);
  498. r.timeDateStamp = buf.readUInt32LE(4);
  499. r.majorVersion = buf.readUInt16LE(8);
  500. r.minorVersion = buf.readUInt16LE(10);
  501. var numberOfNamedEntries = buf.readUInt16LE(12);
  502. var numberOfIdEntries = buf.readUInt16LE(14);
  503. r.entries = [];
  504. var totalResources = numberOfNamedEntries + numberOfIdEntries;
  505. //console.log('readResourceTable', offset, 16 + (totalResources) * 8, offset + (16 + (totalResources) * 8));
  506. for (var i = 0; i < totalResources; i++) {
  507. buf = readFileSlice(ptr + offset + 16 + (i * 8), 8);
  508. var resource = {};
  509. resource.name = buf.readUInt32LE(0);
  510. var offsetToData = buf.readUInt32LE(4);
  511. if ((resource.name & 0x80000000) != 0) {
  512. var oname = resource.name;
  513. resource.name = readLenPrefixUnicodeString(ptr + (resource.name - 0x80000000));
  514. //console.log('readResourceName', offset + (oname - 0x80000000), 2 + (resource.name.length * 2), offset + (oname - 0x80000000) + (2 + resource.name.length * 2), resource.name);
  515. }
  516. if ((offsetToData & 0x80000000) != 0) { resource.table = readResourceTable(ptr, offsetToData - 0x80000000); } else { resource.item = readResourceItem(ptr, offsetToData); }
  517. r.entries.push(resource);
  518. }
  519. return r;
  520. }
  521. // Read a resource item
  522. // ptr: The pointer to the start of the resource section
  523. // offset: The offset start of the resource item to read
  524. function readResourceItem(ptr, offset) {
  525. //console.log('readResourceItem', offset, 16, offset + 16);
  526. var buf = readFileSlice(ptr + offset, 16), r = {};
  527. r.offsetToData = buf.readUInt32LE(0);
  528. r.size = buf.readUInt32LE(4);
  529. //console.log('readResourceData', r.offsetToData - obj.header.sections['.rsrc'].virtualAddr, r.size, r.offsetToData + r.size - obj.header.sections['.rsrc'].virtualAddr);
  530. r.codePage = buf.readUInt32LE(8);
  531. //r.reserved = buf.readUInt32LE(12);
  532. return r;
  533. }
  534. // Read a unicode stting that starts with the string length as the first byte.
  535. function readLenPrefixUnicodeString(ptr) {
  536. var nameLen = readFileSlice(ptr, 2).readUInt16LE(0);
  537. var buf = readFileSlice(ptr + 2, nameLen * 2), name = '';
  538. for (var i = 0; i < nameLen; i++) { name += String.fromCharCode(buf.readUInt16LE(i * 2)); }
  539. return name;
  540. }
  541. // Generate a complete resource section and pad the section
  542. function generateResourceSection(resources) {
  543. // Call a resursive method the compute the size needed for each element
  544. const resSizes = { tables: 0, items: 0, names: 0, data: 0 };
  545. getResourceSectionSize(resources, resSizes);
  546. // Pad the resource section & allocate the buffer
  547. const fileAlign = obj.header.peWindows.fileAlignment
  548. var resSizeTotal = resSizes.tables + resSizes.items + resSizes.names + resSizes.data;
  549. var resNoPadding = resSizeTotal + 4; // TODO: Not sure why this is off by 4
  550. if ((resSizeTotal % fileAlign) != 0) { resSizeTotal += (fileAlign - (resSizeTotal % fileAlign)); }
  551. const resSectionBuffer = Buffer.alloc(resSizeTotal);
  552. // Write the resource section, calling a recursive method
  553. const resPointers = { tables: 0, items: resSizes.tables, names: resSizes.tables + resSizes.items, data: resSizes.tables + resSizes.items + resSizes.names };
  554. createResourceSection(resources, resSectionBuffer, resPointers);
  555. //console.log('generateResourceSection', resPointers);
  556. // Done, return the result
  557. return { size: resNoPadding, data: resSectionBuffer };
  558. }
  559. // Return the total size of a resource header, this is a recursive method
  560. function getResourceSectionSize(resources, sizes) {
  561. sizes.tables += (16 + (resources.entries.length * 8));
  562. for (var i in resources.entries) {
  563. if (typeof resources.entries[i].name == 'string') {
  564. var dataSize = (2 + (resources.entries[i].name.length * 2));
  565. if ((dataSize % 8) != 0) { dataSize += (8 - (dataSize % 8)); }
  566. sizes.names += dataSize;
  567. }
  568. if (resources.entries[i].table) { getResourceSectionSize(resources.entries[i].table, sizes); }
  569. else if (resources.entries[i].item) {
  570. sizes.items += 16;
  571. if (resources.entries[i].item.buffer) {
  572. sizes.data += resources.entries[i].item.buffer.length;
  573. } else {
  574. var dataSize = resources.entries[i].item.size;
  575. if ((dataSize % 8) != 0) { dataSize += (8 - (dataSize % 8)); }
  576. sizes.data += dataSize;
  577. }
  578. }
  579. }
  580. }
  581. // Write the resource section in the buffer, this is a recursive method
  582. function createResourceSection(resources, buf, resPointers) {
  583. var numberOfNamedEntries = 0, numberOfIdEntries = 0, ptr = resPointers.tables;
  584. //console.log('createResourceSection', resPointers, ptr);
  585. // Figure out how many items we have to save
  586. for (var i in resources.entries) {
  587. if (typeof resources.entries[i].name == 'string') { numberOfNamedEntries++; } else { numberOfIdEntries++; }
  588. }
  589. // Move the table pointer forward
  590. resPointers.tables += (16 + (8 * numberOfNamedEntries) + (8 * numberOfIdEntries));
  591. // Write the table header
  592. buf.writeUInt32LE(resources.characteristics, ptr);
  593. buf.writeUInt32LE(resources.timeDateStamp, ptr + 4);
  594. buf.writeUInt16LE(resources.majorVersion, ptr + 8);
  595. buf.writeUInt16LE(resources.minorVersion, ptr + 10);
  596. buf.writeUInt16LE(numberOfNamedEntries, ptr + 12);
  597. buf.writeUInt16LE(numberOfIdEntries, ptr + 14);
  598. // For each table entry, write the entry for it
  599. for (var i in resources.entries) {
  600. // Write the name
  601. var name = resources.entries[i].name;
  602. if (typeof resources.entries[i].name == 'string') {
  603. // Set the pointer to the name
  604. name = resPointers.names + 0x80000000;
  605. // Write the name length, followed by the name string in unicode
  606. buf.writeUInt16LE(resources.entries[i].name.length, resPointers.names);
  607. for (var j = 0; j < resources.entries[i].name.length; j++) {
  608. buf.writeUInt16LE(resources.entries[i].name.charCodeAt(j), 2 + resPointers.names + (j * 2));
  609. }
  610. // Move the names pointer forward, 8 byte align
  611. var dataSize = (2 + (resources.entries[i].name.length * 2));
  612. if ((dataSize % 8) != 0) { dataSize += (8 - (dataSize % 8)); }
  613. resPointers.names += dataSize;
  614. }
  615. buf.writeUInt32LE(name, ptr + 16 + (i * 8));
  616. // Write the data
  617. var data;
  618. if (resources.entries[i].table) {
  619. // This is a pointer to a table entry
  620. data = resPointers.tables + 0x80000000;
  621. createResourceSection(resources.entries[i].table, buf, resPointers);
  622. } else if (resources.entries[i].item) {
  623. // This is a pointer to a data entry
  624. data = resPointers.items;
  625. // Write the data
  626. var entrySize = 0;
  627. if (resources.entries[i].item.buffer) {
  628. // Write the data from given buffer
  629. resources.entries[i].item.buffer.copy(buf, resPointers.data, 0, resources.entries[i].item.buffer.length);
  630. entrySize = resources.entries[i].item.buffer.length;
  631. } else {
  632. // Write the data from original file
  633. const actualPtr = (resources.entries[i].item.offsetToData - obj.header.sections['.rsrc'].virtualAddr) + obj.header.sections['.rsrc'].rawAddr;
  634. const tmp = readFileSlice(actualPtr, resources.entries[i].item.size);
  635. tmp.copy(buf, resPointers.data, 0, tmp.length);
  636. entrySize = resources.entries[i].item.size;;
  637. }
  638. // Write the item entry
  639. buf.writeUInt32LE(resPointers.data + obj.header.sections['.rsrc'].virtualAddr, resPointers.items); // Write the pointer relative to the virtual address
  640. buf.writeUInt32LE(entrySize, resPointers.items + 4);
  641. buf.writeUInt32LE(resources.entries[i].item.codePage, resPointers.items + 8);
  642. buf.writeUInt32LE(resources.entries[i].item.reserved, resPointers.items + 12);
  643. // Move items pointers forward
  644. resPointers.items += 16;
  645. var dataSize = entrySize;
  646. if ((dataSize % 8) != 0) { dataSize += (8 - (dataSize % 8)); }
  647. resPointers.data += dataSize;
  648. }
  649. buf.writeUInt32LE(data, ptr + 20 + (i * 8));
  650. }
  651. }
  652. // Convert a unicode buffer to a string
  653. function unicodeToString(buf) {
  654. var r = '', c;
  655. for (var i = 0; i < (buf.length / 2) ; i++) {
  656. c = buf.readUInt16LE(i * 2);
  657. if (c != 0) { r += String.fromCharCode(c); } else { return r; }
  658. }
  659. return r;
  660. }
  661. // Convert a string to a unicode buffer
  662. // Input is a string, a buffer to write to and the offset in the buffer (0 is default).
  663. function stringToUnicode(str, buf, offset) {
  664. if (offset == null) { offset = 0; }
  665. for (var i = 0; i < str.length; i++) { buf.writeInt16LE(str.charCodeAt(i), offset + (i * 2)); }
  666. }
  667. var resourceDefaultNames = {
  668. 'bitmaps': 2,
  669. 'icon': 3,
  670. 'dialogs': 5,
  671. 'iconGroups': 14,
  672. 'versionInfo': 16,
  673. 'configurationFiles': 24
  674. }
  675. // Return the raw signature block buffer with padding removed
  676. obj.getRawSignatureBlock = function () {
  677. if ((obj.header.sigpos == 0) || (obj.header.siglen == 0)) return null;
  678. var pkcs7raw = readFileSlice(obj.header.sigpos + 8, obj.header.siglen - 8);
  679. var derlen = forge.asn1.getBerValueLength(forge.util.createBuffer(pkcs7raw.slice(1, 5))) + 4;
  680. if (derlen != pkcs7raw.length) { pkcs7raw = pkcs7raw.slice(0, derlen); }
  681. return pkcs7raw;
  682. }
  683. // Get bitmaps information from resource
  684. obj.getBitmapInfo = function () {
  685. const r = {}, ptr = obj.header.sections['.rsrc'].rawAddr;
  686. // Find and parse each icon
  687. const bitmaps = {}
  688. for (var i = 0; i < obj.resources.entries.length; i++) {
  689. if (obj.resources.entries[i].name == resourceDefaultNames.bitmaps) {
  690. for (var j = 0; j < obj.resources.entries[i].table.entries.length; j++) {
  691. const bitmapName = obj.resources.entries[i].table.entries[j].name;
  692. const offsetToData = obj.resources.entries[i].table.entries[j].table.entries[0].item.offsetToData;
  693. const size = obj.resources.entries[i].table.entries[j].table.entries[0].item.size;
  694. const actualPtr = (offsetToData - obj.header.sections['.rsrc'].virtualAddr) + ptr;
  695. bitmaps[bitmapName] = readFileSlice(actualPtr, size);
  696. }
  697. }
  698. }
  699. return bitmaps;
  700. }
  701. // Get icon information from resource
  702. obj.getIconInfo = function () {
  703. const r = {}, ptr = obj.header.sections['.rsrc'].rawAddr;
  704. // Find and parse each icon
  705. const icons = {}
  706. for (var i = 0; i < obj.resources.entries.length; i++) {
  707. if (obj.resources.entries[i].name == resourceDefaultNames.icon) {
  708. for (var j = 0; j < obj.resources.entries[i].table.entries.length; j++) {
  709. const iconName = obj.resources.entries[i].table.entries[j].name;
  710. const offsetToData = obj.resources.entries[i].table.entries[j].table.entries[0].item.offsetToData;
  711. const size = obj.resources.entries[i].table.entries[j].table.entries[0].item.size;
  712. const actualPtr = (offsetToData - obj.header.sections['.rsrc'].virtualAddr) + ptr;
  713. icons[iconName] = readFileSlice(actualPtr, size);
  714. }
  715. }
  716. }
  717. // Find and parse each icon group
  718. for (var i = 0; i < obj.resources.entries.length; i++) {
  719. if (obj.resources.entries[i].name == resourceDefaultNames.iconGroups) {
  720. for (var j = 0; j < obj.resources.entries[i].table.entries.length; j++) {
  721. const groupName = obj.resources.entries[i].table.entries[j].name;
  722. const offsetToData = obj.resources.entries[i].table.entries[j].table.entries[0].item.offsetToData;
  723. const size = obj.resources.entries[i].table.entries[j].table.entries[0].item.size;
  724. const actualPtr = (offsetToData - obj.header.sections['.rsrc'].virtualAddr) + ptr;
  725. const group = {};
  726. const groupData = readFileSlice(actualPtr, size);
  727. // Parse NEWHEADER structure: https://docs.microsoft.com/en-us/windows/win32/menurc/newheader
  728. group.resType = groupData.readUInt16LE(2);
  729. group.resCount = groupData.readUInt16LE(4);
  730. // Parse many RESDIR structure: https://docs.microsoft.com/en-us/windows/win32/menurc/resdir
  731. group.icons = {};
  732. for (var p = 6; p < size; p += 14) {
  733. var icon = {}
  734. icon.width = groupData[p];
  735. icon.height = groupData[p + 1];
  736. icon.colorCount = groupData[p + 2];
  737. icon.planes = groupData.readUInt16LE(p + 4);
  738. icon.bitCount = groupData.readUInt16LE(p + 6);
  739. icon.bytesInRes = groupData.readUInt32LE(p + 8);
  740. icon.iconCursorId = groupData.readUInt16LE(p + 12);
  741. icon.icon = icons[icon.iconCursorId];
  742. group.icons[icon.iconCursorId] = icon;
  743. }
  744. // Add an icon group
  745. r[groupName] = group;
  746. }
  747. }
  748. }
  749. return r;
  750. }
  751. // Set bitmap information
  752. obj.setBitmapInfo = function (bitmapInfo) {
  753. // Delete all bitmaps resources
  754. var resourcesEntries = [];
  755. for (var i = 0; i < obj.resources.entries.length; i++) {
  756. if (obj.resources.entries[i].name != resourceDefaultNames.bitmaps) {
  757. resourcesEntries.push(obj.resources.entries[i]);
  758. }
  759. }
  760. obj.resources.entries = resourcesEntries;
  761. // Add all bitmap entries
  762. const bitmapEntry = { name: resourceDefaultNames.bitmaps, table: { characteristics: 0, timeDateStamp: 0, majorVersion: 0, minorVersion: 0, entries: [] } };
  763. for (var i in bitmapInfo) {
  764. var name = i;
  765. if (parseInt(i) == name) { name = parseInt(i); }
  766. const bitmapItemEntry = { name: name, table: { characteristics: 0, timeDateStamp: 0, majorVersion: 0, minorVersion: 0, entries: [{ name: 1033, item: { buffer: bitmapInfo[i], codePage: 0 } }] } }
  767. bitmapEntry.table.entries.push(bitmapItemEntry);
  768. }
  769. obj.resources.entries.push(bitmapEntry);
  770. // Sort the resources by name. This is required.
  771. function resSort(a, b) {
  772. if ((typeof a == 'string') && (typeof b == 'string')) { if (a < b) return -1; if (a > b) return 1; return 0; }
  773. if ((typeof a == 'number') && (typeof b == 'number')) { return a - b; }
  774. if ((typeof a == 'string') && (typeof b == 'number')) { return -1; }
  775. return 1;
  776. }
  777. const names = [];
  778. for (var i = 0; i < obj.resources.entries.length; i++) { names.push(obj.resources.entries[i].name); }
  779. names.sort(resSort);
  780. var newEntryOrder = [];
  781. for (var i in names) {
  782. for (var j = 0; j < obj.resources.entries.length; j++) {
  783. if (obj.resources.entries[j].name == names[i]) { newEntryOrder.push(obj.resources.entries[j]); }
  784. }
  785. }
  786. obj.resources.entries = newEntryOrder;
  787. }
  788. // Set icon information
  789. obj.setIconInfo = function (iconInfo) {
  790. // Delete all icon and icon groups resources
  791. var resourcesEntries = [];
  792. for (var i = 0; i < obj.resources.entries.length; i++) {
  793. if ((obj.resources.entries[i].name != resourceDefaultNames.icon) && (obj.resources.entries[i].name != resourceDefaultNames.iconGroups)) {
  794. resourcesEntries.push(obj.resources.entries[i]);
  795. }
  796. }
  797. obj.resources.entries = resourcesEntries;
  798. // Count the icon groups and re-number all icons
  799. var iconGroupCount = 0, nextIconNumber = 1;
  800. for (var i in iconInfo) {
  801. iconGroupCount++;
  802. var xicons = {};
  803. for (var j in iconInfo[i].icons) { xicons[nextIconNumber++] = iconInfo[i].icons[j]; }
  804. iconInfo[i].icons = xicons;
  805. }
  806. if (iconGroupCount == 0) return; // If there are no icon groups, we are done
  807. // Add the new icons entry
  808. const iconsEntry = { name: resourceDefaultNames.icon, table: { characteristics: 0, timeDateStamp: 0, majorVersion: 0, minorVersion: 0, entries: [] } };
  809. for (var i in iconInfo) {
  810. for (var j in iconInfo[i].icons) {
  811. var name = j;
  812. if (parseInt(j) == name) { name = parseInt(j); }
  813. const iconItemEntry = { name: name, table: { characteristics: 0, timeDateStamp: 0, majorVersion: 0, minorVersion: 0, entries: [{ name: 1033, item: { buffer: iconInfo[i].icons[j].icon, codePage: 0 } }] } }
  814. iconsEntry.table.entries.push(iconItemEntry);
  815. }
  816. }
  817. obj.resources.entries.push(iconsEntry);
  818. // Add the new icon group entry
  819. const groupEntry = { name: resourceDefaultNames.iconGroups, table: { characteristics: 0, timeDateStamp: 0, majorVersion: 0, minorVersion: 0, entries: [] } };
  820. for (var i in iconInfo) {
  821. // Build icon group struct
  822. var iconCount = 0, p = 6;
  823. for (var j in iconInfo[i].icons) { iconCount++; }
  824. const buf = Buffer.alloc(6 + (iconCount * 14));
  825. buf.writeUInt16LE(iconInfo[i].resType, 2);
  826. buf.writeUInt16LE(iconCount, 4);
  827. for (var j in iconInfo[i].icons) {
  828. buf[p] = iconInfo[i].icons[j].width;
  829. buf[p + 1] = iconInfo[i].icons[j].height;
  830. buf[p + 2] = iconInfo[i].icons[j].colorCount;
  831. buf.writeUInt16LE(iconInfo[i].icons[j].planes, p + 4);
  832. buf.writeUInt16LE(iconInfo[i].icons[j].bitCount, p + 6);
  833. buf.writeUInt32LE(iconInfo[i].icons[j].bytesInRes, p + 8);
  834. buf.writeUInt16LE(j, p + 12);
  835. p += 14;
  836. }
  837. var name = i;
  838. if (parseInt(i) == name) { name = parseInt(i); }
  839. const groupItemEntry = { name: name, table: { characteristics: 0, timeDateStamp: 0, majorVersion: 0, minorVersion: 0, entries: [{ name: 1033, item: { buffer: buf, codePage: 0 } }] } }
  840. groupEntry.table.entries.push(groupItemEntry);
  841. }
  842. obj.resources.entries.push(groupEntry);
  843. // Sort the resources by name. This is required.
  844. function resSort(a, b) {
  845. if ((typeof a == 'string') && (typeof b == 'string')) { if (a < b) return -1; if (a > b) return 1; return 0; }
  846. if ((typeof a == 'number') && (typeof b == 'number')) { return a - b; }
  847. if ((typeof a == 'string') && (typeof b == 'number')) { return -1; }
  848. return 1;
  849. }
  850. const names = [];
  851. for (var i = 0; i < obj.resources.entries.length; i++) { names.push(obj.resources.entries[i].name); }
  852. names.sort(resSort);
  853. var newEntryOrder = [];
  854. for (var i in names) {
  855. for (var j = 0; j < obj.resources.entries.length; j++) {
  856. if (obj.resources.entries[j].name == names[i]) { newEntryOrder.push(obj.resources.entries[j]); }
  857. }
  858. }
  859. obj.resources.entries = newEntryOrder;
  860. }
  861. // Decode the version information from the resource
  862. obj.getVersionInfo = function () {
  863. var r = {}, info = readVersionInfo(getVersionInfoData(), 0);
  864. if ((info == null) || (info.stringFiles == null)) return null;
  865. var StringFileInfo = null;
  866. for (var i in info.stringFiles) { if (info.stringFiles[i].szKey == 'StringFileInfo') { StringFileInfo = info.stringFiles[i]; } }
  867. if ((StringFileInfo == null) || (StringFileInfo.stringTable == null) || (StringFileInfo.stringTable.strings == null)) return null;
  868. const strings = StringFileInfo.stringTable.strings;
  869. for (var i in strings) { r[strings[i].key] = strings[i].value; }
  870. r['~FileVersion'] = (info.fixedFileInfo.dwFileVersionMS >> 16) + '.' + (info.fixedFileInfo.dwFileVersionMS & 0xFFFF) + '.' + (info.fixedFileInfo.dwFileVersionLS >> 16) + '.' + (info.fixedFileInfo.dwFileVersionLS & 0xFFFF);
  871. r['~ProductVersion'] = (info.fixedFileInfo.dwProductVersionMS >> 16) + '.' + (info.fixedFileInfo.dwProductVersionMS & 0xFFFF) + '.' + (info.fixedFileInfo.dwProductVersionLS >> 16) + '.' + (info.fixedFileInfo.dwProductVersionLS & 0xFFFF);
  872. return r;
  873. }
  874. // Encode the version information to the resource
  875. obj.setVersionInfo = function (versions) {
  876. // Convert the version information into a string array
  877. const stringArray = [];
  878. for (var i in versions) { if (!i.startsWith('~')) { stringArray.push({ key: i, value: versions[i] }); } }
  879. // Get the existing version data and switch the strings to the new strings
  880. var r = {}, info = readVersionInfo(getVersionInfoData(), 0);
  881. if ((info == null) || (info.stringFiles == null)) return;
  882. var StringFileInfo = null;
  883. for (var i in info.stringFiles) { if (info.stringFiles[i].szKey == 'StringFileInfo') { StringFileInfo = info.stringFiles[i]; } }
  884. if ((StringFileInfo == null) || (StringFileInfo.stringTable == null) || (StringFileInfo.stringTable.strings == null)) return;
  885. StringFileInfo.stringTable.strings = stringArray;
  886. // Set the file version
  887. if (versions['~FileVersion'] != null) {
  888. const FileVersionSplit = versions['~FileVersion'].split('.');
  889. info.fixedFileInfo.dwFileVersionMS = (parseInt(FileVersionSplit[0]) << 16) + parseInt(FileVersionSplit[1]);
  890. info.fixedFileInfo.dwFileVersionLS = (parseInt(FileVersionSplit[2]) << 16) + parseInt(FileVersionSplit[3]);
  891. }
  892. // Set the product version
  893. if (versions['~ProductVersion'] != null) {
  894. const ProductVersionSplit = versions['~ProductVersion'].split('.');
  895. info.fixedFileInfo.dwProductVersionMS = (parseInt(ProductVersionSplit[0]) << 16) + parseInt(ProductVersionSplit[1]);
  896. info.fixedFileInfo.dwProductVersionLS = (parseInt(ProductVersionSplit[2]) << 16) + parseInt(ProductVersionSplit[3]);
  897. }
  898. // Re-encode the version information into a buffer
  899. var verInfoResBufArray = [];
  900. writeVersionInfo(verInfoResBufArray, info);
  901. var verInfoRes = Buffer.concat(verInfoResBufArray);
  902. // Display all buffers
  903. //console.log('--WRITE BUF ARRAY START--');
  904. //for (var i in verInfoResBufArray) { console.log(verInfoResBufArray[i].toString('hex')); }
  905. //console.log('--WRITE BUF ARRAY END--');
  906. //console.log('OUT', Buffer.concat(verInfoResBufArray).toString('hex'));
  907. // Set the new buffer as part of the resources
  908. for (var i = 0; i < obj.resources.entries.length; i++) {
  909. if (obj.resources.entries[i].name == resourceDefaultNames.versionInfo) {
  910. const verInfo = obj.resources.entries[i].table.entries[0].table.entries[0].item;
  911. delete verInfo.size;
  912. delete verInfo.offsetToData;
  913. verInfo.buffer = verInfoRes;
  914. obj.resources.entries[i].table.entries[0].table.entries[0].item = verInfo;
  915. }
  916. }
  917. }
  918. // Return the version info data block
  919. function getVersionInfoData() {
  920. if (obj.resources == null) return null;
  921. const ptr = obj.header.sections['.rsrc'].rawAddr;
  922. for (var i = 0; i < obj.resources.entries.length; i++) {
  923. if (obj.resources.entries[i].name == resourceDefaultNames.versionInfo) {
  924. const verInfo = obj.resources.entries[i].table.entries[0].table.entries[0].item;
  925. if (verInfo.buffer != null) {
  926. return verInfo.buffer;
  927. } else {
  928. const actualPtr = (verInfo.offsetToData - obj.header.sections['.rsrc'].virtualAddr) + ptr;
  929. return readFileSlice(actualPtr, verInfo.size);
  930. }
  931. }
  932. }
  933. return null;
  934. }
  935. // Create a VS_VERSIONINFO structure as a array of buffer that is ready to be placed in the resource section
  936. // VS_VERSIONINFO structure: https://docs.microsoft.com/en-us/windows/win32/menurc/vs-versioninfo
  937. function writeVersionInfo(bufArray, info) {
  938. const buf = Buffer.alloc(40);
  939. buf.writeUInt16LE(0, 4); // wType
  940. stringToUnicode('VS_VERSION_INFO', buf, 6);
  941. bufArray.push(buf);
  942. var wLength = 40;
  943. var wValueLength = 0;
  944. if (info.fixedFileInfo != null) {
  945. const buf2 = Buffer.alloc(52);
  946. wLength += 52;
  947. wValueLength += 52;
  948. buf2.writeUInt32LE(info.fixedFileInfo.dwSignature, 0); // dwSignature
  949. buf2.writeUInt32LE(info.fixedFileInfo.dwStrucVersion, 4); // dwStrucVersion
  950. buf2.writeUInt32LE(info.fixedFileInfo.dwFileVersionMS, 8); // dwFileVersionMS
  951. buf2.writeUInt32LE(info.fixedFileInfo.dwFileVersionLS, 12); // dwFileVersionLS
  952. buf2.writeUInt32LE(info.fixedFileInfo.dwProductVersionMS, 16); // dwProductVersionMS
  953. buf2.writeUInt32LE(info.fixedFileInfo.dwProductVersionLS, 20); // dwProductVersionLS
  954. buf2.writeUInt32LE(info.fixedFileInfo.dwFileFlagsMask, 24); // dwFileFlagsMask
  955. buf2.writeUInt32LE(info.fixedFileInfo.dwFileFlags, 28); // dwFileFlags
  956. buf2.writeUInt32LE(info.fixedFileInfo.dwFileOS, 32); // dwFileOS
  957. buf2.writeUInt32LE(info.fixedFileInfo.dwFileType, 36); // dwFileType
  958. buf2.writeUInt32LE(info.fixedFileInfo.dwFileSubtype, 40); // dwFileSubtype
  959. buf2.writeUInt32LE(info.fixedFileInfo.dwFileDateMS, 44); // dwFileDateMS
  960. buf2.writeUInt32LE(info.fixedFileInfo.dwFileDateLS, 48); // dwFileDateLS
  961. bufArray.push(buf2);
  962. }
  963. if (info.stringFiles != null) { wLength += writeStringFileInfo(bufArray, info.stringFiles); }
  964. buf.writeUInt16LE(Buffer.concat(bufArray).length, 0); // wLength
  965. buf.writeUInt16LE(wValueLength, 2); // wValueLength
  966. return wLength;
  967. }
  968. // StringFileInfo structure: https://docs.microsoft.com/en-us/windows/win32/menurc/stringfileinfo
  969. function writeStringFileInfo(bufArray, stringFiles) {
  970. var totalLen = 0;
  971. for (var i in stringFiles) {
  972. var l = 6 + (stringFiles[i].szKey.length * 2);
  973. if (stringFiles[i].szKey == 'VarFileInfo') { l += 4; } // TODO: This is a hack, not sure what the correct code should be
  974. const buf2 = Buffer.alloc(padPointer(l));
  975. buf2.writeUInt16LE(1, 4); // wType
  976. stringToUnicode(stringFiles[i].szKey, buf2, 6);
  977. bufArray.push(buf2);
  978. var wLength = 0, wValueLength = 0;
  979. if (stringFiles[i].szKey == 'StringFileInfo') { wLength += writeStringTableStruct(bufArray, stringFiles[i].stringTable); }
  980. if (stringFiles[i].szKey == 'VarFileInfo') { wLength += writeVarFileInfoStruct(bufArray, stringFiles[i].varFileInfo); }
  981. buf2.writeUInt16LE(l + wLength, 0); // wLength
  982. buf2.writeUInt16LE(wValueLength, 2); // wValueLength
  983. totalLen += buf2.length + wLength;
  984. }
  985. return totalLen;
  986. }
  987. // VarFileInfo structure: https://docs.microsoft.com/en-us/windows/win32/menurc/var-str
  988. function writeVarFileInfoStruct(bufArray, varFileInfo) {
  989. var l = 8 + (varFileInfo.szKey.length * 2);
  990. const buf = Buffer.alloc(padPointer(l));
  991. buf.writeUInt16LE(0, 4); // wType
  992. stringToUnicode(varFileInfo.szKey, buf, 6);
  993. bufArray.push(buf);
  994. var wLength = 0;
  995. var wValueLength = 0;
  996. if (varFileInfo.value) {
  997. bufArray.push(varFileInfo.value);
  998. wLength += varFileInfo.value.length;
  999. wValueLength += varFileInfo.value.length;
  1000. }
  1001. buf.writeUInt16LE(buf.length + wLength, 0); // wLength
  1002. buf.writeUInt16LE(wValueLength, 2); // wValueLength
  1003. return buf.length + wLength;
  1004. }
  1005. // StringTable structure: https://docs.microsoft.com/en-us/windows/win32/menurc/stringtable
  1006. function writeStringTableStruct(bufArray, stringTable) {
  1007. //console.log('writeStringTableStruct', stringTable);
  1008. var l = 6 + (stringTable.szKey.length * 2);
  1009. const buf = Buffer.alloc(padPointer(l));
  1010. buf.writeUInt16LE(1, 4); // wType
  1011. stringToUnicode(stringTable.szKey, buf, 6);
  1012. bufArray.push(buf);
  1013. var wLength = 0;
  1014. var wValueLength = 0;
  1015. if (stringTable.strings) { wLength += writeStringStructs(bufArray, stringTable.strings); }
  1016. buf.writeUInt16LE(l + wLength, 0); // wLength
  1017. buf.writeUInt16LE(wValueLength, 2); // wValueLength
  1018. return buf.length + wLength;
  1019. }
  1020. // String structure: https://docs.microsoft.com/en-us/windows/win32/menurc/string-str
  1021. function writeStringStructs(bufArray, stringTable) {
  1022. //console.log('writeStringStructs', stringTable);
  1023. var totalLen = 0, bufadd = 0;
  1024. for (var i in stringTable) {
  1025. //console.log('writeStringStructs', stringTable[i]);
  1026. const buf = Buffer.alloc(padPointer(6 + ((stringTable[i].key.length + 1) * 2)));
  1027. var buf2, wLength = buf.length;
  1028. var wValueLength = 0;
  1029. stringToUnicode(stringTable[i].key, buf, 6);
  1030. bufArray.push(buf);
  1031. bufadd += buf.length;
  1032. if (typeof stringTable[i].value == 'string') {
  1033. // wType (string)
  1034. buf.writeUInt16LE(1, 4);
  1035. var l = (stringTable[i].value.length + 1) * 2;
  1036. buf2 = Buffer.alloc(padPointer(l));
  1037. stringToUnicode(stringTable[i].value, buf2, 0);
  1038. bufArray.push(buf2);
  1039. bufadd += buf2.length;
  1040. wValueLength = stringTable[i].value.length + 1;
  1041. wLength += l;
  1042. }
  1043. if (typeof stringTable[i].value == 'object') {
  1044. // wType (binary)
  1045. buf.writeUInt16LE(2, 4); // TODO: PADDING
  1046. bufArray.push(stringTable[i].value);
  1047. bufadd += stringTable[i].value.length;
  1048. wValueLength = stringTable[i].value.length;
  1049. wLength += wValueLength;
  1050. }
  1051. buf.writeUInt16LE(wLength, 0); // wLength
  1052. buf.writeUInt16LE(wValueLength, 2); // wValueLength
  1053. //console.log('WStringStruct', buf.toString('hex'), buf2.toString('hex'));
  1054. totalLen += wLength;
  1055. }
  1056. //return totalLen;
  1057. return bufadd;
  1058. }
  1059. // VS_VERSIONINFO structure: https://docs.microsoft.com/en-us/windows/win32/menurc/vs-versioninfo
  1060. function readVersionInfo(buf, ptr) {
  1061. const r = {};
  1062. if (buf.length < 2) return null;
  1063. const wLength = buf.readUInt16LE(ptr);
  1064. if (buf.length < wLength) return null;
  1065. const wValueLength = buf.readUInt16LE(ptr + 2);
  1066. const wType = buf.readUInt16LE(ptr + 4);
  1067. r.szKey = unicodeToString(buf.slice(ptr + 6, ptr + 36));
  1068. if (r.szKey != 'VS_VERSION_INFO') return null;
  1069. //console.log('getVersionInfo', wLength, wValueLength, wType, r.szKey.toString());
  1070. if (wValueLength == 52) { r.fixedFileInfo = readFixedFileInfoStruct(buf, ptr + 40); }
  1071. r.stringFiles = readStringFilesStruct(buf, ptr + 40 + wValueLength, wLength - 40 - wValueLength);
  1072. return r;
  1073. }
  1074. // VS_FIXEDFILEINFO structure: https://docs.microsoft.com/en-us/windows/win32/api/verrsrc/ns-verrsrc-vs_fixedfileinfo
  1075. function readFixedFileInfoStruct(buf, ptr) {
  1076. if (buf.length - ptr < 50) return null;
  1077. var r = {};
  1078. r.dwSignature = buf.readUInt32LE(ptr);
  1079. if (r.dwSignature != 0xFEEF04BD) return null;
  1080. r.dwStrucVersion = buf.readUInt32LE(ptr + 4);
  1081. r.dwFileVersionMS = buf.readUInt32LE(ptr + 8);
  1082. r.dwFileVersionLS = buf.readUInt32LE(ptr + 12);
  1083. r.dwProductVersionMS = buf.readUInt32LE(ptr + 16);
  1084. r.dwProductVersionLS = buf.readUInt32LE(ptr + 20);
  1085. r.dwFileFlagsMask = buf.readUInt32LE(ptr + 24);
  1086. r.dwFileFlags = buf.readUInt32LE(ptr + 28);
  1087. r.dwFileOS = buf.readUInt32LE(ptr + 32);
  1088. r.dwFileType = buf.readUInt32LE(ptr + 36);
  1089. r.dwFileSubtype = buf.readUInt32LE(ptr + 40);
  1090. r.dwFileDateMS = buf.readUInt32LE(ptr + 44);
  1091. r.dwFileDateLS = buf.readUInt32LE(ptr + 48);
  1092. return r;
  1093. }
  1094. // StringFileInfo structure: https://docs.microsoft.com/en-us/windows/win32/menurc/stringfileinfo
  1095. function readStringFilesStruct(buf, ptr, len) {
  1096. var t = [], startPtr = ptr;
  1097. while (ptr < (startPtr + len)) {
  1098. const r = {};
  1099. const wLength = buf.readUInt16LE(ptr);
  1100. if (wLength == 0) return t;
  1101. const wValueLength = buf.readUInt16LE(ptr + 2);
  1102. const wType = buf.readUInt16LE(ptr + 4); // 1 = Text, 2 = Binary
  1103. r.szKey = unicodeToString(buf.slice(ptr + 6, ptr + 6 + (wLength - 6))); // String value
  1104. //console.log('readStringFileStruct', wLength, wValueLength, wType, r.szKey);
  1105. if (r.szKey == 'StringFileInfo') { r.stringTable = readStringTableStruct(buf, ptr + 36); }
  1106. if (r.szKey == 'VarFileInfo') { r.varFileInfo = readVarFileInfoStruct(buf, ptr + 32); }
  1107. t.push(r);
  1108. ptr += wLength;
  1109. ptr = padPointer(ptr);
  1110. }
  1111. return t;
  1112. }
  1113. // VarFileInfo structure: https://docs.microsoft.com/en-us/windows/win32/menurc/var-str
  1114. function readVarFileInfoStruct(buf, ptr) {
  1115. const r = {};
  1116. const wLength = buf.readUInt16LE(ptr);
  1117. const wValueLength = buf.readUInt16LE(ptr + 2);
  1118. const wType = buf.readUInt16LE(ptr + 4); // 1 = Text, 2 = Binary
  1119. r.szKey = unicodeToString(buf.slice(ptr + 6, ptr + wLength)); // "VarFileInfo"
  1120. r.value = buf.slice(ptr + wLength - wValueLength, ptr + wLength)
  1121. //console.log('readVarFileInfoStruct', wLength, wValueLength, wType, r.szKey, r.value.toString('hex'));
  1122. return r;
  1123. }
  1124. // StringTable structure: https://docs.microsoft.com/en-us/windows/win32/menurc/stringtable
  1125. function readStringTableStruct(buf, ptr) {
  1126. const r = {};
  1127. const wLength = buf.readUInt16LE(ptr);
  1128. const wValueLength = buf.readUInt16LE(ptr + 2);
  1129. const wType = buf.readUInt16LE(ptr + 4); // 1 = Text, 2 = Binary
  1130. //console.log('RStringTableStruct', buf.slice(ptr, ptr + wLength).toString('hex'));
  1131. r.szKey = unicodeToString(buf.slice(ptr + 6, ptr + 6 + 16)); // An 8-digit hexadecimal number stored as a Unicode string.
  1132. //console.log('readStringTableStruct', wLength, wValueLength, wType, r.szKey);
  1133. r.strings = readStringStructs(buf, ptr + 24 + wValueLength, wLength - 22);
  1134. return r;
  1135. }
  1136. // String structure: https://docs.microsoft.com/en-us/windows/win32/menurc/string-str
  1137. function readStringStructs(buf, ptr, len) {
  1138. var t = [], startPtr = ptr;
  1139. while ((ptr + 6) < (startPtr + len)) {
  1140. const r = {};
  1141. const wLength = buf.readUInt16LE(ptr);
  1142. if (wLength == 0) return t;
  1143. //console.log('RStringStruct', buf.slice(ptr, ptr + wLength).toString('hex'));
  1144. const wValueLength = buf.readUInt16LE(ptr + 2);
  1145. const wType = buf.readUInt16LE(ptr + 4); // 1 = Text, 2 = Binary
  1146. //console.log('R', buf.slice(ptr, ptr + wLength).toString('hex'));
  1147. r.key = unicodeToString(buf.slice(ptr + 6, ptr + (wLength - (wValueLength * 2)) - 2)); // Key
  1148. if (wType == 1) { r.value = unicodeToString(buf.slice(ptr + wLength - (wValueLength * 2), ptr + wLength - 2)); } // String value
  1149. if (wType == 2) { r.value = buf.slice(ptr + wLength - (wValueLength * 2), ptr + wLength); } // Binary value
  1150. //console.log('readStringStruct', wLength, wValueLength, wType, r.key, r.value);
  1151. t.push(r);
  1152. ptr += wLength;
  1153. ptr = padPointer(ptr);
  1154. }
  1155. return t;
  1156. }
  1157. // Return the next 4 byte aligned number
  1158. function padPointer(ptr) { return ptr + (((ptr % 4) == 0) ? 0 : (4 - (ptr % 4))); }
  1159. //function padPointer(ptr) { return ptr + (ptr % 4); }
  1160. // Hash the file using the selected hashing system
  1161. // This hash skips the executables CRC and code signing data and signing block
  1162. obj.getHash = function(algo) {
  1163. const hash = crypto.createHash(algo);
  1164. runHash(hash, 0, obj.header.peHeaderLocation + 88);
  1165. runHash(hash, obj.header.peHeaderLocation + 88 + 4, obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  1166. runHash(hash, obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16) + 8, obj.header.sigpos > 0 ? obj.header.sigpos : obj.filesize);
  1167. return hash.digest();
  1168. }
  1169. // Hash of an open file using the selected hashing system
  1170. // This hash skips the executables CRC and code signing data and signing block
  1171. obj.getHashOfFile = function(fd, algo, filesize) {
  1172. const hash = crypto.createHash(algo);
  1173. runHashOnFile(fd, hash, 0, obj.header.peHeaderLocation + 88);
  1174. runHashOnFile(fd, hash, obj.header.peHeaderLocation + 88 + 4, obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  1175. runHashOnFile(fd, hash, obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16) + 8, obj.header.sigpos > 0 ? obj.header.sigpos : filesize);
  1176. return hash.digest();
  1177. }
  1178. // Hash the file using the selected hashing system skipping resource section
  1179. // This hash skips the executables CRC, sections table, resource section, code signing data and signing block
  1180. obj.getHashNoResources = function (algo) {
  1181. if (obj.header.sections['.rsrc'] == null) { return obj.getHash(algo); } // No resources in this executable, return a normal hash
  1182. // Get the sections table start and size
  1183. const sectionHeaderPtr = obj.header.SectionHeadersPtr;
  1184. const sectionHeaderSize = obj.header.coff.numberOfSections * 40;
  1185. // Get the resource section start and size
  1186. const resPtr = obj.header.sections['.rsrc'].rawAddr;
  1187. const resSize = obj.header.sections['.rsrc'].rawSize;
  1188. // Get the end-of-file location
  1189. const eof = obj.header.sigpos > 0 ? obj.header.sigpos : obj.filesize;
  1190. // Hash the remaining data
  1191. const hash = crypto.createHash(algo);
  1192. runHash(hash, 0, obj.header.peHeaderLocation + 88);
  1193. runHash(hash, obj.header.peHeaderLocation + 88 + 4, obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  1194. runHash(hash, obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16) + 8, sectionHeaderPtr);
  1195. runHash(hash, sectionHeaderPtr + sectionHeaderSize, resPtr);
  1196. runHash(hash, resPtr + resSize, eof);
  1197. return hash.digest();
  1198. }
  1199. // Hash the file using the selected hashing system skipping resource section
  1200. // This hash skips the executables CRC, sections table, resource section, code signing data and signing block
  1201. obj.getHashOfSection = function (algo, sectionName) {
  1202. if (obj.header.sections[sectionName] == null) return null;
  1203. // Get the section start and size
  1204. const sectionPtr = obj.header.sections[sectionName].rawAddr;
  1205. const sectionSize = obj.header.sections[sectionName].rawSize;
  1206. // Hash the remaining data
  1207. const hash = crypto.createHash(algo);
  1208. runHash(hash, sectionPtr, sectionPtr + sectionSize);
  1209. return hash.digest();
  1210. }
  1211. // Hash the file from start to end loading 64k chunks
  1212. function runHash(hash, start, end) {
  1213. var ptr = start;
  1214. while (ptr < end) { const buf = readFileSlice(ptr, Math.min(65536, end - ptr)); hash.update(buf); ptr += buf.length; }
  1215. }
  1216. // Hash the open file loading 64k chunks
  1217. // TODO: Do chunks on this!!!
  1218. function runHashOnFile(fd, hash, start, end) {
  1219. const buf = Buffer.alloc(end - start);
  1220. const len = fs.readSync(fd, buf, 0, buf.length, start);
  1221. if (len != buf.length) { console.log('BAD runHashOnFile'); }
  1222. hash.update(buf);
  1223. }
  1224. // Checksum the file loading 64k chunks
  1225. function runChecksum() {
  1226. var ptr = 0, c = createChecksum(((obj.header.peOptionalHeaderLocation + 64) / 4));
  1227. while (ptr < obj.filesize) { const buf = readFileSlice(ptr, Math.min(65536, obj.filesize - ptr)); c.update(buf); ptr += buf.length; }
  1228. return c.digest();
  1229. }
  1230. // Checksum the open file loading 64k chunks
  1231. function runChecksumOnFile(fd, filesize, checksumLocation) {
  1232. var ptr = 0, c = createChecksum(checksumLocation), buf = Buffer.alloc(65536);
  1233. while (ptr < filesize) { var len = fs.readSync(fd, buf, 0, Math.min(65536, filesize - ptr), ptr); c.update(buf, len); ptr += len; }
  1234. return c.digest();
  1235. }
  1236. // Steaming checksum methods
  1237. // TODO: Works only with files padded to 4 byte.
  1238. function createChecksum(checksumLocation) {
  1239. const obj = { checksum: 0, length: 0 };
  1240. obj.update = function (data, len) {
  1241. if (!len) { len = data.length; }
  1242. for (var i = 0; i < (len / 4) ; i++) {
  1243. if (((obj.length / 4) + i) == checksumLocation) continue; // Skip PE checksum location
  1244. const dword = data.readUInt32LE(i * 4);
  1245. var checksumlo = (obj.checksum > 4294967296) ? (obj.checksum - 4294967296) : obj.checksum;
  1246. var checksumhi = (obj.checksum > 4294967296) ? 1 : 0;
  1247. obj.checksum = checksumlo + dword + checksumhi;
  1248. if (obj.checksum > 4294967296) {
  1249. checksumlo = (obj.checksum > 4294967296) ? (obj.checksum - 4294967296) : obj.checksum;
  1250. checksumhi = (obj.checksum > 4294967296) ? 1 : 0;
  1251. obj.checksum = checksumlo + checksumhi;
  1252. }
  1253. }
  1254. obj.length += len;
  1255. }
  1256. obj.digest = function () {
  1257. obj.checksum = (obj.checksum & 0xffff) + (obj.checksum >>> 16);
  1258. obj.checksum = (obj.checksum) + (obj.checksum >>> 16);
  1259. obj.checksum = obj.checksum & 0xffff;
  1260. obj.checksum += obj.length;
  1261. return obj.checksum;
  1262. }
  1263. return obj;
  1264. }
  1265. // Compute the PE checksum of an entire file
  1266. function getChecksum(data, checksumLocation) {
  1267. var checksum = 0;
  1268. for (var i = 0; i < (data.length / 4) ; i++) {
  1269. if (i == (checksumLocation / 4)) continue; // Skip PE checksum location
  1270. var dword = data.readUInt32LE(i * 4);
  1271. var checksumlo = (checksum > 4294967296) ? (checksum - 4294967296) : checksum;
  1272. var checksumhi = (checksum > 4294967296) ? 1 : 0;
  1273. checksum = checksumlo + dword + checksumhi;
  1274. if (checksum > 4294967296) {
  1275. checksumlo = (checksum > 4294967296) ? (checksum - 4294967296) : checksum;
  1276. checksumhi = (checksum > 4294967296) ? 1 : 0;
  1277. checksum = checksumlo + checksumhi;
  1278. }
  1279. }
  1280. checksum = (checksum & 0xffff) + (checksum >>> 16);
  1281. checksum = (checksum) + (checksum >>> 16);
  1282. checksum = checksum & 0xffff;
  1283. checksum += data.length;
  1284. return checksum;
  1285. }
  1286. // Sign the file using the certificate and key. If none is specified, generate a dummy one
  1287. obj.sign = function (cert, args, func) {
  1288. if (cert == null) { cert = createSelfSignedCert({ cn: 'Test' }); }
  1289. // Set the hash algorithm hash OID
  1290. var hashOid = null, fileHash = null;
  1291. if (args.hash == null) { args.hash = 'sha384'; }
  1292. if (args.hash == 'sha256') { hashOid = forge.pki.oids.sha256; fileHash = obj.getHash('sha256'); }
  1293. if (args.hash == 'sha384') { hashOid = forge.pki.oids.sha384; fileHash = obj.getHash('sha384'); }
  1294. if (args.hash == 'sha512') { hashOid = forge.pki.oids.sha512; fileHash = obj.getHash('sha512'); }
  1295. if (args.hash == 'sha224') { hashOid = forge.pki.oids.sha224; fileHash = obj.getHash('sha224'); }
  1296. if (args.hash == 'md5') { hashOid = forge.pki.oids.md5; fileHash = obj.getHash('md5'); }
  1297. if (hashOid == null) { func('Invalid signing hash: ' + args.hash); return; };
  1298. // Create the signature block
  1299. var xp7 = forge.pkcs7.createSignedData();
  1300. var content = { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 6, 'constructed': false, 'composed': false, 'value': forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.15').data }, { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 3, 'constructed': false, 'composed': false, 'value': '\u0000', 'bitStringContents': '\u0000', 'original': { 'tagClass': 0, 'type': 3, 'constructed': false, 'composed': false, 'value': '\u0000' } }, { 'tagClass': 128, 'type': 0, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 2, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': '' }] }] }] }] }, { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 6, 'constructed': false, 'composed': false, 'value': forge.asn1.oidToDer(hashOid).data }, { 'tagClass': 0, 'type': 5, 'constructed': false, 'composed': false, 'value': '' }] }, { 'tagClass': 0, 'type': 4, 'constructed': false, 'composed': false, 'value': fileHash.toString('binary') }] }] };
  1301. xp7.contentInfo = forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.SEQUENCE, true, [forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.OID, false, forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.4').getBytes())]);
  1302. xp7.contentInfo.value.push(forge.asn1.create(forge.asn1.Class.CONTEXT_SPECIFIC, 0, true, [content]));
  1303. xp7.content = {}; // We set .contentInfo and have .content empty to bypass node-forge limitation on the type of content it can sign.
  1304. xp7.addCertificate(cert.cert);
  1305. if (cert.extraCerts) { for (var i = 0; i < cert.extraCerts.length; i++) { xp7.addCertificate(cert.extraCerts[0]); } } // Add any extra certificates that form the cert chain
  1306. // Build authenticated attributes
  1307. var authenticatedAttributes = [
  1308. { type: forge.pki.oids.contentType, value: forge.pki.oids.data },
  1309. { type: forge.pki.oids.messageDigest } // This value will populated at signing time by node-forge
  1310. ]
  1311. if ((typeof args.desc == 'string') || (typeof args.url == 'string')) {
  1312. var codeSigningAttributes = { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [] };
  1313. if (args.desc != null) { // Encode description as big-endian unicode.
  1314. var desc = "", ucs = Buffer.from(args.desc, 'ucs2').toString()
  1315. for (var k = 0; k < ucs.length; k += 2) { desc += String.fromCharCode(ucs.charCodeAt(k + 1), ucs.charCodeAt(k)); }
  1316. codeSigningAttributes.value.push({ 'tagClass': 128, 'type': 0, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': desc }] });
  1317. }
  1318. if (args.url != null) { codeSigningAttributes.value.push({ 'tagClass': 128, 'type': 1, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': args.url }] }); }
  1319. authenticatedAttributes.push({ type: obj.Oids.SPC_SP_OPUS_INFO_OBJID, value: codeSigningAttributes });
  1320. }
  1321. // Add the signer and sign
  1322. xp7.addSigner({
  1323. key: cert.key,
  1324. certificate: cert.cert,
  1325. digestAlgorithm: forge.pki.oids.sha384,
  1326. authenticatedAttributes: authenticatedAttributes
  1327. });
  1328. xp7.sign();
  1329. var p7signature = Buffer.from(forge.pkcs7.messageToPem(xp7).split('-----BEGIN PKCS7-----')[1].split('-----END PKCS7-----')[0], 'base64');
  1330. if (args.time == null) {
  1331. // Sign the executable without timestamp
  1332. signEx(args, p7signature, obj.filesize, func);
  1333. } else {
  1334. // Decode the signature block
  1335. var pkcs7der = null;
  1336. try { pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(p7signature)); } catch (ex) { func('' + ex); return; }
  1337. // To work around ForgeJS PKCS#7 limitation, this may break PKCS7 verify if ForgeJS adds support for it in the future
  1338. // Switch content type from "1.3.6.1.4.1.311.2.1.4" to "1.2.840.113549.1.7.1"
  1339. pkcs7der.value[1].value[0].value[2].value[0].value = forge.asn1.oidToDer(forge.pki.oids.data).data;
  1340. // Decode the PKCS7 message
  1341. var pkcs7 = p7.messageFromAsn1(pkcs7der);
  1342. // Create the timestamp request in DER format
  1343. const asn1 = forge.asn1;
  1344. const pkcs7dataOid = asn1.oidToDer('1.2.840.113549.1.7.1').data;
  1345. const microsoftCodeSigningOid = asn1.oidToDer('1.3.6.1.4.1.311.3.2.1').data;
  1346. const asn1obj =
  1347. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  1348. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, microsoftCodeSigningOid),
  1349. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  1350. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, pkcs7dataOid),
  1351. asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
  1352. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, pkcs7.rawCapture.signature.toString('binary')) // Signature here
  1353. ])
  1354. ])
  1355. ]);
  1356. // Re-decode the PKCS7 from the executable, this time, no workaround needed
  1357. try { pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(p7signature)); } catch (ex) { func('' + ex); return; }
  1358. // Serialize an ASN.1 object to DER format in Base64
  1359. const requestBody = Buffer.from(asn1.toDer(asn1obj).data, 'binary').toString('base64');
  1360. // Make an HTTP request
  1361. const options = { url: args.time, proxy: args.proxy };
  1362. // Make a request to the time server
  1363. httpRequest(options, requestBody, function (err, data) {
  1364. if (err != null) { func(err); return; }
  1365. // Decode the timestamp signature block
  1366. var timepkcs7der = null;
  1367. try { timepkcs7der = forge.asn1.fromDer(forge.util.createBuffer(Buffer.from(data, 'base64').toString('binary'))); } catch (ex) { func("Unable to parse time-stamp response: " + ex); return; }
  1368. try {
  1369. // Get the ASN1 certificates used to sign the timestamp and add them to the certs in the PKCS7 of the executable
  1370. // TODO: We could look to see if the certificate is already present in the executable
  1371. const timeasn1Certs = timepkcs7der.value[1].value[0].value[3].value;
  1372. for (var i in timeasn1Certs) { pkcs7der.value[1].value[0].value[3].value.push(timeasn1Certs[i]); }
  1373. // Get the time signature and add it to the executables PKCS7
  1374. const timeasn1Signature = timepkcs7der.value[1].value[0].value[4];
  1375. const countersignatureOid = asn1.oidToDer('1.2.840.113549.1.9.6').data;
  1376. const asn1obj2 =
  1377. asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
  1378. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  1379. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, countersignatureOid),
  1380. timeasn1Signature
  1381. ])
  1382. ]);
  1383. pkcs7der.value[1].value[0].value[4].value[0].value.push(asn1obj2);
  1384. // Re-encode the executable signature block
  1385. const p7signature = Buffer.from(forge.asn1.toDer(pkcs7der).data, 'binary');
  1386. // Write the file with the signature block
  1387. signEx(args, p7signature, obj.filesize, func);
  1388. } catch (ex) { func('' + ex); }
  1389. });
  1390. }
  1391. }
  1392. // Make a HTTP request, use a proxy if needed
  1393. function httpRequest(options, requestBody, func) {
  1394. // Decode the URL
  1395. const timeServerUrl = new URL(options.url);
  1396. options.protocol = timeServerUrl.protocol;
  1397. options.hostname = timeServerUrl.hostname;
  1398. options.path = timeServerUrl.pathname;
  1399. options.port = ((timeServerUrl.port == '') ? 80 : parseInt(timeServerUrl.port));
  1400. if (options.proxy == null) {
  1401. // No proxy needed
  1402. // Setup the options
  1403. delete options.url;
  1404. options.method = 'POST';
  1405. options.headers = {
  1406. 'accept': 'application/octet-stream',
  1407. 'cache-control': 'no-cache',
  1408. 'user-agent': 'Transport',
  1409. 'content-type': 'application/octet-stream',
  1410. 'content-length': Buffer.byteLength(requestBody)
  1411. };
  1412. // Set up the request
  1413. var responseAccumulator = '';
  1414. var req = require('http').request(options, function (res) {
  1415. res.setEncoding('utf8');
  1416. res.on('data', function (chunk) { responseAccumulator += chunk; });
  1417. res.on('end', function () { func(null, responseAccumulator); });
  1418. });
  1419. // Post the data
  1420. req.on('error', function (err) { func('' + err); });
  1421. req.write(requestBody);
  1422. req.end();
  1423. } else {
  1424. // We are using a proxy
  1425. // This is a fairly basic proxy implementation, should work most of the time.
  1426. // Setup the options and decode the proxy URL
  1427. var proxyOptions = { method: 'CONNECT' };
  1428. if (options.proxy) {
  1429. const proxyUrl = new URL(options.proxy);
  1430. proxyOptions.protocol = proxyUrl.protocol;
  1431. proxyOptions.hostname = proxyUrl.hostname;
  1432. proxyOptions.path = options.hostname + ':' + options.port;
  1433. proxyOptions.port = ((proxyUrl.port == '') ? 80 : parseInt(proxyUrl.port));
  1434. }
  1435. // Set up the proxy request
  1436. var responseAccumulator = '';
  1437. var req = require('http').request(proxyOptions);
  1438. req.on('error', function (err) { func('' + err); });
  1439. req.on('connect', function (res, socket, head) {
  1440. // Make a request over the HTTP tunnel
  1441. socket.write('POST ' + options.path + ' HTTP/1.1\r\n' +
  1442. 'host: ' + options.hostname + ':' + options.port + '\r\n' +
  1443. 'accept: application/octet-stream\r\n' +
  1444. 'cache-control: no-cache\r\n' +
  1445. 'user-agent: Transport\r\n' +
  1446. 'content-type: application/octet-stream\r\n' +
  1447. 'content-length: ' + Buffer.byteLength(requestBody) + '\r\n' +
  1448. '\r\n' + requestBody);
  1449. socket.on('data', function (chunk) {
  1450. responseAccumulator += chunk.toString();
  1451. var responseData = parseHttpResponse(responseAccumulator);
  1452. if (responseData != null) { try { socket.end(); } catch (ex) { console.log('ex', ex); } socket.xdone = true; func(null, responseData); }
  1453. });
  1454. socket.on('end', function () {
  1455. if (socket.xdone == true) return;
  1456. var responseData = parseHttpResponse(responseAccumulator);
  1457. if (responseData != null) { func(null, responseData); } else { func("Unable to parse response."); }
  1458. });
  1459. });
  1460. req.end();
  1461. }
  1462. }
  1463. // Parse the HTTP response and return data if available
  1464. function parseHttpResponse(data) {
  1465. var dataSplit = data.split('\r\n\r\n');
  1466. if (dataSplit.length < 2) return null;
  1467. // Parse the HTTP header
  1468. var headerSplit = dataSplit[0].split('\r\n'), headers = {};
  1469. for (var i in headerSplit) {
  1470. if (i != 0) {
  1471. var x = headerSplit[i].indexOf(':');
  1472. headers[headerSplit[i].substring(0, x).toLowerCase()] = headerSplit[i].substring(x + 2);
  1473. }
  1474. }
  1475. // If there is a content-length in the header, keep accumulating data until we have the right length
  1476. if (headers['content-length'] != null) {
  1477. const contentLength = parseInt(headers['content-length']);
  1478. if (dataSplit[1].length < contentLength) return null; // Wait for more data
  1479. return dataSplit[1];
  1480. }
  1481. return dataSplit[1];
  1482. }
  1483. // Complete the signature of an executable
  1484. function signEx(args, p7signature, filesize, func) {
  1485. // Open the output file
  1486. var output = null;
  1487. try { output = fs.openSync(args.out, 'w+'); } catch (ex) { }
  1488. if (output == null) { func('Unable to open output file: ' + args.out); return; }
  1489. var tmp, written = 0, executableSize = obj.header.sigpos ? obj.header.sigpos : filesize;
  1490. // Compute pre-header length and copy that to the new file
  1491. var preHeaderLen = (obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  1492. var tmp = readFileSlice(written, preHeaderLen);
  1493. fs.writeSync(output, tmp);
  1494. written += tmp.length;
  1495. // Quad Align the results, adding padding if necessary
  1496. var len = executableSize + p7signature.length;
  1497. var padding = (8 - ((len) % 8)) % 8;
  1498. // Write the signature header
  1499. var addresstable = Buffer.alloc(8);
  1500. addresstable.writeUInt32LE(executableSize);
  1501. addresstable.writeUInt32LE(8 + p7signature.length + padding, 4);
  1502. fs.writeSync(output, addresstable);
  1503. written += addresstable.length;
  1504. // Copy the rest of the file until the start of the signature block
  1505. while ((executableSize - written) > 0) {
  1506. tmp = readFileSlice(written, Math.min(executableSize - written, 65536));
  1507. fs.writeSync(output, tmp);
  1508. written += tmp.length;
  1509. }
  1510. // Write the signature block header and signature
  1511. var win = Buffer.alloc(8); // WIN CERTIFICATE Structure
  1512. win.writeUInt32LE(p7signature.length + padding + 8); // DWORD length
  1513. win.writeUInt16LE(512, 4); // WORD revision
  1514. win.writeUInt16LE(2, 6); // WORD type
  1515. fs.writeSync(output, win);
  1516. fs.writeSync(output, p7signature);
  1517. if (padding > 0) { fs.writeSync(output, Buffer.alloc(padding, 0)); }
  1518. written += (p7signature.length + padding + 8);
  1519. // Compute the checksum and write it in the PE header checksum location
  1520. var tmp = Buffer.alloc(4);
  1521. tmp.writeUInt32LE(runChecksumOnFile(output, written, ((obj.header.peOptionalHeaderLocation + 64) / 4)));
  1522. fs.writeSync(output, tmp, 0, 4, obj.header.peOptionalHeaderLocation + 64);
  1523. // Close the file
  1524. fs.closeSync(output);
  1525. func(null);
  1526. }
  1527. // Save an executable without the signature
  1528. obj.unsign = function (args) {
  1529. // Open the file
  1530. var output = fs.openSync(args.out, 'w+');
  1531. var written = 0, totalWrite = obj.header.sigpos;
  1532. // Compute pre-header length and copy that to the new file
  1533. var preHeaderLen = (obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  1534. var tmp = readFileSlice(written, preHeaderLen);
  1535. fs.writeSync(output, tmp);
  1536. written += tmp.length;
  1537. // Write the new signature header
  1538. fs.writeSync(output, Buffer.alloc(8));
  1539. written += 8;
  1540. // Copy the rest of the file until the start of the signature block
  1541. while ((totalWrite - written) > 0) {
  1542. tmp = readFileSlice(written, Math.min(totalWrite - written, 65536));
  1543. fs.writeSync(output, tmp);
  1544. written += tmp.length;
  1545. }
  1546. // Compute the checksum and write it in the PE checksum header at position
  1547. var tmp = Buffer.alloc(4);
  1548. tmp.writeUInt32LE(runChecksumOnFile(output, written));
  1549. fs.writeSync(output, tmp, 0, 4, obj.header.peOptionalHeaderLocation + 64);
  1550. fs.closeSync(output);
  1551. }
  1552. // Find where a directory value is in the old sections and map it to the new sections
  1553. function correctDirectoryValue(oldSections, newSections, value) {
  1554. for (var i in oldSections) {
  1555. if ((value >= oldSections[i].virtualAddr) && (value < (oldSections[i].virtualAddr + oldSections[i].virtualSize))) {
  1556. return newSections[i].virtualAddr + (value - oldSections[i].virtualAddr);
  1557. }
  1558. }
  1559. return 0;
  1560. }
  1561. // Save the executable
  1562. obj.writeExecutable = function (args, cert, func) {
  1563. // Open the file
  1564. var output = fs.openSync(args.out, 'w+');
  1565. var tmp, written = 0;
  1566. // Compute the size of the complete executable header up to after the sections header
  1567. var fullHeaderLen = obj.header.SectionHeadersPtr + (obj.header.coff.numberOfSections * 40);
  1568. var fullHeader = readFileSlice(written, fullHeaderLen);
  1569. // Create the resource section
  1570. const rsrcSectionX = generateResourceSection(obj.resources); // This section is created with padding already included
  1571. var rsrcSection = rsrcSectionX.data;
  1572. var rsrcSectionVirtualSize = rsrcSectionX.size;
  1573. var rsrcSectionRawSize = rsrcSection.length;
  1574. // Calculate the location and original and new size of the resource segment
  1575. var fileAlign = obj.header.peWindows.fileAlignment
  1576. var resPtr = obj.header.sections['.rsrc'].rawAddr;
  1577. var oldResSize = obj.header.sections['.rsrc'].rawSize;
  1578. var newResSize = rsrcSection.length;
  1579. var resDeltaSize = newResSize - oldResSize;
  1580. // Compute the sizeOfInitializedData
  1581. var sizeOfInitializedData = 0;
  1582. for (var i in obj.header.sections) {
  1583. if (i != '.text') {
  1584. if (i == '.rsrc') {
  1585. sizeOfInitializedData += rsrcSectionRawSize;
  1586. } else {
  1587. sizeOfInitializedData += obj.header.sections[i].rawSize;
  1588. }
  1589. }
  1590. }
  1591. // Change PE optional header sizeOfInitializedData standard field
  1592. fullHeader.writeUInt32LE(sizeOfInitializedData, obj.header.peOptionalHeaderLocation + 8);
  1593. // Update the checksum to zero
  1594. fullHeader.writeUInt32LE(0, obj.header.peOptionalHeaderLocation + 64);
  1595. // We are going to setup the old a new sections here, we need this to correct directory values
  1596. var oldSections = obj.header.sections;
  1597. var newSections = {};
  1598. // Make changes to the segments table
  1599. var virtualAddress = 4096;
  1600. for (var i in obj.header.sections) {
  1601. const section = obj.header.sections[i];
  1602. newSections[i] = { virtualSize: section.virtualSize };
  1603. if (i == '.rsrc') {
  1604. // Change the size of the resource section
  1605. fullHeader.writeUInt32LE(rsrcSectionVirtualSize, section.ptr + 8); // virtualSize
  1606. fullHeader.writeUInt32LE(rsrcSectionRawSize, section.ptr + 16); // rawSize
  1607. // Set the virtual address of the section
  1608. fullHeader.writeUInt32LE(virtualAddress, section.ptr + 12); // Virtual address
  1609. newSections[i].virtualAddr = virtualAddress;
  1610. var virtualAddressPadding = (rsrcSectionVirtualSize % 4096);
  1611. virtualAddress += rsrcSectionVirtualSize;
  1612. if (virtualAddressPadding != 0) { virtualAddress += (4096 - virtualAddressPadding); }
  1613. } else {
  1614. // Change the location of any other section if located after the resource section
  1615. if (section.rawAddr > resPtr) { fullHeader.writeUInt32LE(section.rawAddr + resDeltaSize, section.ptr + 20); }
  1616. // Set the virtual address of the section
  1617. fullHeader.writeUInt32LE(virtualAddress, section.ptr + 12); // Virtual address
  1618. newSections[i].virtualAddr = virtualAddress;
  1619. var virtualAddressPadding = (section.virtualSize % 4096);
  1620. virtualAddress += section.virtualSize;
  1621. if (virtualAddressPadding != 0) { virtualAddress += (4096 - virtualAddressPadding); }
  1622. }
  1623. }
  1624. // Make change to the data directories header to fix resource segment size and add/remove signature
  1625. const pePlusOffset = (obj.header.pe32plus == 0) ? 0 : 16; // This header is the same for 32 and 64 bit, but 64bit is offset by 16 bytes.
  1626. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.exportTable.addr), obj.header.peOptionalHeaderLocation + 96 + pePlusOffset);
  1627. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.importTable.addr), obj.header.peOptionalHeaderLocation + 104 + pePlusOffset);
  1628. fullHeader.writeUInt32LE(rsrcSectionVirtualSize, obj.header.peOptionalHeaderLocation + 116 + pePlusOffset); // Change the resource segment size
  1629. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.exceptionTableAddr.addr), obj.header.peOptionalHeaderLocation + 120 + pePlusOffset);
  1630. fullHeader.writeUInt32LE(0, obj.header.peOptionalHeaderLocation + 128 + pePlusOffset); // certificate table addr (TODO)
  1631. fullHeader.writeUInt32LE(0, obj.header.peOptionalHeaderLocation + 132 + pePlusOffset); // certificate table size (TODO)
  1632. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.baseRelocationTable.addr), obj.header.peOptionalHeaderLocation + 136 + pePlusOffset);
  1633. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.debug.addr), obj.header.peOptionalHeaderLocation + 144 + pePlusOffset);
  1634. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.globalPtr.addr), obj.header.peOptionalHeaderLocation + 160 + pePlusOffset);
  1635. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.tLSTable.addr), obj.header.peOptionalHeaderLocation + 168 + pePlusOffset);
  1636. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.loadConfigTable.addr), obj.header.peOptionalHeaderLocation + 176 + pePlusOffset);
  1637. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.boundImport.addr), obj.header.peOptionalHeaderLocation + 184 + pePlusOffset);
  1638. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.iAT.addr), obj.header.peOptionalHeaderLocation + 192 + pePlusOffset);
  1639. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.delayImportDescriptor.addr), obj.header.peOptionalHeaderLocation + 200 + pePlusOffset);
  1640. fullHeader.writeUInt32LE(correctDirectoryValue(oldSections, newSections, obj.header.dataDirectories.clrRuntimeHeader.addr), obj.header.peOptionalHeaderLocation + 208 + pePlusOffset);
  1641. // Write size of image. We put the next virtual address.
  1642. fullHeader.writeUInt32LE(virtualAddress, obj.header.peOptionalHeaderLocation + 56); // sizeOfImage
  1643. // Write the entire header to the destination file
  1644. //console.log('Write header', fullHeader.length, written);
  1645. fs.writeSync(output, fullHeader);
  1646. written += fullHeader.length;
  1647. // Write the entire executable until the start to the resource segment
  1648. var totalWrite = resPtr;
  1649. //console.log('Write until res', totalWrite, written);
  1650. while ((totalWrite - written) > 0) {
  1651. tmp = readFileSlice(written, Math.min(totalWrite - written, 65536));
  1652. fs.writeSync(output, tmp);
  1653. written += tmp.length;
  1654. }
  1655. // Write the new resource section
  1656. fs.writeSync(output, rsrcSection);
  1657. written += rsrcSection.length;
  1658. //console.log('Write res', rsrcSection.length, written);
  1659. // Write until the signature block
  1660. if (obj.header.sigpos > 0) {
  1661. // Since the original file was signed, write from the end of the resources to the start of the signature block.
  1662. totalWrite = obj.header.sigpos + resDeltaSize;
  1663. } else {
  1664. // The original file was not signed, write from the end of the resources to the end of the file.
  1665. totalWrite = obj.filesize + resDeltaSize;
  1666. }
  1667. //console.log('Write until signature', totalWrite, written);
  1668. while ((totalWrite - written) > 0) {
  1669. tmp = readFileSlice(written - resDeltaSize, Math.min(totalWrite - written, 65536));
  1670. fs.writeSync(output, tmp);
  1671. written += tmp.length;
  1672. }
  1673. //console.log('Write to signature', written);
  1674. // Write the signature if needed
  1675. if (cert != null) {
  1676. //if (cert == null) { cert = createSelfSignedCert({ cn: 'Test' }); }
  1677. // Set the hash algorithm hash OID
  1678. var hashOid = null, fileHash = null;
  1679. if (args.hash == null) { args.hash = 'sha384'; }
  1680. if (args.hash == 'sha256') { hashOid = forge.pki.oids.sha256; fileHash = obj.getHashOfFile(output, 'sha256', written); }
  1681. if (args.hash == 'sha384') { hashOid = forge.pki.oids.sha384; fileHash = obj.getHashOfFile(output, 'sha384', written); }
  1682. if (args.hash == 'sha512') { hashOid = forge.pki.oids.sha512; fileHash = obj.getHashOfFile(output, 'sha512', written); }
  1683. if (args.hash == 'sha224') { hashOid = forge.pki.oids.sha224; fileHash = obj.getHashOfFile(output, 'sha224', written); }
  1684. if (args.hash == 'md5') { hashOid = forge.pki.oids.md5; fileHash = obj.getHashOfFile(output, 'md5', written); }
  1685. if (hashOid == null) { func('Bad hash method OID'); return; }
  1686. // Create the signature block
  1687. var xp7 = forge.pkcs7.createSignedData();
  1688. var content = { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 6, 'constructed': false, 'composed': false, 'value': forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.15').data }, { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 3, 'constructed': false, 'composed': false, 'value': '\u0000', 'bitStringContents': '\u0000', 'original': { 'tagClass': 0, 'type': 3, 'constructed': false, 'composed': false, 'value': '\u0000' } }, { 'tagClass': 128, 'type': 0, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 2, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': '' }] }] }] }] }, { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 0, 'type': 6, 'constructed': false, 'composed': false, 'value': forge.asn1.oidToDer(hashOid).data }, { 'tagClass': 0, 'type': 5, 'constructed': false, 'composed': false, 'value': '' }] }, { 'tagClass': 0, 'type': 4, 'constructed': false, 'composed': false, 'value': fileHash.toString('binary') }] }] };
  1689. xp7.contentInfo = forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.SEQUENCE, true, [forge.asn1.create(forge.asn1.Class.UNIVERSAL, forge.asn1.Type.OID, false, forge.asn1.oidToDer('1.3.6.1.4.1.311.2.1.4').getBytes())]);
  1690. xp7.contentInfo.value.push(forge.asn1.create(forge.asn1.Class.CONTEXT_SPECIFIC, 0, true, [content]));
  1691. xp7.content = {}; // We set .contentInfo and have .content empty to bypass node-forge limitation on the type of content it can sign.
  1692. xp7.addCertificate(cert.cert);
  1693. if (cert.extraCerts) { for (var i = 0; i < cert.extraCerts.length; i++) { xp7.addCertificate(cert.extraCerts[0]); } } // Add any extra certificates that form the cert chain
  1694. // Build authenticated attributes
  1695. var authenticatedAttributes = [
  1696. { type: forge.pki.oids.contentType, value: forge.pki.oids.data },
  1697. { type: forge.pki.oids.messageDigest } // This value will populated at signing time by node-forge
  1698. ]
  1699. if ((typeof args.desc == 'string') || (typeof args.url == 'string')) {
  1700. var codeSigningAttributes = { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [] };
  1701. if (args.desc != null) { // Encode description as big-endian unicode.
  1702. var desc = "", ucs = Buffer.from(args.desc, 'ucs2').toString()
  1703. for (var k = 0; k < ucs.length; k += 2) { desc += String.fromCharCode(ucs.charCodeAt(k + 1), ucs.charCodeAt(k)); }
  1704. codeSigningAttributes.value.push({ 'tagClass': 128, 'type': 0, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': desc }] });
  1705. }
  1706. if (args.url != null) { codeSigningAttributes.value.push({ 'tagClass': 128, 'type': 1, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': args.url }] }); }
  1707. authenticatedAttributes.push({ type: obj.Oids.SPC_SP_OPUS_INFO_OBJID, value: codeSigningAttributes });
  1708. }
  1709. // Add the signer and sign
  1710. xp7.addSigner({
  1711. key: cert.key,
  1712. certificate: cert.cert,
  1713. digestAlgorithm: forge.pki.oids.sha384,
  1714. authenticatedAttributes: authenticatedAttributes
  1715. });
  1716. xp7.sign();
  1717. var p7signature = Buffer.from(forge.pkcs7.messageToPem(xp7).split('-----BEGIN PKCS7-----')[1].split('-----END PKCS7-----')[0], 'base64');
  1718. //console.log('Signature', Buffer.from(p7signature, 'binary').toString('base64'));
  1719. if (args.time == null) {
  1720. // Write the signature block to the output executable without time stamp
  1721. writeExecutableEx(output, p7signature, written, func);
  1722. } else {
  1723. // Decode the signature block
  1724. var pkcs7der = null;
  1725. try { pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(p7signature)); } catch (ex) { func('' + ex); return; }
  1726. // To work around ForgeJS PKCS#7 limitation, this may break PKCS7 verify if ForgeJS adds support for it in the future
  1727. // Switch content type from "1.3.6.1.4.1.311.2.1.4" to "1.2.840.113549.1.7.1"
  1728. pkcs7der.value[1].value[0].value[2].value[0].value = forge.asn1.oidToDer(forge.pki.oids.data).data;
  1729. // Decode the PKCS7 message
  1730. var pkcs7 = p7.messageFromAsn1(pkcs7der);
  1731. // Create the timestamp request in DER format
  1732. const asn1 = forge.asn1;
  1733. const pkcs7dataOid = asn1.oidToDer('1.2.840.113549.1.7.1').data;
  1734. const microsoftCodeSigningOid = asn1.oidToDer('1.3.6.1.4.1.311.3.2.1').data;
  1735. const asn1obj =
  1736. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  1737. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, microsoftCodeSigningOid),
  1738. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  1739. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, pkcs7dataOid),
  1740. asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
  1741. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, pkcs7.rawCapture.signature.toString('binary')) // Signature here
  1742. ])
  1743. ])
  1744. ]);
  1745. // Re-decode the PKCS7 from the executable, this time, no workaround needed
  1746. try { pkcs7der = forge.asn1.fromDer(forge.util.createBuffer(p7signature)); } catch (ex) { func('' + ex); return; }
  1747. // Serialize an ASN.1 object to DER format in Base64
  1748. const requestBody = Buffer.from(asn1.toDer(asn1obj).data, 'binary').toString('base64');
  1749. // Make an HTTP request
  1750. const options = { url: args.time, proxy: args.proxy };
  1751. // Make a request to the time server
  1752. httpRequest(options, requestBody, function (err, data) {
  1753. if (err != null) { func(err); return; }
  1754. // Decode the timestamp signature block
  1755. var timepkcs7der = null;
  1756. try { timepkcs7der = forge.asn1.fromDer(forge.util.createBuffer(Buffer.from(data, 'base64').toString('binary'))); } catch (ex) { func("Unable to parse time-stamp response: " + ex); return; }
  1757. // Get the ASN1 certificates used to sign the timestamp and add them to the certs in the PKCS7 of the executable
  1758. // TODO: We could look to see if the certificate is already present in the executable
  1759. try {
  1760. var timeasn1Certs = timepkcs7der.value[1].value[0].value[3].value;
  1761. for (var i in timeasn1Certs) { pkcs7der.value[1].value[0].value[3].value.push(timeasn1Certs[i]); }
  1762. // Get the time signature and add it to the executables PKCS7
  1763. const timeasn1Signature = timepkcs7der.value[1].value[0].value[4];
  1764. const countersignatureOid = asn1.oidToDer('1.2.840.113549.1.9.6').data;
  1765. const asn1obj2 =
  1766. asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
  1767. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
  1768. asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, countersignatureOid),
  1769. timeasn1Signature
  1770. ])
  1771. ]);
  1772. pkcs7der.value[1].value[0].value[4].value[0].value.push(asn1obj2);
  1773. // Re-encode the executable signature block
  1774. const p7signature = Buffer.from(forge.asn1.toDer(pkcs7der).data, 'binary');
  1775. // Write the file with the signature block
  1776. writeExecutableEx(output, p7signature, written, func);
  1777. } catch (ex) { func('' + ex); return; } // Something failed
  1778. });
  1779. }
  1780. return;
  1781. }
  1782. // Close the file
  1783. fs.closeSync(output);
  1784. // Indicate success
  1785. func(null);
  1786. }
  1787. function writeExecutableEx(output, p7signature, written, func) {
  1788. // Quad Align the results, adding padding if necessary
  1789. var len = written + p7signature.length;
  1790. var padding = (8 - ((len) % 8)) % 8;
  1791. // Write the signature block header and signature
  1792. var win = Buffer.alloc(8); // WIN CERTIFICATE Structure
  1793. win.writeUInt32LE(p7signature.length + padding + 8); // DWORD length
  1794. win.writeUInt16LE(512, 4); // WORD revision
  1795. win.writeUInt16LE(2, 6); // WORD type
  1796. fs.writeSync(output, win);
  1797. fs.writeSync(output, p7signature);
  1798. if (padding > 0) { fs.writeSync(output, Buffer.alloc(padding, 0)); }
  1799. // Write the signature header
  1800. var addresstable = Buffer.alloc(8);
  1801. addresstable.writeUInt32LE(written);
  1802. addresstable.writeUInt32LE(8 + p7signature.length + padding, 4);
  1803. var signatureHeaderLocation = (obj.header.peHeaderLocation + 152 + (obj.header.pe32plus * 16));
  1804. fs.writeSync(output, addresstable, 0, 8, signatureHeaderLocation);
  1805. written += (p7signature.length + padding + 8); // Add the signature block to written counter
  1806. // Compute the checksum and write it in the PE header checksum location
  1807. var tmp = Buffer.alloc(4);
  1808. tmp.writeUInt32LE(runChecksumOnFile(output, written, ((obj.header.peOptionalHeaderLocation + 64) / 4)));
  1809. fs.writeSync(output, tmp, 0, 4, obj.header.peOptionalHeaderLocation + 64);
  1810. // Close the file
  1811. fs.closeSync(output);
  1812. // Indicate success
  1813. func(null);
  1814. }
  1815. // Return null if we could not open the file
  1816. return (openFile() ? obj : null);
  1817. }
  1818. function start() {
  1819. // Parse the arguments
  1820. const args = require('minimist')(process.argv.slice(2));
  1821. // Show tool help
  1822. if (process.argv.length < 3) {
  1823. console.log("MeshCentral Authenticode Tool.");
  1824. console.log("Usage:");
  1825. console.log(" node authenticode.js [command] [options]");
  1826. console.log("Commands:");
  1827. console.log(" info: Show information about an executable.");
  1828. console.log(" --exe [file] Required executable to view information.");
  1829. console.log(" --json Show information in JSON format.");
  1830. console.log(" sign: Sign an executable.");
  1831. console.log(" --exe [file] Required executable to sign.");
  1832. console.log(" --out [file] Resulting signed executable.");
  1833. console.log(" --pem [pemfile] Certificate & private key to sign the executable with.");
  1834. console.log(" --desc [description] Description string to embbed into signature.");
  1835. console.log(" --url [url] URL to embbed into signature.");
  1836. console.log(" --hash [method] Default is SHA384, possible value: MD5, SHA224, SHA256, SHA384 or SHA512.");
  1837. console.log(" --time [url] The time signing server URL.");
  1838. console.log(" --proxy [url] The HTTP proxy to use to contact the time signing server, must start with http://");
  1839. console.log(" unsign: Remove the signature from the executable.");
  1840. console.log(" --exe [file] Required executable to un-sign.");
  1841. console.log(" --out [file] Resulting executable with signature removed.");
  1842. console.log(" createcert: Create a code signging self-signed certificate and key.");
  1843. console.log(" --out [pemfile] Required certificate file to create.");
  1844. console.log(" --cn [value] Required certificate common name.");
  1845. console.log(" --country [value] Certificate country name.");
  1846. console.log(" --state [value] Certificate state name.");
  1847. console.log(" --locality [value] Certificate locality name.");
  1848. console.log(" --org [value] Certificate organization name.");
  1849. console.log(" --ou [value] Certificate organization unit name.");
  1850. console.log(" --serial [value] Certificate serial number.");
  1851. console.log(" timestamp: Add a signed timestamp to an already signed executable.");
  1852. console.log(" --exe [file] Required executable to timestamp.");
  1853. console.log(" --out [file] Resulting signed executable.");
  1854. console.log(" --time [url] The time signing server URL.");
  1855. console.log(" --proxy [url] The HTTP proxy to use to contact the time signing server, must start with http://");
  1856. console.log(" bitmaps: Show bitmap resources in the executable.");
  1857. console.log(" --exe [file] Input executable.");
  1858. console.log(" savebitmap: Save a single bitmap to a .bmp file.");
  1859. console.log(" --exe [file] Input executable.");
  1860. console.log(" --out [file] Resulting .ico file.");
  1861. console.log(" --bitmap [number] Bitmap number to save to file.");
  1862. console.log(" icons: Show the icon resources in the executable.");
  1863. console.log(" --exe [file] Input executable.");
  1864. console.log(" saveicon: Save a single icon bitmap to a .ico file.");
  1865. console.log(" --exe [file] Input executable.");
  1866. console.log(" --out [file] Resulting .ico file.");
  1867. console.log(" --icon [number] Icon number to save to file.");
  1868. console.log(" saveicons: Save an icon group to a .ico file.");
  1869. console.log(" --exe [file] Input executable.");
  1870. console.log(" --out [file] Resulting .ico file.");
  1871. console.log(" --icongroup [groupNumber] Icon groupnumber to save to file.");
  1872. console.log("");
  1873. console.log("Note that certificate PEM files must first have the signing certificate,");
  1874. console.log("followed by all certificates that form the trust chain.");
  1875. console.log("");
  1876. console.log("When doing sign/unsign, you can also change resource properties of the generated file.");
  1877. console.log("");
  1878. console.log(" --fileversionnumber n.n.n.n");
  1879. console.log(" --productversionnumber n.n.n.n");
  1880. console.log(" --filedescription [value]");
  1881. console.log(" --fileversion [value]");
  1882. console.log(" --internalname [value]");
  1883. console.log(" --legalcopyright [value]");
  1884. console.log(" --originalfilename [value]");
  1885. console.log(" --productname [value]");
  1886. console.log(" --productversion [value]");
  1887. console.log(" --removeicongroup [number]");
  1888. console.log(" --removebitmap [number]");
  1889. console.log(" --icon [groupNumber],[filename.ico]");
  1890. console.log(" --bitmap [number],[filename.bmp]");
  1891. return;
  1892. }
  1893. // Check that a valid command is passed in
  1894. if (['info', 'sign', 'unsign', 'createcert', 'icons', 'bitmaps', 'saveicon', 'saveicons', 'savebitmap', 'header', 'sections', 'timestamp', 'signblock'].indexOf(process.argv[2].toLowerCase()) == -1) {
  1895. console.log("Invalid command: " + process.argv[2]);
  1896. console.log("Valid commands are: info, sign, unsign, createcert, timestamp");
  1897. return;
  1898. }
  1899. var exe = null;
  1900. if (args.exe) {
  1901. // Check the file exists and open the file
  1902. var stats = null;
  1903. try { stats = require('fs').statSync(args.exe); } catch (ex) { }
  1904. if (stats == null) { console.log("Unable to executable open file: " + args.exe); return; }
  1905. exe = createAuthenticodeHandler(args.exe);
  1906. if (exe == null) { console.log("Unable to parse executable file: " + args.exe); return; }
  1907. }
  1908. // Parse the string resources and make any required changes
  1909. var resChanges = false, versionStrings = null;
  1910. if (exe != null) {
  1911. versionStrings = exe.getVersionInfo();
  1912. var versionProperties = ['FileDescription', 'FileVersion', 'InternalName', 'LegalCopyright', 'OriginalFilename', 'ProductName', 'ProductVersion'];
  1913. for (var i in versionProperties) {
  1914. const prop = versionProperties[i], propl = prop.toLowerCase();
  1915. if (args[propl] && (args[propl] != versionStrings[prop])) { versionStrings[prop] = args[propl]; resChanges = true; }
  1916. }
  1917. if (args['fileversionnumber'] != null) {
  1918. const fileVerSplit = args['fileversionnumber'].split('.');
  1919. if (fileVerSplit.length != 4) { console.log("--fileversionnumber must be of format n.n.n.n, for example: 1.2.3.4"); return; }
  1920. for (var i in fileVerSplit) { var n = parseInt(fileVerSplit[i]); if ((n < 0) || (n > 65535)) { console.log("--fileversionnumber numbers must be between 0 and 65535."); return; } }
  1921. if (args['fileversionnumber'] != versionStrings['~FileVersion']) { versionStrings['~FileVersion'] = args['fileversionnumber']; resChanges = true; }
  1922. }
  1923. if (args['productversionnumber'] != null) {
  1924. const productVerSplit = args['productversionnumber'].split('.');
  1925. if (productVerSplit.length != 4) { console.log("--productversionnumber must be of format n.n.n.n, for example: 1.2.3.4"); return; }
  1926. for (var i in productVerSplit) { var n = parseInt(productVerSplit[i]); if ((n < 0) || (n > 65535)) { console.log("--productversionnumber numbers must be between 0 and 65535."); return; } }
  1927. if (args['productversionnumber'] != versionStrings['~ProductVersion']) { versionStrings['~ProductVersion'] = args['productversionnumber']; resChanges = true; }
  1928. }
  1929. if (resChanges == true) { exe.setVersionInfo(versionStrings); }
  1930. }
  1931. // Parse the icon changes
  1932. resChanges = false;
  1933. var icons = null, bitmaps = null;
  1934. if (exe != null) {
  1935. icons = exe.getIconInfo();
  1936. bitmaps = exe.getBitmapInfo();
  1937. if (typeof args['removeicongroup'] == 'string') { // If --removeicongroup is used, it's to remove an existing icon group
  1938. const groupsToRemove = args['removeicongroup'].split(',');
  1939. for (var i in groupsToRemove) { if (icons[groupsToRemove[i]] != null) { delete icons[groupsToRemove[i]]; resChanges = true; } }
  1940. } else if (typeof args['removeicongroup'] == 'number') {
  1941. if (icons[args['removeicongroup']] != null) { delete icons[args['removeicongroup']]; resChanges = true; }
  1942. }
  1943. if (typeof args['removebitmap'] == 'string') { // If --removebitmap is used
  1944. const bitmapsToRemove = args['removebitmap'].split(',');
  1945. for (var i in bitmapsToRemove) { if (bitmaps[bitmapsToRemove[i]] != null) { delete bitmaps[bitmapsToRemove[i]]; resChanges = true; } }
  1946. } else if (typeof args['removebitmap'] == 'number') {
  1947. if (bitmaps[args['removebitmap']] != null) { delete bitmaps[args['removebitmap']]; resChanges = true; }
  1948. }
  1949. if (typeof args['icon'] == 'string') { // If --icon is used, it's to add or replace an existing icon group
  1950. const iconToAddSplit = args['icon'].split(',');
  1951. if (iconToAddSplit.length != 2) { console.log("The --icon format is: --icon [number],[file]."); return; }
  1952. const iconName = parseInt(iconToAddSplit[0]);
  1953. const iconFile = iconToAddSplit[1];
  1954. const icon = loadIcon(iconFile);
  1955. if (icon == null) { console.log("Unable to load icon: " + iconFile); return; }
  1956. if (icons[iconName] != null) {
  1957. const iconHash = hashObject(icon); // Compute the new icon group hash
  1958. const iconHash2 = hashObject(icons[iconName]); // Computer the old icon group hash
  1959. if (iconHash != iconHash2) { icons[iconName] = icon; resChanges = true; } // If different, replace the icon group
  1960. } else {
  1961. icons[iconName] = icon; // We are adding an icon group
  1962. resChanges = true;
  1963. }
  1964. }
  1965. if (typeof args['bitmap'] == 'string') { // If --bitmap is used, it's to add or replace an existing bitmap
  1966. const bitmapToAddSplit = args['bitmap'].split(',');
  1967. if (bitmapToAddSplit.length != 2) { console.log("The --bitmap format is: --bitmap [number],[file]."); return; }
  1968. const bitmapName = parseInt(bitmapToAddSplit[0]);
  1969. const bitmapFile = bitmapToAddSplit[1];
  1970. const bitmap = loadBitmap(bitmapFile);
  1971. if (bitmap == null) { console.log("Unable to load bitmap: " + bitmapFile); return; }
  1972. if (bitmaps[bitmapName] != null) {
  1973. const bitmapHash = hashObject(bitmap); // Compute the new bitmap hash
  1974. const bitmapHash2 = hashObject(bitmaps[bitmapName]); // Computer the old bitmap hash
  1975. if (bitmapHash != bitmapHash2) { bitmaps[bitmapName] = bitmap; resChanges = true; } // If different, replace the new bitmap
  1976. } else {
  1977. bitmaps[bitmapName] = bitmap; // We are adding an new bitmap
  1978. resChanges = true;
  1979. }
  1980. }
  1981. if (resChanges == true) {
  1982. exe.setIconInfo(icons);
  1983. exe.setBitmapInfo(bitmaps);
  1984. }
  1985. }
  1986. // Execute the command
  1987. var command = process.argv[2].toLowerCase();
  1988. if (command == 'info') { // Get signature information about an executable
  1989. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  1990. if (args.json) {
  1991. var r = {}, stringInfo = exe.getVersionInfo();
  1992. if (stringInfo != null) {
  1993. r.versionInfo = {};
  1994. r.stringInfo = {};
  1995. for (var i in stringInfo) { if (i.startsWith('~')) { r.versionInfo[i.substring(1)] = stringInfo[i]; } else { r.stringInfo[i] = stringInfo[i]; } }
  1996. }
  1997. if (exe.fileHashAlgo != null) {
  1998. r.signture = {};
  1999. if (exe.fileHashAlgo != null) { r.signture.hashMethod = exe.fileHashAlgo; }
  2000. if (exe.fileHashSigned != null) { r.signture.hashSigned = exe.fileHashSigned.toString('hex'); }
  2001. if (exe.fileHashActual != null) { r.signture.hashActual = exe.fileHashActual.toString('hex'); }
  2002. if (exe.signingAttribs && exe.signingAttribs.length > 0) { r.signture.attributes = exe.signingAttribs; }
  2003. }
  2004. console.log(JSON.stringify(r, null, 2));
  2005. } else {
  2006. var versionInfo = exe.getVersionInfo();
  2007. if (versionInfo != null) {
  2008. console.log("Version Information:");
  2009. for (var i in versionInfo) { if (i.startsWith('~') == true) { console.log(' ' + i.substring(1) + ': ' + versionInfo[i] + ''); } }
  2010. console.log("String Information:");
  2011. for (var i in versionInfo) { if (i.startsWith('~') == false) { if (versionInfo[i] == null) { console.log(' ' + i + ': (Empty)'); } else { console.log(' ' + i + ': \"' + versionInfo[i] + '\"'); } } }
  2012. }
  2013. console.log("Checksum Information:");
  2014. console.log(" Header CheckSum: 0x" + exe.header.peWindows.checkSum.toString(16));
  2015. console.log(" Actual CheckSum: 0x" + exe.header.peWindows.checkSumActual.toString(16));
  2016. console.log("Signature Information:");
  2017. if (exe.fileHashAlgo != null) {
  2018. console.log(" Hash Method:", exe.fileHashAlgo);
  2019. if (exe.fileHashSigned != null) { console.log(" Signed Hash:", exe.fileHashSigned.toString('hex')); }
  2020. if (exe.fileHashActual != null) { console.log(" Actual Hash:", exe.fileHashActual.toString('hex')); }
  2021. } else {
  2022. console.log(" This file is not signed.");
  2023. }
  2024. if (exe.signingAttribs && exe.signingAttribs.length > 0) { console.log("Signature Attributes:"); for (var i in exe.signingAttribs) { console.log(' ' + exe.signingAttribs[i]); } }
  2025. }
  2026. }
  2027. if (command == 'header') { // Display the full executable header in JSON format
  2028. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2029. console.log(exe.header);
  2030. // Check that the header is valid
  2031. var ptr = 1024, sizeOfCode = 0, sizeOfInitializedData = 0;
  2032. for (var i in exe.header.sections) {
  2033. if (i == '.text') { sizeOfCode += exe.header.sections[i].rawSize; } else { sizeOfInitializedData += exe.header.sections[i].rawSize; }
  2034. if (exe.header.sections[i].rawAddr != ptr) { console.log('WARNING: ' + i + ' section should have a rawAddr or ' + ptr + ', but has ' + exe.header.sections[i].rawAddr + ' instead.'); }
  2035. ptr += exe.header.sections[i].rawSize;
  2036. }
  2037. if (exe.header.peStandard.sizeOfCode != sizeOfCode) { console.log('WARNING: Size of code is ' + exe.header.peStandard.sizeOfCode + ', should be ' + sizeOfCode + '.'); }
  2038. if (exe.header.peStandard.sizeOfInitializedData != sizeOfInitializedData) { console.log('WARNING: Size of initialized data is ' + exe.header.peStandard.sizeOfInitializedData + ', should be ' + sizeOfInitializedData + '.'); }
  2039. }
  2040. if (command == 'sections') { // Display sections in CSV format
  2041. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2042. var csvHeader = 'section';
  2043. for (var i in exe.header.sections['.text']) { csvHeader += ',' + i; }
  2044. console.log(csvHeader);
  2045. for (var i in exe.header.sections) {
  2046. var csvData = i;
  2047. for (var j in exe.header.sections[i]) { csvData += ',' + exe.header.sections[i][j]; }
  2048. console.log(csvData);
  2049. }
  2050. }
  2051. if (command == 'sign') { // Sign an executable
  2052. if (typeof args.exe != 'string') { console.log("Missing --exe [filename]"); return; }
  2053. if (typeof args.hash == 'string') { args.hash = args.hash.toLowerCase(); if (['md5', 'sha224', 'sha256', 'sha384', 'sha512'].indexOf(args.hash) == -1) { console.log("Invalid hash method, must be SHA256 or SHA384"); return; } }
  2054. if (args.hash == null) { args.hash = 'sha384'; }
  2055. createOutFile(args, args.exe);
  2056. var cert = loadCertificates(args.pem);
  2057. if (cert == null) { console.log("Unable to load certificate and/or private key, generating test certificate."); cert = createSelfSignedCert({ cn: 'Test' }); }
  2058. if (resChanges == false) {
  2059. console.log("Signing to " + args.out);
  2060. exe.sign(cert, args, function (err) { // Simple signing, copy most of the original file.
  2061. if (err == null) { console.log("Done."); } else { console.log(err); }
  2062. if (exe != null) { exe.close(); }
  2063. });
  2064. return;
  2065. } else {
  2066. console.log("Changing resources and signing to " + args.out);
  2067. exe.writeExecutable(args, cert, function (err) { // Signing with resources decoded and re-encoded.
  2068. if (err == null) { console.log("Done."); } else { console.log(err); }
  2069. if (exe != null) { exe.close(); }
  2070. });
  2071. return;
  2072. }
  2073. }
  2074. if (command == 'unsign') { // Unsign an executable
  2075. if (typeof args.exe != 'string') { console.log("Missing --exe [filename]"); return; }
  2076. createOutFile(args, args.exe);
  2077. if (resChanges == false) {
  2078. if (exe.header.signed) {
  2079. console.log("Unsigning to " + args.out);
  2080. exe.unsign(args); // Simple unsign, copy most of the original file.
  2081. console.log("Done.");
  2082. } else {
  2083. console.log("Executable is not signed.");
  2084. }
  2085. } else {
  2086. console.log("Changing resources and unsigning to " + args.out);
  2087. exe.writeExecutable(args, null, function (err) { // Unsigning with resources decoded and re-encoded.
  2088. if (err == null) { console.log("Done."); } else { console.log(err); }
  2089. if (exe != null) { exe.close(); }
  2090. });
  2091. }
  2092. }
  2093. if (command == 'createcert') { // Create a code signing certificate and private key
  2094. if (typeof args.out != 'string') { console.log("Missing --out [filename]"); return; }
  2095. if (typeof args.cn != 'string') { console.log("Missing --cn [name]"); return; }
  2096. if (typeof args.serial == 'string') { if (args.serial != parseInt(args.serial)) { console.log("Invalid serial number."); return; } else { args.serial = parseInt(args.serial); } }
  2097. if (typeof args.serial == 'number') { args.serial = '0' + args.serial; } // Serial number must be a integer string with a single leading '0'
  2098. const cert = createSelfSignedCert(args);
  2099. console.log("Writing to " + args.out);
  2100. fs.writeFileSync(args.out, pki.certificateToPem(cert.cert) + '\r\n' + pki.privateKeyToPem(cert.key));
  2101. console.log("Done.");
  2102. }
  2103. if (command == 'bitmaps') { // Show bitmaps in the executable
  2104. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2105. if (args.json) {
  2106. var bitmapInfo = exe.getBitmapInfo();
  2107. console.log(JSON.stringify(bitmapInfo, null, 2));
  2108. } else {
  2109. var bitmapInfo = exe.getBitmapInfo();
  2110. if (bitmapInfo != null) {
  2111. console.log("Bitmap Information:");
  2112. for (var i in bitmapInfo) { console.log(' ' + i + ': ' + bitmapInfo[i].length + ' byte' + ((bitmapInfo[i].length > 1) ? 's' : '') + '.'); }
  2113. }
  2114. }
  2115. }
  2116. if (command == 'savebitmap') { // Save an bitmap to file
  2117. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2118. if (typeof args.out != 'string') { console.log("Missing --out [filename]"); return; }
  2119. if (typeof args.bitmap != 'number') { console.log("Missing or incorrect --bitmap [number]"); return; }
  2120. const bitmapInfo = exe.getBitmapInfo();
  2121. if (bitmapInfo[args.bitmap] == null) { console.log("Unknown bitmap: " + args.bitmap); return; }
  2122. console.log("Writing to " + args.out);
  2123. var bitmapHeader = Buffer.from('424D000000000000000036000000', 'hex');
  2124. bitmapHeader.writeUInt32LE(14 + bitmapInfo[args.bitmap].length, 2); // Write the full size of the bitmap file
  2125. fs.writeFileSync(args.out, Buffer.concat([bitmapHeader, bitmapInfo[args.bitmap]]));
  2126. console.log("Done.");
  2127. }
  2128. if (command == 'icons') { // Show icons in the executable
  2129. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2130. if (args.json) {
  2131. var r = {}, iconInfo = exe.getIconInfo();
  2132. if (iconInfo != null) { r.iconInfo = iconInfo; }
  2133. console.log(JSON.stringify(r, null, 2));
  2134. } else {
  2135. var iconInfo = exe.getIconInfo();
  2136. if (iconInfo != null) {
  2137. console.log("Icon Information:");
  2138. for (var i in iconInfo) { console.log(' Group ' + i + ':'); for (var j in iconInfo[i].icons) { console.log(' Icon ' + j + ': ' + ((iconInfo[i].icons[j].width == 0) ? 256 : iconInfo[i].icons[j].width) + 'x' + ((iconInfo[i].icons[j].height == 0) ? 256 : iconInfo[i].icons[j].height) + ', size: ' + iconInfo[i].icons[j].icon.length); } }
  2139. }
  2140. }
  2141. }
  2142. if (command == 'saveicon') { // Save an icon to file
  2143. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2144. if (typeof args.out != 'string') { console.log("Missing --out [filename]"); return; }
  2145. if (typeof args.icon != 'number') { console.log("Missing or incorrect --icon [number]"); return; }
  2146. const iconInfo = exe.getIconInfo();
  2147. var icon = null;
  2148. for (var i in iconInfo) { if (iconInfo[i].icons[args.icon]) { icon = iconInfo[i].icons[args.icon]; } }
  2149. if (icon == null) { console.log("Unknown icon: " + args.icon); return; }
  2150. // .ico header: https://en.wikipedia.org/wiki/ICO_(file_format)
  2151. var buf = Buffer.alloc(22);
  2152. buf.writeUInt16LE(1, 2); // 1 = Icon, 2 = Cursor
  2153. buf.writeUInt16LE(1, 4); // Icon Count, always 1 in our case
  2154. buf[6] = icon.width; // Width (0 = 256)
  2155. buf[7] = icon.height; // Height (0 = 256)
  2156. buf[8] = icon.colorCount; // Colors
  2157. buf.writeUInt16LE(icon.planes, 10); // Color planes
  2158. buf.writeUInt16LE(icon.bitCount, 12); // Bits per pixel
  2159. buf.writeUInt32LE(icon.icon.length, 14); // Size
  2160. buf.writeUInt32LE(22, 18); // Offset, always 22 in our case
  2161. console.log("Writing to " + args.out);
  2162. fs.writeFileSync(args.out, Buffer.concat([buf, icon.icon]));
  2163. console.log("Done.");
  2164. }
  2165. if (command == 'saveicons') { // Save an icon group to file
  2166. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2167. if (typeof args.out != 'string') { console.log("Missing --out [filename]"); return; }
  2168. if (typeof args.icongroup != 'number') { console.log("Missing or incorrect --icongroup [number]"); return; }
  2169. const iconInfo = exe.getIconInfo();
  2170. const iconGroup = iconInfo[args.icongroup];
  2171. if (iconGroup == null) { console.log("Invalid or incorrect --icongroup [number]"); return; }
  2172. // Count the number of icons in the group
  2173. var iconCount = 0;
  2174. for (var i in iconGroup.icons) { iconCount++; }
  2175. // .ico header: https://en.wikipedia.org/wiki/ICO_(file_format)
  2176. const iconFileData = [];
  2177. const header = Buffer.alloc(6);
  2178. header.writeUInt16LE(1, 2); // 1 = Icon, 2 = Cursor
  2179. header.writeUInt16LE(iconCount, 4); // Icon Count, always 1 in our case
  2180. iconFileData.push(header);
  2181. // Store each icon header
  2182. var offsetPtr = 6 + (16 * iconCount);
  2183. for (var i in iconGroup.icons) {
  2184. const buf = Buffer.alloc(16);
  2185. buf[0] = iconGroup.icons[i].width; // Width (0 = 256)
  2186. buf[1] = iconGroup.icons[i].height; // Height (0 = 256)
  2187. buf[2] = iconGroup.icons[i].colorCount; // Colors
  2188. buf.writeUInt16LE(iconGroup.icons[i].planes, 4); // Color planes
  2189. buf.writeUInt16LE(iconGroup.icons[i].bitCount, 6); // Bits per pixel
  2190. buf.writeUInt32LE(iconGroup.icons[i].icon.length, 8); // Size
  2191. buf.writeUInt32LE(offsetPtr, 12); // Offset
  2192. offsetPtr += iconGroup.icons[i].icon.length;
  2193. iconFileData.push(buf);
  2194. }
  2195. // Store each icon
  2196. for (var i in iconGroup.icons) { iconFileData.push(iconGroup.icons[i].icon); }
  2197. // Write the .ico file
  2198. console.log("Writing to " + args.out);
  2199. fs.writeFileSync(args.out, Buffer.concat(iconFileData));
  2200. console.log("Done.");
  2201. }
  2202. if (command == 'signblock') { // Display the raw signature block of the executable in hex
  2203. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2204. var buf = exe.getRawSignatureBlock();
  2205. if (buf == null) { console.log("Executable is not signed."); return } else { console.log(buf.toString('hex')); return }
  2206. }
  2207. if (command == 'timestamp') {
  2208. if (exe == null) { console.log("Missing --exe [filename]"); return; }
  2209. if (exe.signature == null) { console.log("Executable is not signed."); return; }
  2210. if (typeof args.time != 'string') { console.log("Missing --time [url]"); return; }
  2211. createOutFile(args, args.exe);
  2212. console.log("Requesting time signature...");
  2213. exe.timeStampRequest(args, function (err) {
  2214. if (err == null) { console.log("Done."); } else { console.log(err); }
  2215. if (exe != null) { exe.close(); }
  2216. })
  2217. return;
  2218. }
  2219. // Close the file
  2220. if (exe != null) { exe.close(); }
  2221. }
  2222. // If this is the main module, run the command line version
  2223. if (require.main === module) { start(); }
  2224. // Exports
  2225. module.exports.createAuthenticodeHandler = createAuthenticodeHandler;
  2226. module.exports.loadCertificates = loadCertificates;
  2227. module.exports.loadIcon = loadIcon;
  2228. module.exports.loadBitmap = loadBitmap;
  2229. module.exports.hashObject = hashObject;