Skip to content
Snippets Groups Projects

Windows Admin SSH Key

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    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")
    Edited
    admin-key.ps1 1.17 KiB
    $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
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment