-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hardening-VM-v1.ps1
367 lines (250 loc) · 11.1 KB
/
Hardening-VM-v1.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
##################################################
# #
# #
#Script to verify and apply VMs hardening #
# #
# Autor Nicolas Ventre. #
# #
# Version 1.0 #
# #
# #
##################################################
#Connect to vcenter
$vcenter = Read-Host "vCenter name:"
$user = Read-Host "User:"
$password = Read-Host "Password:"
#Write-Host -f green "Connecting to vCenter Server..."
Connect-VIServer -Server $vcenter -User $user -Password $password
#Create folder for logs output
$today = (get-date)
$dateStr = '{0:yyyyMMdd-HHmm}' -f $today
New-Item $env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr -ItemType directory
Write-Host "Depending on the number of VMs, this take a while..."
###############################################
#Persistent Disk Verify #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Persistent Disk Verify ... #"
Write-Host -f White "###############################################"
$persistent = Get-VM | Get-HardDisk | Select Parent, Name, Filename, DiskType, Persistence | FT -AutoSize
$persistent | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\persistent-drive.txt"
if ($persistent -like "*Nonpersistent*") {
Write-Host -f red "You need to verify the VM configuration because there is Non-Persistent disks present"
Write-Host -f red "VMs must be powered off prior to changing the hard disk mode"
Write-Host -f red "File '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\persistent-drive.txt' was generated to validate information."
}
else {
Write-Host -f green "All scanned disk have Persistence"
}
###############################################
#Passthrough Verify #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Passthrough Verify ... #"
Write-Host -f White "###############################################"
$passthrough = Get-VM | Get-PassthroughDevice -Type Pci
$passthrough | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\passthrough-pci-device.txt"
if ($passthrough -ne $Null) {
Write-Host -f red "You need to verify the VM configuration because there is Passthrough device present"
Write-Host -f red "VMs must be powered off prior to changing the device"
Write-Host -f red "File '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\passthrough-pci-device.txt' was generated to validate information."
}
else {
Write-Host -f green "All scanned disk have Persistence"
}
###############################################
#Verify Floppy #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Verifying if Floppy is present... #"
Write-Host -f White "###############################################"
#Verify Floppy Drive
$floppy = Get-VM | Get-FloppyDrive | Select Parent, Name, ConnectionState
$floppy | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\floppy-drive.txt"
if ($floppy -ne $Null) {
Write-Host -f red "Floppy drive detected, you need to verify your VMs"
Write-Host -f red "VMs must be powered off prior to changing the hard disk mode"
Write-Host -f red "File '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\floppy-drive.txt' was generated to validate information."
}
else {
Write-Host -f green "No floppy drive detected"
}
###############################################
#Verify Parallel device #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Verifying if Parallel device is present... #"
Write-Host -f White "###############################################"
#Verify Parallel Device
$parallel = Get-VM | Where {$_.ExtensionData.Config.Hardware.Device.DeviceInfo.Label -match "parallel"}
$parallel | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\parallel-device.txt"
if ($parallel -ne $Null) {
Write-Host -f red "Parallel device detected, you need to verify your VMs"
Write-Host -f red "VMs must be powered off prior to changing the hard disk mode"
Write-Host -f red "File '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\parallel-device.txt' was generated to validate information."
}
else {
Write-Host -f green "No Parallel device detected"
}
###############################################
#Verify Serial device #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Verifying if Serial device is present... #"
Write-Host -f White "###############################################"
#Verify Serial Device
$serial = Get-VM | Where {$_.ExtensionData.Config.Hardware.Device.DeviceInfo.Label -match "serial"}
$serial | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\serial-device.txt"
if ($serial -ne $Null) {
Write-Host -f red "Serial device detected, you need to verify your VMs"
Write-Host -f red "VMs must be powered off prior to changing the hard disk mode"
Write-Host -f red "File '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\serial-device.txt' was generated to validate information."
}
else {
Write-Host -f green "No Serial device detected"
}
###############################################
#Verify USB device #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Verifying if USB device is present... #"
Write-Host -f White "###############################################"
#Verify USB Device
$usb = Get-VM | Where {$_.ExtensionData.Config.Hardware.Device.DeviceInfo.Label -match "usb"}
$usb | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\usb-device.txt"
if ($usb -ne $Null) {
Write-Host -f red "USB device detected, you need to verify your VMs"
Write-Host -f red "VMs must be powered off prior to changing the hard disk mode"
Write-Host -f red "File '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\usb-device.txt' was generated to validate information."
}
else {
Write-Host -f green "No Serial device detected"
}
###############################################
#Verify Share Force Salting #
###############################################
Write-Host -f White "###############################################"
Write-Host -f White "#Verifying Share Force Salting... #"
Write-Host -f White "###############################################"
#Verify Parallel Device
#Check and generate file log
$shareforcesalting = Get-VMhost | select Name, @{Name="Value"; Expression={Get-AdvancedSetting -Name Mem.ShareForceSalting -Entity $_}} | sort Value | ft -AutoSize
$shareforcesalting | Out-String | ForEach-Object { $_.Trim() } > "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\shareforcesalting.txt"
#Generate file if config not match
$emptycheck = (gc "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\shareforcesalting.txt") | where{$_ -notlike "*Mem.ShareForceSalting:2*"}
function shareforcesalting {
if ($emptycheck -eq $Null) {
Write-Host -f green "Share Force Salting is set to 2 in all Hosts"
}
else {
Write-Host -f red "Wrong Share Force Salting detected"
Write-Host -f red "Fixing Hosts"
$var1 = "" #Put your domain, for example if your hostname is host.test.local, put only test.local. If you don't use FQDN then comment this line and eliminate ".$var1" in next line.
$esxhost = (gc "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\shareforcesalting.txt") | where{$_ -notlike "*Mem.ShareForceSalting:2*"} | foreach{$_.split(".")[0]}
$esxhost | ForEach-Object {Get-VMhost "$_.$var1" | Get-AdvancedSetting -Name Mem.ShareForceSalting | Set-AdvancedSetting –Value 2 -Confirm:$false}
Write-Host -f green "Hosts Fixed"
}
}
shareforcesalting
$desired = @(
@{
Name = 'isolation.tools.copy.disable'
Value = $true
},
@{
Name = 'isolation.tools.dnd.disable'
Value = $true
},
@{
Name = 'isolation.tools.paste.disable'
Value = $true
},
@{
Name = 'isolation.tools.diskShrink.disable'
Value = $true
},
@{
Name = 'isolation.tools.setGUIOptions.enable'
Value = $false
},
@{
Name = 'isolation.tools.diskShrink.disable'
Value = $true
},
@{
Name = 'isolation.tools.diskWiper.disable'
Value = $true
},
@{
Name = 'isolation.tools.hgfs.disable'
Value = $true
},
@{
Name = 'isolation.tools.memSchedFakeSampleStats.disable'
Value = $true
},
@{
Name = 'tools.setInfo.sizeLimit'
Value = 1048576
},
@{
Name = 'isolation.device.edit.disable'
Value = $true
},
@{
Name = 'isolation.device.connectable.disable'
Value = $true
},
@{
Name = 'log.keepOld'
Value = 10
},
@{
Name = 'log.rotateSize'
Value = 1024000
},
@{
Name = 'RemoteDisplay.vnc.enabled'
Value = $false
},
@{
Name = 'tools.guestlib.enableHostInfo'
Value = $false
},
@{
Name = 'RemoteDisplay.maxConnections'
Value = 1
},
@{
Name = 'mks.enable3d'
Value = $false
},
@{
Name = 'isolation.tools.memSchedFakeSampleStats.disable'
Value = $true
}
)
$poweredoffvmsincluster = Get-VM
#File that containing the entire trace.
$Logfile = "$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr\vm-config.txt"
Start-Transcript -Path $Logfile
ForEach($VM in $poweredoffvmsincluster) {
$desired | %{
$setting = Get-AdvancedSetting -Entity $VM -Name $_.Name | Select Entity, Name, Value
if($setting){
if($setting.Value -eq $_.Value){
Write-Output "$VM $($_.Name) present and set correctly"
}
else{
Write-Output "$VM $($_.Name) present but not set correctly"
Set-AdvancedSetting -AdvancedSetting $setting -Value $_.Value -Confirm:$false
}
}
else{
Write-Output "$VM $($_.Name) not present."
New-AdvancedSetting -Name $_.Name -Value $_.Value -Entity $VM -Confirm:$false
}
}
}
Stop-Transcript
Write-Host -f green "Folder '$env:USERPROFILE\Documents\HardeningVM-Logs-$dateStr' was generated with the logs inside"