Wednesday, November 22, 2023

Script to scan IPs and return SSL certificate information

I've compiled this script from various sources and tweaked it to the current state. It has been helpful in scanning a list of IP addresses on a port you specify (443, 8443, etc.) and returning the information in a CSV file. This has been quite useful when a certificate management solution is not in place.

This script goes through the IPs pretty quickly and I scanned a list of 35k IP addresses in 6 hours.

 function testport{
  Param($IP,$port,$timeout)
  $requestCallback = $state = $null
  $client = New-Object System.Net.Sockets.TcpClient
  $beginConnect = $client.BeginConnect($IP,$port,$requestCallback,$state)
  Start-Sleep -milli $timeOut
  if ($client.Connected) { $open = $true } else { $open = $false }
  $client.Close()
  [pscustomobject]@{IP=$IP;port=$port;open=$open}
}
$Port=443
$Timeout=300
Import-csv C:\temp\ips.txt|foreach-object{
write-host $_.IP
$IP = $_.IP
$test=testport -IP $IP -port $Port -timeout $Timeout
If($test.open -eq "True"){
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$url = "https://" + $IP
$req = [Net.HttpWebRequest]::Create($url)
$req.Timeout=2000
$req.GetResponse() | Out-Null
$output = [PSCustomObject]@{
   IPAddress = $IP
   'StartDate' = $req.ServicePoint.Certificate.GetEffectiveDateString()
   'EndDate' = $req.ServicePoint.Certificate.GetExpirationDateString()
   'Subject' = $req.ServicePoint.Certificate.subject
}
#write-host $output
$output|export-csv C:\temp\certresults.csv -notypeinformation -append
$output=$null
}
$output=$null
}

No comments:

Post a Comment