-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-certificates.ps1
209 lines (204 loc) · 7.32 KB
/
local-certificates.ps1
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
<#
.SYNOPSIS
add, remove, list - file certificates to the local store and hostsnames added to hosts file for devops
.DESCRIPTION
help manage localmachine root certificate store and hosts file using crt files in the current directory
.PARAMETER Option
Specifiy what to do
.EXAMPLE
C:\PS> local-certificate.ps1 -option add
Adds *.crt to local certificate store and modifies the hosts file to add hostname.
Changing add to remove will do the opposite
.EXAMPLE
C:\PS> local-certificate.ps1
shows the certifcates found in the root store that are in the current directory
.OUTPUTS
System.String
.NOTES
Author: John Clark
Date: 5/15/2023
#>
[CmdletBinding(DefaultParameterSetName = 'Option')]
param(
[Parameter(
Position = 0,
ParameterSetName = 'Option')
]
[ValidateSet('add', 'remove', 'list', '', IgnoreCase)]
[AllowEmptyString()]
[string]$Option
)
$IsElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
function Edit-TrustedRootMachineCertificate($certificate)
{
$store = get-item Cert:\LocalMachine\Root;
$store.Open("MaxAllowed")
#get host file each iteration in case it changed
$HostsFileContent = Get-Content $env:windir\System32\drivers\etc\hosts
if ($Option -cmatch 'list')
{
if ($store.Certificates.Find("FindByThumbprint", $certificate.Thumbprint, $true).Count -eq 1)
{
if ($HostsFileContent -match "`t$CertFileName")
{
ForEach ($line in $HostsFileContent)
{
if ($line -match "`t$CertFileName")
{
Write-Host $line
}
}
}
else
{
Write-Host "$CertFilename not found in hosts file"
}
Write-Host "------------------------------------------------------------------------------------------"
Write-Host $certificate;
}
else
{
ForEach ($line in $HostsFileContent)
{
if ($line -match "`t$CertFileName")
{
Write-Host "Found dns: $line, in hosts file without certificate installed."
}
}
Write-Host "Cert File: $CertFileName, NOT FOUND in certificate store."
}
}
elseif ( $Option -cmatch 'add' )
{
if ($store.Certificates.Find("FindByThumbprint", $certificate.Thumbprint, $false).Count -eq 0)
{
if ($IsElevated)
{
$store.Add($certificate);
if ($null -eq (Select-String -Path $env:windir\System32\drivers\etc\hosts -Pattern "`t$CertFileName"))
{
Write-Output "Updating hosts file"
# TODO: will have to figure out ip but for now use known set ip
Add-Content -Path $env:windir\System32\drivers\etc\hosts -Value "172.16.0.10`t$CertFileName" -Force
}
else
{
Write-Output "$CertFileName exists in hosts file already"
}
Write-Host "Added root certificate: $CertFilename";
}
else
{
#rerun as admin
$proc = Start-Process powershell -WorkingDirectory $PSScriptRoot -Verb runAs -PassThru -ArgumentList "-noprofile -file $PSCommandPath add"
$proc.WaitForExit()
if ($proc.ExitCode -ne 0)
{
Write-Warning "$_ exited with status code $( $proc.ExitCode )"
Exit
}
else
{
Write-Host "Success"
}
exit
}
}
else
{
Write-Host "Already added root certificate: $CertFileName";
if ($HostsFileContent -match "`t$CertFileName")
{
ForEach ($line in $HostsFileContent)
{
if ($line -match "`t$CertFileName")
{
Write-Host "Found this line in hosts file: $line"
}
}
}
else
{
Write-Host "$CertFilename not found in hosts file"
}
}
}
elseif ( $Option -cmatch 'remove' )
{
if ($store.Certificates.Find("FindByThumbprint", $certificate.Thumbprint, $true).Count -eq 1)
{
if ($IsElevated)
{
if (Select-String -Path $env:windir\System32\drivers\etc\hosts -Pattern "`t$CertFileName")
{
Write-Output "Removing $CertFileName from hosts file"
# TODO: will have to figure out ip but for now use known set ip
$NewHostsFileContent = Get-Content $HostFile | Where-Object {$_ -notmatch $CertFileName}
$NewHostsFileContent | Out-File $HostFile -enc ascii -Force
}
else
{
Write-Output "$CertFileName does not exist in hosts file"
}
$store.Remove($certificate);
Write-Host "Removed root certificate $CertFilename from store.";
}
else
{
#rerun as admin
$proc = Start-Process powershell -WorkingDirectory $PSScriptRoot -Verb runAs -PassThru -ArgumentList "-noprofile -file $PSCommandPath remove"
$proc.WaitForExit()
if ($proc.ExitCode -ne 0)
{
Write-Warning "$_ exited with status code $( $proc.ExitCode )"
Exit
}
else
{
Write-Host "Success"
}
}
}
else
{
Write-Host "Root Certificate NOT found: $CertFileName";
if (Select-String -Path $env:windir\System32\drivers\etc\hosts -Pattern "`t$CertFileName")
{
Write-Output "Found $CertFileName in hosts file, removing..."
# TODO: will have to figure out ip but for now use known set ip
$NewHostsFileContent = Get-Content $HostFile | Where-Object {$_ -notmatch $CertFileName}
$NewHostsFileContent | Out-File $HostFile -enc ascii -Force
}
else
{
Write-Output "$CertFileName does not exist in hosts file"
}
}
}
else
{
Write-Host "How to use:"
Write-Host " .\local-certificate.ps1 {add|remove|list}"
Exit
}
$store.Close();
}
#some basics
$HostFile = "$env:windir\System32\drivers\etc\hosts"
$currentDir = Split-Path $MyInvocation.MyCommand.Path -Parent;
#look for certs in local folder
$files = Get-ChildItem "$currentDir" -Filter "*.crt"
if ($files.Count -eq 0)
{
Write-Host "no .crt files found"
#show hosts here
}
else
{
foreach ($file in $files)
{
$CertFileName = $file -replace ".crt"
$certfile = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($file.FullName);
Edit-TrustedRootMachineCertificate -certificate $certfile;
}
}