Assessing Remote Certificates with Powershell

Published: 2016-01-25
Last Updated: 2016-01-25 01:01:18 UTC
by Rob VandenBrink (Version: 1)
3 comment(s)

Building on our last conversation about HTTPS and Powershell, let’s look at another common thing you’d do with HTTPS in a system administrator, or in a security assessment or penetration test – let’s assess the HTTPS certificates themselves.  As in our last “episode”, we’ll need to rely on .NET to deal with certificates.  Powershell on its own doesn’t have the commands to do the job. The following code worked OK for anything with a valid, trusted certificate:

However, if the host has a self-signed certificate, it will choke on the “AuthenticateAsClient” line.  Since we’re almost guaranteed to find these on an internal subnet (and lots of internet-facing subnets too), we’ll need to get around this problem.  Let’s try another approach:

Now again, with a bit of error checking this time, and some cleanup – we’ll also close the SSL Stream and the TCP Socket (always a good idea)

Note that you don’t want to run this indiscriminately across a subnet – if you try to “assess” a host that doesn’t have an open port, or doesn’t have an HTTPS server on that port, then you’ll get some not-so-pretty error messages, and either your script will have a lengthy delay or an ungraceful exit.  Stay tuned – tomorrow’s story we’ll deal with those two issues.

Let’s take a look at the contents of the ISC’s certificate:


Which certificate values might you most frequently look at?  Likely the dates, issuer and signature algorithms for starters.  Let’s  look at two examples of a self-signed certificate – first, an  ASA Firewall:

Compared to a Palo Alto firewall:

Or a certificate issued by a “real” CA (the certificate for

Checking a certificate's expiry date:

And signature algorithms:

Finally, Powershell / .Net has a neat “is this a valid certificate” check, where the local workstation checks validity from its perspective- checking it’s local certificate store, then chaining up to trusted CA's and so on.   Let’s look first at an internal host, with a self-signed certificate, and then the cert on the ISC’s site:

The cool thing?  You can easily, just by changing the port number, check certificates on things like:

  • RDP services on Windows or Linux hosts
  • Lync (or Skype for Business) server certificates
  • STARTTLS certificates on mail servers

... really anything.

If this seems pretty basic, stay tuned - we'll use these functions for some serious automation tomorrow

Rob VandenBrink

3 comment(s)


Took the liberty of transcribing your screen captures. Hope you don't mind!

Great script. if you run

# CertInfo.ps1
# Written by: Rob VandenBrink
# Params: Site name or IP ($ip), Port ($port)

function ChkCert
Param ($ip,[int] $Port)
$TCPClient = New-Object -TypeName System.Net.Sockets.TCPClient
$TcpSocket = New-Object Net.Sockets.TcpClient($ip,$port)
$tcpstream = $TcpSocket.GetStream()
$Callback = {param($sender,$cert,$chain,$errors) return $true}
$SSLStream = New-Object -TypeName System.Net.Security.SSLStream -ArgumentList @($tcpstream, $True, $Callback)
$Certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($SSLStream.RemoteCertificate)
return $Certificate

Last year I had to audit our SSLs, I found some code on Stack Overflow and tweaked it into a loop that does basic error handling and met my needs well enough:

# Script: SSL_Epire_Check_Loop.ps1
# Version: 1.4.3
# Notes: Original Version Found Here:
# Refined By: Ben Personick
$WebsiteURLs= @("","","","")
Write-Host "# Website_URL: Current Certificate: Expiration Date: Days Remaining: Errors:"
foreach ($WebsiteURL in $WebsiteURLs){
$Conn = New-Object System.Net.Sockets.TcpClient($WebsiteURL,$WebsitePort)
Try {
$Stream = New-Object System.Net.Security.SslStream($Conn.GetStream(),$false, {
param($sender, $certificate, $chain, $sslPolicyErrors)
return $true

$Cert = $Stream.Get_RemoteCertificate()
$CN=(($cert.Subject -split "=")[1] -split ",")[0]
$ValidTo = [datetime]::Parse($Cert.GetExpirationDatestring())

$ValidDays = $($ValidTo - [datetime]::Now).Days
if ($ValidDays -lt $Threshold) {
if ($ValidDays -lt $Severe) {
Write-Host "$ID $WebsiteURL $CN $ValidTo $ValidDays" -ForegroundColor $MyFontColor
Catch { Throw $_ }
Finally { $Conn.close() }
Catch {
Write-Host "$ID $WebsiteURL " $_.exception.innerexception.message -ForegroundColor red


Example Output:

# Website_URL: Current Certificate: Expiration Date: Days Remaining: Errors:
1 No connection could be made because the target machine actively refused it
2 04/13/2016 20:00:00 79
3 * 12/30/2016 07:00:00 339
4 11/04/2016 19:59:59 284
Hello Rob

Great post, above it you mention the following

stay tuned - we'll use these functions for some serious automation tomorrow

I cannot find the next post in this series can you please point me to the correct URL etc.

Thanks very much

Diary Archives