Authenticode.js fix when changing resources and signing an un-signed executable.

Ylian Saint-Hilaire committed Jun 20, 2022 at 13:04 UTC 63de362bc00e825d7a6ca57076e8c5a052e02cfe
1 file changed +17 -6
authenticode.js
+17 -6
@@ -1240,7 +1240,7 @@ function createAuthenticodeHandler(path) {
1240 if ((typeof args.desc == 'string') || (typeof args.url == 'string')) {
1241 var codeSigningAttributes = { 'tagClass': 0, 'type': 16, 'constructed': true, 'composed': true, 'value': [] };
1242 if (args.desc != null) { // Encode description as big-endian unicode.
1243 - var desc = '', ucs = Buffer.from(args.desc, 'ucs2').toString()
1243 + var desc = "", ucs = Buffer.from(args.desc, 'ucs2').toString()
1244 for (var k = 0; k < ucs.length; k += 2) { desc += String.fromCharCode(ucs.charCodeAt(k + 1), ucs.charCodeAt(k)); }
1245 codeSigningAttributes.value.push({ 'tagClass': 128, 'type': 0, 'constructed': true, 'composed': true, 'value': [{ 'tagClass': 128, 'type': 0, 'constructed': false, 'composed': false, 'value': desc }] });
1246 }
@@ -1486,13 +1486,13 @@ function createAuthenticodeHandler(path) {
1486 }
1487
1488 // Write the entire header to the destination file
1489 - //console.log('Write header', fullHeader.length);
1489 + //console.log('Write header', fullHeader.length, written);
1490 fs.writeSync(output, fullHeader);
1491 written += fullHeader.length;
1492
1493 // Write the entire executable until the start to the resource segment
1494 var totalWrite = resPtr;
1495 - //console.log('Write until res', totalWrite);
1495 + //console.log('Write until res', totalWrite, written);
1496 while ((totalWrite - written) > 0) {
1497 tmp = readFileSlice(written, Math.min(totalWrite - written, 65536));
1498 fs.writeSync(output, tmp);
@@ -1503,15 +1503,24 @@ function createAuthenticodeHandler(path) {
1503 var rsrcSection = generateResourceSection(obj.resources);
1504 fs.writeSync(output, rsrcSection);
1505 written += rsrcSection.length;
1506 + //console.log('Write res', rsrcSection.length, written);
1507
1508 // Write until the signature block
1508 - totalWrite = obj.header.sigpos + resDeltaSize;
1509 - //console.log('Write until signature', totalWrite);
1509 + if (obj.header.sigpos > 0) {
1510 + // Since the original file was signed, write from the end of the resources to the start of the signature block.
1511 + totalWrite = obj.header.sigpos + resDeltaSize;
1512 + } else {
1513 + // The original file was not signed, write from the end of the resources to the end of the file.
1514 + totalWrite = obj.filesize + resDeltaSize;
1515 + }
1516 +
1517 + //console.log('Write until signature', totalWrite, written);
1518 while ((totalWrite - written) > 0) {
1519 tmp = readFileSlice(written - resDeltaSize, Math.min(totalWrite - written, 65536));
1520 fs.writeSync(output, tmp);
1521 written += tmp.length;
1522 }
1523 + //console.log('Write to signature', written);
1524
1525 // Write the signature if needed
1526 if (cert != null) {
@@ -1829,14 +1838,16 @@ function start() {
1838 if (err == null) { console.log("Done."); } else { console.log(err); }
1839 if (exe != null) { exe.close(); }
1840 });
1841 + return;
1842 } else {
1843 console.log("Changing resources and signing to " + args.out);
1844 exe.writeExecutable(args, cert, function (err) { // Signing with resources decoded and re-encoded.
1845 if (err == null) { console.log("Done."); } else { console.log(err); }
1846 if (exe != null) { exe.close(); }
1847 });
1848 + return;
1849 }
1839 - return;
1850 + console.log("Done.");
1851 }
1852 if (command == 'unsign') { // Unsign an executable
1853 if (typeof args.exe != 'string') { console.log("Missing --exe [filename]"); return; }