Format:PE (Portable Executable)/Authenticode
How a PE file carries a code signature: data directory 4 points (by file offset) to WIN_CERTIFICATE records holding a PKCS#7 SignedData whose content is an SpcIndirectDataContent with the file's Authenticode digest, optional page hashes, timestamps and nested signatures.
Certificate table
Data directory 4 (security) has a file offset and size, not an RVA. The table normally sits at the very end of the file (8-byte aligned) and is never mapped. It is a sequence of WIN_CERTIFICATE records, each padded to 8 bytes:
| Offset | Size | Field |
|---|---|---|
| 0 | 4 | dwLength, size of this record including the 8-byte header (before padding) |
| 4 | 2 | wRevision: 0x0100 (old) or 0x0200 (WIN_CERT_REVISION_2_0, current)
|
| 6 | 2 | wCertificateType: 1 X.509, 2 WIN_CERT_TYPE_PKCS_SIGNED_DATA (Authenticode), 3 reserved, 4 TS_STACK_SIGNED
|
| 8 | dwLength - 8 | bCertificate: a DER PKCS#7 ContentInfo |
A file may hold several records. Remove a signature by zeroing the directory entry and truncating the file; the CheckSum in the optional header then needs recomputing because the table is included in it.
The signature blob
The blob is ContentInfo { signedData 1.2.840.113549.1.7.2, SignedData }, with the following Microsoft-specific parts:
- The encapsulated content type is
SPC_INDIRECT_DATA_CONTENT(1.3.6.1.4.1.311.2.1.4), aSpcIndirectDataContent { data SpcAttributeTypeAndOptionalValue, messageDigest DigestInfo }. Thedatatype isSPC_PE_IMAGE_DATA(1.3.6.1.4.1.311.2.1.15) holding a flags bit string and anSpcLink(normally a legacy empty file link, or anSpcSerializedObjectfor page hashes). ThemessageDigestis the Authenticode digest of the PE file (algorithm indigestAlgorithm: SHA-1, SHA-256 ...). - Signed attributes of the signer: contentType, messageDigest (digest of the SpcIndirectDataContent),
SPC_SP_OPUS_INFO(1.3.6.1.4.1.311.2.1.12, program name and URL) andSPC_STATEMENT_TYPE(1.3.6.1.4.1.311.2.1.11, individual or commercial code signing). - Unsigned attributes (not covered by the signature, so they can be added later without changing the file digest): a timestamp as legacy counter-signature
MS_COUNTER_SIGN(1.3.6.1.4.1.311.3.3.1) or as RFC 3161 tokenTIME_STAMP_TOKEN(1.2.840.113549.1.9.16.2.14), andSPC_NESTED_SIGNATURES(1.3.6.1.4.1.311.2.4.1), which carries a second signature (usually SHA-1 outside, SHA-256 nested) in the same record. - The certificate chain is inside SignedData; trust needs the roots and a code-signing EKU (1.3.6.1.5.5.7.3.3).
What the digest covers
The digest is computed over the file with these parts left out (offsets from the start of the file, e_lfanew = the PE header offset):
- the optional header CheckSum (4 bytes at e_lfanew + 88),
- the security directory entry (8 bytes at e_lfanew + 152 for PE32, e_lfanew + 168 for PE32+),
- the certificate table itself (everything from the directory's file offset to its end).
Everything else is hashed in file order, including data between sections and an overlay that lies before the certificate table. An unsigned file is hashed with zero bytes added up to an 8-byte boundary so that the later-appended table starts aligned. Consequences: editing a signed binary (a patch, a resource change, an overlay append) invalidates the signature, but changing only the CheckSum or the unsigned attributes does not. Some loaders and installers append data after the table; the table must still end at the end of the file for strict verifiers (osslsigncode requires offset + size == file size).
Page hashes
Signatures made for drivers and some executables (signtool /ph) add a page hash table so the kernel can verify pages lazily. It is a SpcSerializedObject with class id bytes A6 B5 86 D5 B4 A1 24 66 AE 05 A2 17 DA 8E 60 D6, whose serialized data holds an attribute of type SPC_PE_IMAGE_PAGE_HASHES_V1 (1.3.6.1.4.1.311.2.3.1, SHA-1) or _V2 (1.3.6.1.4.1.311.2.3.2, SHA-256) with a set of one octet string. That string is a table of entries u32 file_offset, hash[20 or 32]: the first entry has offset 0 and the hash of the headers (up to SizeOfHeaders, minus the CheckSum and security-directory fields, zero padded to one page), then one entry per page of each section's raw data (page = SectionAlignment, normally 0x1000; each entry holds the file offset of that page), and a last entry with the offset of the end of the last section and an all-zero hash.
Reading signatures
- LIEF (Apache-2.0):
Parser::parse_signaturewalks the table with the 8-byte header, aligning to 8 after each record (src/PE/Parser.cpp lines 1237-1310), andSignatureParserhandles SpcIndirectData and TSTInfo. - osslsigncode (GPL-3.0):
pe.cimplements the digest, page hash calculation (pe_page_hash_calc) and checkssigpos + siglen == filesize. - Tools:
signtool verify /pa /v,sigcheck,openssl pkcs7 -inform DER -print_certson the extracted blob (skip the 8-byte header),osslsigncode extract-signature.
Evidence & references
- Microsoft, "Windows Authenticode Portable Executable Signature Format" (docx) and "PE Format", section "Attribute Certificate Table".
- LIEF commit 15777f1 (OIDToString.cpp lines 606, 981-986, 1002, 694, 2247); osslsigncode commit 868d6e9 (osslsigncode.h lines 179-186, 207-208; pe.c lines 14-17, 575-592, 770-830, 974-1200).
- The security directory offsets 152/168 follow from the optional header layout: data directories start at offset 96 (PE32) or 112 (PE32+) of the optional header, which is e_lfanew + 24; entry 4 is 32 bytes further.