Created
June 13, 2018 11:24
-
-
Save matt-FFFFFF/a056abf6cbe9d1b09bb4e098dcaa79ee to your computer and use it in GitHub Desktop.
Gets a certificate from a webserver and returns AIA and CDP FQDNs. Note, does not do OCSP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New-Variable -Name 'OIDCDP' -Value '2.5.29.31' -Option ReadOnly -Force | |
New-Variable -Name 'OIDAIA' -Value '1.3.6.1.5.5.7.1.1' -Option ReadOnly -Force | |
#New-Variable -Name 'OIDAIAOCSP' -Value '1.3.6.1.5.5.7.48.1' -Option ReadOnly -Force | |
Function Get-RemoteTLSCertificate | |
{ | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Hostname, | |
[int] | |
$Port = 443 | |
) | |
$Certificate = $null | |
$TcpClient = New-Object -TypeName System.Net.Sockets.TcpClient | |
try { | |
$TcpClient.Connect($Hostname, $Port) | |
$TcpStream = $TcpClient.GetStream() | |
$Callback = { param($sender, $cert, $chain, $errors) return $true } | |
$TlsStream = New-Object -TypeName System.Net.Security.SslStream -ArgumentList @($TcpStream, $true, $Callback) | |
try { | |
$TlsStream.AuthenticateAsClient('') | |
$Certificate = $TlsStream.RemoteCertificate | |
} finally { | |
$TlsStream.Dispose() | |
} | |
} finally { | |
$TcpClient.Dispose() | |
} | |
if ($Certificate) { | |
if ($Certificate -isnot [System.Security.Cryptography.X509Certificates.X509Certificate2]) { | |
$Certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $Certificate | |
} | |
return $Certificate | |
} | |
} | |
function Get-IssuerCertificate | |
{ | |
param( | |
[Parameter(Mandatory=$true)] | |
[System.Security.Cryptography.X509Certificates.X509Certificate2] | |
$X509Certificate | |
) | |
foreach ($ext in $X509Certificate.Extensions) | |
{ | |
if ($ext.Oid.Value -match $OIDAIA) | |
{ | |
# Try to download the AIA certificate to memory and validate using a recursive call | |
$wc = New-Object -TypeName System.Net.WebClient | |
foreach ($val in $($ext.Format($false) | Select-String '(http[s]?)(:\/\/)([\w\.]+)(\/)([^.]+)(\.[\w\d]+)' -AllMatches).Matches) | |
{ | |
[byte[]]$certbytes = $wc.DownloadData($val.Value) | |
$parentCert = New-Object "System.Security.Cryptography.X509Certificates.X509Certificate2" -ArgumentList @(,$certbytes) | |
if ($parentCert) | |
{ | |
break | |
} | |
} | |
$wc.Dispose() | |
if ($parentCert) { | |
return $parentCert | |
} | |
else { | |
return $null | |
} | |
} | |
} | |
} | |
function Get-CertificateAIACDPProps | |
{ | |
param( | |
[Parameter(Mandatory=$true)] | |
[System.Security.Cryptography.X509Certificates.X509Certificate2] | |
$X509Certificate, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$OID | |
) | |
Remove-Variable -Name 'cert' -Force -ErrorAction SilentlyContinue | |
$fqdns = @() | |
foreach ($ext in $X509Certificate.Extensions) | |
{ | |
# List all FQDNs in the CDP and AIA extensions | |
if ($ext.Oid.Value -match $OID) | |
{ | |
#foreach ($val in $($ext.Format($false) | Select-String '(http[s]?)(:\/\/)([^\s\)]+)' -AllMatches).Matches) | |
foreach ($val in $($ext.Format($false) | Select-String '(?<=http:\/\/)([\w\.]+)(?=[\/]|[)\.\s]|$)' -AllMatches).Matches) | |
{ | |
if ($fqdns -notcontains $val.Value) | |
{ | |
$fqdns += $val.Value | |
} | |
} | |
} | |
} | |
return $fqdns | |
} | |
$webserver = "www.microsoft.com" | |
$cert = Get-RemoteTLSCertificate -Hostname $webserver | |
#$c.Extensions | Format-Table @{Label = "OID"; Expression={$_.Oid.Value}}, @{Label="FriendlyName"; Expression={$_.Oid.FriendlyName}}, @{Label = "ASN1DecodedData"; Expression={$_.format($false)}} | |
Write-Output "=== Getting AIA/CDP FQDNs from $($webserver) ===" | |
$cdp = Get-CertificateAIACDPProps -X509Certificate $cert -OID $OIDCDP | |
$aia = Get-CertificateAIACDPProps -X509Certificate $cert -OID $OIDAIA | |
Write-Output ">>CDP:" | |
Write-Output $cdp | |
Write-Output ">>AIA:" | |
Write-Output $aia | |
$cert = Get-IssuerCertificate -X509Certificate $cert | |
Do { | |
Write-Output `n | |
Write-Output "=== Getting AIA/CDP FQDNs from $($cert.Subject) ===" | |
$cdp = Get-CertificateAIACDPProps -X509Certificate $cert -OID $OIDCDP | |
$aia = Get-CertificateAIACDPProps -X509Certificate $cert -OID $OIDAIA | |
Write-Output ">>CDP:" | |
Write-Output $cdp | |
Write-Output ">>AIA:" | |
Write-Output $aia | |
$cert = Get-IssuerCertificate -X509Certificate $cert | |
} until ($cert -eq $null) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment