当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

作者:精品下载站 日期:2024-12-14 13:07:35 浏览:16 分类:玩电脑

如何签署 PowerShell 脚本(并有效运行它)


您是否需要确保没有人对您的脚本进行修改并将其作为原始脚本传递?如果是这样,那么您需要学习如何签署 PowerShell 脚本。签名将发布者的身份添加到脚本中,以便用户可以决定是否信任脚本的来源。

在本文中,通过学习如何对 PowerShell 脚本进行签名,了解如何确保仅在您的环境中运行受信任的脚本。

先决条件

如果您要遵循本文中的示例,则需要以下内容。

  • 运行最新版本 Windows 操作系统的计算机。本文使用 Windows 10 版本 20H2。
  • Windows PowerShell 5.1 或 PowerShell 6+。本文中的示例将使用 PowerShell v7.1.3。
  • 用于签名的示例 PowerShell 脚本。您可以随意在任何文件夹中使用任何名称创建脚本。本文将使用名为 C:\ATA\myscript.ps1 的示例脚本,其中包含以下代码。
Write-Host "Script Execution - OK"

获取代码签名证书

在学习如何对 PowerShell 脚本进行签名之前,您需要先获取代码签名证书。在 Microsoft 世界中,代码签名证书也称为 Authenticode 证书。

代码签名证书是一种数字证书,其目的是对文件进行签名。使用代码签名证书对文件或代码进行签名可以证明该文件来自对其进行签名的发布者。

您获取代码签名证书的位置取决于您打算部署或分发签名脚本的位置。与往常一样,成本也是一个重要因素。

  • 全球/公共 - 您需要一个证书,其颁发者是全球可信的证书颁发机构 (CA)。此类 CA 的示例包括 GeoTrust 和 DigiCert。这些证书不是免费的。例如,截至撰写本文时,DigiCert Authenticode 证书每年的费用为 474 美元。
  • 内部/本地 Intranet - 如果您有内部证书颁发机构 (CA) 服务器,则可以从内部 CA 服务器请求并下载签名证书。
  • 个人/开发 - 对于个人测试或开发用途,自签名证书应该没问题。您将在本文中使用这种类型的签名证书。

创建用于代码签名的自签名证书

您在上一节中已经了解到,在学习如何签署 PowerShell 脚本时,您首先需要一个代码签名证书。由于您在本教程中仅进行个人测试,因此自签名证书就足够了。但你从哪里得到它呢?

顾名思义,自签名意味着您的本地计算机将向自己颁发代码签名证书。要生成自签名证书,请按照以下步骤操作。

1. 在计算机上以管理员身份打开 PowerShell。

2. 复制以下命令并在 PowerShell 中运行。此命令使用 New-SelfSignedCertificate cmdlet 创建新的代码签名证书。证书的名称是本地计算机的个人证书存储中的ATA Authenticode

New-SelfSignedCertificate cmdlet 仅支持在当前用户的个人证书存储 (cert:\CurrentUser\My) 或本地计算机的个人证书存储 (cert:\LocalMachine\My) 中创建证书。 cert:\LocalMachine\My 中的证书在计算机范围内可用。

该命令还将证书对象存储到 $authenticode 变量中以供下一步使用。

# Generate a self-signed Authenticode certificate in the local computer's personal certificate store.
 $authenticode = New-SelfSignedCertificate -Subject "ATA Authenticode" -CertStoreLocation Cert:\LocalMachine\My -Type CodeSigningCert

3. 接下来,要使您的计算机信任您创建的新证书,请将自签名证书添加到计算机的受信任的根证书颁发机构受信任的发布者证书存储中。为此,请复制以下代码并在 PowerShell 中运行它。

# Add the self-signed Authenticode certificate to the computer's root certificate store.
## Create an object to represent the LocalMachine\Root certificate store.
 $rootStore = [System.Security.Cryptography.X509Certificates.X509Store]::new("Root","LocalMachine")
## Open the root certificate store for reading and writing.
 $rootStore.Open("ReadWrite")
## Add the certificate stored in the $authenticode variable.
 $rootStore.Add($authenticode)
## Close the root certificate store.
 $rootStore.Close()
 
# Add the self-signed Authenticode certificate to the computer's trusted publishers certificate store.
## Create an object to represent the LocalMachine\TrustedPublisher certificate store.
 $publisherStore = [System.Security.Cryptography.X509Certificates.X509Store]::new("TrustedPublisher","LocalMachine")
## Open the TrustedPublisher certificate store for reading and writing.
 $publisherStore.Open("ReadWrite")
## Add the certificate stored in the $authenticode variable.
 $publisherStore.Add($authenticode)
## Close the TrustedPublisher certificate store.
 $publisherStore.Close()

在三个不同的证书存储中安装自签名证书有三个主要原因。

  • 您在个人证书存储中创建的证书将用作代码签名证书。
  • 将相同的证书复制到“受信任的发布者”存储可确保您的本地计算机信任签署脚本的发布者。 PowerShell 检查此存储中的证书以验证脚本的签名。
  • 最后,将自签名证书添加到受信任的根证书颁发机构可确保您的本地计算机信任个人受信任的发布者存储中的证书。

4. 确认主题为 ATA Authenticode 的证书位于个人受信任的发布者中证书存储,在 PowerShell 中运行以下命令。

# Confirm if the self-signed Authenticode certificate exists in the computer's Personal certificate store
 Get-ChildItem Cert:\LocalMachine\My | Where-Object {$_.Subject -eq "CN=ATA Authenticode"}
# Confirm if the self-signed Authenticode certificate exists in the computer's Root certificate store
 Get-ChildItem Cert:\LocalMachine\Root | Where-Object {$_.Subject -eq "CN=ATA Authenticode"}
# Confirm if the self-signed Authenticode certificate exists in the computer's Trusted Publishers certificate store
 Get-ChildItem Cert:\LocalMachine\TrustedPublisher | Where-Object {$_.Subject -eq "CN=ATA Authenticode"}

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

5. 要在 GUI 中查看证书,请打开证书管理单元并查找您在个人证书文件夹中创建的证书 >受信任的根证书颁发机构受信任的发布者证书存储。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

如何签署 PowerShell 脚本

现在您已创建代码签名证书并将其安装到三个证书存储中,您可以使用它来签署示例 PowerShell 脚本。当您需要签署脚本时,Set-AuthenticodeSignaturecmdlet 是主要的明星。

要签署 PowerShell 脚本,请在 PowerShell 中运行以下代码。第一个命令从本地计算机的个人证书存储中获取代码签名证书。第二条命令向 PowerShell 脚本文件添加数字签名。

# Get the code-signing certificate from the local computer's certificate store with the name *ATA Authenticode* and store it to the $codeCertificate variable.
$codeCertificate = Get-ChildItem Cert:\LocalMachine\My | Where-Object {$_.Subject -eq "CN=ATA Authenticode"}

# Sign the PowerShell script
# PARAMETERS:
# FilePath - Specifies the file path of the PowerShell script to sign, eg. C:\ATA\myscript.ps1.
# Certificate - Specifies the certificate to use when signing the script.
# TimeStampServer - Specifies the trusted timestamp server that adds a timestamp to your script's digital signature. Adding a timestamp ensures that your code will not expire when the signing certificate expires.
Set-AuthenticodeSignature -FilePath C:\ATA\myscript.ps1 -Certificate $codeCertificate -TimeStampServer *<http://timestamp.digicert.com>*

大多数受信任的证书提供商都有时间戳服务器,您可以从提供商的网站找到它们。例如,DigiCert 的时间戳服务器是 http://timestamp.digicert.com,Comodo 的时间戳服务器是 http://timestamp.comodoca.com。

签署脚本后,您应该会看到与下面的屏幕截图类似的输出。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

检查 PowerShell 脚本的数字签名

到目前为止,您已使用您创建的自签名证书签署了 PowerShell 脚本。但你怎么知道脚本是否有数字签名呢?

打开代码

确认脚本数字签名的一种方法是打开脚本并在文本编辑器中查看代码。如下例所示,签名脚本在代码末尾有一个签名块。签名块以#SIG#Begin Signature Block开始,以#SIG#End Signature Block结束。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

从脚本代码中删除数字签名块会将脚本恢复为未签名。

打开脚本的文件属性

检查脚本数字签名的另一种方法是在 Windows 资源管理器中打开脚本的文件属性。为此:

  1. 在 Windows 资源管理器中,导航到 PowerShell 脚本的位置。在此示例中,脚本位于C:\ATA\myscript.ps1
  2. 右键单击该脚本,然后单击属性
  3. 在文件的“属性”窗口中,单击数字签名选项卡,您应该会在签名列表下看到数字签名。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

使用 Get-AuthenticodeSignature

您是否会感到惊讶,您还可以在 PowerShell 中检查脚本的签名?可能不会。您可以调用来检索文件签名的 cmdlet 是 Get-AuthenticodeSignature

要获取脚本的数字签名,请运行以下命令。此命令获取 C:\ATA\myscript.ps1 文件的签名。 Select-Object -Property * cmdlet 显示签名的所有详细信息。

Get-AuthenticodeSignature -FilePath C:\ATA\myscript.ps1 | Select-Object -Property *

运行命令后,您应该看到与下面的屏幕截图类似的结果。如您所见,SignerCertificate 属性显示签名证书的详细信息。而TimerStamperCertificate属性显示时间戳服务器的证书。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

运行签名的 PowerShell 脚本

此时,您已经签署了 PowerShell 脚本并确认数字签名存在。但是,是否正确完成所有步骤的最终测试是执行脚本并确认它运行。

PowerShell 具有一项安全功能,可以保护用户免受无意运行脚本的影响。此安全功能称为执行策略根据执行策略,PowerShell 可能会阻止或允许脚本运行。

要了解不同的执行策略以及它们如何影响脚本执行,请参阅 PowerShell 执行策略:了解和管理。

要运行签名的 PowerShell 脚本,请按照以下步骤操作。

首先,将执行策略更改为AllSigned,以确保只有签名的脚本才能运行。如果不执行此步骤,您将无法准确测试您的签名脚本是否有效。为此,请通过在 PowerShell 中以管理员身份运行以下命令来调用 Set-ExecutionPolicy cmdlet。

Set-ExecutionPolicy AllSigned

接下来,执行签名的 PowerShell 脚本。

C:\ATA\myscript.ps1

该脚本应该运行并且没有错误或警告,如下面的结果所示。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

但是,如果不知何故脚本未正确签名或根本未签名,您将收到类似于下图的错误。在这种情况下,请重新访问您的步骤并尝试再次签署脚本。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

如果您最终更新了脚本怎么办?数字签名仍然有效吗?答案是不。对签名脚本的任何修改都会使脚本的数字签名失效。运行修改后的脚本将失败并导致错误。

按照以下步骤测试修改后的签名脚本。

1. 在代码或文本编辑器中打开已签名的 myscript.ps1 脚本。

2. 修改代码,添加一个字符,例如本例中的下划线。不要更改任何其他内容。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

3.修改代码后保存脚本。

4. 最后,通过运行以下命令在 PowerShell 中执行修改后的脚本。

C:\ATA\myscript.ps1

由于您修改了签名脚本,因此执行该脚本将导致如下所示的错误。您需要再次签署脚本以更新和修复其数字签名。

[玩转系统] 如何签署 PowerShell 脚本(并有效运行它)

数字签名并不能保证没有人修改脚本的原始版本。任何带有恶意代码的 PowerShell 脚本也可能经过数字签名。从您不完全信任的来源运行脚本时,请务必小心谨慎。

结论

在本文中,您了解了为什么可能需要根据执行策略对 PowerShell 脚本进行签名。您还学习了如何区分签名脚本和未签名脚本。最后,您学习了如何对 PowerShell 脚本进行数字签名以及如何测试和运行它们。

现在您已经知道如何签署 PowerShell 脚本,您会在分发或部署脚本之前开始签署脚本吗?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯