使用自签名证书签署 PowerShell 脚本(并且不使用 makecert.exe)

2024-05-04

我正在尝试签署一份.ps1使用自签名证书(用例是我自己在私人开发站上编写的脚本,因此无需使用 - 或付费 - 真正的 CA)。但是,无论我阅读多少关于证书生成和数字签名主题的指南,我似乎都无法使其正常工作。

这是我到目前为止所取得的成就:

# Create a certificate to use as trusted root of the signing chain
$root = New-SelfSignedCertificate `
    -Subject "CN=PowerShell Trusted Authority" `
    -FriendlyName "PowerShell Trusted Authority" `
    -KeyUsageProperty Sign `
    -KeyUsage CertSign, CRLSign, DigitalSignature `
    -CertStoreLocation Cert:\LocalMachine\My\ `
    -NotAfter (Get-Date).AddYears(10)

# Create a certificate to use for signing powershell scripts
New-SelfSignedCertificate `
    -Signer $root `
    -Subject "CN=PowerShell Code Signing" `
    -KeyAlgorithm RSA `
    -KeyLength 2048 `
    -Type CodeSigningCert `
    -CertStoreLocation Cert:\LocalMachine\My\

# Move the root cert into Trusted Root CAs
 Move-Item "Cert:\LocalMachine\My\$($root.Thumbprint)" Cert:\LocalMachine\Root

上述所有操作都是通过管理 powershell 实例完成的。完成后,我可以在管理控制台的预期位置看到两个证书,并且签名证书的证书路径检查为有效。

然后,我打开常规 PS 提示符并尝试签署脚本:

# Obtain a reference to the signing certificate
PS> $cert = Get-ChildItem Cert:\LocalMachine\My\ -CodeSigningCert

# Attempt at signing
PS> Set-AuthenticodeSignature .\Microsoft.PowerShell_profile.ps1 $cert


    Directory: C:\Users\tomas\Documents\WindowsPowerShell


SignerCertificate          Status                Path
-----------------          ------                ----
                           UnknownError          Microsoft.PowerShell_profile.ps1

如您所见,实际签名失败。查看 powershell 文件,我发现脚本中没有附加任何签名。

如果我在管理员提示下进行签名,我似乎会更进一步;签名块添加到脚本中,并且签名证书的指纹打印在输出中Set-AuthenticodeSignature,但状态仍然是UnknownError并根据AllSigned政策仍然不允许。

# Output some info about the certificate:
PS> $cert | Format-List

Subject      : CN=PowerShell Code Signing
Issuer       : CN=PowerShell Trusted Authority
Thumbprint   : <omitted>
FriendlyName :
NotBefore    : 9/20/2017 10:48:59 PM
NotAfter     : 9/20/2018 11:08:59 PM
Extensions   : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid, 
                System.Security.Cryptography.Oid, System.Security.Cryptography.Oid}

我尝试过多种变体New-SelfSignedCertificate咒语,特别是生成用于代码签名的证书,但始终具有相同的状态消息(UnknownError).

我的最终目标是能够拥有Set-ExecutionPolicy AllSigned并且仍然运行我自己创建的脚本。为了实现这一目标,我在这个过程中缺少什么?


考虑到这一点,您不需要证书链信任,因此,您不需要第一个证书。您可以使用第二个证书并将其移动到受信任的根文件夹中,它将起作用。使用第一个证书然后创建另一个证书似乎失败,因为“根”是自签名的,然后无法签署另一个证书。

自签名证书方法

# Create a certificate to use for signing powershell scripts
$selfsigncert = New-SelfSignedCertificate `
                -Subject "CN=PowerShell Code Signing" `
                -KeyAlgorithm RSA `
                -KeyLength 2048 `
                -Type CodeSigningCert `
                -CertStoreLocation Cert:\LocalMachine\My\

# Move the root cert into Trusted Root CAs
Move-Item "Cert:\LocalMachine\My\$($selfsigncert.Thumbprint)" Cert:\LocalMachine\Root

# Obtain a reference to the code signing cert in Trusted Root
$selfsignrootcert = "Cert:\LocalMachine\Root\$($selfsigncert.Thumbprint)"

# Sign script
Set-AuthenticodeSignature C:\powershell.ps1 $selfsignrootcert

如果您有权访问企业根 CA,则可以使用您在问题中使用的方法。

企业根 CA 方法(与您问题中的方法相同)- 您需要知道您的根 CA 证书指纹

# Get Enterprise Root CA thumbprint
$rootcert = get-childitem Cert:\LocalMachine\Root\XXXXXXXXXXXX


# Generate certificate
$fromrootcert = New-SelfSignedCertificate `
                -Signer $rootcert `
                -Subject "CN=PowerShell Code Signing" `
                -KeyAlgorithm RSA `
                -KeyLength 2048 `
                -Type CodeSigningCert `
                -CertStoreLocation Cert:\LocalMachine\My\

# Sign script
Set-AuthenticodeSignature C:\powershell.ps1 $fromrootcert
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用自签名证书签署 PowerShell 脚本(并且不使用 makecert.exe) 的相关文章

随机推荐