-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathPosh-SysMon.psm1
317 lines (288 loc) · 11.5 KB
/
Posh-SysMon.psm1
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
# Load functions
. "$($PSScriptRoot)\Functions\Get-SysmonEventData.ps1"
. "$($PSScriptRoot)\Functions\Get-SysmonHashingAlgorithm.ps1"
. "$($PSScriptRoot)\Functions\Get-SysmonRule.ps1"
. "$($PSScriptRoot)\Functions\Get-SysmonRuleFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonConfiguration.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonCreateRemoteThreadFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonDriverLoadFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonFileCreateFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonFileCreateStreamHashFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonImageLoadFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonNetworkConnectFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonPipeFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonProcessAccessFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonProcessCreateFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonProcessTerminateFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonRawAccessReadFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonRegistryFilter.ps1"
. "$($PSScriptRoot)\Functions\New-SysmonWmiFilter.ps1"
. "$($PSScriptRoot)\Functions\Remove-SysmonRule.ps1"
. "$($PSScriptRoot)\Functions\Remove-SysmonRuleFilter.ps1"
. "$($PSScriptRoot)\Functions\Set-SysmonHashingAlgorithm.ps1"
. "$($PSScriptRoot)\Functions\Set-SysmonRule.ps1"
. "$($PSScriptRoot)\Functions\Get-SysmonConfiguration.ps1"
. "$($PSScriptRoot)\Functions\ConvertTo-SysmonXMLConfiguration.ps1"
. "$($PSScriptRoot)\Functions\ConvertFrom-SysmonBinaryConfiguration.ps1"
# Supporteted Sysmon schema versions.
$SysMonSupportedVersions = @(
'4.0'
'4.1'
)
# Table that maps schema version to Sysmon version.
$sysmonVerMap = @{
'2.0' = '3.0'
'3.0' = '4.0'
'3.1' = '4.11'
'3.2' = '5.0'
'3.3' = '6.0'
'3.4' = '6.1, 6.2'
'4.0' = '7.0'
'4.1' = '8.0'
}
function Get-RuleWithFilter
{
Param
(
[Parameter(Mandatory=$true)]
$Rules
)
foreach ($rule in $Rules)
{
$RuleObjOptions = [ordered]@{}
$RuleObjOptions['EventType'] = $Rule.Name
if ($Rule.onmatch -eq $null -or $Rule.onmatch -eq 'exclude')
{
$RuleObjOptions.Add('DefaultAction','Exclude')
}
else
{
$RuleObjOptions.Add('DefaultAction','Include')
}
# Process individual filters
$Nodes = $Rule.selectnodes('*')
if ($Nodes.count -eq 0)
{
$RuleObjOptions.add('Scope','All Events')
}
else
{
$RuleObjOptions.add('Scope','Filtered')
$Filters = @()
foreach ($Node in $Nodes)
{
$FilterObjProps = [ordered]@{}
$FilterObjProps['EventField'] = $Node.LocalName
$FilterObjProps['RuleName'] = $Node.Name
$FilterObjProps['Condition'] = &{if($Node.condition -eq $null){'is'}else{$Node.condition}}
$FilterObjProps['Value'] = $Node.'#text'
$FilterObj = New-Object -TypeName psobject -Property $FilterObjProps
$FilterObj.pstypenames.insert(0,'Sysmon.Rule.Filter')
$Filters += $FilterObj
}
$RuleObjOptions.add('Filters',$Filters)
}
$RuleObj = New-Object -TypeName psobject -Property $RuleObjOptions
$RuleObj.pstypenames.insert(0,'Sysmon.Rule')
$RuleObj
}
}
<#
.Synopsis
Creates a filter for an event field for an event type in a Sysmon XML configuration file.
.DESCRIPTION
Creates a filter for an event field for an event type in a Sysmon XML configuration file.
.EXAMPLE
New-nRuleFilter -Path .\pc_cofig.xml -EventType NetworkConnect -EventField image -Condition Is -Value 'iexplorer.exe' -Verbose
VERBOSE: No rule for NetworkConnect was found.
VERBOSE: Creating rule for event type with default action if Exclude
VERBOSE: Rule created succesfully
C:\PS>Get-GetSysmonRules -Path .\pc_cofig.xml -EventType NetworkConnect
EventType : NetworkConnect
Scope : Filtered
DefaultAction : Exclude
Filters : {@{EventField=image; Condition=Is; Value=iexplorer.exe}}
Create a filter to capture all network connections from iexplorer.exe.
#>
function New-RuleFilter
{
[CmdletBinding(DefaultParameterSetName = 'Path')]
Param
(
# Path to XML config file.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName='Path',
Position=0)]
[ValidateScript({Test-Path -Path $_})]
[string]$Path,
# Path to XML config file.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
ParameterSetName='LiteralPath',
Position=0)]
[ValidateScript({Test-Path -Path $_})]
[Alias('PSPath')]
[string]$LiteralPath,
# Event type to create filter for.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[ValidateSet('NetworkConnect', 'ProcessCreate', 'FileCreateTime',
'ProcessTerminate', 'ImageLoad', 'DriverLoad',
'CreateRemoteThread', 'ProcessAccess','RawAccessRead',
'FileCreate', 'RegistryEvent', 'FileCreateStreamHash',
'PipeEvent', 'WmiEvent',IgnoreCase = $false)]
[string]
$EventType,
# Event type to create filter for.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[ValidateSet('include', 'exclude')]
[string]
$OnMatch,
# Condition for filtering against and event field.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=3)]
[ValidateSet('Is', 'IsNot', 'Contains', 'Excludes', 'Image',
'BeginWith', 'EndWith', 'LessThan', 'MoreThan')]
[string]
$Condition,
# Event field to filter on.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=4)]
[string]
$EventField,
# Value of Event Field to filter on.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=5)]
[string[]]
$Value,
# Rule Name for the filter.
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$true)]
[string]
$RuleName
)
Begin{}
Process
{
# Check if the file is a valid XML file and if not raise and error.
try
{
switch($psCmdlet.ParameterSetName)
{
'Path'
{
[xml]$Config = Get-Content -Path $Path
$FileLocation = (Resolve-Path -Path $Path).Path
}
'LiteralPath'
{
[xml]$Config = Get-Content -LiteralPath $LiteralPath
$FileLocation = (Resolve-Path -LiteralPath $LiteralPath).Path
}
}
}
catch [Management.Automation.PSInvalidCastException]
{
Write-Error -Message 'Specified file does not appear to be a XML file.'
return
}
# Validate the XML file is a valid Sysmon file.
if ($Config.SelectSingleNode('//Sysmon') -eq $null)
{
Write-Error -Message 'XML file is not a valid Sysmon config file.'
return
}
if ($Config.Sysmon.schemaversion -notin $SysMonSupportedVersions)
{
Write-Error -Message 'This version of Sysmon Rule file is not supported.'
return
}
$Rules = $Config.SelectSingleNode('//Sysmon/EventFiltering')
# Select the proper condition string and make sure it is the proper case.
switch ($Condition)
{
'Is' {$ConditionString = 'is'}
'IsNot' {$ConditionString = 'is not'}
'Contains' {$ConditionString = 'contains'}
'Excludes' {$ConditionString = 'excludes'}
'Image' {$ConditionString = 'image'}
'BeginWith' {$ConditionString = 'begin with'}
'EndWith' {$ConditionString = 'end with'}
'LessThan' {$ConditionString = 'less than'}
'MoreThan' {$ConditionString = 'more than'}
Default {$ConditionString = 'is'}
}
# Check if the event type exists if not create it.
$RuleData = $Rules.SelectNodes("//EventFiltering/$($EventType)")
if($RuleData -eq $null)
{
Write-Error -Message "No rule for $($EventType) was found."
return
} # If only one element this will return null, more than one this will provide a value.
else
{
if ($RuleData.count -eq 1)
{
if ($RuleData.Attributes."#text" -eq $OnMatch)
{
Write-Verbose -Message 'Single node.'
Write-Verbose -Message "Creating filters for event type $($EventType)."
# For each value for the event type create a filter.
foreach($val in $value)
{
Write-Verbose -Message "Creating filter for event filed $($EventField) with condition $($Condition) for value $($val)."
$FieldElement = $Config.CreateElement($EventField)
$Filter = $RuleData.AppendChild($FieldElement)
if ($RuleName) {
$Filter.SetAttribute('name',$RuleName)
}
$Filter.SetAttribute('condition',$ConditionString)
$filter.InnerText = $val
$Config.Save($FileLocation)
}
}
else
{
write-error -Message "Event type $($EventType) with a on match condition of $($OnMatch) was not found."
return
}
}
else
{
Write-Verbose -Message 'Mutiple nodes.'
foreach ($rule in $RuleData)
{
if ($rule.onmatch -eq $OnMatch)
{
Write-Verbose -Message "Found rule for event type $($EventType) with $($OnMatch)"
Write-Verbose -Message "Creating filters for event type $($EventType)."
# For each value for the event type create a filter.
foreach($val in $value)
{
Write-Verbose -Message "Creating filter for event filed $($EventField) with condition $($Condition) for value $($val)."
$FieldElement = $Config.CreateElement($EventField)
$Filter = $rule.AppendChild($FieldElement)
if ($RuleName) {
$Filter.SetAttribute('name',$RuleName)
}
$Filter.SetAttribute('condition',$ConditionString)
$filter.InnerText = $val
$Config.Save($FileLocation)
}
$RuleData = $rule
}
}
}
}
Get-RuleWithFilter($RuleData)
}
End{}
}