Skip to content

Conversation

@Flickdm
Copy link
Member

@Flickdm Flickdm commented Dec 1, 2025

This commit adds comprehensive cryptographic validation to the Authenticode signature combining tool, bringing the same verification capabilities from auth_var_tool.py to PE file signature operations.

Key changes:

  • Added cryptographic signature verification using the 'cryptography' library
  • Implemented SpcIndirectDataContent parsing to extract embedded PE hashes
  • Added certificate extraction and display from PKCS#7 signatures
  • Compute Authenticode hashes using the algorithm specified in the signature
  • Verify signatures mathematically using signer's public key (RSA/ECDSA)
  • Validate that computed PE hash matches the hash in SpcIndirectDataContent

New functions:

  • _get_hash_algorithm_from_oid(): Maps OID strings to hash algorithms
  • _extract_pe_hash_from_spc_indirect_data(): Parses SPC structure for hash
  • _extract_certificates_from_pkcs7(): Extracts X.509 certificates
  • _verify_pkcs7_signature(): Performs full cryptographic verification
  • compute_authenticode_hash(): Flexible hash computation with configurable algorithm

Enhanced functions:

  • validate_pkcs7_signatures(): Now performs cryptographic verification
  • main_verify(): Displays certificate details and verification status
  • main_combine(): Validates signatures cryptographically before combining

Bug fixes:

  • Removed incorrect 8-byte padding from Authenticode hash calculation (padding only applies to WIN_CERTIFICATE structure alignment, not hash data)
  • Consolidated duplicate hash functions into single implementation

Code improvements:

  • Named constants for all magic numbers in SPC parsing
  • Better documentation and inline comments
  • Proper type annotations with Optional types

Testing:

  • Verified against Microsoft-signed bootmgfw.efi files
  • Hash computation now matches Windows AppLocker and UEFI firmware
  • Both multi-signature and nested signature modes validated
  • All test cases pass with cryptographic verification

Follows Microsoft Authenticode PE specification v1.1

Description

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?

How This Was Tested

Ran it against copies of bootmgfw.efi and hellouefi.efi that were both singly signed and

Integration Instructions

N/A

This commit adds comprehensive cryptographic validation to the Authenticode
signature combining tool, bringing the same verification capabilities from
auth_var_tool.py to PE file signature operations.

Key changes:
- Added cryptographic signature verification using the 'cryptography' library
- Implemented SpcIndirectDataContent parsing to extract embedded PE hashes
- Added certificate extraction and display from PKCS#7 signatures
- Compute Authenticode hashes using the algorithm specified in the signature
- Verify signatures mathematically using signer's public key (RSA/ECDSA)
- Validate that computed PE hash matches the hash in SpcIndirectDataContent

New functions:
- _get_hash_algorithm_from_oid(): Maps OID strings to hash algorithms
- _extract_pe_hash_from_spc_indirect_data(): Parses SPC structure for hash
- _extract_certificates_from_pkcs7(): Extracts X.509 certificates
- _verify_pkcs7_signature(): Performs full cryptographic verification
- compute_authenticode_hash(): Flexible hash computation with configurable algorithm

Enhanced functions:
- validate_pkcs7_signatures(): Now performs cryptographic verification
- main_verify(): Displays certificate details and verification status
- main_combine(): Validates signatures cryptographically before combining

Bug fixes:
- Removed incorrect 8-byte padding from Authenticode hash calculation
  (padding only applies to WIN_CERTIFICATE structure alignment, not hash data)
- Consolidated duplicate hash functions into single implementation

Code improvements:
- Named constants for all magic numbers in SPC parsing
- Better documentation and inline comments
- Proper type annotations with Optional types
- Enhanced logging with ✓/✗ symbols for verification results

Testing:
- Verified against Microsoft-signed bootmgfw.efi files
- Hash computation now matches Windows AppLocker and UEFI firmware
- Both multi-signature and nested signature modes validated
- All test cases pass with cryptographic verification

Follows Microsoft Authenticode PE specification v1.1
@Flickdm Flickdm marked this pull request as ready for review December 1, 2025 22:42
@Flickdm Flickdm requested a review from Copilot December 1, 2025 22:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive cryptographic verification capabilities to the Authenticode signature tool, enabling validation of PE file signatures using the cryptography library. The changes introduce hash extraction from SPC structures, certificate parsing, and full signature verification against PE files.

Key changes:

  • Added cryptographic signature verification using new helper functions for OID mapping, certificate extraction, and PKCS7 verification
  • Enhanced compute_authenticode_hash() to support multiple hash algorithms (SHA1/256/384/512) and removed incorrect padding logic
  • Updated validate_pkcs7_signatures() and verification commands to perform cryptographic validation before accepting signatures

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Flickdm and others added 7 commits December 1, 2025 15:08
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Flickdm Flickdm force-pushed the feature/auth_transplant/cryptographic_validation branch from 91c807c to d8166a7 Compare January 20, 2026 22:54
Fix ruff linting errors by adding the missing OID constants:
- OID_SHA384_DER and OID_SHA384_STRING for SHA-384 hash algorithm
- OID_SHA512_DER and OID_SHA512_STRING for SHA-512 hash algorithm

These constants are referenced in the hash extraction logic but were not defined.
@Flickdm Flickdm force-pushed the feature/auth_transplant/cryptographic_validation branch from d8166a7 to 0cee114 Compare January 21, 2026 18:11
- Add try-finally block in compute_authenticode_hash to ensure PE object is properly closed
- Add cryptography==43.0.0 to pip-requirements.txt (missing dependency)
- Improve type annotations: change dict to Dict[str, Any] for _verify_pkcs7_signature
- Enhance docstring to document return dictionary structure
@Flickdm Flickdm requested review from Javagedes and apop5 January 21, 2026 19:23
return None, None

except Exception as e:
logger.debug(f"Failed to parse SpcIndirectDataContent: {e}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: probably an error log.

return hash_bytes, algorithm_oid
i += 1

logger.debug("No hash found in SpcIndirectDataContent")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit; maybe a warning is more appropriate? or info?

certificates.append(cert)

except Exception as e:
logger.debug(f"Failed to extract certificates: {e}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe info or warn?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants