1
+ function Copy-Sysmon1-to-4688 {
2
+ <#
3
+ . SYNOPSIS
4
+ Takes a savedsearches.conf and looks for rules based on Sysmon Event ID 1, then copies them to another file.
5
+ Then, replaces key values in the rules to work with Windows Security Event ID 4688.
6
+
7
+ . DESCRIPTION
8
+ Takes a savedsearches.conf and looks for rules based on Sysmon Event ID 1, then copies them to another file.
9
+ Then, replaces key values in the rules to work with Windows Security Event ID 4688.
10
+
11
+ . EXAMPLE
12
+ cd c:\path\to\savedsearches
13
+ Copy-Sysmon1-to-4688
14
+
15
+ . EXAMPLE
16
+ Copy-Sysmon1-to-4688 -inputFile c:\savedsearches.conf
17
+
18
+ . EXAMPLE
19
+ Copy-Sysmon1-to-4688 -inputFile c:\savedsearches.conf -outputFile c:\4688\savedsearches.conf
20
+
21
+
22
+ . NOTES
23
+ Updated: 2024-08-29
24
+
25
+ Contributing Authors:
26
+ Anthony Phipps
27
+
28
+ LEGAL: Copyright (C) 2024
29
+ This program is free software: you can redistribute it and/or modify
30
+ it under the terms of the GNU General Public License as published by
31
+ the Free Software Foundation, either version 3 of the License, or
32
+ (at your option) any later version.
33
+
34
+ This program is distributed in the hope that it will be useful,
35
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
36
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37
+ GNU General Public License for more details.
38
+
39
+ You should have received a copy of the GNU General Public License
40
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
41
+
42
+ . LINK
43
+
44
+ #>
45
+
46
+ [CmdletBinding ()]
47
+ param (
48
+ [Parameter ()]
49
+ [String ] $inputFile = " .\savedsearches.conf" ,
50
+
51
+ [Parameter ()]
52
+ [String ] $outputFile = " .\savedsearches_4688.conf"
53
+ )
54
+
55
+ begin {
56
+
57
+ $DateScanned = ((Get-Date ).ToUniversalTime()).ToString(" yyyy-MM-dd HH:mm:ssZ" )
58
+ Write-Information - InformationAction Continue - MessageData (" Started at {0}" -f $DateScanned )
59
+
60
+ $stopwatch = New-Object System.Diagnostics.Stopwatch
61
+ $stopwatch.Start ()
62
+ }
63
+
64
+ process {
65
+
66
+ # Define the string to search for
67
+ $searchString = " EventID=1 "
68
+
69
+ # Initialize a dynamic array (ArrayList) to hold the last two lines
70
+ $lineBuffer = New-Object System.Collections.ArrayList
71
+
72
+ # Clear or create the output file
73
+ New-Item - Path $outputFile - ItemType File - Force
74
+
75
+ # Read the input file line by line
76
+ Get-Content $inputFile | ForEach-Object {
77
+ $currentLine = $_
78
+
79
+ # Check if the current line contains the search string
80
+ if ($currentLine -match $searchString ) {
81
+ # Write the last two lines and the current line to the output file
82
+ foreach ($line in $lineBuffer ) {
83
+ Add-Content - Path $outputFile - Value $line
84
+ }
85
+ Add-Content - Path $outputFile - Value $currentLine
86
+ }
87
+
88
+ # Update the line buffer with the last two lines
89
+ if ($lineBuffer.Count -ge 2 ) {
90
+ $lineBuffer.RemoveAt (0 )
91
+ }
92
+ $lineBuffer.Add ($currentLine ) | Out-Null
93
+ }
94
+
95
+ # Replace values to match Security Log 4688
96
+ (Get-Content $outputFile ) | ForEach-Object {
97
+ # Apply the replacements to the line
98
+ $_ = $_ -replace ' Channel="Microsoft-Windows-Sysmon/Operational"' , ' Channel="Security"'
99
+ $_ = $_ -replace ' EventID=1 ' , ' EventID=4688 '
100
+ $_ = $_ -replace ' ParentImage' , ' ParentProcessName'
101
+ $_ = $_ -replace ' Image' , ' NewProcessName'
102
+ $_ = $_ -replace ' OR OriginalFileName=".*?"' , ' '
103
+ $_ = $_ -replace ' OriginalFileName=".*?"' , ' '
104
+ $_ = $_ -replace ' OR Product=".*?"' , ' '
105
+ $_ = $_ -replace ' Product=".*?"' , ' '
106
+ $_ = $_ -replace ' OR Company=".*?"' , ' '
107
+ $_ = $_ -replace ' Company=".*?"' , ' '
108
+ $_ = $_ -replace ' OR Description=".*?"' , ' '
109
+ $_ = $_ -replace ' Description=".*?"' , ' '
110
+ $_ = $_ -replace ' OR IntegrityLevel=".*?"' , ' '
111
+ $_ = $_ -replace ' IntegrityLevel=".*?"' , ' '
112
+ $_ = $_ -replace ' OR CurrentDirectory=".*?"' , ' '
113
+ $_ = $_ -replace ' CurrentDirectory=".*?"' , ' '
114
+
115
+ # Return the modified line
116
+ $_
117
+ } | Set-Content $outputFile
118
+
119
+ }
120
+
121
+ end {
122
+
123
+ $elapsed = $stopwatch.Elapsed
124
+
125
+ Write-Information - InformationAction Continue - MessageData (" Total time elapsed: {0}" -f $elapsed )
126
+ Write-Information - InformationAction Continue - MessageData (" Ended at {0}" -f ((Get-Date ).ToUniversalTime()).ToString(" yyyy-MM-dd HH:mm:ssZ" ))
127
+ }
128
+ }
0 commit comments