#!/usr/bin/env python3 """Check a PE for an embedded Authenticode signature and summarize the certificate blob. Parses the PE optional-header security data directory (index 4) to detect an embedded PKCS#7 signature (WIN_CERTIFICATE), reporting its presence, size, and revision/type. Deep certificate parsing and cryptographic verification require a PKCS#7 library / OS tooling; this gives a fast, dependency-free presence check. It parses bytes statically and never executes the sample. """ from __future__ import annotations import argparse import json import struct import sys from pathlib import Path WIN_CERT_TYPE = {0x0001: "X509", 0x0002: "PKCS_SIGNED_DATA", 0x0003: "RESERVED", 0x0004: "TS_STACK_SIGNED"} def check(path: str) -> dict: data = Path(path).read_bytes() if data[:2] != b"MZ": return {"error": "not a PE"} e_lfanew = struct.unpack_from(" int: parser = argparse.ArgumentParser(description="Check Authenticode signature presence") sub = parser.add_subparsers(dest="cmd", required=True) p = sub.add_parser("check") p.add_argument("file") args = parser.parse_args(argv) print(json.dumps(check(args.file), indent=2)) return 0 if __name__ == "__main__": sys.exit(main())