1+ $ErrorActionPreference = ' Stop'
2+ Import-Module vm.common - Force - DisableNameChecking
3+ Import-Module powershell- yaml
4+
5+ # need to install yaml handling module 'Install-Module -Name powershell-yaml'
6+
7+ $toolName = " Suricata"
8+ $category = VM- Get-Category ($MyInvocation.MyCommand.Definition )
9+ $filetype = " MSI"
10+ $toolDir = Join-Path ${Env: ProgramFiles} $toolName
11+ $executablePath = Join-Path $toolDir " suricata.exe"
12+ $url = " https://www.openinfosecfoundation.org/download/windows/Suricata-7.0.8-1-64bit.msi"
13+ $sha256 = " 8bdd78d2978e4efc6d23ab1ced024342cac7d38afb152a6e3f70ac5182bd8cd4"
14+ $silentArgs = " /qn /norestart"
15+
16+ $packageArgs = @ {
17+ toolName = $toolName
18+ category = $category
19+ filetype = $filetype
20+ silentArgs = $silentArgs
21+ executablePath = $executablePath
22+ url = $url
23+ sha256 = $sha256
24+ consoleApp = $true
25+ }
26+
27+ VM- Install-With - Installer @packageArgs
28+
29+ try {
30+ $desktopShortcutPath = " ${Env: HomeDrive} \Users\*\Desktop\$toolName *.lnk"
31+ Remove-Item - Path $desktopShortcutPath - ErrorAction SilentlyContinue
32+ }
33+ catch {
34+ VM- Write-Log - Exception $_
35+ }
36+
37+ $rulesXmlPath = " $ ( Split-Path - parent $MyInvocation.MyCommand.Definition ) /rules.xml"
38+ $rulesXml = [xml ](Get-Content $rulesXmlPath )
39+
40+ $rulesDir = Join-Path $toolDir " rules"
41+ $rulesConfigPath = Join-Path $toolDir " suricata.yaml"
42+
43+ $rulesConfig = ConvertFrom-Yaml (Get-Content - Raw - Path $rulesConfigPath )
44+
45+ $failures = @ ()
46+ $rules = $rulesXml.rules.rule
47+
48+ $tempToolDir = Join-Path ${Env: TEMP} $toolName
49+ $tempToolDir += " .vm"
50+ $tempRuleDir = Join-Path $tempToolDir " rules"
51+
52+ foreach ($rule in $rules ) {
53+
54+ Write-Host " [+] Attempting to install rule: $ ( $rule.name ) "
55+
56+ $filePath = Join-Path $tempToolDir ([System.IO.Path ]::GetFileName($rule.url ))
57+
58+ try {
59+ Invoke-WebRequest - Uri $rule.url - OutFile $filePath
60+
61+ # If the file ends in .zip, unzip it
62+ if ($filePath -like ' *.zip' ) {
63+
64+ Write-Host " ZIP file detected."
65+
66+ Get-ChocolateyUnzip - FileFullPath $filePath - Destination $tempRuleDir
67+
68+ if ($rule.innerFolder ){
69+ $innerFolder = Join-Path $tempRuleDir $rule.innerFolder
70+
71+ Get-ChildItem - Path $innerFolder - File | ForEach-Object {
72+ Copy-Item - Path $_.FullName - Destination $tempRuleDir - Force
73+ }
74+ }
75+
76+ } elseif ($filePath -like ' *.rules' ) {
77+
78+ Write-Host " Rules file detected. Moving to $tempRuleDir ..."
79+
80+ Move-Item - Path $filePath - Destination $tempRuleDir
81+
82+ } else {
83+ throw " `t [!] Unsupported file type: '$filePath '. Only .zip and .rule are allowed."
84+ }
85+ } catch {
86+ $failures += $rule.name
87+ }
88+ }
89+
90+ $allRuleFiles = Get-ChildItem - Path $tempRuleDir - Recurse - File - Filter * .rules
91+
92+ foreach ($ruleFile in $allRuleFiles ){
93+ Move-Item - Path $ruleFile.FullName - Destination $rulesDir - Force
94+ $rulesConfig .' rule-files' += $ruleFile.Name
95+ Write-Host " `t [+] Rule $ ( $ruleFile.Name ) added to $rulesDir ..."
96+ }
97+
98+ $rulesConfig | ConvertTo-Yaml | Set-Content - Path $rulesConfigPath
99+
100+ if ($failures.Count -gt 0 ) {
101+ foreach ($module in $failures ) {
102+ VM- Write-Log " ERROR" " Failed to install rule: $ ( $rule.name ) "
103+ }
104+ exit 1
105+ }
0 commit comments