【问题标题】:downloading a web page with powershell using a x.509 certificate使用 x.509 证书下载带有 powershell 的网页
【发布时间】:2020-05-10 05:25:58
【问题描述】:

我在尝试从使用 X.509 证书对客户端进行身份验证的站点下载文件时遇到问题,Windows 10 上的 PowerShell。我在 IE 的证书存储中安装了此证书,并且还导出了 PFX 文件。这是我尝试过的:

  • [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::ssl3 还有 tls12
  • 尝试使用 System.Security.Cryptography.X509Certificates.X509Certificates2 获取证书
  • 尝试通过 Get-Credential 设置凭据然后传递它

我已经使用了 Invoke-WebRequest 命令以及 System.Net.WebClient 试图让它工作。我得到的错误信息如下:

PS P:\PSScripts> P:\PSScripts\DownloadSslHtml.ps1
VERBOSE: GET https://somewebsite.com/GetReports.do?reportTypeId=1234
with 0-byte payload
Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.
At P:\PSScripts\DownloadSslHtml.ps1:20 char:1
+ Invoke-WebRequest -Uri $url -UserAgent ([Microsoft.PowerShell.Command ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke- 
WebRequest], WebException + FullyQualifiedErrorId : 
WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

现在我看到很多人遇到此“SSL/TLS 安全通道”错误,他们通过将加密方法指定为 TLS12 或 SSL3 来修复它,这两种方法都不适合我。我很困惑如何进行。有什么想法吗?该网站使用 TLS1.2 AES 与 256 位加密 ECDH 256 位交换。

编辑:这是我的 powershell 代码:

$url = "https://somewebsite.com/GetReports.do?reportTypeId=1234"
$outputFile = "P:/DownloadedData/file.html"
$PFXPath = "P:/Properties/Config/mycert.pfx"
$PFXPassword = "cert_password"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::tls12
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($PFXPath,$PFXPassword,'DefaultKeySet')
Invoke-WebRequest -Uri $url -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) -Certificate $cert -OutFile $outputFile -Verbose

【问题讨论】:

  • 发布你的脚本,至少是 webrequest 部分
  • @MikeTwc 更新了我的代码。
  • 尝试使用 PersistKeySet 而不是 DefaultKeySet。 AFAIK 指定 tls 版本只是设置您允许它使用的 Tls 的最低版本(早期版本不安全)。 UserAgent 参数看起来很奇怪,为什么要使用它?对于 cert auth,提供 cert 参数就足够了。如果您安装了证书,您还可以使用指纹而不是一遍又一遍地创建证书。
  • @MikeTwc 感谢 Mike,所以我将代码切换为在 Import 调用中使用“PersistKeySet”,并将用户代理从 Invoke-WebRequest 中取出。仍然收到错误:“请求被中止:无法创建 SLL/TLS 安全通道。”有什么想法吗?
  • @MikeTwc 我把它改成了 'UserKeySet' 就可以了!

标签: powershell ssl webclient x509certificate webrequest


【解决方案1】:

我改变了这一行:

$cert.Import($PFXPath,$PFXPassword,'DefaultKeySet')

到这里:

$cert.Import($PFXPath,$PFXPassword,'UserKeySet')

现在它可以工作了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 2019-04-12
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2011-05-05
    • 2012-07-22
    • 1970-01-01
    相关资源
    最近更新 更多