-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-openvas.ps1
More file actions
513 lines (431 loc) · 14.6 KB
/
install-openvas.ps1
File metadata and controls
513 lines (431 loc) · 14.6 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
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
<#
.SYNOPSIS
One-stop OpenVAS (Greenbone) installer for Windows.
.DESCRIPTION
Automatically installs all prerequisites and deploys OpenVAS via Docker.
Handles Chocolatey, WSL2, Docker Desktop, and Greenbone container setup.
.PARAMETER CheckOnly
Show current OpenVAS status without making changes.
.PARAMETER Uninstall
Remove OpenVAS containers and optionally volumes.
.PARAMETER LogPath
Path to write log file. If not specified, logs to console only.
.EXAMPLE
.\install-openvas.ps1
Full installation with all prerequisites.
.EXAMPLE
.\install-openvas.ps1 -CheckOnly
Check current status.
.EXAMPLE
.\install-openvas.ps1 -Uninstall
Remove OpenVAS installation.
#>
[CmdletBinding()]
param(
[switch]$CheckOnly,
[switch]$Uninstall,
[string]$LogPath
)
$ErrorActionPreference = "Stop"
$script:LogFile = $LogPath
#region Logging
function Write-Log {
param(
[string]$Message,
[ValidateSet("INFO", "WARN", "ERROR", "SUCCESS")]
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$color = switch ($Level) {
"INFO" { "White" }
"WARN" { "Yellow" }
"ERROR" { "Red" }
"SUCCESS" { "Green" }
}
$logMessage = "[$timestamp] [$Level] $Message"
Write-Host $logMessage -ForegroundColor $color
if ($script:LogFile) {
Add-Content -Path $script:LogFile -Value $logMessage
}
}
#endregion
#region Admin Elevation
function Test-Administrator {
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Request-Elevation {
if (-not (Test-Administrator)) {
Write-Log "Requesting administrator privileges..." -Level WARN
$scriptPath = $MyInvocation.PSCommandPath
if (-not $scriptPath) {
$scriptPath = $PSCommandPath
}
$arguments = @()
if ($CheckOnly) { $arguments += "-CheckOnly" }
if ($Uninstall) { $arguments += "-Uninstall" }
if ($LogPath) { $arguments += "-LogPath `"$LogPath`"" }
$argString = $arguments -join " "
try {
Start-Process powershell.exe -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$scriptPath`" $argString"
exit 0
}
catch {
Write-Log "Failed to elevate. Please run as Administrator." -Level ERROR
exit 1
}
}
Write-Log "Running with administrator privileges" -Level SUCCESS
}
#endregion
#region Chocolatey
function Install-Chocolatey {
Write-Log "Checking Chocolatey..."
$chocoPath = Get-Command choco -ErrorAction SilentlyContinue
if ($chocoPath) {
Write-Log "Chocolatey already installed" -Level SUCCESS
return $true
}
Write-Log "Installing Chocolatey..."
try {
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# Refresh environment
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
# Verify
$chocoPath = Get-Command choco -ErrorAction SilentlyContinue
if ($chocoPath) {
Write-Log "Chocolatey installed successfully" -Level SUCCESS
return $true
}
else {
Write-Log "Chocolatey installation failed - command not found" -Level ERROR
return $false
}
}
catch {
Write-Log "Failed to install Chocolatey: $_" -Level ERROR
return $false
}
}
#endregion
#region WSL2
function Install-WSL2 {
Write-Log "Checking WSL2..."
# Check if WSL is installed and version
try {
$wslVersion = wsl --version 2>&1
if ($LASTEXITCODE -eq 0 -and $wslVersion -match "WSL") {
Write-Log "WSL2 already installed" -Level SUCCESS
return $true
}
}
catch {
# WSL not installed
}
Write-Log "Installing WSL2..."
try {
# Enable WSL feature
Write-Log "Enabling Windows Subsystem for Linux..."
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart | Out-Null
# Enable Virtual Machine Platform
Write-Log "Enabling Virtual Machine Platform..."
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart | Out-Null
# Install WSL via wsl --install (Windows 10 2004+)
Write-Log "Running WSL install..."
wsl --install --no-distribution 2>&1 | Out-Null
# Set WSL2 as default
wsl --set-default-version 2 2>&1 | Out-Null
Write-Log "WSL2 installation initiated." -Level SUCCESS
# Check if this is a fresh WSL2 install that needs restart
$wslCheck = wsl --version 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Log "RESTART REQUIRED: WSL2 was just enabled. Please restart Windows and run this script again." -Level ERROR
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 0
}
return $true
}
catch {
Write-Log "Failed to install WSL2: $_" -Level ERROR
return $false
}
}
#endregion
#region Docker
function Install-Docker {
Write-Log "Checking Docker Desktop..."
# Check if Docker is running
try {
$dockerVersion = docker version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "Docker Desktop already running" -Level SUCCESS
return $true
}
}
catch {
# Docker not running
}
# Check if Docker Desktop is installed but not running
$dockerDesktopPath = "$env:ProgramFiles\Docker\Docker\Docker Desktop.exe"
if (Test-Path $dockerDesktopPath) {
Write-Log "Docker Desktop installed but not running. Starting..."
Start-Process $dockerDesktopPath
# Wait for Docker to start
$maxWait = 120
$waited = 0
while ($waited -lt $maxWait) {
Start-Sleep -Seconds 5
$waited += 5
try {
docker version 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Log "Docker Desktop started" -Level SUCCESS
return $true
}
}
catch {}
Write-Log "Waiting for Docker to start... ($waited/$maxWait seconds)"
}
Write-Log "Docker Desktop failed to start in time" -Level ERROR
return $false
}
# Install Docker Desktop via Chocolatey
Write-Log "Installing Docker Desktop via Chocolatey..."
try {
choco install docker-desktop -y
Write-Log "Docker Desktop installed. Starting..." -Level SUCCESS
# Refresh environment
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
# Start Docker Desktop
$dockerDesktopPath = "$env:ProgramFiles\Docker\Docker\Docker Desktop.exe"
if (Test-Path $dockerDesktopPath) {
Start-Process $dockerDesktopPath
}
# Wait for Docker
$maxWait = 180
$waited = 0
while ($waited -lt $maxWait) {
Start-Sleep -Seconds 10
$waited += 10
try {
docker version 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Log "Docker Desktop ready" -Level SUCCESS
return $true
}
}
catch {}
Write-Log "Waiting for Docker to initialize... ($waited/$maxWait seconds)"
}
Write-Log "Docker installed but may need restart. Please restart and run again." -Level WARN
return $false
}
catch {
Write-Log "Failed to install Docker Desktop: $_" -Level ERROR
return $false
}
}
#endregion
#region OpenVAS Container
function Get-OpenVASComposeFile {
return @"
version: '3.8'
services:
openvas:
image: immauss/openvas:latest
container_name: openvas
restart: unless-stopped
ports:
- "9392:9392"
- "9390:9390"
volumes:
- openvas_data:/data
environment:
- PASSWORD=PLACEHOLDER_PASSWORD
- USERNAME=admin
deploy:
resources:
limits:
memory: 8G
volumes:
openvas_data:
"@
}
function Install-OpenVAS {
$composeDir = "$env:USERPROFILE\.openvas"
$composeFile = "$composeDir\docker-compose.yml"
# Create directory
if (-not (Test-Path $composeDir)) {
New-Item -ItemType Directory -Path $composeDir -Force | Out-Null
}
# Pull image
Write-Log "Pulling OpenVAS container (this may take several minutes)..."
docker pull immauss/openvas:latest
if ($LASTEXITCODE -ne 0) {
Write-Log "Failed to pull container image" -Level ERROR
return $false
}
Write-Log "Container image pulled" -Level SUCCESS
# Generate password
$password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 16 | ForEach-Object { [char]$_ })
# Create compose file
$composeContent = Get-OpenVASComposeFile
$composeContent = $composeContent -replace "PLACEHOLDER_PASSWORD", $password
Set-Content -Path $composeFile -Value $composeContent
Write-Log "Docker Compose file created" -Level SUCCESS
# Start container
Write-Log "Starting OpenVAS container..."
Push-Location $composeDir
docker-compose up -d
$startResult = $LASTEXITCODE
Pop-Location
if ($startResult -ne 0) {
Write-Log "Failed to start container" -Level ERROR
return $false
}
# Wait for container health
Write-Log "Waiting for OpenVAS to initialize (this takes several minutes)..."
$maxWait = 300
$waited = 0
while ($waited -lt $maxWait) {
Start-Sleep -Seconds 15
$waited += 15
$health = docker inspect --format='{{.State.Health.Status}}' openvas 2>&1
if ($health -eq "healthy") {
Write-Log "OpenVAS container is healthy" -Level SUCCESS
break
}
Write-Log "Container initializing... ($waited/$maxWait seconds) Status: $health"
}
# Save credentials
$credFile = "$composeDir\credentials.txt"
@"
OpenVAS Admin Credentials
=========================
Username: admin
Password: $password
Web UI: http://localhost:9392
Generated: $(Get-Date)
IMPORTANT: Change this password after first login!
"@ | Set-Content -Path $credFile
Write-Log "========================================" -Level SUCCESS
Write-Log "OpenVAS Installation Complete!" -Level SUCCESS
Write-Log "========================================" -Level SUCCESS
Write-Log ""
Write-Log "Web UI: http://localhost:9392" -Level INFO
Write-Log "Username: admin" -Level INFO
Write-Log "Password: $password" -Level INFO
Write-Log ""
Write-Log "Credentials saved to: $credFile" -Level INFO
Write-Log ""
Write-Log "NOTE: Initial feed sync may take 30-60 minutes." -Level WARN
Write-Log "The scanner won't have full vulnerability data until sync completes." -Level WARN
return $true
}
#endregion
#region Status Check
function Show-Status {
Write-Log "OpenVAS Status Check" -Level INFO
Write-Log "===================="
$composeDir = "$env:USERPROFILE\.openvas"
# Check if installed
if (-not (Test-Path "$composeDir\docker-compose.yml")) {
Write-Log "OpenVAS is not installed" -Level WARN
return
}
# Check container status
try {
$containerStatus = docker inspect --format='{{.State.Status}}' openvas 2>&1
$health = docker inspect --format='{{.State.Health.Status}}' openvas 2>&1
Write-Log "Container Status: $containerStatus"
Write-Log "Health: $health"
if ($containerStatus -eq "running") {
# Get resource usage
$stats = docker stats openvas --no-stream --format "CPU: {{.CPUPerc}}, Memory: {{.MemUsage}}"
Write-Log "Resources: $stats"
}
}
catch {
Write-Log "Container not found or Docker not running" -Level WARN
}
# Check credentials file
$credFile = "$composeDir\credentials.txt"
if (Test-Path $credFile) {
Write-Log ""
Write-Log "Credentials file: $credFile" -Level INFO
}
}
#endregion
#region Uninstall
function Uninstall-OpenVAS {
Write-Log "Uninstalling OpenVAS..." -Level WARN
$composeDir = "$env:USERPROFILE\.openvas"
# Stop and remove container
try {
docker stop openvas 2>&1 | Out-Null
docker rm openvas 2>&1 | Out-Null
Write-Log "Container removed" -Level SUCCESS
}
catch {
Write-Log "Container not running or already removed" -Level INFO
}
# Ask about volumes
$removeVolumes = Read-Host "Remove data volumes? This will delete all scan data. (y/N)"
if ($removeVolumes -eq "y" -or $removeVolumes -eq "Y") {
docker volume rm openvas_data openvas_logs 2>&1 | Out-Null
Write-Log "Volumes removed" -Level SUCCESS
}
# Remove compose file
if (Test-Path $composeDir) {
Remove-Item -Path $composeDir -Recurse -Force
Write-Log "Configuration removed" -Level SUCCESS
}
Write-Log "OpenVAS uninstalled" -Level SUCCESS
}
#endregion
#region Main
function Main {
Write-Log "========================================"
Write-Log "OpenVAS Installer for Windows"
Write-Log "========================================"
# Check mode
if ($CheckOnly) {
Show-Status
return
}
if ($Uninstall) {
Request-Elevation
Uninstall-OpenVAS
return
}
# Full installation
Request-Elevation
# Step 1: Chocolatey
if (-not (Install-Chocolatey)) {
Write-Log "Cannot continue without Chocolatey" -Level ERROR
exit 1
}
# Step 2: WSL2
if (-not (Install-WSL2)) {
Write-Log "WSL2 installation may require restart" -Level WARN
}
# Step 3: Docker
if (-not (Install-Docker)) {
Write-Log "Cannot continue without Docker" -Level ERROR
exit 1
}
# Step 4: OpenVAS
if (-not (Install-OpenVAS)) {
Write-Log "OpenVAS installation failed" -Level ERROR
exit 1
}
Write-Log ""
Write-Log "Installation complete! Open http://localhost:9392 in your browser." -Level SUCCESS
}
Main
#endregion