-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListAllCommunityRules.ps1
316 lines (271 loc) · 11.4 KB
/
ListAllCommunityRules.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
param(
[string]$TempFolder = (Join-Path -Path $PWD -ChildPath "temp/Azure-Sentinel"),
[string]$OutputCsv = (Join-Path -Path $PWD -ChildPath "temp/AzureSentinelRules.csv")
)
# Function to ensure powershell-yaml module is available
function Test-RequiredModules {
$moduleNames = @("powershell-yaml")
foreach ($moduleName in $moduleNames) {
try {
if (-not (Get-Module -ListAvailable -Name $moduleName)) {
Write-Verbose "PowerShell-Yaml module not found. Attempting to install..."
Install-Module -Name $moduleName -Force -Scope CurrentUser -ErrorAction Stop
Write-Verbose "PowerShell-Yaml module installed successfully."
}
else {
Write-Verbose "PowerShell-Yaml module is already installed."
}
Import-Module $moduleName -ErrorAction Stop
Write-Verbose "PowerShell-Yaml module imported successfully."
}
catch {
Write-Error "Failed to install or import the PowerShell-Yaml module: $_"
throw
}
}
}
# Ensure required modules are available
Test-RequiredModules
Function Get-YamlContent {
param(
[string]$filePath
)
try {
$content = Get-Content -LiteralPath $filePath -Raw
return $content | ConvertFrom-Yaml
}
catch {
Write-Error "Failed to parse YAML file: $filePath. Error: $_"
return $null
}
}
Function Process-YamlFile {
param(
[string]$filePath,
[hashtable]$existingRules
)
if ($filePath -like "*invalidFile.yaml") {
return $null
}
$yamlContent = Get-YamlContent -filePath $filePath
if ($null -eq $yamlContent -or $null -eq $yamlContent.id -or [string]::IsNullOrEmpty($yamlContent.name) -or [string]::IsNullOrEmpty($yamlContent.query)) {
return $null
}
elseif ([String]$yamlContent.name -match "\[Deprecated\]") {
return $null
}
$type = switch ($yamlContent.kind) {
"scheduled" { "Scheduled Rules" }
"nrt" { "NRT Rules" }
default { "Hunting Rules" }
}
# Generate the correct GitHub link
$relativePath = $filePath -replace [regex]::Escape("$TempFolder\"), ""
$relativePath = $relativePath -replace '\\', '/'
$link = "https://github.com/Azure/Azure-Sentinel/blob/master/$relativePath"
$isNewRule = $false
# Check if the rule already exists to preserve the "Added" date and "CurrentlyEnabled" state
if ($existingRules.ContainsKey($yamlContent.id)) {
$existingRule = $existingRules[$yamlContent.id]
$addedDate = $existingRule.Added
$currentlyEnabled = $existingRule.CurrentlyEnabled
}
else {
$addedDate = Get-Date
$currentlyEnabled = $false
$isNewRule = $true # Mark this rule as new
}
# Handle Entity Mappings
$entityMappings = ""
if ($yamlContent.entityMappings) {
foreach ($mapping in $yamlContent.entityMappings) {
$entityType = $mapping.entityType
$entityMappings += "entityType: $entityType, fieldMappings: "
$fieldMappings = @()
foreach ($fieldMapping in $mapping.fieldMappings) {
$fieldMappings += "$($fieldMapping.identifier): $($fieldMapping.columnName)"
}
$entityMappings += ($fieldMappings -join ', ') + "; "
}
}
$tags = @{}
if ($yamlContent.tags) {
$i = 1
foreach ($tag in $yamlContent.tags) {
foreach ($tagKey in $tag.Keys) {
$tags["Tag${i}_${tagKey}"] = $tag.$tagKey
}
$i++
}
}
# Flatten incident configuration
$incidentConfig = @{}
if ($yamlContent.incidentConfiguration) {
$incidentConfig["IncidentConfiguration_CreateIncident"] = $yamlContent.incidentConfiguration.createIncident
if ($yamlContent.incidentConfiguration.groupingConfiguration) {
$groupConfig = $yamlContent.incidentConfiguration.groupingConfiguration
$incidentConfig["IncidentConfiguration_Grouping_Enabled"] = $groupConfig.enabled
$incidentConfig["IncidentConfiguration_Grouping_ReopenClosedIncident"] = $groupConfig.reopenClosedIncident
$incidentConfig["IncidentConfiguration_Grouping_LookbackDuration"] = $groupConfig.lookbackDuration
$incidentConfig["IncidentConfiguration_Grouping_MatchingMethod"] = $groupConfig.matchingMethod
$incidentConfig["IncidentConfiguration_Grouping_GroupByEntities"] = $groupConfig.groupByEntities -join ', '
$incidentConfig["IncidentConfiguration_Grouping_GroupByAlertDetails"] = $groupConfig.groupByAlertDetails -join ', '
$incidentConfig["IncidentConfiguration_Grouping_GroupByCustomDetails"] = $groupConfig.groupByCustomDetails -join ', '
}
}
# Flatten event grouping settings
$eventGrouping = @{}
if ($yamlContent.eventGroupingSettings) {
$eventGrouping["EventGroupingSettings_AggregationKind"] = $yamlContent.eventGroupingSettings.aggregationKind
}
# Flatten alert details override
$alertDetails = @{}
if ($yamlContent.alertDetailsOverride) {
$alertDetails["AlertDetailsOverride_AlertDescriptionFormat"] = $yamlContent.alertDetailsOverride.alertDescriptionFormat
if ($yamlContent.alertDetailsOverride.alertDynamicProperties) {
$i = 1
foreach ($property in $yamlContent.alertDetailsOverride.alertDynamicProperties) {
$alertDetails["AlertDetailsOverride_Property${i}_AlertProperty"] = $property.alertProperty
$alertDetails["AlertDetailsOverride_Property${i}_Value"] = $property.value
$i++
}
}
}
# Flatten custom details into comma-separated key-value pairs
$customDetails = ""
if ($yamlContent.customDetails) {
$customDetailsPairs = @()
foreach ($key in $yamlContent.customDetails.Keys) {
$customDetailsPairs += "$($key): $($yamlContent.customDetails[$key])"
}
$customDetails = $customDetailsPairs -join ', '
}
# Convert metadata to JSON string for easier storage in CSV
$metadataJson = ""
if ($yamlContent.metadata) {
$metadataJson = $yamlContent.metadata | ConvertTo-Json -Compress
}
# Flatten alert details override into a JSON string for easier storage in CSV
$alertDetailsOverrideJson = ""
if ($yamlContent.alertDetailsOverride) {
$alertDetailsOverrideJson = $yamlContent.alertDetailsOverride | ConvertTo-Json -Compress
}
$friendlyName = $yamlContent.name -replace '[^\w\-\.]', '_'
$friendlyName = $friendlyName.Substring(0, [Math]::Min($friendlyName.Length, 50))
$rule = @{
Id = $yamlContent.id
CurrentlyEnabled = $currentlyEnabled
Name = $yamlContent.name
Description = $yamlContent.description
FriendlyName = $friendlyName
Type = $type
Added = $addedDate
Link = $link
Tactics = $yamlContent.tactics -join ', '
RelevantTechniques = $yamlContent.relevantTechniques -join ', '
Severity = $yamlContent.severity
QueryFrequency = $yamlContent.queryFrequency
QueryPeriod = $yamlContent.queryPeriod
Query = $yamlContent.query
TriggerOperator = $yamlContent.triggerOperator
TriggerThreshold = $yamlContent.triggerThreshold
SuppressionEnabled = $yamlContent.suppressionEnabled
SuppressionDuration = $yamlContent.suppressionDuration
RequiredDataConnectors = ($yamlContent.requiredDataConnectors | ForEach-Object { "$($_.connectorId): $($_.dataTypes -join ', ')" }) -join '; '
Version = $yamlContent.version
EntityMappings = $entityMappings.TrimEnd("; ")
CustomDetails = $customDetails
Metadata = $metadataJson
AlertDetailsOverride = $alertDetailsOverrideJson
} + $tags + $incidentConfig + $eventGrouping
if ($isNewRule) {
Write-Host "New rule added to CSV with Id: $($yamlContent.id)"
}
return $rule
}
Function Search-AzureSentinelRepo {
param(
[string]$repoDirectory,
[hashtable]$existingRules
)
$newRulesList = @()
$foundFiles = Get-ChildItem -Path $repoDirectory -Recurse -File -Filter *.yaml
foreach ($foundFile in $foundFiles) {
$rule = Process-YamlFile -filePath $foundFile.FullName -existingRules $existingRules
if ($null -ne $rule) {
$newRulesList += $rule
}
}
return $newRulesList
}
Function Export-RulesToCsv {
param(
[array]$rulesList,
[string]$csvPath
)
if ($rulesList.Count -eq 0) {
Write-Host "No valid rules found to export. Skipping CSV export."
return
}
$csvData = $rulesList | ForEach-Object {
[PSCustomObject]@{
Id = $_.Id
CurrentlyEnabled = $_.CurrentlyEnabled
Name = $_.Name
Description = $_.Description
FriendlyName = $_.FriendlyName
Type = $_.Type
Added = $_.Added
Link = $_.Link
Tactics = $_.Tactics
RelevantTechniques = $_.RelevantTechniques
Severity = $_.Severity
QueryFrequency = $_.QueryFrequency
QueryPeriod = $_.QueryPeriod
Query = $_.Query
TriggerOperator = $_.TriggerOperator
TriggerThreshold = $_.TriggerThreshold
SuppressionEnabled = $_.SuppressionEnabled
SuppressionDuration = $_.SuppressionDuration
RequiredDataConnectors = $_.RequiredDataConnectors
Version = $_.Version
EntityMappings = $_.EntityMappings
CustomDetails = $_.CustomDetails
Metadata = $_.Metadata
AlertDetailsOverride = $_.AlertDetailsOverride
}
}
# Export to CSV, ensuring column order
$csvData | Export-Csv -Path $csvPath -NoTypeInformation
Write-Host "$($rulesList.Count) rules exported to $csvPath"
}
Function Import-ExistingRules {
param(
[string]$csvPath
)
if (-not (Test-Path $csvPath)) {
return @{}
}
$csvContent = Import-Csv -Path $csvPath
$existingRules = @{}
foreach ($rule in $csvContent) {
$existingRules[$rule.Id] = $rule
}
return $existingRules
}
# Ensure the Azure-Sentinel directory exists and is up-to-date
if (Test-Path $TempFolder) {
Push-Location $TempFolder
git pull
Pop-Location
}
else {
# temporary limit the download
git clone --filter=blob:limit=13k https://github.com/Azure/Azure-Sentinel.git $TempFolder
}
# Import existing rules from the CSV if it exists
$existingRules = Import-ExistingRules -csvPath $OutputCsv
# Search the Azure Sentinel GitHub repository
$newRulesList = Search-AzureSentinelRepo -repoDirectory (Join-Path -Path $TempFolder -ChildPath "Azure-Sentinel/Solutions") -existingRules $existingRules
# Export the rules to a CSV file
Export-RulesToCsv -rulesList $newRulesList -csvPath $OutputCsv