我可以在IIS中更改证书指纹:使用PowerShell进行SSLBindings吗?

问题描述 投票:0回答:1

我是一个强大的新手,这是我的第一篇文章,因此我会因缺乏知识和/或不正确使用条款而提前道歉...我花了几个小时搜索并尝试各种代码片段来创建一个脚本,可用于替换IIS(7.5及更高版本)Web服务器上的数百个SSL证书。到目前为止我发现的是SSL绑定显然存储在[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ services \ HTTP \ Parameters \ SslBindingInfo]中。以下powershell脚本允许我检索服务器上存在的所有IIS SSL绑定,并且CNames的简单比较允许我找到与我们正在更新的证书匹配的特定绑定。我还发现我可以使用不同的指纹在上面位置下方的注册表项中手动改写SslCertHash值,并更改IIS中的证书绑定。所以似乎在powershell中应该有一个非常简单的方法,只需用适当的绑定中的新指纹替换当前的指纹,而不必检索IP地址,端口和/或siteID(运行脚本已经让我在绑定我需要改变的实例)。我知道有一些改变绑定的迂回方式,但我对powershell的有限接触表明应该有更直接的命令来实现这一点。这只是一厢情愿的想法???

我的脚本到目前为止:

    Import-Module -Name WebAdministration

    $SearchCName = '*myhost.domain.com*'
    $NewCertThumbprint = '1A2B3C4D5E6F7G8H9I0J1K2L3M4N5O6P7Q8R9S0T'

    Get-ChildItem -Path IIS:SSLBindings | ForEach-Object -Process {
        if ($_.Sites)
        {
            $certificate = Get-ChildItem -Path CERT:LocalMachine/My |
                Where-Object -Property Thumbprint -EQ -Value $_.Thumbprint

            [PsCustomObject]@{
                Sites                        = $_.Sites.Value
                CertificateDnsNameList       = $certificate.DnsNameList
                CertificateNotAfter          = $certificate.NotAfter
                CertificateThumbprint        = $certificate.Thumbprint
                CertificateSubject           = $certificate.Subject
                CertificateIssuer            = $certificate.Issuer
                CertificateTPfromSSLBinding  = $_.Thumbprint
                CertificateBindingIPAddr     = $_.IPAddress
                CertificatePort              = $_.Port
                }

                if ($certificate.Subject -like $SearchCName)
                    { Write-Output 'True' } 
                Else 
                    { Write-Output 'False' }
        }
    } 

    Sample output:

    Sites                       : testsite1
    CertificateDnsNameList      : {}
    CertificateNotAfter         : 2/16/2021 1:41:55 PM
    CertificateThumbprint       : 6DA6A25F5C756D710D033D1146A3097EE0E9F430
    CertificateSubject          : CN=testsite1.domain.com, OU=IT Security, O=My
                          Company, L=MyCity, S=MyState, C=US
    CertificateIssuer           : CN=My Company Internal CA, OU=PKI, O=My Company, 
                          C=US
    CertificateTPfromSSLBinding : 6DA6A25F5C756D710D033D1146A3097EE0E9F430
    CertificateBindingIPAddr    : 10.39.205.19
    CertificatePort             : 443
    False

    Sites                       : testsite2
    CertificateDnsNameList      : {}
    CertificateNotAfter         : 1/2/2022 4:39:12 PM
    CertificateThumbprint       : 6D2DC913256CCADBD3983773CAC20440D918F091
    CertificateSubject          : CN=testsite2.domain.com, OU=IT Security, O=My 
                          Company, L=MyCity, S=MyState, C=US
    CertificateIssuer           : CN=My Company Internal CA, OU=PKI, O=My Company, 
                          C=US
    CertificateTPfromSSLBinding : 6D2DC913256CCADBD3983773CAC20440D918F091
    CertificateBindingIPAddr    : 10.39.205.24
    CertificatePort             : 443
    False

我的Powershell版本:主要次要版本修订版


5 0 10586 117

谢谢,兰迪

powershell iis ssl-certificate
1个回答
0
投票

老实说,这可能是此stackoverflow讨论和提供的答案的重复。

Powershell update IIS Bindings

对某事做新事物是可以的,但在你开始减轻任何不必要的挫折/困惑之前,你必须接受一些训练是至关重要的。

网上有大量免费的视频,音频和电子书资源。说到IIS的东西,你的关键资源是MS IIS Team站点。

https://www.iis.net

然而,也有很多资源可供使用。

正如您发现获取SSL站点/绑定信息的方式一样,应该使用相同的工作来完成您的任务。这种类型不仅可以在Microsoft文档站点上直接使用,而且可以在整个Web上使用。

至于您的需求,下面的MS文档详细信息应该处理您的用例。

PowerShell Snap-in: Configuring SSL with the IIS PowerShell Snap-in

由IIS团队

要启用SSL,需要执行三个步骤:

  1. 获取并安装证书
  2. 在IIS中创建SSL绑定
  3. 将证书分配给IP:IIS绑定的端口

并且可选地:

•在您的网站上实施SSL

您可以使用证书提供程序查看证书存储中的证书:

dir cert:\localmachine\my

    Directory: Microsoft.PowerShell.Security\Certificate::localmachine\my

Thumbprint                                Subject
----------                                -------
7ABF581E134280162AFFFC81E62011787B3B19B5  CN=MyTestServer

创建SSL绑定

我们使用一个名为New-WebBinding的基于任务的cmdlet将SSL绑定添加到默认网站:

New-WebBinding -Name "Default Web Site" -IP "*" -Port 443 -Protocol https

cd SslBindings
dir

get-item cert:\LocalMachine\MY\7ABF581E134280162AFFFC81E62011787B3B19B5 | new-item 0.0.0.0!443

现在,您可以使用我们在第一步中获得的证书哈希,并将其与所有IP地址(0.0.0.0)和SSL端口443相关联:

或者您可以使用以下说明:

Use Powershell to bind SSL Certificates to an IIS Host Header Site

$hostname="test.west-wind.com"
$iisSite="Default Web Site"
dir certs:\localmachine\my
$cert = (Get-ChildItem cert:\LocalMachine\My 
      | where-object { $_.Subject -like "*$hostname*" } 
      | Select-Object -First 1).Thumbprint

# If you have an existing certificate you can import it with CertUtil:

# From a PFX:
certutil -importpfx <pfx_file>


# From a CER:
certutil –addstore MY <Cer_File>

dir certs:\localmachine\my
$cert = "<thumbprintOfYourCert>"

# To bind hostname and IP to your certificate you can now do:
$guid = [guid]::NewGuid().ToString("B")
netsh http add sslcert hostnameport="${hostname}:443" certhash=$cert certstorename=MY appid="$guid"

Bind the Web Site to the Host Header IP

Finally we need to also bind Web site to the IP/Hostheader/Port combination.
New-WebBinding -name $iisSite -Protocol https  -HostHeader $hostname -Port 443 -SslFlags 1


# Notice the -SslFlag=1 setting, which enables SNI on this binding.

# If for whatever reason you need to remove the binding you can use:
netsh http delete sslcert hostnameport=test.west-wind.com:443

在理解了上述所有内容之后,您可以在一次调用中使用单行代码执行此操作:

Get-ChildItem -Path cert:\LocalMachine\MY | Where {$_.Subject -match "CN=[*.mycrt.mydomain.com]*"}  | Select -First 1 | ni 0.0.0.0!443

也可以看看:

Weekend Scripter: Use PowerShell to Update SSL Bindings

© www.soinside.com 2019 - 2024. All rights reserved.