4

I use signtool.exe to codesign a Windows executable.

Which options of this tool can be used to define that a few bytes should be omitted from the hash calculation, and thus allowing that a digitally signed executable can have 8 or 16 bytes modified later?

This technique has been used by Mozilla (their .exe installer is different for each download, but has the same digital signature), see How can a .exe be modified and still keep a valid digital signature?.

The WinAPI function ImageGetDigestStream has an option DigestLevel to exclude resource information from the hash computation, but how to use this option when actually with signtool.exe or a similar tool?

7
  • What bytes are included into a signature is fixed because the signer and the verifier have to use the same specification. To my understanding Mozilla simply places the changes in fields that are by default not covered by the Authenticode signature. Commented Mar 22, 2022 at 8:35
  • I don't think signtool won't help you there. Here are a few pointers that explain this: isc.sans.edu/diary/It%27s+in+the+signature./22928, blog.didierstevens.com/2008/01/11/… and a tool blog.didierstevens.com/programs/disitool Commented Mar 22, 2022 at 9:07
  • Thank you @SimonMourier. "It is not a tool to digitally sign executables, use signtool for this." How does disitool work, can you maybe post an answer with an example? Is there a difference between "authenticode signature" (something more specific) and general digital signature? Commented Mar 22, 2022 at 10:38
  • @Robert Which fields are by default not covered by Authenticode signature, and how to do this on any exe file? Commented Mar 22, 2022 at 10:39
  • They are mentioned in the linked answer in your question. You can also check how Mozilla did it, just get multiple versions and compare them. Commented Mar 22, 2022 at 10:45

1 Answer 1

3

This is explained by Didier Stevens in an article here: It's in the signature and he also provides a tool "disitool" to manipulate the signature.

Here are the steps to sign a file using Authenticode, and to append some data to it without breaking the signature:

A) Create a code siging certificate (you'll be required to enter passwords):

MakeCert /n "CN=MyOrg" /r /h 0 /eku "1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13" /sv MyOrg.pvk MyOrg.cer

note: 1.3.6.1.5.5.7.3.3 is szOID_PKIX_KP_CODE_SIGNING and 1.3.6.1.4.1.311.10.3.13 is szOID_KP_LIFETIME_SIGNING. This will create a private key file and a certificate file.

B) Add certificate to store (needs admin rights, could be a different store):

Certutil -addStore TrustedPeople MyOrg.cer

C) Create a Pfx file to sign:

Pvk2Pfx /pvk MyOrg.pvk /pi [Password goes here] /spc MyOrg.cer /pfx MyOrg.pfx

D) Sign your file:

SignTool.exe sign /fd SHA256 /v /a /f MyOrg.pfx /p [Password goes here] MyFile.exe

At this point MyFile.exe is signed using Authenticode:

enter image description here

E) Create some data.txt file. I've created one that just contains the "[Kilroy was here!]" text.

F) Now run disitool:

python.exe disitool.py inject --paddata MyFile.exe data.txt MyFile2.exe

And here is the result, MyFile2.exe is still valid without resigning the file:

enter image description here

While you can see data.txt's content added to its end:

enter image description here

From the original file's end:

enter image description here

In my answer here How to read altered certificate data using WinApi?, I explain how to read the extra data using the Windows API.

Sign up to request clarification or add additional context in comments.

2 Comments

Tested, it works, thank you! In fact this script adds the bytes at the end of the exe, 8-byte padded + updates a checksum metadata in the beginning of the exe file.
If someone know, how to read this added data, please, post an answer in this thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.