【问题标题】:Extract Software Signing Cert using Python from a PE File使用 Python 从 PE 文件中提取软件签名证书
【发布时间】:2018-12-20 05:08:53
【问题描述】:

当尝试使用cryptography 从 PE 文件中提取证书时,它会以ValueError: Unable to load certificate 失败。我可以使用subprocessopenssl 命令行从同一个PE 文件中正确提取证书。我想了解使用cryptography 的代码版本出了什么问题。

我正在使用 Python 3.7.1、密码学 2.4.2 和 pefile 2018.8.8

import pefile
from cryptography import x509
from cryptography.hazmat.backends import default_backend

pe = pefile.PE(fname)
pe.parse_data_directories(directories=[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']])
sigoff = 0
siglen = 0
for s in pe.__structures__:
    if s.name == 'IMAGE_DIRECTORY_ENTRY_SECURITY':
        sigoff = s.VirtualAddress
        siglen = s.Size
pe.close()
with open(fname, 'rb') as fh:
    fh.seek(sigoff)
    thesig = fh.read(siglen)
cert = x509.load_der_x509_certificate(thesig[8:], default_backend())

ValueError: Unable to load certificate 失败

【问题讨论】:

    标签: python openssl portable-executable pyopenssl python-cryptography


    【解决方案1】:

    问题在于签名是一个 PKCS7 对象。 MS 已将其记录在 Word 中。我还没有找到PDF版本...

    所以你需要先解析 PKCS7 对象。我为此使用asn1crypto

    这对我有用:

    import pefile
    from cryptography import x509
    from cryptography.hazmat.backends import default_backend
    
    from asn1crypto import cms
    
    pe = pefile.PE(fname)
    sigoff = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_SECURITY"]].VirtualAddress
    siglen = pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_SECURITY"]].Size
    pe.close()
    
    with open(fname, 'rb') as fh:
        fh.seek(sigoff)
        thesig = fh.read(siglen)
    
    signature = cms.ContentInfo.load(thesig[8:])
    
    for cert in signature["content"]["certificates"]:
        parsed_cert = x509.load_der_x509_certificate(cert.dump(), default_backend())
        print(parsed_cert)
    

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 1970-01-01
      • 2019-04-25
      • 2015-06-06
      • 2015-04-05
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2016-05-26
      相关资源
      最近更新 更多