-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_ssh_key_windows.ps1
194 lines (164 loc) · 5.88 KB
/
add_ssh_key_windows.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Script to add SSH keys to Windows administrators authorized_keys
# Must be run as administrator
# Check if running as administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "This script must be run as Administrator" -ForegroundColor Red
exit 1
}
# Variables
$programData = $env:ProgramData
$sshPath = Join-Path $programData "ssh"
$adminKeys = Join-Path $sshPath "administrators_authorized_keys"
# Function to create necessary directories and files
function Initialize-SshEnvironment {
if (-not (Test-Path -Path $sshPath)) {
New-Item -ItemType Directory -Path $sshPath -Force
Write-Host "✓ Created $sshPath" -ForegroundColor Green
}
if (-not (Test-Path -Path $adminKeys)) {
New-Item -ItemType File -Path $adminKeys -Force
Write-Host "✓ Created $adminKeys" -ForegroundColor Green
}
}
# Function to get keys from GitHub
function Get-GitHubKeys {
param (
[string]$username
)
try {
$response = Invoke-RestMethod -Uri "https://api.github.com/users/$username/keys" -ErrorAction Stop
return $response
}
catch {
Write-Host "✗ Failed to fetch keys from GitHub: $_" -ForegroundColor Red
return $null
}
}
# Function to add a key if it doesn't exist
function Add-UniqueKey {
param (
[string]$key
)
$existingKeys = Get-Content -Path $adminKeys
if ($existingKeys -contains $key) {
Write-Host "! Key already exists in $adminKeys" -ForegroundColor Yellow
return
}
Add-Content -Path $adminKeys -Value $key
Write-Host "✓ Added new key to $adminKeys" -ForegroundColor Green
}
# Function to draw menu
function Show-Menu {
param (
[int]$selectedIndex
)
Clear-Host
Write-Host "`n Windows SSH Key Manager`n" -ForegroundColor Cyan
Write-Host " Use ↑↓ arrows to select and Enter to confirm:`n" -ForegroundColor Gray
$options = @("Import keys from GitHub", "Enter key manually")
for ($i = 0; $i -lt $options.Count; $i++) {
if ($i -eq $selectedIndex) {
Write-Host " > " -NoNewline -ForegroundColor Cyan
Write-Host $options[$i] -ForegroundColor White -BackgroundColor DarkBlue
} else {
Write-Host " $($options[$i])" -ForegroundColor Gray
}
}
}
# Function to handle GitHub key import
function Import-GitHubKeys {
Write-Host "`nEnter GitHub username: " -ForegroundColor Cyan -NoNewline
$githubUsername = Read-Host
Write-Host "`nFetching keys from GitHub..." -ForegroundColor Yellow
$keys = Get-GitHubKeys -username $githubUsername
if ($keys) {
Write-Host "`nFound $($keys.Count) keys for user " -NoNewline
Write-Host $githubUsername -ForegroundColor Cyan
foreach ($key in $keys) {
Write-Host "`nKey ID: " -NoNewline
Write-Host $key.id -ForegroundColor Cyan
$addThis = Read-Host "Add this key? (y/n)"
if ($addThis -eq 'y') {
Add-UniqueKey -key $key.key
}
}
}
}
# Function to handle manual key entry
function Add-ManualKey {
Write-Host "`nPaste your public key: " -ForegroundColor Cyan
$manualKey = Read-Host
if ($manualKey) {
Add-UniqueKey -key $manualKey
}
}
# Function to restart SSH service
function Restart-SshService {
Write-Host "`nRestarting SSH service..." -ForegroundColor Yellow
try {
Stop-Service sshd -Force -ErrorAction Stop
Start-Sleep -Seconds 2
Start-Service sshd -ErrorAction Stop
Write-Host "✓ SSH service restarted successfully" -ForegroundColor Green
}
catch {
Write-Host "✗ Failed to restart SSH service: $_" -ForegroundColor Red
}
}
# Function to fix SSH key permissions
function Repair-SshKeyPermissions {
param (
[string]$keyPath = "$env:USERPROFILE\.ssh\id_rsa"
)
Write-Host "`nChecking private key permissions..." -ForegroundColor Yellow
if (-not (Test-Path -Path $keyPath)) {
Write-Host "! No private key found at $keyPath - skipping permissions fix" -ForegroundColor Yellow
return
}
try {
# Remove all existing permissions
icacls $keyPath /inheritance:r
# Add permission only for current user
icacls $keyPath /grant ${env:USERNAME}:"(R)"
Write-Host "✓ Fixed permissions for $keyPath" -ForegroundColor Green
}
catch {
Write-Host "✗ Failed to set key permissions: $_" -ForegroundColor Red
}
}
# Main script
Initialize-SshEnvironment
# Menu navigation
$selectedIndex = 0
$options = @("Import keys from GitHub", "Enter key manually")
do {
Show-Menu -selectedIndex $selectedIndex
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
switch ($key.VirtualKeyCode) {
38 { # Up arrow
$selectedIndex = ($selectedIndex - 1) % $options.Count
if ($selectedIndex -lt 0) { $selectedIndex = $options.Count - 1 }
}
40 { # Down arrow
$selectedIndex = ($selectedIndex + 1) % $options.Count
}
13 { # Enter
switch ($selectedIndex) {
0 { Import-GitHubKeys }
1 { Add-ManualKey }
}
break
}
}
} while ($key.VirtualKeyCode -ne 13)
# Set correct permissions
Write-Host "`nSetting permissions..." -ForegroundColor Yellow
icacls $adminKeys /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
Write-Host "✓ Set permissions for $adminKeys" -ForegroundColor Green
# Restart SSH service
Restart-SshService
# Add this before the final "Press any key to exit"
Repair-SshKeyPermissions
Write-Host "`nPress any key to exit..." -ForegroundColor Gray
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")