Skip to content

Commit 900bbe9

Browse files
committed
suricata-package addition
1 parent 6b1b502 commit 900bbe9

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed

packages/common.vm/common.vm.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
33
<metadata>
44
<id>common.vm</id>
5-
<version>0.0.0.20250425</version>
5+
<version>0.0.0.20250502</version>
66
<description>Common libraries for VM-packages</description>
77
<authors>Mandiant</authors>
88
</metadata>

packages/common.vm/tools/vm.common/vm.common.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function VM-Write-Log {
8181
[CmdletBinding()]
8282
Param(
8383
[Parameter(Mandatory=$true, Position=0)]
84-
[ValidateSet("INFO","WARN","ERROR")]
84+
[ValidateSet("INFO","WARN","ERROR","FATAL")]
8585
[String] $level,
8686
[Parameter(Mandatory=$true, Position=1)]
8787
[string] $message
@@ -1803,7 +1803,7 @@ function VM-Get-MSIInstallerPathByProductName {
18031803

18041804
try {
18051805
# Get a list of all installed MSI products
1806-
$installedProducts = Get-CimInstance -Class Win32_Product | Where-Object { $_.Name -like $ProductName }
1806+
$installedProducts = Get-CimInstance -Class Win32_Product | Where-Object { $_.Name -match $ProductName }
18071807

18081808
if (-not $installedProducts) {
18091809
VM-Write-Log "WARN" "No product found with name like '$ProductName'"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
3+
<metadata>
4+
<id>suricata.vm</id>
5+
<version>7.0.10</version>
6+
<authors>Open Information Security Foundation</authors>
7+
<description>Suricata is a network IDS, IPS and NSM engine developed by the OISF and the Suricata community</description>
8+
<dependencies>
9+
<dependency id="common.vm" version="0.0.0.20250206" />
10+
<dependency id="npcap.vm" version="1.80.20250219" />
11+
</dependencies>
12+
<tags>Networking</tags>
13+
</metadata>
14+
</package>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
$ErrorActionPreference = 'Stop'
2+
Import-Module vm.common -Force -DisableNameChecking
3+
4+
try{
5+
# set configurations
6+
$toolName = 'suricata'
7+
$category = VM-Get-Category($MyInvocation.MyCommand.Definition)
8+
$toolDir = Join-Path ${Env:ProgramFiles} $toolName
9+
$executablePath = Join-Path $toolDir "$toolName.exe"
10+
$exeUrl = "https://www.openinfosecfoundation.org/download/windows/Suricata-7.0.10-1-64bit.msi"
11+
$sha256 = "b32a6ca8a793a603a23de307c83831c874099f50bbcd2710ee8325d69a49fb44"
12+
13+
$packageArgs = @{
14+
toolName = $toolName
15+
category = $category
16+
filetype = "MSI"
17+
silentArgs = "/qn /norestart"
18+
executablePath = $executablePath
19+
url = $exeUrl
20+
sha256 = $sha256
21+
consoleApp = $true
22+
}
23+
24+
VM-Install-With-Installer @packageArgs
25+
26+
# delete default desktop shortcut
27+
$desktopShortcutPath = "${Env:HomeDrive}\Users\*\Desktop\$toolName*.lnk"
28+
Remove-Item -Path $desktopShortcutPath -ErrorAction SilentlyContinue
29+
30+
# rules configuration and download
31+
$rulesXmlPath = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)/rules.xml"
32+
$rulesXml = [xml](Get-Content $rulesXmlPath)
33+
$rulesDir = Join-Path $toolDir "rules" -Resolve
34+
$rules = $rulesXml.rules.rule
35+
36+
# tempdir for rules been added
37+
# rules are added to tempdir before been added to default rule folder as other default rules exist in default folder
38+
# rules filenames are needed for adding to config files
39+
$tempToolDir = Join-Path ${Env:TEMP} "$toolName.vm"
40+
$tempRuleDir = Join-Path $tempToolDir "rules"
41+
42+
foreach ($rule in $rules) {
43+
VM-Write-Log "INFO" "Attempting to install rule: $($rule.name)"
44+
$filePath = Join-Path $tempToolDir ([System.IO.Path]::GetFileName($rule.url))
45+
46+
# create rule specific temp folder
47+
$tempRuleSpecificFolder = Join-Path $tempRuleDir $rule.name
48+
if (-not (Test-Path -Path $tempRuleSpecificFolder)) {
49+
New-Item -Path $tempRuleSpecificFolder -ItemType Directory
50+
}
51+
try{
52+
Invoke-WebRequest -Uri $rule.url -OutFile $filePath -ErrorAction Stop
53+
54+
# If the rule url is of a zip archive (collection of multiple rule files)
55+
if ($filePath -like '*.zip') {
56+
VM-Write-Log "INFO" "ZIP file detected."
57+
Get-ChocolateyUnzip -FileFullPath $filePath -Destination $tempRuleSpecificFolder | Out-Null
58+
59+
# if the rule url is for only one rules file
60+
} elseif ($filePath -like '*.rules') {
61+
VM-Write-Log "INFO" "Rules file detected. Moving to $tempRuleSpecificFolder..."
62+
Move-Item -Path $filePath -Destination $tempRuleSpecificFolder
63+
64+
# any other types of url resource is unsupported
65+
} else {
66+
throw "Unsupported file type: '$filePath'. Only .zip and .rule are allowed."
67+
}
68+
} catch {
69+
VM-Write-Log "WARN" "Failed rule: $filePath. Cause: $($_.Exception.Message)"
70+
}
71+
}
72+
73+
$allRuleFiles = Get-ChildItem -Path $tempRuleDir -Recurse -File -Filter *.rules
74+
75+
$rulesConfigPath = Join-Path $toolDir "suricata.yaml" -Resolve
76+
$rulesConfig = Get-Content -Path $rulesConfigPath
77+
78+
# collect the list of all existent rules
79+
$rulesList = $rulesConfig -split "`n" | Where-Object { $_.Trim() -match '\.rules$' } | ForEach-Object { $_.TrimStart(' -').Trim() }
80+
81+
# index of the location in the yaml where `rule-files:` is specified
82+
$ruleFilesIndex = $null
83+
for ($i = 0; $i -lt $rulesConfig.Count; $i++) {
84+
if ($rulesConfig[$i] -match '^rule-files:$') {
85+
$ruleFilesIndex = $i
86+
break
87+
}
88+
}
89+
# If `rule-files:` was not found, throw an error
90+
if ($ruleFilesIndex -eq $null) {
91+
throw "Line with 'rule-files:' string not found in the config file."
92+
}
93+
94+
# move all rule files in temp rule folder to the suricata rule folder
95+
# add rules to `suricata.yaml`
96+
VM-Write-Log "INFO" "Moving rule-files to $rulesDir..."
97+
foreach ($ruleFile in $allRuleFiles){
98+
Move-Item -Path $ruleFile.FullName -Destination $rulesDir -Force
99+
if (-not ($rulesList -contains $ruleFile.Name)){
100+
$newRuleLine = " - $($ruleFile.Name)"
101+
$rulesConfig =
102+
$rulesConfig[0..$ruleFilesIndex] + # lines to `rule-files:` line
103+
$newRuleLine + # new rule line
104+
$rulesConfig[($ruleFilesIndex + 1)..($rulesConfig.Length - 1)] # lines after `rule-files:` line
105+
VM-Write-Log "INFO" "[+] Rule-file $($ruleFile.Name) added to $rulesDir. Added rule-file reference to config file."
106+
}
107+
else{
108+
VM-Write-Log "INFO" "[+] Rule-file $($ruleFile.Name) added to $rulesDir. Rule-file reference already exist in config file."
109+
}
110+
}
111+
112+
# Save the updated content back to the file
113+
$rulesConfig | Set-Content -Path $rulesConfigPath
114+
}
115+
catch{
116+
VM-Write-Log-Exception $_
117+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$ErrorActionPreference = 'Continue'
2+
Import-Module vm.common -Force -DisableNameChecking
3+
4+
$toolName = 'suricata'
5+
$category = VM-Get-Category($MyInvocation.MyCommand.Definition)
6+
7+
VM-Uninstall-With-Uninstaller $toolName $category "MSI" "/qn /norestart"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<rules>
3+
<rule name="emerging-all" url="https://rules.emergingthreats.net/open/suricata-7.0.3/emerging.rules.zip" innerFolder="rules"/>
4+
</rules>

0 commit comments

Comments
 (0)