From e1015090c1d864853e5ee3dbda61f7a8a09b2deb Mon Sep 17 00:00:00 2001 From: Jose Luis Carreras Marin Date: Tue, 17 Dec 2024 11:49:57 +0100 Subject: [PATCH] add: files related to package generation, deprecating vbs into ps1 scripts --- packages/installers/win32/InstallerScript.ps1 | 244 ++++++ packages/installers/win32/RemoveAllScript.ps1 | 82 ++ packages/installers/win32/upgrade.bat | 18 + packages/installers/win32/wazuh_installer.wxs | 733 ++++++++++++++++++ 4 files changed, 1077 insertions(+) create mode 100644 packages/installers/win32/InstallerScript.ps1 create mode 100644 packages/installers/win32/RemoveAllScript.ps1 diff --git a/packages/installers/win32/InstallerScript.ps1 b/packages/installers/win32/InstallerScript.ps1 new file mode 100644 index 0000000000..030796dbae --- /dev/null +++ b/packages/installers/win32/InstallerScript.ps1 @@ -0,0 +1,244 @@ +# Script for configuration Windows agent. +# Copyright (C) 2015, Wazuh Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# ------------------------------------------------ + +function Get-UniqueArrayValues { + param ( + [array]$Array + ) + + $hashSet = @{} + foreach ($item in $Array) { + $hashSet[$item] = $null + } + + return $hashSet.Keys +} + +function Config { + param ( + [string]$CustomActionData + ) + + $ForReading = 1 + $ForWriting = 2 + + # Parse custom parameters + $args = $CustomActionData -split "/\+/" + $homeDir = $args[0].Trim('"') + $OS_VERSION = $args[1].Trim('"') + $WAZUH_MANAGER = $args[2].Trim('"') + $WAZUH_MANAGER_PORT = $args[3].Trim('"') + $WAZUH_PROTOCOL = $args[4].Trim('"') + $NOTIFY_TIME = $args[5].Trim('"') + $WAZUH_REGISTRATION_SERVER = $args[6].Trim('"') + $WAZUH_REGISTRATION_PORT = $args[7].Trim('"') + $WAZUH_REGISTRATION_PASSWORD = $args[8].Trim('"') + $WAZUH_KEEP_ALIVE_INTERVAL = $args[9].Trim('"') + $WAZUH_TIME_RECONNECT = $args[10].Trim('"') + $WAZUH_REGISTRATION_CA = $args[11].Trim('"') + $WAZUH_REGISTRATION_CERTIFICATE = $args[12].Trim('"') + $WAZUH_REGISTRATION_KEY = $args[13].Trim('"') + $WAZUH_AGENT_NAME = $args[14].Trim('"') + $WAZUH_AGENT_GROUP = $args[15].Trim('"') + $ENROLLMENT_DELAY = $args[16].Trim('"') + + # Create client.keys if it doesn't exist + if (!(Test-Path -Path "$homeDir\client.keys")) { + New-Item -ItemType File -Path "$homeDir\client.keys" | Out-Null + } + + if (Test-Path -Path "$homeDir\ossec.conf") { + # Read the ossec.conf file + $strText = Get-Content -Path "$homeDir\ossec.conf" -Raw + + # Update ossec.conf based on custom parameters + if ($WAZUH_MANAGER -or $WAZUH_MANAGER_PORT -or $WAZUH_PROTOCOL -or $WAZUH_KEEP_ALIVE_INTERVAL -or $WAZUH_TIME_RECONNECT) { + $protocolList = if ($WAZUH_PROTOCOL -and $WAZUH_PROTOCOL -match ",") { + $WAZUH_PROTOCOL.ToLower().Split(",") + } else { + @($WAZUH_PROTOCOL.ToLower()) + } + + if ($WAZUH_MANAGER) { + # Update server configuration + $ipList = if ($WAZUH_MANAGER -match ",") { + $WAZUH_MANAGER.Split(",") + } else { + @($WAZUH_MANAGER) + } + + $formattedList = "`n" + for ($i = 0; $i -lt $ipList.Count; $i++) { + if ($ipList[$i]) { + $protocol = if ($i -lt $protocolList.Count -and $protocolList[$i]) { + $protocolList[$i].ToLower() + } else { + "tcp" + } + $formattedList += " `n" + $formattedList += "
$($ipList[$i])
`n" + $formattedList += " 1514`n" + $formattedList += " $protocol`n" + $formattedList += if ($i -eq $ipList.Count - 1) { + "
" + } else { + " `n" + } + } + } + $strText = $strText -replace "\s+(.|\n)+?", $formattedList + } + + if ($WAZUH_MANAGER_PORT) { + $strText = $strText -replace "1514", "$WAZUH_MANAGER_PORT" + } + + if ($WAZUH_KEEP_ALIVE_INTERVAL) { + $strText = $strText -replace ".*", "$WAZUH_KEEP_ALIVE_INTERVAL" + } + + if ($WAZUH_TIME_RECONNECT) { + $strText = $strText -replace ".*", "$WAZUH_TIME_RECONNECT" + } + } + + # Handle enrollment configuration + if ($WAZUH_REGISTRATION_SERVER -or $WAZUH_REGISTRATION_PORT -or $WAZUH_REGISTRATION_PASSWORD -or $WAZUH_REGISTRATION_CA -or $WAZUH_REGISTRATION_CERTIFICATE -or $WAZUH_REGISTRATION_KEY -or $WAZUH_AGENT_NAME -or $WAZUH_AGENT_GROUP -or $ENROLLMENT_DELAY) { + $enrollmentList = @" + + yes + + +"@ + $strText = $strText.Replace(" ", $enrollmentList) + + if ($WAZUH_REGISTRATION_SERVER) { + $strText = $strText.Replace(" ", " $WAZUH_REGISTRATION_SERVER`n ") + } + + if ($WAZUH_REGISTRATION_PORT) { + $strText = $strText.Replace(" ", " $WAZUH_REGISTRATION_PORT`n ") + } + + if ($WAZUH_REGISTRATION_PASSWORD) { + Set-Content -Path "$homeDir\authd.pass" -Value $WAZUH_REGISTRATION_PASSWORD + $strText = $strText.Replace(" ", " authd.pass`n ") + } + + if ($WAZUH_REGISTRATION_CA) { + $strText = $strText.Replace(" ", " $WAZUH_REGISTRATION_CA`n ") + } + + if ($WAZUH_REGISTRATION_CERTIFICATE) { + $strText = $strText.Replace(" ", " $WAZUH_REGISTRATION_CERTIFICATE`n ") + } + + if ($WAZUH_REGISTRATION_KEY) { + $strText = $strText.Replace(" ", " $WAZUH_REGISTRATION_KEY`n ") + } + + if ($WAZUH_AGENT_NAME) { + $strText = $strText.Replace(" ", " $WAZUH_AGENT_NAME`n ") + } + + if ($WAZUH_AGENT_GROUP) { + $strText = $strText.Replace(" ", " $WAZUH_AGENT_GROUP`n ") + } + + if ($ENROLLMENT_DELAY) { + $strText = $strText.Replace(" ", " $ENROLLMENT_DELAY`n ") + } + } + + # Write the updated ossec.conf + Set-Content -Path "$homeDir\ossec.conf" -Value $strText + } + + Set-WazuhPermissions + + return 0 +} + +function Get-Version { + $osVersion = (Get-CimInstance -ClassName Win32_OperatingSystem).Version + return $osVersion.Split('.')[0] +} + +function Check-SvcRunning { + $serviceNames = @("OssecSvc", "WazuhSvc") + foreach ($serviceName in $serviceNames) { + $service = Get-CimInstance -ClassName Win32_Service | Where-Object { $_.Name -eq $serviceName } + if ($service) { + $state = $service.State + if ($serviceName -eq "OssecSvc") { + $env:OSSECRUNNING = $state + } elseif ($serviceName -eq "WazuhSvc") { + $env:WAZUHRUNNING = $state + } + } + } + return 0 +} + +function Kill-GUITask { + Stop-Process -Name "win32ui" -Force -ErrorAction SilentlyContinue + return 0 +} + +function Start-WazuhSvc { + Start-Service -Name "WazuhSvc" + return 0 +} + +function Set-WazuhPermissions { + param ( + [string]$CustomActionData + ) + + $args = $CustomActionData -split "/\+/" + $homeDir = $args[0].Trim('"') + + if ([int](Get-Version) -ge 6) { + $installDir = $homeDir.TrimEnd('\') + + icacls "$installDir" /reset /t + icacls "$installDir" /inheritancelevel:r /q + icacls "$installDir" /grant *S-1-5-32-544:(OI)(CI)F + icacls "$installDir" /grant *S-1-5-18:(OI)(CI)F + icacls "$installDir\*" /grant *S-1-5-11:(OI)(CI)RX + icacls "$installDir\*" /grant *S-1-5-11:RX + icacls "$installDir" /grant *S-1-5-11:RX + + icacls "$homeDir*ossec.conf" /remove *S-1-5-11 /q + icacls "$homeDir\client.keys" /remove *S-1-5-11 /q + icacls "$homeDir\tmp" /remove:g *S-1-5-11 /q + } + return 0 +} + +function Create-DumpRegistryKey { + $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\wazuh-agent.exe" + if (-not (Test-Path -Path $regPath)) { + New-Item -Path $regPath -Force | Out-Null + } + + Set-ItemProperty -Path $regPath -Name "DumpFolder" -Value "%LOCALAPPDATA%\WazuhCrashDumps" + Set-ItemProperty -Path $regPath -Name "DumpType" -Value 2 + return 0 +} diff --git a/packages/installers/win32/RemoveAllScript.ps1 b/packages/installers/win32/RemoveAllScript.ps1 new file mode 100644 index 0000000000..7a4b5afabc --- /dev/null +++ b/packages/installers/win32/RemoveAllScript.ps1 @@ -0,0 +1,82 @@ +# Script for configuration Windows agent. +# Copyright (C) 2015, Wazuh Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# ------------------------------------------------ + + +# This function is called only when uninstalling the product. +# Remove everything, but a few specified items. + +function Remove-All { + param ( + [string]$CustomActionData + ) + + # Retrieve the parameters + $args = $CustomActionData -split "," + $homeDir = $args[0].Trim('"') # APPLICATIONFOLDER + + # Check and delete specific files + $filesToDelete = @( + "ossec.conf.save", "client.keys.save", + "local_internal_options.conf.save", "installer.log.save" + ) + $filesToRename = @( + "ossec.conf", "client.keys", + "local_internal_options.conf", "installer.log" + ) + + foreach ($file in $filesToDelete) { + $filePath = Join-Path $homeDir $file + if (Test-Path $filePath) { + Remove-Item -Path $filePath -Force + } + } + + foreach ($file in $filesToRename) { + $filePath = Join-Path $homeDir $file + $newFilePath = Join-Path $homeDir "$file.save" + if (Test-Path $filePath) { + Rename-Item -Path $filePath -NewName "$file.save" -Force + } + } + + # Delete all other files and folders except specified + if (Test-Path $homeDir) { + $filesToKeep = @( + "ossec.conf.save", "client.keys.save", + "local_internal_options.conf.save", "installer.log.save" + ) + $subfoldersToKeep = @("backup", "upgrade") + + # Delete files + Get-ChildItem -Path $homeDir -File | ForEach-Object { + if ($filesToKeep -notcontains $_.Name) { + Remove-Item -Path $_.FullName -Force -ErrorAction SilentlyContinue + } + } + + # Delete subfolders + Get-ChildItem -Path $homeDir -Directory | ForEach-Object { + if ($subfoldersToKeep -notcontains $_.Name) { + Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue + } + } + } + + return 0 +} diff --git a/packages/installers/win32/upgrade.bat b/packages/installers/win32/upgrade.bat index e69de29bb2..63df2a40ce 100644 --- a/packages/installers/win32/upgrade.bat +++ b/packages/installers/win32/upgrade.bat @@ -0,0 +1,18 @@ +@ECHO off + +IF "%1"=="B" GOTO background + +COPY upgrade\upgrade.bat . > NUL +COPY upgrade\do_upgrade.ps1 . > NUL +COPY upgrade\wazuh-agent*.msi . > NUL + +START /B upgrade.bat B +GOTO end + +:background +SLEEP 5 2> NUL || ping -n 5 127.0.0.1 > NUL +powershell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process powershell '-File \".\do_upgrade.ps1\"'}" + +DEL upgrade.bat + +:end diff --git a/packages/installers/win32/wazuh_installer.wxs b/packages/installers/win32/wazuh_installer.wxs index e69de29bb2..0885e1e185 100644 --- a/packages/installers/win32/wazuh_installer.wxs +++ b/packages/installers/win32/wazuh_installer.wxs @@ -0,0 +1,733 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed + + + + + + ( VersionNT = 501 OR VersionNT64 = 501 ) + ( VersionNT = 502 OR VersionNT64 = 502 ) + ( VersionNT = 600 OR VersionNT64 = 600 ) AND MsiNTProductType = 1 + ( VersionNT = 600 OR VersionNT64 = 600 ) AND MsiNTProductType > 1 + ( VersionNT = 601 OR VersionNT64 = 601 ) AND MsiNTProductType = 1 + ( VersionNT = 601 OR VersionNT64 = 601 ) AND MsiNTProductType > 1 + ( VersionNT = 602 OR VersionNT64 = 602 ) AND MsiNTProductType = 1 + ( VersionNT = 602 OR VersionNT64 = 602 ) AND MsiNTProductType > 1 + ( VersionNT = 603 OR VersionNT64 = 603 ) AND MAJORVERSION <> "#10" AND MsiNTProductType = 1 + ( VersionNT = 603 OR VersionNT64 = 603 ) AND MAJORVERSION <> "#10" AND MsiNTProductType > 1 + MAJORVERSION = "#10" AND MsiNTProductType = 1 + BUILDVERSION = "14393" AND MsiNTProductType > 1 + BUILDVERSION = "17763" AND MsiNTProductType > 1 + + + + + + ADDRESS <> "" AND WAZUH_MANAGER = "" + SERVER_PORT <> "" AND WAZUH_MANAGER_PORT = "" + PROTOCOL <> "" AND WAZUH_PROTOCOL = "" + AUTHD_SERVER <> "" AND WAZUH_REGISTRATION_SERVER = "" + AUTHD_PORT <> "" AND WAZUH_REGISTRATION_PORT = "" + PASSWORD <> "" AND WAZUH_REGISTRATION_PASSWORD = "" + NOTIFY_TIME <> "" AND WAZUH_KEEP_ALIVE_INTERVAL = "" + TIME_RECONNECT <> "" AND WAZUH_TIME_RECONNECT = "" + CERTIFICATE <> "" AND WAZUH_REGISTRATION_CA = "" + PEM <> "" AND WAZUH_REGISTRATION_CERTIFICATE = "" + KEY <> "" AND WAZUH_REGISTRATION_KEY = "" + AGENT_NAME <> "" AND WAZUH_AGENT_NAME = "" + GROUP <> "" AND WAZUH_AGENT_GROUP = "" + + + + OSSECINSTALLED + OSSECINSTALLED + + + (NOT WIX_UPGRADE_DETECTED) AND (NOT PATCH) AND (NOT Installed) + (NOT WIX_UPGRADE_DETECTED) AND (NOT PATCH) AND (NOT Installed) + + + (WIX_UPGRADE_DETECTED) OR (PATCH) + (WIX_UPGRADE_DETECTED) OR (PATCH) + + + ((WIX_UPGRADE_DETECTED) OR (PATCH)) AND ((OSSECRUNNING = "Running") OR (WAZUHRUNNING = "Running")) + + + (NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL") + (NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL") + + + GENERATE_DUMP = "1" AND DEFAULT_GENERATE_DUMP = "1" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + VersionNT >= 600 + + + + + + VersionNT < 600 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ( VersionNT = 501 OR VersionNT64 = 501 ) + + + + ( VersionNT = 501 OR VersionNT64 = 501 ) + + + + ( VersionNT = 501 OR VersionNT64 = 501 ) + + + + ( VersionNT = 502 OR VersionNT64 = 502 ) + + + + ( VersionNT = 502 OR VersionNT64 = 502 ) + + + + ( VersionNT = 502 OR VersionNT64 = 502 ) + + + + ( VersionNT = 600 OR VersionNT64 = 600 ) AND MsiNTProductType = 1 + + + + ( VersionNT = 600 OR VersionNT64 = 600 ) AND MsiNTProductType > 1 + + + + ( VersionNT = 601 OR VersionNT64 = 601 ) AND MsiNTProductType = 1 + + + + ( VersionNT = 601 OR VersionNT64 = 601 ) AND MsiNTProductType > 1 + + + + ( VersionNT = 602 OR VersionNT64 = 602 ) AND MsiNTProductType = 1 + + + + VersionNT64 = 602 AND MsiNTProductType > 1 + + + + ( VersionNT = 603 OR VersionNT64 = 603) AND MAJORVERSION <> "#10" AND MsiNTProductType = 1 + + + + VersionNT64 = 603 AND MAJORVERSION <> "#10" AND MsiNTProductType > 1 + + + + MAJORVERSION = "#10" AND MsiNTProductType = 1 + + + + BUILDVERSION = "14393" AND MsiNTProductType > 1 + + + + BUILDVERSION = "17763" AND MsiNTProductType > 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + MAJORVERSION = "#10" AND BUILDVERSION < 22000 AND MsiNTProductType = 1 + + + + MAJORVERSION = "#10" AND BUILDVERSION > 21999 AND MsiNTProductType = 1 + + + + VersionNT64 = 603 AND MAJORVERSION <> "#10" AND MsiNTProductType > 1 + + + + VersionNT64 = 603 AND MAJORVERSION = 6 AND BUILDVERSION = 9200 AND MsiNTProductType > 1 + + + + BUILDVERSION = "14393" AND MsiNTProductType > 1 + + + + BUILDVERSION = "17763" AND MsiNTProductType > 1 + + + + BUILDVERSION > 20000 AND MsiNTProductType > 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + + + + 1 + + + + +