-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001_SFTP_main_Script
More file actions
253 lines (216 loc) · 9.99 KB
/
Copy path001_SFTP_main_Script
File metadata and controls
253 lines (216 loc) · 9.99 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
function Show-Menu {
Write-Host "=========================" -ForegroundColor Cyan
Write-Host " OpenSSH Server Control" -ForegroundColor Green
Write-Host " Developed by André Cruz" -ForegroundColor Green
Write-Host "=========================" -ForegroundColor Cyan
Write-Host "1. Install OpenSSH Server"
Write-Host "2. Start SSH Server"
Write-Host "3. Stop SSH Server"
Write-Host "4. Restart SSH Server"
Write-Host "5. View SSHD Config"
Write-Host "6. Add SFTP"
Write-Host "7. Uninstall OpenSSH Server"
Write-Host "8. Exit"
}
function Install-OpenSSH {
Write-Host "Installing OpenSSH Server..."
if (-not (Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' | Where-Object State -eq 'Installed')) {
try {
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Write-Host "OpenSSH Server installed successfully." -ForegroundColor Green
# Set sshd service to start automatically
Set-Service -Name sshd -StartupType Automatic
# Start the sshd service
Start-Service -Name sshd
Write-Host "sshd service set to start automatically and started successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to install OpenSSH Server: $_" -ForegroundColor Red
}
} else {
Write-Host "OpenSSH Server is already installed." -ForegroundColor Yellow
# Ensure the service is set to automatic and started, even if already installed
Set-Service -Name sshd -StartupType Automatic -ErrorAction SilentlyContinue
Start-Service -Name sshd -ErrorAction SilentlyContinue
Write-Host "Ensured sshd service is set to automatic and running." -ForegroundColor Green
}
}
function Uninstall-OpenSSH {
Write-Host "Starting OpenSSH Server uninstallation process..."
# Check if OpenSSH Server is installed
$openssh = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' | Where-Object State -eq 'Installed'
if ($openssh) {
# Stop the sshd service if it is running
$service = Get-Service -Name sshd -ErrorAction SilentlyContinue
if ($service -and $service.Status -eq 'Running') {
try {
Write-Host "Stopping sshd service..."
Stop-Service -Name sshd -Force
Write-Host "sshd service stopped successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to stop sshd service: $_" -ForegroundColor Red
}
} else {
Write-Host "sshd service is not running or not found." -ForegroundColor Yellow
}
# Uninstall OpenSSH Server
try {
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Write-Host "OpenSSH Server uninstalled successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to uninstall OpenSSH Server: $_" -ForegroundColor Red
}
# Remove the SSH configuration folder
$sshFolder = "C:\ProgramData\ssh"
if (Test-Path $sshFolder) {
try {
Remove-Item -Path $sshFolder -Recurse -Force
Write-Host "Deleted SSH configuration folder: $sshFolder" -ForegroundColor Green
} catch {
Write-Host "Failed to delete SSH configuration folder: $_" -ForegroundColor Red
}
} else {
Write-Host "SSH configuration folder not found: $sshFolder" -ForegroundColor Yellow
}
} else {
Write-Host "OpenSSH Server is not installed." -ForegroundColor Yellow
}
}
function Start-SSHServer {
Write-Host "Starting SSH Server..."
try {
Start-Service sshd
Write-Host "SSH Server started successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to start SSH Server: $_" -ForegroundColor Red
}
}
function Stop-SSHServer {
Write-Host "Stopping SSH Server..."
try {
Stop-Service sshd
Write-Host "SSH Server stopped successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to stop SSH Server: $_" -ForegroundColor Red
}
}
function Restart-SSHServer {
Write-Host "Restarting SSH Server..."
try {
Restart-Service sshd
Write-Host "SSH Server restarted successfully." -ForegroundColor Green
} catch {
Write-Host "Failed to restart SSH Server: $_" -ForegroundColor Red
}
}
function View-SSHDConfig {
Write-Host "Opening SSHD config in Notepad (as Administrator)..."
$sshdConfigPath = "$env:ProgramData\ssh\sshd_config"
if (Test-Path $sshdConfigPath) {
try {
Start-Process notepad.exe $sshdConfigPath -Verb RunAs
Write-Host "Notepad opened as administrator." -ForegroundColor Green
} catch {
Write-Host "Failed to open Notepad as administrator: $_" -ForegroundColor Red
}
} else {
Write-Host "SSHD config file not found." -ForegroundColor Red
}
}
function Add-SFTP {
Write-Host "Adding SFTP functionality..."
# Prompt to select between user or group
$selectionType = Read-Host "Select (1) User or (2) Group"
if ($selectionType -eq "1") {
$selectedUser = Read-Host "Enter the username for SFTP"
$selectedPath = Read-Host "Enter the directory to set as the SFTP root"
$sshdConfigPath = "$env:ProgramData\ssh\sshd_config"
if (-not (Test-Path $sshdConfigPath)) {
Write-Host "SSHD config file not found." -ForegroundColor Red
return
}
# Read the contents of the SSHD config
$sshdConfig = Get-Content -Path $sshdConfigPath
# Find the index of the line that contains "Match All"
$matchIndex = ($sshdConfig | Select-String "Match All").LineNumber - 1
if ($matchIndex -ge 0) {
# Insert the new SFTP configuration entry before the "Match All" line
$entry = "Match User $selectedUserrn PasswordAuthentication yesrn ChrootDirectory $selectedPathrn ForceCommand sftp-server.exe -d priv"
$sshdConfig = $sshdConfig[0..($matchIndex-1)] + $entry + $sshdConfig[$matchIndex..($sshdConfig.Length - 1)]
} else {
Write-Host "'Match All' expression not found. Appending to the end of the file." -ForegroundColor Yellow
# If "Match All" is not found, add the entry at the end
$entry = "Match User $selectedUserrn PasswordAuthentication yesrn ChrootDirectory $selectedPathrn ForceCommand sftp-server.exe -d priv"
$sshdConfig += $entry
}
# Write the updated configuration back to the SSHD config file
Set-Content -Path $sshdConfigPath -Value $sshdConfig
try {
# Adjust the file permissions for the selected path
$acl = Get-Acl -Path $selectedPath
$newOwner = New-Object System.Security.Principal.NTAccount($selectedUser)
$acl.SetOwner($newOwner)
Set-Acl -Path $selectedPath -AclObject $acl
# Restart SSHD service
Restart-Service sshd
Write-Host "SFTP added successfully for user. SSHD restarted." -ForegroundColor Green
} catch {
Write-Host "Failed to configure SFTP: $_" -ForegroundColor Red
}
}
elseif ($selectionType -eq "2") {
$selectedGroup = Read-Host "Enter the group name for SFTP"
$selectedPath = Read-Host "Enter the directory to set as the SFTP root"
$sshdConfigPath = "$env:ProgramData\ssh\sshd_config"
if (-not (Test-Path $sshdConfigPath)) {
Write-Host "SSHD config file not found." -ForegroundColor Red
return
}
# Read the contents of the SSHD config
$sshdConfig = Get-Content -Path $sshdConfigPath
# Find the index of the line that contains "Match All"
$matchIndex = ($sshdConfig | Select-String "Match All").LineNumber - 1
if ($matchIndex -ge 0) {
# Insert the new SFTP configuration entry before the "Match All" line
$entry = "Match Group $selectedGrouprn PasswordAuthentication yesrn ChrootDirectory $selectedPathrn ForceCommand sftp-server.exe -d priv"
$sshdConfig = $sshdConfig[0..($matchIndex-1)] + $entry + $sshdConfig[$matchIndex..($sshdConfig.Length - 1)]
} else {
Write-Host "'Match All' expression not found. Appending to the end of the file." -ForegroundColor Yellow
# If "Match All" is not found, add the entry at the end
$entry = "Match Group $selectedGrouprn PasswordAuthentication yesrn ChrootDirectory $selectedPathrn ForceCommand sftp-server.exe -d priv"
$sshdConfig += $entry
}
# Write the updated configuration back to the SSHD config file
Set-Content -Path $sshdConfigPath -Value $sshdConfig
try {
# Adjust the file permissions for the selected path
$acl = Get-Acl -Path $selectedPath
$newOwner = New-Object System.Security.Principal.NTAccount($selectedGroup)
$acl.SetOwner($newOwner)
Set-Acl -Path $selectedPath -AclObject $acl
# Restart SSHD service
Restart-Service sshd
Write-Host "SFTP added successfully for group. SSHD restarted." -ForegroundColor Green
} catch {
Write-Host "Failed to configure SFTP: $_" -ForegroundColor Red
}
}
else {
Write-Host "Invalid option selected. Please choose either User (1) or Group (2)." -ForegroundColor Red
}
}
# Main Loop
do {
Show-Menu
$choice = Read-Host "Select an option (1-8)"
switch ($choice) {
1 { Install-OpenSSH }
2 { Start-SSHServer }
3 { Stop-SSHServer }
4 { Restart-SSHServer }
5 { View-SSHDConfig }
6 { Add-SFTP }
7 { Uninstall-OpenSSH }
8 { Write-Host "Exiting..." -ForegroundColor Yellow }
default { Write-Host "Invalid choice. Please select a valid option." -ForegroundColor Red }
}
} while ($choice -ne 7)