Windows Admin SSH Key
The snippet can be accessed without any authentication.
Authored by
Adphi
iex (New-Object System.Net.WebClient).DownloadString("https://gitlab.bertha.cloud/snippets/34/raw")
$ErrorActionPreference = "Stop"
$key = "$(((New-Object System.Net.WebClient).DownloadString('https://gitlab.bertha.cloud/marvin.keys')))"
$localKeyDir = "C:\Users\Administrateur\.ssh"
New-Item -Type Directory -Path $localKeyDir
Set-Content "$localKeyDir\authorized_keys" $key
$keyPath = "C:\ProgramData\ssh\administrators_authorized_keys"
Set-Content $keyPath $key
$acl = Get-Acl $keyPath
$acl.SetAccessRuleProtection($true, $false)
$administratorsRule = New-Object system.security.accesscontrol.filesystemaccessrule("Administrateurs","FullControl","Allow")
$systemRule = New-Object system.security.accesscontrol.filesystemaccessrule("SYSTEM","FullControl","Allow")
$acl.SetAccessRule($administratorsRule)
$acl.SetAccessRule($systemRule)
$acl | Set-Acl
$sshConfig = "C:\ProgramData\ssh\sshd_config"
Copy-Item $sshConfig "$sshConfig.bak"
Copy-Item $sshConfig "$sshConfig.tmp"
Get-Content "$sshConfig.bak" | %{$_ -replace "#PasswordAuthentication yes", "PasswordAuthentication no"} | Set-Content "$sshConfig.tmp"
Get-Content "$sshConfig.tmp" | %{$_ -replace "Match Group administrators", "Match Group administrateurs"} | Set-Content $sshConfig
Remove-Item "$sshConfig.tmp"
Restart-Service sshd
Please register or sign in to comment