Skip to content

Instantly share code, notes, and snippets.

@thereisnotime
Created November 16, 2020 22:42
Show Gist options
  • Save thereisnotime/7c9df1ab50d4d5feaa20082dad71ecc0 to your computer and use it in GitHub Desktop.
Save thereisnotime/7c9df1ab50d4d5feaa20082dad71ecc0 to your computer and use it in GitHub Desktop.
Windows - Fix private SSH key file permissions, ownership and inheritance
function Set-PrivateKeyPermissions {
param (
[string]$KeyLocation
)
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error "Must run as administrator. Exiting..."
Return
}
$acl = Get-Acl "$KeyLocation"
$currentUser = New-Object System.Security.Principal.Ntaccount($acl.owner)
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($acl.owner,"FullControl","Allow")
$acl.Access | %{$acl.RemoveAccessRule($_)}
$acl.SetOwner($currentUser)
$acl.SetAccessRuleProtection($true,$false)
$acl.SetAccessRule($accessRule)
$acl | Set-Acl "$KeyLocation"
}
# Example usage:
Set-PrivateKeyPermissions -KeyLocation 'C:\Keys\thereisnotime.pem'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment