-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-Search-UnifiedAuditLogIR.ps1
589 lines (543 loc) · 31.6 KB
/
15-Search-UnifiedAuditLogIR.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# Bitpusher
# \`._,'/
# (_- -_)
# \o/
# The Digital
# Fox
# https://theTechRelay.com
# https://github.com/bitpusher2k
#
# Search-UnifiedAuditLogIR.ps1 - By Bitpusher/The Digital Fox
# v2.8 last updated 2024-05-03
# Script to search the UAC for events particularly
# relevant to incident response.
#
# Usage:
# powershell -executionpolicy bypass -f .\Search-UnifiedAuditLogIR.ps1 -OutputPath "Default"
#
# Run with already existing connection to M365 tenant through
# PowerShell modules.
#
# Uses ExchangePowerShell commands.
#
#comp #m365 #security #bec #script #unified #audit #log #ir
#Requires -Version 5.1
param(
[string]$OutputPath,
[int]$DaysAgo,
[string]$Encoding = "utf8bom" # "ascii","ansi","bigendianunicode","unicode","utf8","utf8","utf8NoBOM","utf32"
)
if ($PSVersionTable.PSVersion.Major -eq 5 -and ($Encoding -eq "utf8bom" -or $Encoding -eq "utf8nobom")) { $Encoding = "utf8" }
# Initial variables
$resultSize = 5000 #Maximum number of records that can be retrieved per query
$date = Get-Date -Format "yyyyMMddHHmmss"
$CheckLog = (Get-AdminAuditLogConfig).UnifiedAuditLogIngestionEnabled
if (!$CheckLog) {
Write-Output "The Unified Audit Log does not appear to be enabled on this tenant. Export of UAL activities may fail. Try running 'Set-AdminAuditLogConfig -UnifiedAuditLogIngestionEnabled $true' if export fails."
}
## If OutputPath variable is not defined, prompt for it
if (!$OutputPath) {
Write-Output ""
$OutputPath = Read-Host "Enter the output base path, e.g. $($env:userprofile)\Desktop\Investigation (default)"
if ($OutputPath -eq '') { $OutputPath = "$($env:userprofile)\Desktop\Investigation" }
Write-Output "Output base path will be in $OutputPath"
} elseif ($OutputPath -eq 'Default') {
Write-Output ""
$OutputPath = "$($env:userprofile)\Desktop\Investigation"
Write-Output "Output base path will be in $OutputPath"
}
## If OutputPath does not exist, create it
$CheckOutputPath = Get-Item $OutputPath -ErrorAction SilentlyContinue
if (!$CheckOutputPath) {
Write-Output ""
Write-Output "Output path does not exist. Directory will be created."
mkdir $OutputPath
}
## Get Primary Domain Name for output subfolder
$PrimaryDomain = Get-AcceptedDomain | Where-Object Default -EQ $true
$DomainName = $PrimaryDomain.DomainName
$CheckSubDir = Get-Item $OutputPath\$DomainName -ErrorAction SilentlyContinue
if (!$CheckSubDir) {
Write-Output ""
Write-Output "Domain sub-directory does not exist. Sub-directory `"$DomainName`" will be created."
mkdir $OutputPath\$DomainName
}
Write-Output ""
Write-Output "Script will search the UAC for entries often relevant to incident investigation & response."
Write-Output "Reports of events in each of these categories will be created if associated events are found:"
Write-Output " * Entra ID Role Changes"
Write-Output " * Entra ID Application Changes"
Write-Output " * Conditional Access Policy Changes"
Write-Output " * Entra ID Domain Changes (Add/Remove, Federation, etc.)"
Write-Output " * Partner Management Changes"
Write-Output " * Users Added or Deleted"
Write-Output " * Password Resets and Changes"
Write-Output " * Update User Events (MFA reg & Security Info changes)"
Write-Output " * Devices Added Or Deleted"
Write-Output " * Exchange Admin Events (Inbox Rules, Mailbox Forwarding, Mailbox Permissions)"
Write-Output " * File Created or Modified"
Write-Output " * File Deleted"
Write-Output " * Mailbox Permission Events"
Write-Output " * External User Events"
Write-Output " * Anonymous Link Events"
Write-Output " * Email Deletion Events"
Write-Output ""
Write-Output "In the future this script may be split up into more targeted scripts which parse UAL output specific to the retrieved records."
Write-Output ""
if (!$DaysAgo) {
$DaysAgo = Read-Host "Enter number of days back to search in the Unified Audit Log (default: 30, max: 90)"
}
if ($DaysAgo -eq '') { $DaysAgo = "30" } elseif ($DaysAgo -gt "90") { $DaysAgo = "90" }
Write-Output "Will search UAC $DaysAgo days back from today for relevant events."
## Set Start and End Dates
$StartDate = (Get-Date).AddDays(- $DaysAgo)
$EndDate = Get-Date
## Get changes to membership in Entra ID roles (new adds could indicate escalation of privilege)
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add member to role.`", `"Remove member from role.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add member to role.", "Remove member from role.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output ""
Write-Output "There are no events matching Entra ID role changes for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_EntraIDRoleChanges_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output ""
Write-Output "See Entra ID Roles Changes events in the output path"
Write-Output ""
}
## Get changes to applications, client app credentials, permissions, and new consents (could indicate app abuse)
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add application.`", `"Add service principal.`", `"Add service principal credentials.`", `"Update application – Certificates and secrets`", `"Add app role assignment to service principal.`", `"Add app role assignment grant to user.`", `"Add delegated permission grant.", "Consent to application.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add application.", "Add service principal.", "Add service principal credentials.", "Update application – Certificates and secrets", "Add app role assignment to service principal.", "Add app role assignment grant to user.", "Add delegated permission grant.", "Consent to application.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no events matching Entra ID app changes for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_EntraIDAppChanges_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See Entra ID application events in the output path"
Write-Output ""
}
## Get Conditional Access policy changes
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add policy.`", `"Update policy.`", `"Delete policy.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add policy.", "Update policy.", "Delete policy.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no Conditional Access events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_ConditionalAccessPolicyChanges_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See Conditional Access Policy events in the output path"
Write-Output ""
}
## Get Domain changes
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add domain to company.`", `"Remove domain from company.`", `"Set domain authentication.`", `"Set federation settings on domain.`", `"Set DirSyncEnabled flag.`", `"Update domain.`", `"Verify domain.`", `"Verify email verified domain.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add domain to company.", "Remove domain from company.", "Set domain authentication.", "Set federation settings on domain.", "Set DirSyncEnabled flag.", "Update domain.", "Verify domain.", "Verify email verified domain.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no Domain Management events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_EntraIDDomainChanges_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See Domain Management events in the output path"
Write-Output ""
}
## Get Partner changes
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add partner to company.`", `"Remove partner from company.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add partner to company.", "Remove partner from company.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no Partner management events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_PartnerManagementChanges_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See Partner Management events in the output path"
Write-Output ""
}
## Get user add and delete events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add user.`", `"Delete user.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add user.", "Delete user.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no Users Added events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_UsersAddedOrDeleted_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See events matching 'Add user' and 'Delete user' in the output path"
Write-Output ""
}
## Get password changes
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Change user password.`", `"Reset user password.`", `"Set force change user password.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Change user password.", "Reset user password.", "Set force change user password.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no Password events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_PasswordResetsAndChanges_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See password events in the output path"
Write-Output ""
}
## Get user update events (this includes MFA registration / security info changes)
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Update user.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Update user.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no events matching 'Update user' for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv "$OutputPath\$DomainName\AuditLog_UpdateUser_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See events matching 'Update user' in the output path (this includes MFA method updates)"
Write-Output ""
}
## Get Entra ID Device add and delete events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations (`"Add device.`", `"Delete device.`") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType AzureActiveDirectory -Operations ("Add device.", "Delete device.") -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no events matching 'Add device' or 'Delete device' for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_DevicesAddedOrDeleted_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See events matching 'Add device' or 'Delete device' in the output path"
Write-Output ""
}
## Get Exchange admin log events (includes new inbox rules, mailbox forwarding, mailbox permissions, mailbox delegations, etc.)
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType ExchangeAdmin -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType ExchangeAdmin -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no Exchange admin events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_ExchangeAdminEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See Exchange Admin events in the output path"
Write-Output ""
}
## Get file creation & modification events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations `"Created,FileModified`" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "Created,FileModified" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no File Creation/Modification events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_FileCreatedModifiedEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See file creation events in the output path"
Write-Output ""
}
## Get file deletion events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations `"FileDeleted,FileDeletedFirstStageRecycleBin,FileDeletedSecondStageRecycleBin`" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "FileDeleted,FileDeletedFirstStageRecycleBin,FileDeletedSecondStageRecycleBin" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no file deletion events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_FileDeletedEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See file deletion events in the output path"
Write-Output ""
}
## Get Mailbox permission change events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations `"Add-RecipientPermission,Remove-RecipientPermission,Set-mailbox,Add-MailboxPermission,Remove-MailboxPermission`" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "Add-RecipientPermission,Remove-RecipientPermission,Set-mailbox,Add-MailboxPermission,Remove-MailboxPermission" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no mailbox permission events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_MailboxPermissionEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See mailbox permission events in the output path"
Write-Output ""
}
## Get all external user activity events
# For just file access events: $SearchResults = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations FileAccessed -UserIds "*#EXT*" -SessionCommand ReturnLargeSet -ResultSize $resultSize
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -UserIds `"*#EXT*`" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -UserIds "*#EXT*" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no external user events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_ExternalUserEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See external user events in the output path"
Write-Output ""
}
## Get anonymous link events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations `"AnonymousLinkRemoved,AnonymousLinkcreated,AnonymousLinkUpdated,AnonymousLinkUsed`" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "AnonymousLinkRemoved,AnonymousLinkcreated,AnonymousLinkUpdated,AnonymousLinkUsed" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no anonymous link events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_AnonymousLinkEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See anonymous link events in the output path"
Write-Output ""
}
## Get email deletion events
$sesid = Get-Random # Get random session number
Write-Output "Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations `"SoftDelete,HardDelete,MoveToDeletedItems`" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize"
$count = 1
do {
Write-Output "Getting unified audit logs page $count - Please wait"
try {
$currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -Operations "SoftDelete,HardDelete,MoveToDeletedItems" -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
} catch {
Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
Write-Output "Exception message:", $_.Exception.Message, "`n"
exit 2 # Terminate script
}
$SearchResults += $currentoutput # Build total results array
++ $count # Increment page count
} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
## Check to see if the variable is null
if (!$SearchResults) {
Write-Output "There are no email deletion events for the time period specified"
Write-Output ""
} else {
## Output the events to CSV
$SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_EmailDeletionEvents_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
Write-Output "See email deletion events in the output path"
Write-Output ""
}
# ## Get XXXXXX events template
#$sesid = Get-Random # Get random session number
#Write-Output "Search-UnifiedAuditLog..."
#$count = 1
#do {
# Write-Output "Getting unified audit logs page $count - Please wait"
# try {
# $currentOutput = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType XXXXXX -Operations XXXXXX -SessionId $sesid -SessionCommand ReturnLargeSet -ResultSize $resultSize
# } catch {
# Write-Output "`n[002] - Search Unified Log error. Typically not connected to Exchange Online. Please connect and re-run script`n"
# Write-Output "Exception message:", $_.Exception.Message, "`n"
# exit 2 # Terminate script
# }
# $SearchResults += $currentoutput # Build total results array
# ++ $count # Increment page count
#} until ($currentoutput.count -eq 0) # Until there are no more logs in range to get
#
# ## Check to see if the variable is null
# if (!$SearchResults) {
# Write-Output "There are no ... events for the time period specified"
# Write-Output ""
# } else {
# ## Output the events to CSV
# $SearchResults | Export-Csv -Path "$OutputPath\$DomainName\AuditLog_...Events_Past_$($DaysAgo)_Days_From_$($date).csv" -NoTypeInformation -Encoding $Encoding
# Write-Output "See ... events in the output path"
# Write-Output ""
# }
Write-Output "`nDone! Check output path for results."
Invoke-Item "$OutputPath\$DomainName"
exit