2012-10-08 136 views
0

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

这里是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 

这里是PowerShell的语法来获取HTTP网页内容(而不是HTTPS):

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

下面是PowerShell的语法使用.CRT文件,以获得HTTPS contetn(但不从的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) 

这是我最新的尝试获得网络contetn。没有成功。

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

任何想法? 最好的问候

+0

您不能使用HTTPS客户端验证是CRT文件。它只包含一个证书,但不包含私钥。 – Robert

+0

是的,这是使用certstore的原因。 – user1728814

+0

所以我有脚本下面的波纹管,但仍然无法正常工作。任何想法?问候 – user1728814

回答

-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 
+0

谢谢,但不,这不起作用。它似乎仍然像VBscript。 – user1728814

-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() 
+2

我建议发布一个不仅包含代码块的答案。如果你解释你的解决方案,用户可能会学到更多? – Martin

2

这为我工作:

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

这是为我工作,当我试图创建PRTG客户端证书启用传感器。您可能想要更改超时值。如果出于任何原因网站无法访问,您还需要捕获异常。

$ CertNumber =证书的指纹。

$ CheckURL =要加载的URL。

证书加载并读取现场

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