简体   繁体   English

使用来自 certstore 的客户端证书的 PowerShell HTTPS GET

[英]PowerShell HTTPS GET using client certificate from certstore

I'm trying find easy way how check web content via HTTPS with using client certificate.我正在尝试找到如何使用客户端证书通过 HTTPS 检查 Web 内容的简单方法。 I have VBScript where is defined Client certificate, skipped Server certificate and defined what I'm searching (string "running").我有 VBScript,其中定义了客户端证书,跳过了服务器证书并定义了我正在搜索的内容(字符串“正在运行”)。 My point is rewrite script to PowerShell.我的观点是将脚本重写为 PowerShell。

Here is VBScode:这是 VBS 代码:

device = "10.10.10.10"
link = "5001/test"
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
Http.SetClientCertificate "LOCAL_MACHINE\MY\test"
Http.Option(4) = 256 + 512 + 4096 + 8192 
Http.Open "GET", "https://" & device & ":" & link, false
Http.Send
output = Http.ResponseText
If InStr(1,output,"running") Then
wscript.echo "OK"
Else
wscript.echo "Error"
End If
Set Http = Nothing

Here is Powershell syntax to get HTTP web content (but not https):这是获取 HTTP 网页内容(但不是 https)的 Powershell 语法:

$page = (New-Object System.Net.WebClient).DownloadString("http://10.10.10.10:5001/test")
"$page"

Here is Powershell syntax to get HTTPS contetn using .CRT file (but not from CERTSTORE)这是使用 .CRT 文件(但不是来自 CERTSTORE)获取 HTTPS 内容的 Powershell 语法

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile("C:\Cert\test.crt")
$url = "https://10.10.10.10:5001/test"
$web = [System.Net.WebRequest]::Create($url)
$web.ClientCertificates.Add($Cert)

Here is my latest try to get web contetn.这是我获取网络内容的最新尝试。 No success.没有成功。

#ignore untrusted certificate
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
#load my client certificate defined by thumbprint
$cert = Get-Childitem cert:\LocalMachine\My\5AA55B7B8D99
#get web contetn
$web = [System.Net.WebRequest]::Create($url)
#use my client certificate
$web.ClientCertificates.Add($Cert)

Any ideas ?有任何想法吗 ? Best regards最好的祝福

这对我有用:

Invoke-RestMethod https://10.10.10.10:5001/test -CertificateThumbprint XXXXXXXXXXXXXXXX 

Bit of a late answer but this is what I ended up with after reading the current answers.有点晚的答案,但这是我在阅读当前答案后得出的结论。

You can use "Invoke-WebRequest" and the -Certificate parameter.您可以使用“Invoke-WebRequest”和 -Certificate 参数。 You can also use "wget" in Powershell as it is the alias for "Invoke-WebRequest"您也可以在 Powershell 中使用“wget”,因为它是“Invoke-WebRequest”的别名

$cert = Get-ChildItem -Path cert:\LocalMachine\My\5AA55B7B8D99
$response = wget -Uri "https://10.10.10.10:5001/test" -method "Get" -Certificate $cert
$response.Content.Contains("running")

This is what worked for me when I was trying to create a client certificate enabled sensor for PRTG.当我尝试为 PRTG 创建启用客户端证书的传感器时,这对我有用。 You might want to change the timeout value.您可能想要更改超时值。 Also you'll want to trap exceptions in case the site is unreachable for whatever reason.此外,您还需要捕获异常,以防该站点因任何原因无法访问。

$CertNumber = thumbprint of certificate. $CertNumber = 证书指纹。

$CheckURL = URL to load. $CheckURL = 要加载的 URL。

CERTIFICATE LOADING AND READING SITE证书加载和阅读网站

#*************************************
#********CERTIFICATE LOADING**********
#*************************************

# LOAD CERTIFICATE FROM STORE
$Certificate = Get-ChildItem -Path Cert:\LocalMachine\My\$CertNumber
# CREATE WEB REQUEST
$req = [system.Net.HttpWebRequest]::Create($checkURL)
# ADD CERTS TO WEB REQUEST
$req.ClientCertificates.AddRange($Certificate)

#*************************************
#***********READING SITE**************
#*************************************

#SET TIMEOUT 
$req.Timeout=10000
# GET WEB RESPONSE
$res = $req.GetResponse()
# GET DATA FROM RESPONSE
$ResponseStream = $res.GetResponseStream()
# Create a stream reader and read the stream returning the string value.
$StreamReader = New-Object System.IO.StreamReader -ArgumentList $ResponseStream
# BUILD STRING FROM RESPONSE
$strHtml = $StreamReader.ReadToEnd()

One issue might be that the client machine has to trust the certificate that it's sending.一个问题可能是客户端机器必须信任它发送的证书。 So if the client cert you're trying to send is not self-signed, then the issuer cert needs to be imported into the trusted root of the machine.因此,如果您尝试发送的客户端证书不是自签名的,则需要将颁发者证书导入到机器的受信任根中。 Once you have that, you can use the System.Security.Cryptography.X509Certificates.X509Certificate2 constructor (rather than CreateFromCertFile method) to add MachineKeySet flag完成后,您可以使用System.Security.Cryptography.X509Certificates.X509Certificate2构造函数(而不是CreateFromCertFile方法)添加MachineKeySet标志

For example, on my machine, I have a pfx (with no password, hence the $null for the 2nd argument) and I want to POST some content that requires client cert auth:例如,在我的机器上,我有一个 pfx(没有密码,因此第二个参数为$null ),我想发布一些需要客户端证书身份验证的内容:

[Powershell]

$content = [System.IO.File]::ReadAllBytes("SomeContentIWantToPOST.json")
$endpoint = "https://contoso.com/endpoint"
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("MySuperPrivateCert.pfx",$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet)
$request = [System.Net.WebRequest]::Create($endpoint)
$request.ClientCertificates.Add($Cert)
$request.Method = "POST"
$request.ContentLength = $content.Length
$request.ContentType = "application/json"
$dataStream = $request.GetRequestStream()
$dataStream.Write($content, 0, $content.Length)
$dataStream.Close()

Thy this, but I can't test it你这个,但我无法测试

$device = "10.10.10.10"
$link = "5001/test"
$Http = New-Object 'WinHttp.WinHttpRequest.5.1'
$Http.SetClientCertificate( "LOCAL_MACHINE\MY\test" )
$Http.Option(256 + 512 + 4096 + 8192) # or $Http.Option(4)
$Http.Open("GET", "https://" + $device + ":" + $link, $false)
Http.Send()
$output = Http.ResponseText

I would suggest accessing certificates using .NET class X509Store .我建议使用 .NET 类X509Store访问证书。 Using Certificates property (example below) you can find certificate by thumbprint and attach it to the request.使用Certificates属性(下面的示例),您可以通过指纹查找证书并将其附加到请求中。

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$Store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
        [System.Security.Cryptography.X509Certificates.StoreName]::My, "localmachine")
$Store.Open("MaxAllowed")
$Certificate = $Store.Certificates |  Where-Object Thumbprint -Eq "XXXXXXXXXXXXXXXX"
$Web = [System.Net.WebRequest]::Create($Url)
$Web.ClientCertificates.Add($Certificate)
$Response = $Web.GetResponse()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM