forked from NetSPI/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-SSLCertInfo-Scan.psm1
More file actions
519 lines (454 loc) · 19.8 KB
/
Get-SSLCertInfo-Scan.psm1
File metadata and controls
519 lines (454 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<#
Script: Get-SSLCertInfo-Scan.psm1
Version: 1.2
Author: Scott Sutherland (@_nullbind), NetSPI
References: This was based on work by Rob VandenBrink.
References: https://isc.sans.edu/forums/diary/Assessing+Remote+Certificates+with+Powershell/20645/
Description: This script accepts IP/Port combinations and can be used to read information such as
subject, name, and issuerfrom remote SSL certificates. See examples for more information.
Examples:
# Target specific IP and port
Get-SSLCertInfo-Scan -Verbose -IPAddress 127.0.0.2 -Port 443
# Target a list of IP:Port from file, one per line; Display full records
Get-SSLCertInfo-Scan -Verbose -InputFile C:\temp\list.txt
# Target a list of IP:Port from file, one per line; Display a list of domains discovered.
Get-SSLCertInfo-Scan -Verbose -InputFile C:\temp\list.txt -OnlyDomainList
# Target a list of IP:Port from file, one per line; Look up associated arin info.
Get-SSLCertInfo-Scan -Verbose -InputFile C:\temp\list.txt -ArinLookup
# Target a list of IP:Port from pipeline
"127.0.0.1:50" | Get-SSLCertInfo-Scan -Verbose
# Target a list of IP:Port from file, one per line; Display a list of domains discovered. Show potential
# Active Directory domains in verbose output based on number of ".".
Get-SSLCertInfo-Scan -Verbose -InputFile C:\temp\list.txt -OnlyDomainList -ShowAdDomains
# Target all parameters, pipeline, and file inputs. Store the results and display them
$Results = "127.0.0.1:50","127.0.0.2:50", | Get-SSLCertInfo-Scan -Verbose -InputFile C:\temp\list.txt -IPAddress 127.0.0.3 -Port 443
$Results
$Results | Export-CSV -NoTypeInformation output.csv
$Results | Out-GridView
# Todo
Add network range scan option
Add nmap importer
Add nessus importer
#>
function Get-SSLCertInfo-Scan {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false,
HelpMessage="File containing URLs/IP and ports IP:Port.")]
[string]$InputFile,
[Parameter(Mandatory=$false,
HelpMessage="Only display list of uniq domains.")]
[switch]$OnlyDomainList,
[Parameter(Mandatory=$false,
HelpMessage="List potential Active Directory domains.")]
[switch]$ShowAdDomains,
[Parameter(Mandatory=$false,
HelpMessage="Look up ip address owner.")]
[switch]$ArinLookup,
[Parameter(Mandatory=$false,
HelpMessage="IP Address.")]
[string]$IPAddress,
[Parameter(Mandatory=$false,
HelpMessage="TCP port.")]
[string]$Port,
[Parameter(Mandatory=$false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage="IP port combo IP:PORT.")]
[string]$IPPort
)
Begin
{
Write-Verbose "Creating a list of targets"
# Create output table
$CertificateInfo = New-Object System.Data.DataTable
$null = $CertificateInfo.Columns.Add("IpAddress")
$null = $CertificateInfo.Columns.Add("Port")
$null = $CertificateInfo.Columns.Add("Subject")
$null = $CertificateInfo.Columns.Add("EffectiveDate")
$null = $CertificateInfo.Columns.Add("ExpirationDate")
$null = $CertificateInfo.Columns.Add("Issuser")
$null = $CertificateInfo.Columns.Add("Verified")
# Create targets table
$Targets = New-Object System.Data.DataTable
$null = $targets.Columns.Add("IPAddress")
$null = $targets.Columns.Add("Port")
# Process a list of targets
if($InputFile){
# Get target list from file
if(Test-Path -Path $InputFile){
Write-Verbose " - Importing targets from $InputFile"
$IpPortList = gc $InputFile
$IpPortList |
ForEach-Object {
$Target = $_ -split(":")[0]
$TargetPort = $Target[1]
$TargetIP = $Target[0]
# Add to targets list
$targets.Rows.Add($TargetIp,$TargetPort) | Out-Null
}
}else{
Write-Verbose " - File path is invalid."
}
}
# Process single target
if($IPAddress -and $Port)
{
# Add target to list
Write-Verbose " - Importing targets from parameters"
$targets.Rows.Add($IPAddress,$Port) | Out-Null
}
# Process single IP:Port target
if($IPPort)
{
$Target = $IPPort -split(":")[0]
$TargetIp = $Target[1]
$TargetPort = $Target[0]
# Add to targets list
Write-Verbose " - Importing targets from parameters - alt format"
$targets.Rows.Add($TargetIp,$TargetPort) | Out-Null
}
}
Process
{
# Add targets from the pipeline
if($_){
Write-Verbose " - Importing targets from pipeline"
# Get target list from pipeline
$Target = $_ -split(":")[0]
$TargetIp = $Target[1]
$TargetPort = $Target[0]
# Add to targets list
$targets.Rows.Add($TargetIp,$TargetPort) | Out-Null
}
}
End
{
# ------------------------------
# Scrape cert info from targets
# ------------------------------
Write-Verbose "Grabbing certificate information"
$DraftResults = $targets |
ForEach-Object {
$CurrentIp = $_.IpAddress
$CurrentPort = $_.Port
Get-CertInfo -IPAddress $CurrentIp -Port $CurrentPort -ErrorAction SilentlyContinue
}
# ------------------------------
# Get unique list of domains
# ------------------------------
$AltDomainList = $DraftResults | where SubjectAltName -notlike "" | Select-Object SubjectAltName -ExpandProperty SubjectAltName
$OrgDomainList = $DraftResults | where SubjectDomain -notlike "" | Select-Object SubjectDomain -ExpandProperty SubjectDomain
$DomainList = $AltDomainList+$DomainList | Select-Object @{Name="DomainName";Expression={$_}} | Sort-Object DomainName -Unique
$DomainCount = $DomainList.count
Write-Verbose "$DomainCount unique domains found."
# ------------------------------
# Resolve arin if asked
# ------------------------------
if($ArinLookup -and (-not $OnlyDomainList)){
Write-Verbose "Processing arin lookups"
$DraftResults | Where-Object SubjectDomain -NotLike "" |
foreach {
$rdomain = $_.SubjectDomain
$rsubdomain = $_.SubjectAltName
$rip = $_.ipaddress
$rport = $_.port
Write-Verbose " - Processing arin lookups - $rip ($rdomain - $rsubdomain)"
Invoke-Arin-Lookup -Verbose -IpAddress $rip -IpPort $_.port -Domain $rdomain -SubDomain $rsubdomain -Subject $_.Subject -SubjectCountry $_.SubjectCountry -SubjectState $_.SubjectState -SubjectCity $_.SubjectCity -SubjectOrganization $_.SubjectOrganization -SubjectOU $_.SubjectOU -Issuer $_.Issuer -ExpirationDate $_.ExpirationDate -EffectiveDate $_.EffectiveDate -Verified $_.Verified -thumbprint $_.thumbprint -ErrorAction SilentlyContinue
}
}
# Display Results
If($OnlyDomainList){
$DomainList
}else{
if(-not $ArinLookup){
$DraftResults | Sort-Object SubjectDomain,SubjectAltName -Unique
}
}
# ------------------------------
# Check for potential ad domains
# ------------------------------
if($ShowAdDomains){
# All domains with > 2 ".", filter out www, drop host name, uniuque display
Write-Verbose "Checking for potential Active Directory domains."
$PotentialAdDomains = New-Object System.Data.DataTable
$PotentialAdDomains.Columns.add("DomainName") | Out-Null
$DomainList |
ForEach-Object {
#$CheckDomain = "hostname.subdomain.domain.com"
$CheckDomain = $_.DomainName
$CheckDomainArray = $CheckDomain.Split(".")
$CheckDomainNum = $CheckDomainArray.GetUpperBound(0);
if(($CheckDomainNum -gt 2) -and ($CheckDomain -notlike "*www.*")){
# Grab the potential ad domain by dropping the hostname
#$PotentialAdDomains += [string]($CheckDomainArray[1..$CheckDomainNum] -join("."))
$PotentialAdDomains += [string]($CheckDomainArray[1..$CheckDomainNum] -join("."))
}
}
# Display final list
$PotentialAdDomainsUnique = $PotentialAdDomains | Select-Object -Unique
$AdCount = $PotentialAdDomainsUnique.count
Write-Verbose "$AdCount potential Active Directory domains were found."
$PotentialAdDomainsUnique | foreach {write-verbose "- $_"}
# Write log
if($ADOutputFile){
$PotentialAdDomainsUnique | Out-File $ADOutputFile
}
}
}
}
function Get-CertInfo
{
[CmdletBinding()]
Param (
[string]$IPAddress,
[Parameter(Mandatory=$false,
HelpMessage="TCP port.")]
[string]$Port
)
write-verbose " - Grabbing certificate info from $IPAddress on $port"
# Create connection to server
$TCPClient = New-Object -TypeName System.Net.Sockets.TCPClient
$TcpSocket = New-Object Net.Sockets.TcpClient($IPAddress,$Port)
$TcpSocket.ReceiveTimeout = 5000;
# Establish stream
$tcpstream = $TcpSocket.GetStream()
$Callback = {param($sender,$cert,$chain,$errors) return $true}
$SSLStream = New-Object -TypeName System.Net.Security.SSLStream -ArgumentList @($tcpstream, $True, $Callback)
$SSLStream.AuthenticateAsClient($IPAddress)
# Grab cert information
$Certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($SSLStream.RemoteCertificate)
# Clean up
$SSLStream.Dispose()
$TCPClient.Dispose()
$SubjectArray = $Certificate.GetName() -split(",")
$SubjectArray |
ForEach-Object{
$item = $_.ToString().Trim()
$itemParts = $item -split("=")
# Set country
if($itemParts[0] -like "C"){
$Country = $itemParts[1]
}
# Set state
if(($itemParts[0] -like "S") -or ($itemParts[0] -like "ST")){
$State = $itemParts[1]
}
# Set city
if($itemParts[0] -like "L"){
$City = $itemParts[1]
}
# Set Oranization
if($itemParts[0] -like "O"){
$Organization = $itemParts[1]
}
# Set Oranization Unit
if($itemParts[0] -like "OU"){
$OranizationalUnit = $itemParts[1]
}
# Set Domain
if($itemParts[0] -like "CN"){
$Domain = $itemParts[1]
}
}
# Get Alternative Domain List
if($Certificate.DnsNameList){
$Certificate.DnsNameList |
ForEach-Object{
[string]$AltDomain = $_[0];
$CertInfo = New-Object PSObject
$CertInfo | add-member Noteproperty IpAddress $IPAddress;
$CertInfo | add-member Noteproperty Port $Port;
$CertInfo | add-member Noteproperty Subject $Certificate.GetName();
$CertInfo | add-member Noteproperty SubjectCountry $Country;
$CertInfo | add-member Noteproperty SubjectState $State;
$CertInfo | add-member Noteproperty SubjectCity $City;
$CertInfo | add-member Noteproperty SubjectOrganization $Organization;
$CertInfo | add-member Noteproperty SubjectOU $OranizationalUnit;
$CertInfo | add-member Noteproperty SubjectDomain $Domain;
$CertInfo | add-member Noteproperty SubjectAltName $AltDomain;
$CertInfo | add-member Noteproperty Issuer $Certificate.GetIssuerName();
$CertInfo | add-member Noteproperty ExpirationDate $Certificate.GetEffectiveDateString();
$CertInfo | add-member Noteproperty EffectiveDate $Certificate.GetExpirationDateString();
$CertInfo | add-member Noteproperty Verified $Certificate.Verify();
$CertInfo | add-member Noteproperty thumbprint $Certificate.Thumbprint;
$CertInfo
}
}else{
$CertInfo = New-Object PSObject
$CertInfo | add-member Noteproperty IpAddress $IPAddress;
$CertInfo | add-member Noteproperty Port $Port;
$CertInfo | add-member Noteproperty Subject $Certificate.GetName();
$CertInfo | add-member Noteproperty SubjectCountry $Country;
$CertInfo | add-member Noteproperty SubjectState $State;
$CertInfo | add-member Noteproperty SubjectCity $City;
$CertInfo | add-member Noteproperty SubjectOrganization $Organization;
$CertInfo | add-member Noteproperty SubjectOU $OranizationalUnit;
$CertInfo | add-member Noteproperty SubjectDomain $Domain;
$CertInfo | add-member Noteproperty SubjectDomainAlt "";
$CertInfo | add-member Noteproperty Issuer $Certificate.GetIssuerName();
$CertInfo | add-member Noteproperty ExpirationDate $Certificate.GetEffectiveDateString();
$CertInfo | add-member Noteproperty EffectiveDate $Certificate.GetExpirationDateString();
$CertInfo | add-member Noteproperty Verified $Certificate.Verify();
$CertInfo | add-member Noteproperty thumbprint $Certificate.Thumbprint;
$CertInfo
}
}
function Invoke-Arin-Lookup{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage="The IP Address to lookup.")]
[string]$IpAddress,
[Parameter(Mandatory=$false,
HelpMessage="port.")]
[string]$IpPort,
[Parameter(Mandatory=$false,
HelpMessage="Original domain.")]
[string]$Domain,
[Parameter(Mandatory=$false,
HelpMessage="Sub domain.")]
[string]$SubDomain,
[Parameter(Mandatory=$false,
HelpMessage="subjectCountry.")]
[string]$Subject,
[Parameter(Mandatory=$false,
HelpMessage="subjectCountry.")]
[string]$SubjectCountry,
[Parameter(Mandatory=$false,
HelpMessage="SubjectState.")]
[string]$SubjectState,
[Parameter(Mandatory=$false,
HelpMessage="SubjectCity.")]
[string]$SubjectCity,
[Parameter(Mandatory=$false,
HelpMessage="SubjectOrganization.")]
[string]$SubjectOrganization,
[Parameter(Mandatory=$false,
HelpMessage="SubjectOU.")]
[string]$SubjectOU,
[Parameter(Mandatory=$false,
HelpMessage="Issuer.")]
[string]$Issuer,
[Parameter(Mandatory=$false,
HelpMessage="ExpirationDate.")]
[string]$ExpirationDate,
[Parameter(Mandatory=$false,
HelpMessage="EffectiveDate.")]
[string]$EffectiveDate,
[Parameter(Mandatory=$false,
HelpMessage="Verified.")]
[string]$Verified,
[Parameter(Mandatory=$false,
HelpMessage="thumbprint.")]
[string]$thumbprint
)
Begin
{
# IP info table
$TblIPInfo = new-object System.Data.DataTable
$TblIPInfo.Columns.Add("Domain") | Out-Null
$TblIPInfo.Columns.Add("Subdomain") | Out-Null
$TblIPInfo.Columns.Add("IpPort") | Out-Null
$TblIPInfo.Columns.Add("IpAddress") | Out-Null
$TblIPInfo.Columns.Add("IpOwner") | Out-Null
$TblIPInfo.Columns.Add("IpStartRange") | Out-Null
$TblIPInfo.Columns.Add("IpEndRange") | Out-Null
$TblIPInfo.Columns.Add("IpCountry") | Out-Null
$TblIPInfo.Columns.Add("IpState") | Out-Null
$TblIPInfo.Columns.Add("IpCity") | Out-Null
$TblIPInfo.Columns.Add("IpZip") | Out-Null
$TblIPInfo.Columns.Add("IpISP") | Out-Null
$TblIPInfo.Columns.Add("CertSubject") | Out-Null
$TblIPInfo.Columns.Add("CertSubjectCountry") | Out-Null
$TblIPInfo.Columns.Add("CertSubjectState") | Out-Null
$TblIPInfo.Columns.Add("CertSubjectCity") | Out-Null
$TblIPInfo.Columns.Add("CertSubjectOrganization") | Out-Null
$TblIPInfo.Columns.Add("CertSubjectOU") | Out-Null
$TblIPInfo.Columns.Add("CertIssuer") | Out-Null
$TblIPInfo.Columns.Add("CertExpirationDate") | Out-Null
$TblIPInfo.Columns.Add("CertEffectiveDate") | Out-Null
$TblIPInfo.Columns.Add("CertVerified") | Out-Null
$TblIPInfo.Columns.Add("Certthumbprint") | Out-Null
# Lookup source IP owner
if($IpAddress -notlike ""){
# Send whois request to arin via restful api
$targetip = $IpAddress
# arin lookup
$web = new-object system.net.webclient
[xml]$results = $web.DownloadString("http://whois.arin.net/rest/ip/$targetip")
# Send location query to http://ip-api.com via xml api
if ($IpAddress){
$web2 = new-object system.net.webclient
[xml]$results2 = $web2.DownloadString("http://ip-api.com/xml/$targetip")
}
# Parse data from responses
$IpOwner = $results.net.name
$IpStart = $results.net.startAddress
$IpEnd = $results.net.endaddress
$IpCountry = $results2.query.country.'#cdata-section'
$IpState = $results2.query.region.'#cdata-section'
$IpCity = $results2.query.city.'#cdata-section'
$IpZip = $results2.query.zip.'#cdata-section'
$IpISP = $results2.query.isp.'#cdata-section'
# Put results in the data table
$TblIPInfo.Rows.Add(
"$Domain",
"$SubDomain",
"$IpPort",
"$IpAddress",
"$IpOwner",
"$IpStart",
"$IpEnd",
"$IpCountry",
"$IpState",
"$IpCity",
"$IpZip",
"$IpISP",
"$Subject",
"$SubjectCountry",
"$SubjectState",
"$SubjectCity",
"$SubjectOrganization",
"$SubjectOU",
"$Issuer",
"$ExpirationDate",
"$EffectiveDate",
"$Verified",
"$thumbprint" ) | Out-Null
}else{
# Put results in the data table
$TblIPInfo.Rows.Add(
"$Domain",
"$SubDomain",
"$IpPort",
"$IpAddress",
"$IpOwner",
"$IpStart",
"$IpEnd",
"$IpCountry",
"$IpState",
"$IpCity",
"$IpZip",
"$IpISP",
"$Subject",
"$SubjectCountry",
"$SubjectState",
"$SubjectCity",
"$SubjectOrganization",
"$SubjectOU",
"$Issuer",
"$ExpirationDate",
"$EffectiveDate",
"$Verified",
"$thumbprint" ) | Out-Null
}
# Display results
$TblIPInfo
}
End
{
}
}