-
Notifications
You must be signed in to change notification settings - Fork 2
/
Security_Harden_Host_or_Cluster.ps1
562 lines (507 loc) · 29 KB
/
Security_Harden_Host_or_Cluster.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<#
.NOTES
===========================================================================
Created by: Russell Hamker
Date: January 1, 2019
Version: 1.0
Twitter: @butch7903
GitHub: https://github.com/butch7903
===========================================================================
.SYNOPSIS
This script will harden a host or cluster based on the VMware Hardening
Guide.
.DESCRIPTION
Use this script to harden a host.
.NOTES
This script requires a VMware PowerCLI minimum version 11.4 or greater.
You will also want to update whatever NTP Servers you use in your environment
in this script before running it.
.TROUBLESHOOTING
#>
##Check if Modules are installed, if so load them, else install them
if (Get-InstalledModule -Name VMware.PowerCLI -MinimumVersion 11.4) {
Write-Host "-----------------------------------------------------------------------------------------------------------------------"
Write-Host "PowerShell Module VMware PowerCLI required minimum version was found previously installed"
Write-Host "Importing PowerShell Module VMware PowerCLI"
Import-Module -Name VMware.PowerCLI
Write-Host "Importing PowerShell Module VMware PowerCLI Completed"
Write-Host "-----------------------------------------------------------------------------------------------------------------------"
#CLEAR
} else {
Write-Host "-----------------------------------------------------------------------------------------------------------------------"
Write-Host "PowerShell Module VMware PowerCLI does not exist"
Write-Host "Setting Micrsoft PowerShell Gallery as a Trusted Repository"
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Write-Host "Verifying that NuGet is at minimum version 2.8.5.201 to proceed with update"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Confirm:$false
Write-Host "Uninstalling any older versions of the VMware PowerCLI Module"
Get-Module VMware.PowerCLI | Uninstall-Module -Force
Write-Host "Installing Newest version of VMware PowerCLI PowerShell Module"
Install-Module -Name VMware.PowerCLI -Scope AllUsers
Write-Host "Creating a Desktop shortcut to the VMware PowerCLI Module"
$AppLocation = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$Arguments = '-noe -c "Import-Module VMware.PowerCLI"'
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\VMware PowerCLI.lnk")
$Shortcut.TargetPath = $AppLocation
$Shortcut.Arguments = $Arguments
$ShortCut.Hotkey = "CTRL+SHIFT+V"
$Shortcut.IconLocation = "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe,1"
$Shortcut.Description ="Launch VMware PowerCLI"
$Shortcut.WorkingDirectory ="C:\"
$Shortcut.Save()
Write-Host "Shortcut Created"
Write-Host "You may use the CTRL+SHIFT+V method to open VMware PowerCLI"
Write-Host "Importing PowerShell Module VMware PowerCLI"
Import-Module -Name VMware.PowerCLI
Write-Host "PowerShell Module VMware PowerCLI Loaded"
Write-Host "-----------------------------------------------------------------------------------------------------------------------"
#Clear
}
##Specify Variables
$vCenter = Read-Host "Please Provide FQDN of your VCSA:"
$HOSTLIST = Read-Host "Please Enter VMHost FQDN Name:"
IF(!$CREDS)
{
$CREDS = Get-Credential -Message "Provide vCenter Administrative User Account Login Password"
}
#$CLUSTERNAME = "Production"
$DOMAIN = $HOSTLIST.Substring($HOSTLIST.IndexOf(".") + 1)
$JOINDOMAIN = $true #Toggles whether a VMHost is joined to a domain or not. Best practices state to enable this.
$ENABLELOCKDOWN = $false #Configures VMHosts for LockDown Mode. Best practices state to enable this.
$SNMP = $false #Configures SNMP for VMHosts
$SNMPSTRING = "secret"
$NTPCLIENT = $false #If set to True, ESXi host becomes a NTP Server for clients on the network
$NTPCLIENTIPs = "" #This would be IPs that you want to allow a host to be able to connect with so that the client IPs can
$NTPServers = "time-a-g.nist.gov", "time-b-wwv.nist.gov", "time-c-b.nist.gov", "utcnist.colorado.edu", "10.1.1.64", "10.2.1.64" #list of NTP Servers for VMHosts to use
##Disconnect any previous vCenter sessions
disconnect-viserver * -confirm:$false -ErrorAction SilentlyContinue
##Connect to vCenter
connect-viserver $VCENTER -credential $CREDS
Start-Sleep -s 10
##Get Cluster Info
$CLUSTER = Get-VMHost -Location $CLUSTERNAME | Sort Name
##Get Hosts
$SWITCHLIST = Get-VDSwitch | Sort Name
IF($CLUSTERNAME)
{
foreach ($VMHOST in $CLUSTER)
{
Write-Host "Starting Hardening for"$VMHOST.NAME -ForegroundColor Green
#Get VDS Info on Host
Write-Host "Getting Switch Info from VMHost"
$VMHOSTVDSLIST = Get-VMHost $VMHOST | Get-VDSwitch | Sort Name #Virtual Distributed Switch List
$VMHOSTSWLIST = Get-VMHOST $VMHOST | Get-VirtualSwitch | Sort Name #Standard Switch List
#Set Shell Settings
Write-Host "Setting Shell Settings"
Get-VMHost $VMHOST | Get-AdvancedSetting -Name 'UserVars.SuppressShellWarning' | Set-AdvancedSetting -Value "1" -Confirm:$false
Get-VMHost $VMHOST | Get-AdvancedSetting -Name 'UserVars.ESXiShellInteractiveTimeOut' | Set-AdvancedSetting -Value "600" -Confirm:$false
Get-VMHost $VMHOST | Get-AdvancedSetting -Name 'UserVars.ESXiShellTimeOut' | Set-AdvancedSetting -Value "600" -Confirm:$false
#Enable LDAP Authentication
IF ($JOINDOMAIN -eq $true)
{
Write-Host "Setting LDAP Authentication"
Get-VMHost $VMHOST | Get-VMHostAuthentication | Set-VMHostAuthentication -JoinDomain -Domain $DOMAIN -Credential $CREDS -Confirm:$false
} ELSE{
Get-VMHost $VMHOST | Get-VMHostAuthentication | Set-VMHostAuthentication -LeaveDomain -Force -Confirm:$false
}
#Set Standard Switch Security Settings
IF ($VMHOSTSWLIST)
{
Write-Host "Setting Standard Switch Security Settings"
foreach($SWITCH in $VMHOSTSWLIST)
{
#Check Security Policy Settings
$SWPOLICY = $null
Write-Host "Getting Virtual Switch Configuration on"$VMHOST
$SWPOLICY = Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy
Write-Host "Listing Existing Policy Results"
$SWPOLICY
IF($SWPOLICY.AllowPromiscuous -eq $true)
{
Write-Host "Updating Virtual Switch"$SWITCH.NAME"Configuration to set the Security Policy to Not Allow Promiscuous Mode"
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $false #Specify on which vSwitch (Standard) this script will make changes
} ELSE{
Write-Host "Virtual Switch"$SWITCH.NAME" Configuration is already correctly set to the Security Policy to Not Allow Promiscuous Mode" -foregroundcolor Blue
}
IF ($SWPOLICY.ForgedTransmits -eq $true)
{
Write-Host "Updating Virtual Switch"$SWITCH.NAME"Configuration to set the Security Policy to Not Allow Forged Transmits"
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy | Set-SecurityPolicy -ForgedTransmits $false #Specify on which vSwitch (Standard) this script will make changes
} ELSE{
Write-Host "Virtual Switch"$SWITCH.NAME" Configuration is already correctly set to the Security Policy to Not Allow Forged Transmits" -foregroundcolor Blue
}
IF ($SWPOLICY.MacChanges -eq $true)
{
Write-Host "Updating Virtual Switch"$SWITCH.NAME"Configuration to set the Security Policy to Not Allow MAC Changes"
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy | Set-SecurityPolicy -MacChanges $false #Specify on which vSwitch (Standard) this script will make changes
} ELSE{
Write-Host "Virtual Switch"$SWITCH.NAME" Configuration is already correctly set to the Security Policy to Not Allow MAC Changes" -foregroundcolor Blue
}
#List Results
Write-Host "Listing Switch Policy Results for"$SWITCH.NAME
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy
}
}
#Set Virtual Distributed Switch Security Settings
IF ($VMHOSTVDSLIST)
{
Write-Host "Setting Virtual Distributed Switch Security Settings"
foreach($VDS in $VMHOSTVDSLIST)
{
#Check Security Policy Settings
$VDSPOLICY = $null
Write-Host "Getting Virtual Switch Configuration on"$VMHOST
$VDSPOLICY = Get-VMHost $VMHOST | Get-VDSwitch $VDS | Get-VDSecurityPolicy
Write-Host "Listing Existing Policy Results"
$VDSPOLICY
IF($VDSPOLICY.AllowPromiscuous -eq $true)
{
Write-Host "Updating Virtual Switch"$VDS.NAME"Configuration to set to the Security Policy to Not Allow Promiscuous Mode"
Get-VMHost $VMHOST | Get-VDSwitch -Name $VDS | Get-VDSecurityPolicy | Set-VDSecurityPolicy -AllowPromiscuous $false #Specify on which VDSwitch this script will make changes
} ELSE{
Write-Host "Virtual Switch"$VDS.NAME" Configuration is already correctly set to the Security Policy to Not Allow Promiscuous Mode" -foregroundcolor Blue
}
IF($VDSPOLICY.ForgedTransmits -eq $true)
{
Write-Host "Updating Virtual Switch"$VDS.NAME"Configuration to set to the Security Policy to Not Allow Forged Transmits"
Get-VMHost $VMHOST | Get-VDSwitch -Name $VDS | Get-VDSecurityPolicy | Set-VDSecurityPolicy -ForgedTransmits $false #Specify on which VDSwitch this script will make changes
} ELSE{
Write-Host "Virtual Switch"$VDS.NAME" Configuration is already correctly set to the Security Policy to Not Allow Forged Transmits" -foregroundcolor Blue
}
IF($VDSPOLICY.MacChanges -eq $true)
{
Write-Host "Updating Virtual Switch"$VDS.NAME"Configuration to set to the Security Policy to Not Allow MAC Changes"
Get-VMHost $VMHOST | Get-VDSwitch -Name $VDS | Get-VDSecurityPolicy | Set-VDSecurityPolicy -MacChanges $false #Specify on which VDSwitch this script will make changes
} ELSE{
Write-Host "Virtual Switch"$VDS.NAME" Configuration is already correctly set to the Security Policy to Not Allow MAC Changes" -foregroundcolor Blue
}
#List Results
Write-Host "Listing Switch Policy Results for"$VDS.NAME
Get-VMHost $VMHOST | Get-VDSwitch $VDS | Get-VDSecurityPolicy
}
}
#Set NTP Daemon Policy to Enabled
Write-Host "Setting NTPd Policy to On"
Get-VMHost $VMHOST | Get-VMHostService | where {$_.Key -eq "ntpd"} | Set-VMHostService -Policy On
#Set BDPU Filter
#Reference: https://kb.vmware.com/s/article/2047822
Write-Host "Setting the BPDU filter to Enabled"
Get-VMHOST $VMHOST | Get-AdvancedSetting -Name 'Net.BlockGuestBPDU' | Set-AdvancedSetting -Value "1" -Confirm:$false
#Set DCUI Settings
Write-Host "Setting DCUI Service Policy to Off"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "DCUI"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "DCUI"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
#Set TSM/ESXi Shell Settings
Write-Host "Setting TSM(ESXi Shell) Service Policy to Off"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
#Set TSM SSH Settings
Write-Host "Setting TSM-SSH Service Policy to Off"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM-SSH"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM-SSH"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
#Set SNMP Settings
IF ($SNMP -eq $false)
{
Write-Host "Setting SNMP to Disabled"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
}
IF ($SNMP -eq $true)
{
Write-Host "Setting SNMP to Enabled"
Set-VMHostSNMP $VMHOST -Enabled:$true -ReadOnlyCommunity $SNMPSTRING
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Set-VMHostService -policy "on" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Start-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
}
#Remove any NTP Time Servers that were previously set
$NTPSERVERLIST = Get-VMHost $VMHOST | Get-VMHostNtpServer
foreach($NTPSRVR in $NTPSERVERLIST)
{
Get-VMHost $VMHOST | Remove-VMHostNtpServer $NTPSRVR -confirm:$false
}
#Add NTP Time Servers from list
Get-VMHost $VMHOST | Add-VmHostNtpServer $NTPServers -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false
Get-VMHost $VMHOST | Get-VmHostNtpServer
#Set NTP Client Settings
IF ($NTPCLIENT -eq $false)
{
Write-Host "Setting NTP Client to only 127.0.0.1"
#reference https://www.altaro.com/vmware/managing-esxi-firewall/
$esxcli = (Get-EsxCli -VMHost $VMHOST -V2).network.firewall
#List Prior to changes
Write-Host "Checking NTP Client Allowed IP Settings"
$esxcli.ruleset.list.invoke() | where {$_.Name -eq "ntpClient"}
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#Setting Changes to NTP Client
#Note This prevents other servers from getting NTP time from a VMHost
Write-Host "Disabling NTP Client to allow All IPs"
$esxcli.ruleset.set.invoke(@{enabled="true"; allowedall="false"; rulesetid="ntpClient"})
Write-Host "Adding NTP Client IP 127.0.0.1"
$esxcli.ruleset.allowedip.add.Invoke(@{rulesetid="ntpClient"; ipaddress="127.0.0.1"})
#Write-Host "Removing NTP Client IP 127.0.0.1"
#$esxcli.ruleset.allowedip.remove.Invoke(@{rulesetid="ntpClient"; ipaddress="127.0.0.1"})
#Verifying IP Changes
Write-Host "Verifiying NTP Client IP Settings"
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#$esxcli.ruleset.set($false, $true, "ntpd")
#Setting Policies for NTP
Write-Host "Setting NTP Policy Settings"
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled $true -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} #Verify
}
IF ($NTPCLIENT -eq $true)
{
foreach ($IP in $NTPCLIENTIPs)
{
Write-Host "Setting NTP Client to only 127.0.0.1"
#reference https://www.altaro.com/vmware/managing-esxi-firewall/
$esxcli = (Get-EsxCli -VMHost $VMHOST -V2).network.firewall
#List Prior to changes
Write-Host "Checking NTP Client Allowed IP Settings"
$esxcli.ruleset.list.invoke() | where {$_.Name -eq "ntpClient"}
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#Setting Changes to NTP Client
#Note This prevents other servers from getting NTP time from a VMHost
Write-Host "Disabling NTP Client to allow All IPs"
$esxcli.ruleset.set.invoke(@{enabled="true"; allowedall="false"; rulesetid="ntpClient"})
Write-Host "Adding NTP Client IP 127.0.0.1"
$esxcli.ruleset.allowedip.add.Invoke(@{rulesetid="ntpClient"; ipaddress=$IP})
#Verifying IP Changes
Write-Host "Verifiying NTP Client IP Settings"
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#$esxcli.ruleset.set($false, $true, "ntpd")
}
#Setting Policies for NTP
Write-Host "Setting NTP Policy Settings"
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled $true -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} #Verify
}
#Set Lock Down Mode
IF($ENABLELOCKDOWN -eq $true)
{
Write-Host "Setting Host Lock Down Mode to Enabled"
(Get-VMHost $VMHOST | Get-View).EnterLockdownMode() #Replace with .ExitLockdownMode() if you wish this script to disable lockdown mode
}
Write-Host "Completed Hardening for"$VMHOST.name -ForegroundColor Green
}
}
IF($HOSTLIST)
{
foreach ($VMHOST in $HOSTLIST)
{
Write-Host "Starting Hardening for"$VMHOST -ForegroundColor Green
#Get VDS Info on Host
Write-Host "Getting Switch Info from VMHost"
$VMHOSTVDSLIST = Get-VMHost $VMHOST | Get-VDSwitch | Sort Name #Virtual Distributed Switch List
$VMHOSTSWLIST = Get-VMHOST $VMHOST | Get-VirtualSwitch | Sort Name #Standard Switch List
#Set Shell Settings
Write-Host "Setting Shell Settings"
Get-VMHost $VMHOST | Get-AdvancedSetting -Name 'UserVars.SuppressShellWarning' | Set-AdvancedSetting -Value "1" -Confirm:$false
Get-VMHost $VMHOST | Get-AdvancedSetting -Name 'UserVars.ESXiShellInteractiveTimeOut' | Set-AdvancedSetting -Value "600" -Confirm:$false
Get-VMHost $VMHOST | Get-AdvancedSetting -Name 'UserVars.ESXiShellTimeOut' | Set-AdvancedSetting -Value "600" -Confirm:$false
#Enable LDAP Authentication
IF ($JOINDOMAIN -eq $true)
{
Write-Host "Setting LDAP Authentication"
Get-VMHost $VMHOST | Get-VMHostAuthentication | Set-VMHostAuthentication -JoinDomain -Domain $DOMAIN -Credential $CREDS -Confirm:$false
} ELSE{
Get-VMHost $VMHOST | Get-VMHostAuthentication | Set-VMHostAuthentication -LeaveDomain -Force -Confirm:$false
}
#Set Standard Switch Security Settings
IF ($VMHOSTSWLIST)
{
Write-Host "Setting Standard Switch Security Settings"
foreach($SWITCH in $VMHOSTSWLIST)
{
#Check Security Policy Settings
$SWPOLICY = $null
Write-Host "Getting Virtual Switch Configuration on"$VMHOST
$SWPOLICY = Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy
Write-Host "Listing Existing Policy Results"
$SWPOLICY
IF($SWPOLICY.AllowPromiscuous -eq $true)
{
Write-Host "Updating Virtual Switch"$SWITCH.NAME"Configuration to set the Security Policy to Not Allow Promiscuous Mode"
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuous $false #Specify on which vSwitch (Standard) this script will make changes
} ELSE{
Write-Host "Virtual Switch"$SWITCH.NAME" Configuration is already correctly set to the Security Policy to Not Allow Promiscuous Mode" -foregroundcolor Blue
}
IF ($SWPOLICY.ForgedTransmits -eq $true)
{
Write-Host "Updating Virtual Switch"$SWITCH.NAME"Configuration to set the Security Policy to Not Allow Forged Transmits"
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy | Set-SecurityPolicy -ForgedTransmits $false #Specify on which vSwitch (Standard) this script will make changes
} ELSE{
Write-Host "Virtual Switch"$SWITCH.NAME" Configuration is already correctly set to the Security Policy to Not Allow Forged Transmits" -foregroundcolor Blue
}
IF ($SWPOLICY.MacChanges -eq $true)
{
Write-Host "Updating Virtual Switch"$SWITCH.NAME"Configuration to set the Security Policy to Not Allow MAC Changes"
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy | Set-SecurityPolicy -MacChanges $false #Specify on which vSwitch (Standard) this script will make changes
} ELSE{
Write-Host "Virtual Switch"$SWITCH.NAME" Configuration is already correctly set to the Security Policy to Not Allow MAC Changes" -foregroundcolor Blue
}
#List Results
Write-Host "Listing Switch Policy Results for"$SWITCH.NAME
Get-VMHost $VMHOST | Get-VirtualSwitch -Name $SWITCH | Get-SecurityPolicy
}
}
#Set Virtual Distributed Switch Security Settings
IF ($VMHOSTVDSLIST)
{
Write-Host "Setting Virtual Distributed Switch Security Settings"
foreach($VDS in $VMHOSTVDSLIST)
{
#Check Security Policy Settings
$VDSPOLICY = $null
Write-Host "Getting Virtual Switch Configuration on"$VMHOST
$VDSPOLICY = Get-VMHost $VMHOST | Get-VDSwitch $VDS | Get-VDSecurityPolicy
Write-Host "Listing Existing Policy Results"
$VDSPOLICY
IF($VDSPOLICY.AllowPromiscuous -eq $true)
{
Write-Host "Updating Virtual Switch"$VDS.NAME"Configuration to set to the Security Policy to Not Allow Promiscuous Mode"
Get-VMHost $VMHOST | Get-VDSwitch -Name $VDS | Get-VDSecurityPolicy | Set-VDSecurityPolicy -AllowPromiscuous $false #Specify on which VDSwitch this script will make changes
} ELSE{
Write-Host "Virtual Switch"$VDS.NAME" Configuration is already correctly set to the Security Policy to Not Allow Promiscuous Mode" -foregroundcolor Blue
}
IF($VDSPOLICY.ForgedTransmits -eq $true)
{
Write-Host "Updating Virtual Switch"$VDS.NAME"Configuration to set to the Security Policy to Not Allow Forged Transmits"
Get-VMHost $VMHOST | Get-VDSwitch -Name $VDS | Get-VDSecurityPolicy | Set-VDSecurityPolicy -ForgedTransmits $false #Specify on which VDSwitch this script will make changes
} ELSE{
Write-Host "Virtual Switch"$VDS.NAME" Configuration is already correctly set to the Security Policy to Not Allow Forged Transmits" -foregroundcolor Blue
}
IF($VDSPOLICY.MacChanges -eq $true)
{
Write-Host "Updating Virtual Switch"$VDS.NAME"Configuration to set to the Security Policy to Not Allow MAC Changes"
Get-VMHost $VMHOST | Get-VDSwitch -Name $VDS | Get-VDSecurityPolicy | Set-VDSecurityPolicy -MacChanges $false #Specify on which VDSwitch this script will make changes
} ELSE{
Write-Host "Virtual Switch"$VDS.NAME" Configuration is already correctly set to the Security Policy to Not Allow MAC Changes" -foregroundcolor Blue
}
#List Results
Write-Host "Listing Switch Policy Results for"$VDS.NAME
Get-VMHost $VMHOST | Get-VDSwitch $VDS | Get-VDSecurityPolicy
}
}
#Set NTP Daemon Policy to Enabled
Write-Host "Setting NTPd Policy to On"
Get-VMHost $VMHOST | Get-VMHostService | where {$_.Key -eq "ntpd"} | Set-VMHostService -Policy On
#Set BDPU Filter
#Reference: https://kb.vmware.com/s/article/2047822
Write-Host "Setting the BPDU filter to Enabled"
Get-VMHOST $VMHOST | Get-AdvancedSetting -Name 'Net.BlockGuestBPDU' | Set-AdvancedSetting -Value "1" -Confirm:$false
#Set DCUI Settings
Write-Host "Setting DCUI Service Policy to Off"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "DCUI"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "DCUI"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
#Set TSM/ESXi Shell Settings
Write-Host "Setting TSM(ESXi Shell) Service Policy to Off"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
#Set TSM SSH Settings
Write-Host "Setting TSM-SSH Service Policy to Off"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM-SSH"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "TSM-SSH"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
#Set SNMP Settings
IF ($SNMP -eq $false)
{
Write-Host "Setting SNMP to Disabled"
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Set-VMHostService -policy "off" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Stop-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
}
IF ($SNMP -eq $true)
{
Write-Host "Setting SNMP to Enabled"
Set-VMHostSNMP $VMHOST -Enabled:$true -ReadOnlyCommunity $SNMPSTRING
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Set-VMHostService -policy "on" -Confirm:$false
Get-VMHostService -VMHost $VMHOST | Where-Object {$_.Key -eq "snmpd"} | Start-VMHostService -Confirm:$false #Replace with Start-VMHostService to start service via script
}
#Remove any NTP Time Servers that were previously set
$NTPSERVERLIST = Get-VMHost $VMHOST | Get-VMHostNtpServer
foreach($NTPSRVR in $NTPSERVERLIST)
{
Get-VMHost $VMHOST | Remove-VMHostNtpServer $NTPSRVR -confirm:$false
}
#Add NTP Time Servers from list
Get-VMHost $VMHOST | Add-VmHostNtpServer $NTPServers -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false
Get-VMHost $VMHOST | Get-VmHostNtpServer
#Set NTP Client Settings
IF ($NTPCLIENT -eq $false)
{
Write-Host "Setting NTP Client to only 127.0.0.1"
#reference https://www.altaro.com/vmware/managing-esxi-firewall/
$esxcli = (Get-EsxCli -VMHost $VMHOST -V2).network.firewall
#List Prior to changes
Write-Host "Checking NTP Client Allowed IP Settings"
$esxcli.ruleset.list.invoke() | where {$_.Name -eq "ntpClient"}
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#Setting Changes to NTP Client
#Note This prevents other servers from getting NTP time from a VMHost
Write-Host "Disabling NTP Client to allow All IPs"
$esxcli.ruleset.set.invoke(@{enabled="true"; allowedall="false"; rulesetid="ntpClient"})
Write-Host "Adding NTP Client IP 127.0.0.1"
$esxcli.ruleset.allowedip.add.Invoke(@{rulesetid="ntpClient"; ipaddress="127.0.0.1"})
#Write-Host "Removing NTP Client IP 127.0.0.1"
#$esxcli.ruleset.allowedip.remove.Invoke(@{rulesetid="ntpClient"; ipaddress="127.0.0.1"})
#Verifying IP Changes
Write-Host "Verifiying NTP Client IP Settings"
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#$esxcli.ruleset.set($false, $true, "ntpd")
#Setting Policies for NTP
Write-Host "Setting NTP Policy Settings"
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled $true -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} #Verify
}
IF ($NTPCLIENT -eq $true)
{
foreach ($IP in $NTPCLIENTIPs)
{
Write-Host "Setting NTP Client to only 127.0.0.1"
#reference https://www.altaro.com/vmware/managing-esxi-firewall/
$esxcli = (Get-EsxCli -VMHost $VMHOST -V2).network.firewall
#List Prior to changes
Write-Host "Checking NTP Client Allowed IP Settings"
$esxcli.ruleset.list.invoke() | where {$_.Name -eq "ntpClient"}
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#Setting Changes to NTP Client
#Note This prevents other servers from getting NTP time from a VMHost
Write-Host "Disabling NTP Client to allow All IPs"
$esxcli.ruleset.set.invoke(@{enabled="true"; allowedall="false"; rulesetid="ntpClient"})
Write-Host "Adding NTP Client IP 127.0.0.1"
$esxcli.ruleset.allowedip.add.Invoke(@{rulesetid="ntpClient"; ipaddress=$IP})
#Verifying IP Changes
Write-Host "Verifiying NTP Client IP Settings"
$esxcli.ruleset.allowedip.list.invoke() | where {$_.Ruleset -eq "ntpClient"}
#$esxcli.ruleset.set($false, $true, "ntpd")
}
#Setting Policies for NTP
Write-Host "Setting NTP Policy Settings"
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled $true -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false
Get-VmHostService -VMHost $VMHOST | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false
Get-VMHost $VMHOST | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} #Verify
}
#Set Lock Down Mode
IF($ENABLELOCKDOWN -eq $true)
{
Write-Host "Setting Host Lock Down Mode to Enabled"
(Get-VMHost $VMHOST | Get-View).EnterLockdownMode() #Replace with .ExitLockdownMode() if you wish this script to disable lockdown mode
}
Write-Host "Completed Hardening for"$VMHOST -ForegroundColor Green
}
}