Cla*_*gan 3 powershell python certificate command-line digital-signature
如果证书本身未安装在工作站上但仅用于签署该特定文件,是否可以以某种方式以编程方式导出文件的数字证书主题?
我需要以某种方式从文件中提取该信息并检查它是否正确。最好使用Python/CMD/PowerShell
编辑:
抱歉缺少详细信息。
我目前正在使用这个 python 脚本(我修改为在 Python 3.6 上运行):http://www.zedwood.com/article/python-openssl-x509-parse-certificate来解析我从中提取的 .cer 文件我找到的带有这个小工具的原始可执行文件(我还对其进行了修改以与 Python 3 一起使用): https: //blog.didierstevens.com/programs/disitool/但只有在我将其从 DER 编码的二进制文件转换之后使用 Windows certutil 转换为 base-64。
不过,disitool 脚本的问题在于,它使用 pefile python 模块从可执行文件本身中剪切了一个“签名”字节数组,这使得提取的 .cer 文件无效,正如我在尝试执行操作时不断收到的 python 错误一样使用 OpenSSL.crypto 模块加载证书:
[('asn1 encoding routines', 'asn1_check_tlen', 'wrong tag'), ('asn1 encoding routines', 'asn1_item_embed_d2i', 'nested asn1 error'), ('asn1 encoding routines', 'asn1_template_noexp_d2i', 'nested asn1 error'), ('PEM routines', 'PEM_ASN1_read_bio', 'ASN1 lib')]
Run Code Online (Sandbox Code Playgroud)
但是解析一个好的提取证书(使用我上面发布的第一个脚本)是有效的,正如您在此处看到的:
所以,我想我只需要一种从可执行文件中提取证书的方法。或者,如果您发现我的解决方案太复杂,如果您知道如何从证书的主题字段中获取“Redmond”文本,我非常愿意接受想法:)
在 Powershell 中:
Get-AuthenticodeSignature C:\Path\TO\File.exe
Run Code Online (Sandbox Code Playgroud)
因此,使用您的 explorer.exe 示例,这将得到 Redmond:
(Get-AuthenticodeSignature C:\Windows\explorer.exe).SignerCertificate.subject.split(',')[2].split('=')[1]
Run Code Online (Sandbox Code Playgroud)
由于您要求详细说明,Get-AuthenticodeSignature
因此返回一个 System.Management.Automation.Signature 对象。您可以通过几种方式找到这一点。就我个人而言,我更喜欢将它分配给一个变量,这样我就可以进一步使用返回的对象。一旦将其分配给变量,您就可以了解有关它的信息。Get-Member
应该是您在 Powershell 中常用的 cmdlet 之一。在这种情况下:
$foo = Get-AuthenticodeSignature C:\Windows\explorer.exe
Get-Member -InputObject $foo
TypeName: System.Management.Automation.Signature
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
IsOSBinary Property bool IsOSBinary {get;}
Path Property string Path {get;}
SignatureType Property System.Management.Automation.SignatureType SignatureType {get;}
SignerCertificate Property System.Security.Cryptography.X509Certificates.X509Certificate2 SignerCertificate {...
Status Property System.Management.Automation.SignatureStatus Status {get;}
StatusMessage Property string StatusMessage {get;}
TimeStamperCertificate Property System.Security.Cryptography.X509Certificates.X509Certificate2 TimeStamperCertific...
Run Code Online (Sandbox Code Playgroud)
所以你可以看到该对象有一些方法和一些属性(我知道,所有对象都有)。在本例中,这些方法都是从 System.Object 继承的标准方法。但这些属性很有趣。SignerCertificate 看起来像您想要的,所以让我们看看它是什么样的:
$foo.SignerCertificate
Thumbprint Subject
---------- -------
419E77AED546A1A6CF4DC23C1F977542FE289CF7 CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
Run Code Online (Sandbox Code Playgroud)
指纹显然很重要,因为它是标识证书的内容,但您已经询问了主题中的雷德蒙德。现在我们知道如何将其作为字符串来获取:
$foo.SignerCertificate.Subject
Run Code Online (Sandbox Code Playgroud)
所以从这里开始就是直接的字符串解析。
我将再补充一点,因为看来您可能正在学习 Powershell。您应该定期尝试的另一个 cmdlet 是 Get-Command。在这种情况下,在您提出问题之前,我什至不知道 Get-AuthenticodeSignature cmdlet 存在。所以我这样做了:
Get-Command *signature*
CommandType Name Version Source
----------- ---- ------- ------
Function Update-MpSignature 1.0 Defender
Cmdlet Get-AuthenticodeSignature 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Save-VolumeSignatureCatalog 1.0.0.0 ShieldedVMDataFile
Cmdlet Set-AuthenticodeSignature 3.0.0.0 Microsoft.PowerShell.Security
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6469 次 |
最近记录: |