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

时间:2012-10-08 13:04:45

标签: powershell https get certificate

我正在尝试使用客户端证书通过HTTPS检查Web内容的简单方法。 我有VBScript定义客户端证书,跳过服务器证书并定义我正在搜索的内容(字符串“正在运行”)。我的观点是重写PowerShell脚本。

这是VBScode:

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

以下是获取HTTP Web内容(但不是https)的Powershell语法:

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

以下是使用.CRT文件获取HTTPS竞争的Powershell语法(但不是来自CERTSTORE)

[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)

这是我最近试图获得网络竞争的尝试。没有成功。

#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)

有什么想法吗? 最好的问候

6 个答案:

答案 0 :(得分:5)

这对我有用:

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

答案 1 :(得分:1)

当我尝试为PRTG创建一个支持客户端证书的传感器时,这对我有用。您可能想要更改超时值。此外,如果网站由于某种原因无法访问,您还需要捕获异常。

$ CertNumber =证书的指纹。

$ CheckURL =要加载的网址。

证书加载和阅读网站

#*************************************
#********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()

答案 2 :(得分:0)

一个较晚的答案,但这是我在阅读当前答案后得到的结果。

您可以使用“ Invoke-WebRequest”和-Certificate参数。您也可以在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")

答案 3 :(得分:0)

一个问题可能是客户端计算机必须信任其发送的证书。因此,如果您尝试发送的客户端证书不是自签名的,则需要将颁发者证书导入到计算机的受信任根目录中。一旦有了它,就可以使用System.Security.Cryptography.X509Certificates.X509Certificate2构造函数(而不是CreateFromCertFile方法)来添加MachineKeySet标志

例如,在我的计算机上,我有一个pfx(没有密码,因此第二个参数为$null),并且我想发布一些需要客户端证书auth的内容:

[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()

答案 4 :(得分:-1)

这是,但我无法测试

$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

答案 5 :(得分:-1)

我建议使用.NET类X509Store访问证书。使用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()