Skip to main content
9 of 10
Added missing curly brackets to PowerShell variable ; Added missing attribute to /remove command
JW0914
  • 8.3k
  • 7
  • 31
  • 50

Keys must only be accessible to the user they're intended for and no other account, service, or group.

  • GUI:
    [File] PropertiesSecurityAdvanced
    1. Owner: The key's user
    2. Permission Entries: Remove all except for the key's user
    3. Set key's user to Full Control

  • Cmd:
    ::# Set Key File Variable:
        Set Key="%UserProfile%\.ssh\id_rsa"
    
    ::# Remove Inheritance:
        Icacls %Key% /c /t /Inheritance:d
    
    ::# Set Ownership to Owner:
        :: # Key's within %UserProfile%:
             Icacls %Key% /c /t /Grant %UserName%:F
    
        :: # Key's outside of %UserProfile%:
             TakeOwn /F %Key%
             Icacls %Key% /c /t /Grant:r %UserName%:F
    
    ::# Remove All Users, except for Owner:
        Icacls %Key% /c /t /Remove:g "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users
    
    ::# Verify:
        Icacls %Key%
    
    ::# Remove Variable:
        set "Key="
    

  • PowerShell:
    # Set Key File Variable:
      New-Variable -Name Key -Value "$env:UserProfile\.ssh\id_rsa"
    
    # Remove Inheritance:
      Icacls $Key /c /t /Inheritance:d
    
    # Set Ownership to Owner:
      # Key's within $env:UserProfile:
        Icacls $Key /c /t /Grant ${env:UserName}:F
    
       # Key's outside of $env:UserProfile:
         TakeOwn /F $Key
         Icacls $Key /c /t /Grant:r ${env:UserName}:F
    
    # Remove All Users, except for Owner:
      Icacls $Key /c /t /Remove:g Administrator "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users
    
    # Verify:
      Icacls $Key
    
    # Remove Variable:
      Remove-Variable -Name Key
    
JW0914
  • 8.3k
  • 7
  • 31
  • 50