-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigureOpenSSH.ps1
164 lines (132 loc) · 6.97 KB
/
ConfigureOpenSSH.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
function Test-IsAdmin {
return ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Invoke-ElevateScript {
Start-Process PowerShell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
function Set-SSHClientConfig {
param (
[Parameter(Mandatory)]
[string]$Path
)
if ($Path -eq $script:ConfigPath -and (Test-Path -Path $script:ConfigPathUser)) {
$deleteUserConfig = Read-Host "A user-specific configuration file exists which overrides the system-wide configuration. Do you wish to remove it? (Y/N)"
if ($deleteUserConfig -eq "Y" -or $deleteUserConfig -eq "y") {
Remove-Item -Path $script:ConfigPathUser -Force
Write-Host "User-specific configuration file has been removed."
}
}
$applyOverrides = Read-Host "Do you wish to apply additional overrides to the configuration? (Y/N)"
if ($applyOverrides -eq "Y" -or $applyOverrides -eq "y") {
$script:sshConfig = $script:sshConfig -replace '(?<=MACs\s)([^\r\n]*)', '$1,hmac-sha2-256'
}
Set-Content -Path $Path -Value $script:sshConfig
if ($script:sshVersion -match "_9\.") {
Add-Content -Path $Path -Value $script:sshConfigV9
}
Write-Host "SSH configuration has been added to $Path"
}
function Set-SSHServerConfig {
param (
[Parameter(Mandatory)]
[string]$Path
)
Stop-Service -Name sshd
if (Test-Path -Path $script:ConfigPathServer) {
$removeSSHD = Read-Host "A configuration file for the OpenSSH server already exists. Do you wish to remove it? (Y/N)"
if ($removeSSHD -eq "Y" -or $removeSSHD -eq "y") {
Remove-Item -Path $script:ConfigPathServer -Force
Write-Host "OpenSSH server configuration file has been removed."
}
}
Restart-Service -Name sshd -Force
$lineToFind = "#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key"
$fileContent = Get-Content -Path $Path
$lineIndex = $fileContent.IndexOf($lineToFind)
if ($lineIndex -ge 0) {
$newContent = $fileContent[$lineIndex] + "`r`n" + $script:sshConfigServer
$fileContent[$lineIndex] = $newContent
Set-Content -Path $Path -Value $fileContent
Write-Host "OpenSSH server configuration has been added to $Path"
}
else {
Write-Warning "The specified line '$lineToFind' was not found in the file."
}
Read-Host "Press any key to exit..."
exit
}
# Define the paths to the .ssh directory & the configuration files
$script:ConfigPath = Join-Path -Path $env:PROGRAMDATA -ChildPath "ssh\ssh_config"
$script:ConfigPathUser = Join-Path -Path $env:USERPROFILE -ChildPath ".ssh\config"
$script:ConfigPathServer = Join-Path -Path $env:PROGRAMDATA -ChildPath "ssh\sshd_config"
$script:sshVersion = & ssh -V 2>&1
# SSH configuration
$script:sshConfig = @"
Host *
KexAlgorithms curve25519-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
HostKeyAlgorithms [email protected],[email protected],[email protected],[email protected],rsa-sha2-512,rsa-sha2-256
CASignatureAlgorithms [email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256
PubkeyAcceptedAlgorithms [email protected],[email protected],[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
"@
$script:sshConfigV9 = @"
HostbasedAcceptedAlgorithms [email protected],[email protected],[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
"@
$script:sshConfigServer = @"
HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key
KexAlgorithms curve25519-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256
Ciphers [email protected],[email protected],aes256-ctr,aes192-ctr,aes128-ctr
HostKeyAlgorithms [email protected],[email protected],[email protected],[email protected],rsa-sha2-512,rsa-sha2-256
CASignatureAlgorithms [email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256
HostbasedAcceptedAlgorithms [email protected],[email protected],[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
PubkeyAcceptedAlgorithms [email protected],[email protected],[email protected],rsa-sha2-512,[email protected],rsa-sha2-256
"@
if ($script:sshVersion -match "_8\.") {
$updateOpenSSH = Read-Host "Do you wish to install the latest version of OpenSSH? (Y/N)"
if ($updateOpenSSH -eq "Y" -or $updateOpenSSH -eq "y") {
Start-Process winget -ArgumentList "install -e --id Microsoft.OpenSSH.Beta" -Wait -NoNewWindow
$script:sshVersion = & ssh -V 2>&1
}
}
if (-not (Test-IsAdmin)) {
Write-Host "This script requires administrator privileges to harden the OpenSSH client configuration system-wide & the OpenSSH server configuration!"
$elevateScript = Read-Host "Do you wish to run this script with elevated privileges? (Y/N)"
if ($elevateScript -eq "Y" -or $elevateScript -eq "y") {
Invoke-ElevateScript
}
else {
$hardenClientUser = Read-Host "Do you wish to harden the OpenSSH client configuration for the current user profile? (Y/N)"
if ($hardenClientUser -eq "Y" -or $hardenClientUser -eq "y") {
Set-SSHClientConfig -Path $script:ConfigPathUser
}
}
}
else {
$hardenClient = Read-Host "Do you wish to harden the OpenSSH client configuration? (Y/N)"
if ($hardenClient -eq "Y" -or $hardenClient -eq "y") {
$currentUserOnly = Read-Host "Choose an option:
1 - Apply the hardening script to the current user only. (This will only affect your user profile.)
2 - Apply the hardening script system-wide. (Requires administrator privileges; affects all users on this system.)
Enter 1 or 2"
switch ($currentUserOnly) {
"1" {
Set-SSHClientConfig -Path $script:ConfigPathUser
}
"2" {
Set-SSHClientConfig -Path $script:ConfigPath
}
default {
Read-Host "Invalid input. Press any key to exit..."
exit
}
}
}
$hardenServer = Read-Host "Do you wish to harden the OpenSSH server configuration? (Y/N)"
if ($hardenServer -eq "Y" -or $hardenServer -eq "y") {
Set-SSHServerConfig -Path $script:ConfigPathServer
}
}