forked from jepayneMSFT/WEFFLES
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WEFFLESreporting.ps1
277 lines (209 loc) · 15.8 KB
/
WEFFLESreporting.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
# Download the bootstrapper for sysmon install/config
(New-Object System.Net.WebClient).DownloadFile('https://github.com/mdavis332/sysmon-config/blob/master/Install%20Sysmon.bat','C:\ProgramData\sysmon\InstallSysmon.bat')"
# Install sysmon and the config file to begin logging events on this server
& 'C:\ProgramData\sysmon\InstallSysmon.bat'
$workingdir = "c:\weffles"
cd $workingdir
#The following line was needed on a 2008 R2 setup at one customer It shouldn't hurt having it here however this can be removed in some cases
Add-Type -AssemblyName System.Core
#Following added to unregister any existing watcher Event in case script has already been run
Unregister-Event weffles -ErrorAction SilentlyContinue
Remove-Job weffles -ErrorAction SilentlyContinue
Import-Module .\EventLogWatcher.psm1
#Verify Bookmark currently exists prior to setting as start point
$TestStream = $null
$ECName = (((Get-Content .\bookmark.stream)[1]) -split "'")[1]
$ERId = (((Get-Content .\bookmark.stream)[1]) -split "'")[3]
$TestStream = Get-WinEvent -LogName $ECName -FilterXPath "*[System[(EventRecordID=$ERID)]]"
If ($TestStream -eq $null) {Remove-Item .\bookmark.stream}
$BookmarkToStartFrom = Get-BookmarkToStartFrom
$EventLogQuery = New-EventLogQuery "ForwardedEvents" -Query "*[System[EventID=4964]] or *[System[EventID=4624]] or *[System[EventID=4625]] or *[System[EventID=4648]]
or *[System[EventID=1102]] or *[System[EventID=7045]] or *[System[EventID=4720]] or *[System[EventID=106]] or *[System[EventID=200]] or *[System[EventID=4740]]"
$EventLogWatcher = New-EventLogWatcher -EventLogQuery $EventLogQuery -BookmarkToStartFrom $BookmarkToStartFrom
# $EventLogQuery $BookmarkToStartFrom
$action = {
#Following is debug line while developing as it will let you enter the nested prompt/session where you
#can actually query the $EventRecord / $EventRecordXML etc for troubleshooting
#$host.EnterNestedPrompt()
$outfile = "c:\weffles\weffles.csv"
<#
The Add-Member items below are what read out the portions of the XML of the event record and add them to the csv file.
Not all Event IDs have the same record data, so as you can see below we customize which fields we read out and add to the csv as text.
If you want to add new Events in the future, find a relevant record in Event Viewer, and click on the details tab and then XML view of the event, and you'll
be able to see what the labels are for all the fields of the event. You just add them to the Add-Member area below, and then create a new If statement section for your event.
#>
#Creating object to output to .csv
$EventObj = New-Object psobject
#Adding Date in Short Format to .csv object
$EventObj | Add-Member noteproperty EventDate $EventRecord.TimeCreated.ToShortDateString()
#Adding Computername to .csv object
$EventObj | Add-Member noteproperty EventHost $EventRecord.MachineName
#Adding EventID to .csv object
$EventObj | Add-Member noteproperty EventID $EventRecord.ID
#Adding TargetUserName to .csv object
$EventObj | Add-Member noteproperty TargetUserName $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
#Adding TargetUserName to .csv object
$EventObj | Add-Member noteproperty SamAccountName $EventRecordXml.SelectSingleNode("//*[@Name='SamAccountName']")."#text"
#Adding LogonType to .csv object
$EventObj | Add-Member noteproperty LogonType $EventRecordXml.SelectSingleNode("//*[@Name='LogonType']")."#text"
#Adding TargetDomainName to .csv object
$EventObj | Add-Member noteproperty TargetDomainName $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
#Adding SubjectUserName to .csv object
$EventObj | Add-Member noteproperty SubjectUserName $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
#Adding SubjectDomainName to .csv object
$EventObj | Add-Member noteproperty SubjectDomainName $EventRecordXml.SelectSingleNode("//*[@Name='SubjectDomainName']")."#text"
#Adding IpAddress to .csv object
$EventObj | Add-Member noteproperty IpAddress $EventRecordXml.SelectSingleNode("//*[@Name='IpAddress']")."#text"
#Adding WorkstationName to .csv object
$EventObj | Add-Member noteproperty WorkstationName $EventRecordXml.SelectSingleNode("//*[@Name='WorkstationName']")."#text"
#Adding ProcessName to .csv object
$EventObj | Add-Member noteproperty ProcessName $EventRecordXml.SelectSingleNode("//*[@Name='ProcessName']")."#text"
#Adding AuthenticationPackageName to .csv object
$EventObj | Add-Member noteproperty AuthenticationPackageName $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
#Adding Message body to .csv object
$EventObj | Add-Member noteproperty Message $EventRecordXml.SelectSingleNode("//*[@Name='Message']")."#text"
#Adding ActionName to .csv object
$EventObj | Add-Member noteproperty ActionName $EventRecordXml.SelectSingleNode("//*[@Name='ActionName']")."#text"
#Adding ServiceName to .csv object
$EventObj | Add-Member noteproperty ServiceName $EventRecordXml.SelectSingleNode("//*[@Name='ServiceName']")."#text"
#Adding ImagePath to .csv object
$EventObj | Add-Member noteproperty ImagePath $EventRecordXml.SelectSingleNode("//*[@Name='ImagePath']")."#text"
#Adding ServiceType to .csv object
$EventObj | Add-Member noteproperty ServiceType $EventRecordXml.SelectSingleNode("//*[@Name='ServiceType']")."#text"
#Adding StartType to .csv object
$EventObj | Add-Member noteproperty StartType $EventRecordXml.SelectSingleNode("//*[@Name='StartType']")."#text"
#Adding AccountName to .csv object
$EventObj | Add-Member noteproperty AccountName $EventRecordXml.SelectSingleNode("//*[@Name='AccountName']")."#text"
#Adding Computer to .csv object
$EventObj | Add-Member noteproperty Computer $EventRecordXml.SelectSingleNode("//*[@Name='Computer']")."#text"
#Adding UserID to .csv object
$EventObj | Add-Member noteproperty UserID $EventRecord.UserID
#Adding Action type after resolving EventID -> Action name to .csv object
#Adding 4624 logon success events to our csv
If ($EventRecord.ID -eq '4624')
{
$hash = New-Object psobject -Property @{
LogonType = $EventRecordXml.SelectSingleNode("//*[@Name='LogonType']")."#text"
IpAddress = $EventRecordXml.SelectSingleNode("//*[@Name='IpAddress']")."#text"
WorkstationName = $EventRecordXml.SelectSingleNode("//*[@Name='WorkstationName']")."#text"
ProcessName = $EventRecordXml.SelectSingleNode("//*[@Name='ProcessName']")."#text"
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectDomainName']")."#text"
AuthenticationPackageName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
TargetUserName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
TargetDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
};
}
#Adding 4625 logon failed events to our csv
If ($EventRecord.ID -eq '4625')
{
$hash = New-Object psobject -Property @{
LogonType = $EventRecordXml.SelectSingleNode("//*[@Name='LogonType']")."#text"
IpAddress = $EventRecordXml.SelectSingleNode("//*[@Name='IpAddress']")."#text"
WorkstationName = $EventRecordXml.SelectSingleNode("//*[@Name='WorkstationName']")."#text"
ProcessName = $EventRecordXml.SelectSingleNode("//*[@Name='ProcessName']")."#text"
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectDomainName']")."#text"
AuthenticationPackageName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
TargetUserName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
TargetDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
FailureReason = $EventRecordXml.SelectSingleNode("//*[@Name='FailureReason']")."#text"
};
}
#Adding 4648 logon with explict credentials to our csv
If ($EventRecord.ID -eq '4648')
{
$hash = New-Object psobject -Property @{
IpAddress = $EventRecordXml.SelectSingleNode("//*[@Name='IpAddress']")."#text"
WorkstationName = $EventRecordXml.SelectSingleNode("//*[@Name='WorkstationName']")."#text"
ProcessName = $EventRecordXml.SelectSingleNode("//*[@Name='ProcessName']")."#text"
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectDomainName']")."#text"
TargetUserName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
TargetDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
TargetServerName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetServerName']")."#text"
};
}
#Adding 4964 Special Logon events to our csv
#see https://blogs.technet.microsoft.com/jepayne/2015/11/26/tracking-lateral-movement-part-one-special-groups-and-specific-service-accounts/ for use
If ($EventRecord.ID -eq '4964')
{
$hash = New-Object psobject -Property @{
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
AuthenticationPackageName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
TargetUserName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
TargetDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
SidList = $EventRecordXml.SelectSingleNode("//*[@Name='SidList']")."#text"
};
}
#Adding 1102 event log cleared to our csv
If ($EventRecord.ID -eq '1102')
{
$hash = New-Object psobject -Property @{
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
};
}
#Adding 106 new scheduled task registered to our csv
If ($EventRecord.ID -eq '106')
{
$hash = New-Object psobject -Property @{
Message = $EventRecordXml.SelectSingleNode("//*[@Name='Message']")."#text"
};
}
#Adding 200 scheduled task execution events to our csv
If ($EventRecord.ID -eq '200')
{
$hash = New-Object psobject -Property @{
ActionName = $EventRecordXml.SelectSingleNode("//*[@Name='ActionName']")."#text"
};
}
#Adding 7045 new service creations to our csv
If ($EventRecord.ID -eq '7045')
{
$hash = New-Object psobject -Property @{
ServiceName = $EventRecordXml.SelectSingleNode("//*[@Name='ServiceName']")."#text"
ImagePath = $EventRecordXml.SelectSingleNode("//*[@Name='ImagePath']")."#text"
ServiceType = $EventRecordXml.SelectSingleNode("//*[@Name='ServiceType']")."#text"
StartType = $EventRecordXml.SelectSingleNode("//*[@Name='StartType']")."#text"
AccountName = $EventRecordXml.SelectSingleNode("//*[@Name='AccountName']")."#text"
};
}
#Adding 4740 account lockouts to our csv
If ($EventRecord.ID -eq '4740')
{
$hash = New-Object psobject -Property @{
TargetUserName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
TargetDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
};
}
#Adding 4720 new local user created events to our csv
ElseIf ($EventRecord.ID -eq '4720')
{
$hash = New-Object psobject -Property @{
SubjectUserName = $EventRecordXml.SelectSingleNode("//*[@Name='SubjectUserName']")."#text"
SubjectDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='AuthenticationPackageName']")."#text"
TargetUserName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetUserName']")."#text"
TargetDomainName = $EventRecordXml.SelectSingleNode("//*[@Name='TargetDomainName']")."#text"
SamAccountName = $EventRecordXml.SelectSingleNode("//*[@Name='SamAccountName']")."#text"
};
}
If ($Outfile -ne $Null)
{
$EventObj | Convertto-CSV -Outvariable OutData -NoTypeInformation
$OutPath = $Outfile
If (Test-Path $OutPath)
{
$Outdata[1..($Outdata.count - 1)] | ForEach-Object {Out-File -InputObject $_ $OutPath -append default}
}
else
{
Out-File -InputObject $Outdata $OutPath -Encoding default
}
}
}
Register-EventRecordWrittenEvent $EventLogWatcher -action $action -SourceIdentifier weffles
$EventLogWatcher.Enabled = $True