-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-RdpServerSecurity.ps1
388 lines (306 loc) · 12.6 KB
/
Get-RdpServerSecurity.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
##############################################################################
#.SYNOPSIS
# Get the security-related RDP server settings.
#
#.DESCRIPTION
# Get the security-related Remote Desktop Protocol server settings, such as
# minimum encryption level, encryption type, and whether NLA is required.
# Requires access to WMI service and membership in the local Administrators
# group at the target.
#
#.PARAMETER ComputerName
# One or more hostnames, FQDNs, or IP addresses. Defaults to localhost.
#
#.NOTES
# To interpret the output properties, search the Internet for the term
# "Win32_TSGeneralSetting" and the terms "rdp security layer tls" and also
# for "rdp network level authentication nla".
#
# Some of the possible property values are:
#
# EncryptionLevel: Low, ClientCompatible, High, FIPS
#
# SecurityLayer: NativeRDP, Negotiate, TLS, NEWTBD
#
# RequireNLA: True, False
#
# Legal: 0BSD.
#
# Author: Enclave Consulting LLC (https://www.sans.org/sec505)
#
# Version: 1.1
#
##############################################################################
[CmdletBinding()]
Param ( [String[]] $ComputerName = @("localhost") )
ForEach ($Computer in $ComputerName)
{
# Create a hashtable for the return:
$out = [Ordered] @{ ComputerName = $Computer.ToUpper();
DnsName = $null; IP = $null; EncryptionLevel = $null; SecurityLayer = $null;
Protocol =$null; RequireNLA = $null; CertificateStatus =$null; CertificateHash = $null
}
# Try to resolve name and extract IP:
Try { $fqdn = [System.Net.Dns]::GetHostByName($Computer) }
Catch { $IP = "CouldNotResolveName" }
if ($IP -eq "CouldNotResolveName")
{
$out.DnsName = "CouldNotResolveName"
$out.IP = "CouldNotResolveName"
}
else
{
$out.DnsName = $fqdn.HostName.ToLower()
$out.IP = ($fqdn.AddressList)[0]
}
# To connect to the \root\CIMV2\TerminalServices namespace, the
# authentication level must be PacketPrivacy:
Try
{
$ts = Get-WmiObject -Query "Select * From Win32_TSGeneralSetting" -Namespace root\CIMv2\TerminalServices -ComputerName $Computer -Authentication PacketPrivacy
}
Catch
{
Write-Verbose "ERROR: Failed to query $Computer"
Continue #to next $Computer
}
switch ($ts.MinEncryptionLevel)
{
1 { $out.EncryptionLevel = "Low" } #56-bit for client-to-server, plaintext for server-to-client
2 { $out.EncryptionLevel = "ClientCompatible" } #Largest key supported by client
3 { $out.EncryptionLevel = "High" } #At least a 128-bit key
4 { $out.EncryptionLevel = "FIPS" } #Federal Information Processing Standard
}
switch ($ts.SecurityLayer)
{
0 { $out.SecurityLayer = "NativeRDP" } #Default
1 { $out.SecurityLayer = "Negotiate" } #TLS preferred, NativeRDP acceptable
2 { $out.SecurityLayer = "TLS" } #SSL (TLS 1.0) only, no NativeRDP
3 { $out.SecurityLayer = "NEWTBD" } #Research this Jason...
}
switch ($ts.SSLCertificateSHA1HashType)
{
0 { $out.CertificateStatus = "NotValid" }
1 { $out.CertificateStatus = "DefaultSelfSigned" }
2 { $out.CertificateStatus = "GroupPolicyDefined" }
3 { $out.CertificateStatus = "Custom" } #Configured explicitly or directly, not auto.
}
switch ($ts.UserAuthenticationRequired)
{
1 { $out.RequireNLA = $True }
0 { $out.RequireNLA = $False }
}
$out.Protocol = $ts.TerminalProtocol
$out.CertificateHash = $ts.SSLCertificateSHA1Hash
#Emit and move on to next machine:
$out
}#END.FOREACH
# RDP Checks
Write-host ""
Write-host "##########################"
Write-host "# Now checking RDP stuff #"
Write-host "##########################"
Write-host "References: https://admx.help/?Category=Windows_10_2016&Policy=Microsoft.Policies.TerminalServer::TS_SECURITY_LAYER_POLICY" -ForegroundColor DarkGray
Write-host "References: https://viperone.gitbook.io/pentest-everything/everything/everything-active-directory/adversary-in-the-middle/rdp-mitm" -ForegroundColor DarkGray
Write-host "References: https://www.tenable.com/plugins/nessus/18405" -ForegroundColor DarkGray
Write-host ""
# Check if RDP is enabled
$rdpEnabled = Get-CimInstance -Namespace "root/CIMv2/TerminalServices" -ClassName "Win32_TerminalServiceSetting" | Select-Object -ExpandProperty AllowTSConnections
if ($rdpEnabled -eq 1) {
Write-Host "Remote Desktop is enabled." -ForegroundColor Magenta
$rdp_enabled = 1
} else {
Write-Host "Remote Desktop is disabled." -ForegroundColor Green
$rdp_enabled = 0
}
# Check Security Settings for RDP
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
$securityLayer = (Get-ItemProperty -Path $regPath -Name "SecurityLayer").SecurityLayer
switch ($securityLayer) {
0 {
Write-Host "RDP Security Layer: Disabled" -ForegroundColor Red
$rdp_sec = 2
break
}
1 {
Write-Host "RDP Security Layer: Negotiate" -ForegroundColor Magenta
$rdp_sec = 1
break
}
2 {
Write-Host "RDP Security Layer: SSL" -ForegroundColor Green
$rdp_sec = 0
break
}
default {
Write-Host "RDP Security Layer: Unknown" -ForegroundColor Yellow
$rdp_sec = 3
break
}
}
####################
<#
#prüft, ob der Remotedesktop-Dienst auf automatisch steht und läuft. Falls nicht, wird dies korrigiert:
# Dienst-Name für Remotedesktop
$serviceName = "TermService"
# Prüfe den aktuellen Status des Dienstes
$service = Get-Service -Name $serviceName
# Prüfe, ob der Dienst auf automatisch steht
if ($service.StartType -ne "Automatic") {
Set-Service -Name $serviceName -StartupType Automatic
Write-Host "Remotedesktop-Dienst wurde auf automatisch gesetzt."
}
# Prüfe, ob der Dienst läuft
if ($service.Status -ne "Running") {
Start-Service -Name $serviceName
Write-Host "Remotedesktop-Dienst wurde gestartet."
}
# Abschließende Statusmeldung
$updatedService = Get-Service -Name $serviceName
Write-Host "Remotedesktop-Dienst Status: $($updatedService.Status), Starttyp: $($updatedService.StartType)"
#>
<#
# Using MS PowerShell to Set RDP Access Settings on Remote Computer
# Sets a registry setting on a remote computer to allow RDP access
# Enter the server name when prompted
# Requires WinRM to access the remote machine/s and rights on that machine
$Server=Read-Host "Enter ServerName"
invoke-command $Server -Scriptblock {
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 0
}
#>
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host " NLA------------------------------------------------------------------------------------------"
# Definiere die beiden möglichen Registry-Pfade
$path1 = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
$path2 = "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"
# Funktion zum Überprüfen des NLA-Status
function Check-NLA($path) {
if (Test-Path $path) {
$value = (Get-ItemProperty $path).UserAuthentication
return @{
Value = $value
Path = $path
}
}
return $null
}
# Überprüfe beide Pfade
$nla1 = Check-NLA $path1
$nla2 = Check-NLA $path2
# Bestimme die effektive NLA-Einstellung
if ($null -ne $nla2) {
$nla = $nla2
$effectivePath = $path2
} else {
$nla = $nla1
$effectivePath = $path1
}
# Ausgabe der Ergebnisse
if ($null -eq $nla -or $nla.Value -ne 1) {
Write-Host "[*] UserAuthentication (NLA)`t: NLA ist nicht erforderlich. Empfohlene Einstellung ist '1'" -ForegroundColor Red
} else {
Write-Host "[*] UserAuthentication (NLA) `t: NLA ist erforderlich" -ForegroundColor Green
}
Write-Host "`nWerte in den Registry-Pfaden:"
if ($null -ne $nla1) {
Write-Host "- $($path1): $($nla1.Value)"
} else {
Write-Host "- $($path1): Nicht gesetzt"
}
if ($null -ne $nla2) {
Write-Host "- $($path2): $($nla2.Value)"
} else {
Write-Host "- $($path2): Nicht gesetzt"
}
Write-Host "`nEffektiver Wert für UserAuthentication (NLA) wird verwendet von: $effectivePath"
Write-Host "------------------------------------------------------------------------------------------"
Write-Host ""
Write-Host ""
Write-Host " MinEncryptionLevel------------------------------------------------------------------------------------------"
# Definiere die beiden möglichen Registry-Pfade
$path1 = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
$path2 = "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"
# Funktion zum Überprüfen der Verschlüsselungsebene
function Check-EncryptionLevel($path) {
if (Test-Path $path) {
return (Get-ItemProperty $path).MinEncryptionLevel
}
return $null
}
# Überprüfe beide Pfade
$encryption1 = Check-EncryptionLevel $path1
$encryption2 = Check-EncryptionLevel $path2
# Bestimme die effektive Verschlüsselungsebene
$encryption = if ($null -ne $encryption2) { $encryption2 } else { $encryption1 }
$effectivePath = if ($null -ne $encryption2) { $path2 } else { $path1 }
# Ausgabe der Ergebnisse
if ($null -eq $encryption -or $encryption -le 2) {
Write-Host "[*] MinEncryptionLevel`t`t: Nicht auf 'High' oder 'FIPS-Compliant' gesetzt. Empfohlene Einstellung ist '3' oder '4'" -ForegroundColor Red
} elseif ($encryption -eq 3) {
Write-Host "[*] MinEncryptionLevel`t`t: Sie verwenden 'High' (3), dies ist akzeptabel" -ForegroundColor Green
} elseif ($encryption -eq 4) {
Write-Host "[*] MinEncryptionLevel`t`t: Sie verwenden 'FIPS-Compliant' (4), dies wird empfohlen" -ForegroundColor Green
}
Write-Host "`nWerte in den Registry-Pfaden:"
Write-Host "- $($path1): $(if ($null -ne $encryption1) { $encryption1 } else { 'Nicht gesetzt' })"
Write-Host "- $($path2): $(if ($null -ne $encryption2) { $encryption2 } else { 'Nicht gesetzt' })"
Write-Host "`nEffektiver Wert für MinEncryptionLevel wird verwendet von: $effectivePath"
Write-Host "------------------------------------------------------------------------------------------"
Write-Host ""
Write-Host ""
Write-Host " SecurityLayer------------------------------------------------------------------------------------------"
# Definiere die beiden möglichen Registry-Pfade
$path1 = "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
$path2 = "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"
# Funktion zum Überprüfen der SecurityLayer
function Check-SecurityLayer($path) {
if (Test-Path $path) {
return (Get-ItemProperty $path).SecurityLayer
}
return $null
}
# Überprüfe beide Pfade
$tlsImplementation1 = Check-SecurityLayer $path1
$tlsImplementation2 = Check-SecurityLayer $path2
# Bestimme die effektive SecurityLayer-Einstellung
$tlsImplementation = if ($null -ne $tlsImplementation2) { $tlsImplementation2 } else { $tlsImplementation1 }
$effectivePath = if ($null -ne $tlsImplementation2) { $path2 } else { $path1 }
# Ausgabe der Ergebnisse
if ($null -eq $tlsImplementation -or $tlsImplementation -ne 2) {
Write-Host "[*] SecurityLayer`t`t: Wurde nicht auf 'High' gesetzt. Empfohlene Einstellung ist '2'" -ForegroundColor Red
} else {
Write-Host "[*] SecurityLayer`t`t: Sie verwenden die sicherste TLS-Implementierung. (2)" -ForegroundColor Green
}
Write-Host "`nWerte in den Registry-Pfaden:"
Write-Host "- $($path1): $(if ($null -ne $tlsImplementation1) { $tlsImplementation1 } else { 'Nicht gesetzt' })"
Write-Host "- $($path2): $(if ($null -ne $tlsImplementation2) { $tlsImplementation2 } else { 'Nicht gesetzt' })"
Write-Host "`nEffektiver Wert für SecurityLayer wird verwendet von: $effectivePath"
# Erklärung der Werte
Write-Host "`nErklärung der Werte:"
Write-Host "0 - 'Low' Sicherheit"
Write-Host "1 - 'Medium' Sicherheit (Standard)"
Write-Host "2 - 'High' Sicherheit (empfohlen)"
Write-Host ""
Write-Host "------------------------------------------------------------------------------------------"
Write-Host ""
# Check if the PortNumber is not 3389
$port = (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\Termin*Server\WinStations\RDP*CP\).PortNumber
if ( $port -eq 3389 )
{
Write-Output "[*] PortNumber `t`t: Malware will look for 3389 by default. Consider altering this."
}
else
{
Write-Output "[*] PortNumber `t`t: Congratulations you have modified the default port."
}
Write-Host ""
Write-Host ""
Write-Host "------------------------------------------------------------------------------------------"
Write-Host ""
Write-Host "https://www.mvps.net/docs/how-to-secure-remote-desktop-rdp/"
Write-Host "https://raw.githubusercontent.com/cornerpirate/rdp-enum/refs/heads/main/rdp-enum.ps1"
Write-Host ""
Write-Host ""