2012-10-08 180 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()