Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell HTTPS GET using client certificate from certstore

I'm trying find easy way how check web content via HTTPS with using client certificate. I have VBScript where is defined Client certificate, skipped Server certificate and defined what I'm searching (string "running"). My point is rewrite script to PowerShell.

Here is 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

Here is Powershell syntax to get HTTP web content (but not https):

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

[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

like image 780
user1728814 Avatar asked Aug 29 '26 07:08

user1728814


2 Answers

This worked for me:

Invoke-RestMethod https://10.10.10.10:5001/test -CertificateThumbprint XXXXXXXXXXXXXXXX 
like image 180
altumano Avatar answered Aug 31 '26 21:08

altumano


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. You can also use "wget" in Powershell as it is the alias for "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")
like image 44
James Avatar answered Aug 31 '26 21:08

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!