-
Notifications
You must be signed in to change notification settings - Fork 12
/
KITT.ps1
1760 lines (1584 loc) · 159 KB
/
KITT.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.NOTES
--------------------------------------------------------------------------------
Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.179
--------------------------------------------------------------------------------
.DESCRIPTION
GUI script generated by PowerShell Studio 2020
#>
#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Show-DFIRPowershellGUI_psf {
#----------------------------------------------
#region Import the Assemblies
#----------------------------------------------
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$MainForm = New-Object 'System.Windows.Forms.Form'
$labelEndDate = New-Object 'System.Windows.Forms.Label'
$LabelStartDate = New-Object 'System.Windows.Forms.Label'
$UserLabel = New-Object 'System.Windows.Forms.Label'
$UserProgressTextbox = New-Object 'System.Windows.Forms.RichTextBox'
$TextBoxUserUPN = New-Object 'System.Windows.Forms.TextBox'
$buttonSearch = New-Object 'System.Windows.Forms.Button'
$buttonExit = New-Object 'System.Windows.Forms.Button'
$SignInLogsTimePickerMax = New-Object 'System.Windows.Forms.DateTimePicker'
$tabcontrol1 = New-Object 'System.Windows.Forms.TabControl'
$UserTab = New-Object 'System.Windows.Forms.TabPage'
$buttonRevokeAzureADRefreshToken = New-Object 'System.Windows.Forms.Button'
$buttonResetUserPassword = New-Object 'System.Windows.Forms.Button'
$UserLicenseOutputTextbox = New-Object 'System.Windows.Forms.RichTextBox'
$UserOutputTextbox = New-Object 'System.Windows.Forms.RichTextBox'
$EmailTab = New-Object 'System.Windows.Forms.TabPage'
$buttonRemoveInboxRule = New-Object 'System.Windows.Forms.Button'
$RuleOutputTextbox = New-Object 'System.Windows.Forms.RichTextBox'
$RuleSelectionListbox = New-Object 'System.Windows.Forms.ListBox'
$LoginsTab = New-Object 'System.Windows.Forms.TabPage'
$buttonUseExistingUsername = New-Object 'System.Windows.Forms.Button'
$SignInStatsTextBox = New-Object 'System.Windows.Forms.RichTextBox'
$buttonViewUserFullLogs = New-Object 'System.Windows.Forms.Button'
$buttonDownloadUserTriageLogs = New-Object 'System.Windows.Forms.Button'
$buttonDownloadUserFullLogs = New-Object 'System.Windows.Forms.Button'
$buttonViewUserTriageLogs = New-Object 'System.Windows.Forms.Button'
$IPTab = New-Object 'System.Windows.Forms.TabPage'
$buttonViewIPFullLogs = New-Object 'System.Windows.Forms.Button'
$buttonViewIPTriageLogs = New-Object 'System.Windows.Forms.Button'
$buttonDownloadIPFullLogs = New-Object 'System.Windows.Forms.Button'
$buttonDownloadIPTriageLogs = New-Object 'System.Windows.Forms.Button'
$IPSearchTextBox = New-Object 'System.Windows.Forms.TextBox'
$IpStatsTextBox = New-Object 'System.Windows.Forms.RichTextBox'
$buttonUseExistingIP = New-Object 'System.Windows.Forms.Button'
$buttonUseNewIP = New-Object 'System.Windows.Forms.Button'
$SearchEmailTab = New-Object 'System.Windows.Forms.TabPage'
$label2 = New-Object 'System.Windows.Forms.Label'
$label1 = New-Object 'System.Windows.Forms.Label'
$labelMessageTraceCmdletOn = New-Object 'System.Windows.Forms.Label'
$MessageTraceTimePickerMax = New-Object 'System.Windows.Forms.DateTimePicker'
$MessageTraceTimePickerMin = New-Object 'System.Windows.Forms.DateTimePicker'
$MessageTraceStatsTextBox = New-Object 'System.Windows.Forms.RichTextBox'
$buttonViewMessageTraceLogs = New-Object 'System.Windows.Forms.Button'
$buttonDownloadMessageTrace = New-Object 'System.Windows.Forms.Button'
$buttonSearchMessageTrace = New-Object 'System.Windows.Forms.Button'
$FromIPLabel = New-Object 'System.Windows.Forms.Label'
$FromIPTextbox = New-Object 'System.Windows.Forms.TextBox'
$RecipientLabel = New-Object 'System.Windows.Forms.Label'
$RecipientTextbox = New-Object 'System.Windows.Forms.TextBox'
$SenderLabel = New-Object 'System.Windows.Forms.Label'
$SenderAddressTextbox = New-Object 'System.Windows.Forms.TextBox'
$SignInLogsTimePickerMin = New-Object 'System.Windows.Forms.DateTimePicker'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
# User Generated Script
#----------------------------------------------
<#
Welcome to KITT - This is a tool designed to make working O365 Business Email Compromise investigations easier and more efficient for DFIR and SOC analysts by pairing the power of PowerShell cmdlets with the ease of use of a GUI.
KITT was built using Sapien's PowerShell Studio. Dates are set to UTC. Powershell Cmdlet limitations limit the pulling of AzureAD SignIn Logs to 30 days, and Message Trace Logs to 10 days. By default, the timepickers are set to those values.
---------------------------------------------------------------------------
BEFORE YOU BEGIN:
1. You'll need to install the following PS modules in order to run this:
AzureADPreview - This is necessary as of 6/2020 because the preview version of AzureAD contains the Get-AzureADAuditSignInLogs cmdlet. You cannot have both AzureAD and AzureADPreview installed at the same time, so it is recommended to use this tool on a Dev system until Microsoft releases that cmdlet to the Prod version of AzureAD.
ExchangeOnlineManagement
MSOnline
2. Please search for "CHANGE_ME" to find two values you will need to change. These are:
1. You'll need to add your email address after Connect-ExchangeOnline -UserPrincipalName.
2. You'll need to change the variable $script:DomainName to be your domain. That will allow the script to get the password policy for your domain to calculate the password expiration date for a user.
---------------------------------------------------------------------------
Feedback can be left on my Github or sent to the following contact details:
@IntrepidTechie on Twitter
intrepidtechie@gmail.com
---------------------------------------------------------------------------
Please feel free to contribute to this tool by fixing bugs or providing feedback. I'm not a developer by trade, and would gladly accept feedback from seasoned devs/PowerShell Gurus.
Special thanks to my wonderful wife Kait for constantly supporting me.
Dedicated to @HumanMalware. RIP bro, you are gone too soon.
#>
#Loads PS Modules, sets dates of TimePickers, and starts a Transcript of the session. Analysts are asked where to save the transcript and what name to give it.
$MainForm_Load = {
#set default time values for the main TimePickers and MessageTrace TimePickers.
$SignInLogsTimePickerMin.MinDate = (Get-Date).AddDays(-30).ToUniversalTime()
$SignInLogsTimePickerMin.Value = (Get-Date).AddDays(-30).ToUniversalTime()
$SignInLogsTimePickerMin.MaxDate = (Get-Date).ToUniversalTime()
$SignInLogsTimePickerMax.MinDate = (Get-Date).AddDays(-30).ToUniversalTime()
$SignInLogsTimePickerMax.Value = (Get-Date).ToUniversalTime()
$SignInLogsTimePickerMax.MaxDate = (Get-Date).ToUniversalTime()
$MessageTraceTimePickerMin.MinDate = (Get-Date).AddDays(-10).ToUniversalTime()
$MessageTraceTimePickerMin.Value = (Get-Date).AddDays(-10).ToUniversalTime()
$MessageTraceTimePickerMin.MaxDate = (Get-Date).ToUniversalTime()
$MessageTraceTimePickerMax.MinDate = (Get-Date).AddDays(-10).ToUniversalTime()
$MessageTraceTimePickerMax.Value = (Get-Date).ToUniversalTime()
$MessageTraceTimePickerMax.MaxDate = (Get-Date).ToUniversalTime()
#Prompts analyst for location and filename to save a transcript of actions taken while running the tool.
$Timestamp = Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }
$TranscriptFileName = "Transcript_$($Timestamp)"
$TranscriptFileDialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
$TranscriptFileDialog.InitialDirectory = ".\"
$TranscriptFileDialog.FileName = $TranscriptFileName
$TranscriptFileDialog.DefaultExt = "txt"
$TranscriptFileDialog.Filter = "txt (*.txt)|*.txt|All files (*.*)|*.*"
$TranscriptFileDialog.Title = "Please choose a location to save the transcript of actions ran while this tool was used:"
if ($TranscriptFileDialog.ShowDialog() -eq 'OK')
{
Start-Transcript -Path $TranscriptFileDialog.FileName
Write-Host "Started Transcript"
}
#Sign into O365
AzureADPreview\Connect-AzureAD
Connect-ExchangeOnline -UserPrincipalName "CHANGE_ME"
Connect-MsolService
}
#This function is designed to pull and display basic information about the user searched for. Please add your Domain Name for the $script:DomainName variable. That variable is used to calculate the password expiration dates.
Function GetUserInformation
{
$script:DomainName = "CHANGE_ME"
#Pull User information
$UserProgressTextbox.Text = "Getting User Information for $script:UserUPN"
$UserInformation = Get-MsolUser -UserPrincipalName $script:UserUPN | Select-Object FirstName, LastName, Title, Department, StreetAddress, City, State, PostalCode, PhoneNumber, MobilePhone, Licenses, isLicensed
Write-Host "Analyst began pull of User Information for $script:UserUPN"
#If Property doesn't exist, set property value to "No Data"
$UserInformation | ForEach-Object {
foreach ($property in $_.PSObject.Properties)
{
if ($property.Value -eq $Null)
{
$property.Value = "No Data"
}
}
}
$UserSummary = "`r`nName: $($UserInformation.FirstName) $($UserInformation.LastName)`r`nTitle: $($UserInformation.Title)`r`nDepartment: $($UserInformation.Department)`r`nPhoneNumber: $($UserInformation.PhoneNumber)`r`nMobile Number: $($UserInformation.MobilePhone)`r`nAddress: `r`n`r`n$($UserInformation.StreetAddress)`r`n$($UserInformation.City), $($UserInformation.State) $($UserInformation.PostalCode)`r`n`r`n" | Out-String
#Pull user password information, compute password expiration date using password policy, and display user password information.
$UserProgressTextbox.Text = "Getting Password Information for $script:UserUPN"
$PasswordPolicy = Get-MsolPasswordPolicy -DomainName $script:DomainName
Write-Host "Analyst began pull of Password Policy"
$UserPasswordChangeDate = Get-MsolUser -UserPrincipalName $script:UserUPN | Select-Object LastPasswordChangeTimestamp
Write-Host "Analyst began pull of Last Password change for $UserUPN"
$UserPasswordChangeDateOutput = $UserPasswordChangeDate.LastPasswordChangeTimestamp | Out-String
$PasswordExpirationDate = $UserPasswordChangeDate.LastPasswordChangeTimestamp.AddDays($PasswordPolicy.ValidityPeriod) | Out-String
$UserOutputTextbox.Text = "User Information: `r`n $UserSummary"
$UserOutputTextbox.AppendText("User's password was last changed on: $($UserPasswordChangeDateOutput)`r`n")
$UserOutputTextbox.AppendText("User's password expires on: $PasswordExpirationDate`r`n")
#Display user license information, if user is licensed, sorted by ProvisioningStatus.
$UserProgressTextbox.Text = "Getting License Information for $script:UserUPN"
Write-Host "Analyst began pull of license information for $script:UserUPN"
if ($UserInformation.IsLicensed -eq $true)
{
$LicenseInfo = $UserInformation.Licenses
$UserLicenses = @()
for ($counter = 0; $counter -lt $LicenseInfo.Count; $counter++)
{
$UserLicenses += "License:" + $LicenseInfo[$counter].AccountSkuId + "`r`n"
$UserLicenses += $LicenseInfo[$counter].ServiceStatus | Sort-Object -Property ProvisioningStatus
$UserLicenses += ""
}
$UserLicenses = $UserLicenses | Out-String
$UserLicenseOutputTextbox.Text = $UserLicenses
}
else
{
$UserLicenseOutputTextbox.Text = "User is not Licensed."
}
#Pull user forwarding settings. These can be changed by an attacker to forward mail out of the organization.
$UserProgressTextbox.Text = "Getting Forwarding Settings for $script:UserUPN"
Write-Host "Analyst began pull of Forwarding settings for $script:UserUPN"
$UserForwardingSettings = Get-EXOMailbox -Identity $script:UserUPN | Select-Object ForwardingSMTPAddress, ForwardingAddress, DeliverToMailboxAndForward
$UserForwardingSettings | ForEach-Object {
foreach ($property in $_.PSObject.Properties)
{
if ($property.Value -eq $Null)
{
$property.Value = "Not set"
}
}
}
$UserOutputTextbox.AppendText("`r`nUser Forwarding settings:`r`nForwarding Address: $($UserForwardingSettings.ForwardingAddress)`r`nForwardingSMTPAddress: $($UserForwardingSettings.ForwardingSMTPAddress)`r`nDeliver to Mailbox and Forward: $($UserForwardingSettings.DeliverToMailboxandForward)")
$UserProgressTextbox.Text = "Done pulling User Information for $script:UserUPN"
Write-Host "Done pulling all information for $script:UserUPN"
}
#This function pulls and displays the user's inbox rules. These are often set by attackers in order to hide nefarious activity.
Function InvestigateInboxRules
{
$UserProgressTextbox.Text = "Getting Inbox Rules for $script:UserUPN"
$RuleOutputTextbox.Text = ""
$script:InboxRules = @(Get-InboxRule -Mailbox $script:UserUPN)
Write-Host "Analyst began pull of Inbox Rules for $script:UserUPN"
$RuleList = @()
$script:InboxRuleSummary = @()
#If any Inbox Rules are found, display their name, discription, and whether or not they are enabled.
if ($script:InboxRules.Count -ne 0)
{
for ($counter = 1; $counter -le $script:InboxRules.Count; $counter++)
{
$RuleList += "Rule " + $counter + ": " + $script:InboxRules[$counter - 1].Name + "`r`n"
}
$RuleSelectionListbox.Items.AddRange($RuleList)
$RuleSelectionListbox.SelectedIndex = 0
$script:RuleIndex = $RuleSelectionListbox.SelectedIndex
for ($counter = 0; $counter -le $script:InboxRules.Count; $counter++)
{
$script:InboxRuleSummary += $($script:InboxRules[$counter]) | Select-Object Name, Description, Enabled
}
$RuleSelectionListbox.SetSelected($script:RuleIndex, $true)
}
elseif ($script:InboxRules.Count -eq 0)
{
$RuleOutputTextbox.Text = "No Rules found for user specified."
}
$UserProgressTextbox.Text = "Done pulling Inbox Rules for $script:UserUPN"
Write-Host "Done pulling Inbox Rules for $script:UserUPN"
}
#This function pulls AzureSignIn logs for the user specified. It will show statistics about the signin logs in the panel on the left.
#You can also view and filter the logs using the "View" buttons as well as download them with the "Download" buttons.
#View and Download are broken up into two groups: Triage and Full. Full logs are exactly what is pulled, with all fields present.
#Triage logs are parsed to show only a few fields to make quick work of confirming a sign-in.
Function PullAzureADAuditSignInLogs
{
$UserProgressTextbox.Text = "Getting Azure AD Sign In Logs for User $script:UserUPN"
$script:FullUserLogs = @()
$script:FullUserLogs = Get-AzureADAuditSignInLogs -Filter "userPrincipalName eq '$script:UserUPN' and createdDateTime ge $script:MinTime and createdDateTime le $script:MaxTime"
Write-Host "Analyst began pull of AzureAD SignIn Logs"
#Cmdlet only pulls 1000 logs at a time. This will loop until all logs are pulled.
If ($script:FullUserLogs.Count -eq 1000)
{
do
{
$lastArrayItem = $script:FullUserLogs.Count - 1
$script:MaxTime = $script:FullUserLogs[$lastArrayItem].createdDateTime
$script:FullUserLogs += Get-AzureADAuditSignInLogs -Filter "userPrincipalName eq '$script:UserUPN' and createdDateTime ge $script:MinTime and createdDateTime lt $script:MaxTime"
}
While ($script:FullUserLogs.Count % 1000 -eq 0)
}
Write-Host $script:FullUserLogs.Count "pulled."
$script:TriageLogs = $script:FullUserLogs | Select-Object CreatedDateTime, UserDisplayName, UserPrincipalName, AppDisplayName, IpAddress, ClientAppUsed, Status, DeviceDetail, Location, AuthenticationProcessingDetails
$IPAddressCount = $script:FullUserLogs.IpAddress | Group-Object -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$BrowserCount = $script:FullUserLogs.DeviceDetail | Group-Object Browser, OperatingSystem -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$ApplicationCount = $script:FullUserLogs.AppDisplayName | Group-Object -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
If ($script:FullUserLogs.Count -eq 0)
{
$SignInStatsTextBox.Text = "No logs found for User during timeframe specified."
}
Else
{
$SignInStatsTextBox.Text = "Logs for User $($script:UserUPN):`r`n"
$SignInStatsTextBox.AppendText("Unique IP Addresses used for SignIn:`r`n")
$SignInStatsTextBox.AppendText($($IPAddressCount))
$SignInStatsTextBox.AppendText("`r`n")
$SignInStatsTextBox.AppendText("Device Details:`r`n")
$SignInStatsTextBox.AppendText($($BrowserCount))
$SignInStatsTextBox.AppendText("`r`n")
$SignInStatsTextBox.AppendText("Application Count:`r`n")
$SignInStatsTextBox.AppendText($($ApplicationCount))
}
$UserProgressTextbox.Text = "Done Pulling AzureAD SignIn Logs for $script:UserUPN"
Write-Host "Done Pulling AzureAD SignIn Logs for $script:UserUPN"
}
#This function pulls AzureSignIn logs for the IP specified. It will show statistics about the signin logs in the panel on the left.
#You can also view and filter the logs using the "View" buttons as well as download them with the "Download" buttons.
#View and Download are broken up into two groups: Triage and Full. Full logs are exactly what is pulled, with all fields present.
#Triage logs are parsed to show only a few fields to make quick work of confirming a sign-in.
Function SearchAzureADSignInLogsExistingIP
{
$UserProgressTextbox.Text = "Getting AzureAD SignIn Logs for $script:ExistingIP"
$script:FullIPLogs = @()
$script:FullIPLogs = Get-AzureADAuditSignInLogs -Filter "IpAddress eq '$script:ExistingIP' and createdDateTime ge $script:MinTime and createdDateTime le $script:MaxTime"
Write-Host "Analyst began pull of AzureAD SignIn Logs for $script:ExistingIP"
#Cmdlet only allows 1000 logs to be pulled at a time. This will loop until all logs are pulled.
If ($script:FullIPLogs.Count -eq 1000)
{
do
{
$lastArrayItem = $script:FullIPLogs.Count - 1
$script:MaxTime = $script:FullIPLogs[$lastArrayItem].createdDateTime
$script:FullIPLogs += Get-AzureADAuditSignInLogs -Filter "IpAddress eq '$script:ExistingIP' and createdDateTime ge $script:MinTime and createdDateTime lt $script:MaxTime"
}
While ($script:FullIPLogs.Count % 1000 -eq 0)
}
Write-Host $script:FullIPLogs.Count " logs pulled."
$script:TriageIPLogs = $script:FullIPLogs | Select-Object CreatedDateTime, UserDisplayName, UserPrincipalName, AppDisplayName, IpAddress, ClientAppUsed, Status, DeviceDetail, Location, AuthenticationProcessingDetails
If ($script:FullIPLogs.Count -ne 0)
{
$UserList = $script:FullIPLogs | Sort-Object -Property UserPrincipalName | Group-Object -Property UserPrincipalName -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$DeviceDetails = $script:FullIPLogs.DeviceDetail | Group-Object Browser, OperatingSystem -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$ApplicationCount = $script:FullIPLogs.AppDisplayName | Group-Object -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$IpStatsTextBox.Text = "List of Users from $($script:ExistingIP): `r`n"
$IpStatsTextBox.AppendText($($UserList))
$IpStatsTextBox.AppendText("`r`n")
$IpStatsTextBox.AppendText("Device Details:`r`n")
$IpStatsTextBox.AppendText($($DeviceDetails))
$IpStatsTextBox.AppendText("`r`n")
$IpStatsTextBox.AppendText("Application Details:`r`n")
$IpStatsTextBox.AppendText($($ApplicationCount))
}
elseif ($script:FullIPLogs.Count -eq 0)
{
$IpStatsTextBox.Text = "No logs found for IP and timeframe specified."
}
$UserProgressTextbox.Text = "Done pulling SignIn Logs for IP $script:ExistingIP"
Write-Host "Done pulling SignIn Logs for IP $script:ExistingIP"
}
#This function pulls AzureSignIn logs for the IP specified. It will show statistics about the signin logs in the panel on the left.
#You can also view and filter the logs using the "View" buttons as well as download them with the "Download" buttons.
#View and Download are broken up into two groups: Triage and Full. Full logs are exactly what is pulled, with all fields present.
#Triage logs are parsed to show only a few fields to make quick work of confirming a sign-in.
Function SearchAzureADSignInLogsNewIP
{
$UserProgressTextbox.Text = "Getting Azure AD SignIn Logs for $script:NewIP"
$script:FullIPLogs = @()
$script:FullIPLogs = Get-AzureADAuditSignInLogs -Filter "IpAddress eq '$script:NewIP' and createdDateTime ge $script:MinTime and createdDateTime le $script:MaxTime"
Write-Host "Analyst began pull of AzureAD SignIn Logs for $script:NewIP"
#Cmdlet only allows pulling of 1000 logs. This will loop until all logs are pulled.
If ($script:FullIPLogs.Count -eq 1000)
{
do
{
$lastArrayItem = $script:FullIPLogs.Count - 1
$script:MaxTime = $script:FullIPLogs[$lastArrayItem].createdDateTime
$script:FullIPLogs += Get-AzureADAuditSignInLogs -Filter "IpAddress eq '$script:NewIP' and createdDateTime ge $script:MinTime and createdDateTime lt $script:MaxTime"
}
While ($script:FullIPLogs.Count % 1000 -eq 0)
}
Write-Host $script:FullIPLogs.Count " logs pulled."
$script:TriageIPLogs = $script:FullIPLogs | Select-Object CreatedDateTime, UserDisplayName, UserPrincipalName, AppDisplayName, IpAddress, ClientAppUsed, Status, DeviceDetail, Location, AuthenticationProcessingDetails
If ($script:FullIPLogs.Count -ne 0)
{
$UserList = $script:FullIPLogs | Sort-Object -Property UserPrincipalName | Group-Object -Property UserPrincipalName -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$DeviceDetails = $script:FullIPLogs.DeviceDetail | Group-Object Browser, OperatingSystem -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$ApplicationCount = $script:FullIPLogs.AppDisplayName | Group-Object -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$IpStatsTextBox.Text = "List of Users from $($script:NewIP): `r`n"
$IpStatsTextBox.AppendText($($UserList))
$IpStatsTextBox.AppendText("`r`n")
$IpStatsTextBox.AppendText("Device Details:`r`n")
$IpStatsTextBox.AppendText($($DeviceDetails))
$IpStatsTextBox.AppendText("`r`n")
$IpStatsTextBox.AppendText("Application Details:`r`n")
$IpStatsTextBox.AppendText($($ApplicationCount))
}
Elseif ($script:FullIPLogs.Count -eq 0)
{
$IpStatsTextBox.Text = "No results found. Please try another search."
}
$UserProgressTextbox.Text = "Done pulling SignIn Logs for IP $script:NewIP"
Write-Host "Done pulling SignIn Logs for IP $script:NewIP"
}
#This function pulls Message Trace logs filtering on a variety of inputs. It will stop and alert the user if it reaches the cmdlet maximum of 1 Million logs.
#Pulls 5000 logs at a time, and will loop until all logs are retrieved.
Function SearchMessageTrace
{
$UserProgressTextbox.Text = "Pulling Message Trace Logs"
$MessageTraceStatsTextBox.Text = ""
$Filter = $null
$script:MessageTraceLogs = @()
$script:MessageTraceStartDate = $MessageTraceTimePickerMin.Value
$script:MessageTraceEndDate = $MessageTraceTimePickerMax.Value
#This horrendous mess is an if-else tree designed to only search for the parameters provided by the analyst.
If ($script:SenderAddress -eq $null -and $script:RecipientAddress -eq $null -and $script:MessageTraceFromIP -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -PageSize 5000
Write-Host "Analyst began Pull of Message Trace Logs with no parameters."
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs += Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -PageSize 5000
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
elseif ($script:RecipientAddress -eq $null -and $script:SenderAddress -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -FromIp $script:MessageTraceFromIP -PageSize 5000
Write-Host "Analyst began pull of Message Trace Logs using parameter $script:MessageTraceFromIP"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -FromIp $script:MessageTraceFromIP -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
elseif ($script:MessageTraceFromIP -eq $null -and $script:SenderAddress -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -RecipientAddress $script:RecipientAddress -PageSize 5000
Write-Host "Analyst began pull of Message Trace Logs using parameter $script:RecipientAddress"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -RecipientAddress $script:RecipientAddress -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
elseif ($script:RecipientAddress -eq $null -and $script:MessageTraceFromIP -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -PageSize 5000
Write-Host "Analyst began pull of Message Trace Logs using parameters $script:SenderAddress"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
elseif ($script:MessageTraceFromIP -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -RecipientAddress $script:RecipientAddress -PageSize 5000
Write-Host "Analyst began pull of Message Trace Logs using parameters of $script:SenderAddress and $script:RecipientAddress"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -RecipientAddress $script:RecipientAddress -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
elseif ($script:SenderAddress -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -RecipientAddress $script:RecipientAddress -FromIP $script:MessageTraceFromIP -PageSize 5000
Write-Host "Analyst began pull of Message Trace Logs using parameters $script:RecipientAddress and $script:MessageTraceFromIP"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -RecipientAddress $script:RecipientAddress -FromIP $script:MessageTraceFromIP -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
elseif ($script:RecipientAddress -eq $null)
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -FromIP $script:MessageTraceFromIP -PageSize 5000
Write-Host "Analyst began pull of Message Trace logs using parameters $script:SenderAddress and $script:MessageTraceFromIP"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -FromIP $script:MessageTraceFromIP -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
else
{
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -RecipientAddress $script:RecipientAddress -FromIP $script:MessageTraceFromIP -PageSize 5000
Write-Host "Analyst began pull of Message Trace Logs using parameters $script:SenderAddress and $script:RecipientAddress and $script:MessageTraceFromIP"
If ($script:MessageTraceLogs.Count -eq 5000)
{
do
{
$lastArrayItem = $script:MessageTraceLogs.Count - 1
$script:MessageTraceStartDate = $script:MessageTraceLogs[$lastArrayItem].Received
$script:MessageTraceLogs = Get-MessageTrace -StartDate $script:MessageTraceStartDate -EndDate $script:MessageTraceEndDate -SenderAddress $script:SenderAddress -RecipeientAddress $script:RecipientAddress -FromIP $script:MessageTraceFromIP -PageSize 5000
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
}
While ($script:MessageTraceLogs.Count % 5000 -eq 0 -and $script:MessageTraceLogs.Count -ne 1000000)
}
Write-Host @($script:MessageTraceLogs).Count " Message Trace Logs pulled"
If ($script:MessageTraceLogs.Count -eq 1000000)
{
$UserProgressTextbox.Text = "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
Write-Host "Pulled Cmdlet Maximum of 1M Logs. Recommend shorter timeframe"
}
}
#Sort and Display statistics about the Message Trace logs pulled.
If ($script:MessageTraceLogs.Count -ne 0)
{
$SenderList = $script:MessageTraceLogs | Sort-Object -Property SenderAddress | Group-Object -Property SenderAddress -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$RecipientList = $script:MessageTraceLogs | Sort-Object -Property RecipientAddress | Group-Object -Property RecipientAddress -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$DeliveryStatus = $script:MessageTraceLogs | Sort-Object -Property Status | Group-Object -Property Status -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$FromIPList = $script:MessageTraceLogs | Sort-Object -Property FromIP | Group-Object -Property FromIP -NoElement | Sort-Object Count -Descending | Format-Table -AutoSize | Out-String
$MessageTraceStatsTextBox.Text = "Message Trace Search Statistics:`r`n`r`n"
$MessageTraceStatsTextBox.AppendText("Total Logs: $($script:MessageTraceLogs.Count)`r`n")
$MessageTraceStatsTextBox.AppendText("List of Senders `r`n")
$MessageTraceStatsTextBox.AppendText($($SenderList))
$MessageTraceStatsTextBox.AppendText("`r`n")
$MessageTraceStatsTextBox.AppendText("Recipient List`r`n")
$MessageTraceStatsTextBox.AppendText($($RecipientList))
$MessageTraceStatsTextBox.AppendText("`r`n")
$MessageTraceStatsTextBox.AppendText("Delivery Overview:`r`n")
$MessageTraceStatsTextBox.AppendText($($DeliveryStatus))
$MessageTraceStatsTextBox.AppendText("From IP List:`r`n")
$MessageTraceStatsTextBox.AppendText($($FromIPList))
}
elseif ($script:MessageTraceLogs.Count -eq 0)
{
$MessageTraceStatsTextBox.Text = "0 logs found for input parameters. Please try other parameters."
}
$UserProgressTextbox.Text = "Done Pulling Message Trace Logs"
Write-Host "Done Pulling Message Trace Logs"
}
#If the analyst clicks a different Inbox Rule in the list, change the Rule information to match the selection.
$RuleSelectionListbox_SelectedIndexChanged = {
$script:RuleIndex = $RuleSelectionListbox.SelectedIndex
$RuleOutputTextbox.Text = "Name: $($script:InboxRuleSummary[$script:RuleIndex].Name)`r`n`r`nDescription: `r`n$($script:InboxRuleSummary[$script:RuleIndex].Description)`r`n`r`nEnabled: $($script:InboxRuleSummary[$script:RuleIndex].Enabled)"
}
#Stops Transcript upon clicking the Exit Button.
$buttonExit_Click = {
Get-PSSession | Remove-PSSession
Stop-Transcript
}
#Prompts the analyst with a Yes/No to ensure they actually want to change the user's password. If Yes, password is set to a randomly chosen password by the cmdlet and that password is displayed to the analyst.
$buttonResetUserPassword_Click = {
Write-Host "Analyst clicked Password Reset Button"
$response = [System.Windows.Forms.MessageBox]::Show('Reset Password for user: ' + $script:UserUPN, 'Reset Password?', 'YesNo')
if ($response -eq 'Yes')
{
$randompassword = Set-MsolUserPassword -UserPrincipalName $script:UserUPN -ForceChangePassword $true
[System.Windows.Forms.MessageBox]::Show('Password Set to: ' + $randompassword)
Write-Host "Analyst reset $script:UserUPN password"
}
if ($response -eq 'No')
{
[System.Windows.Forms.MessageBox]::Show('No Action Taken')
Write-Host "Analyst did not reset password for $script:UserUPN"
}
}
#Set earliest time searched to reflect value changed in the "Start Date" TimePicker. Also restricts the Min Date in the "End Date" TimePicker to reflect the analyst choice.
$SignInLogsTimePickerMin_ValueChanged = {
$script:MinTime = $SignInLogsTimePickerMin.Value.ToString("yyyy-MM-dd")
$SignInLogsTimePickerMax.MinDate = $SignInLogsTimePickerMin.Value
}
#Set latest time searched to reflect value changed in the "End Date" TimePicker. Also restricts the Max Date in the "Start Date" TimePicker to reflect the analyst choice.
$SignInLogsTimePickerMax_ValueChanged = {
$script:MaxTime = $SignInLogsTimePickerMax.Value.ToString("yyyy-MM-dd")
$SignInLogsTimePickerMin.MaxDate = $SignInLogsTimePickerMax.Value
}
#Allows analyst to View triage logs upon button click.
$buttonViewUserTriageLogs_Click = {
$script:TriageLogs | Out-GridView -Title "Triage Logs"
Write-Host "Analyst viewed Triage Logs"
}
#Prompts analyst for save location and file name before saving triage logs.
$buttonDownloadUserTriageLogs_Click = {
$UserSaveFileDialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
$UserSaveFileDialog.InitialDirectory = ".\"
$UserSaveFileDialog.FileName = "$($script:UserUPN)_TriageLogs_.csv"
$UserSaveFileDialog.DefaultExt = "csv"
$UserSaveFileDialog.Filter = "CSV (*.csv)|*.csv|All files (*.*)|*.*"
if ($UserSaveFileDialog.ShowDialog() -eq 'OK')
{
$script:TriageLogs | Export-Csv -Path $UserSaveFileDialog.FileName -NoTypeInformation
Write-Host "Analyst downloaded Triage Logs"
}
}
#Allows analyst to view full logs upon button click.
$buttonViewUserFullLogs_Click = {
$script:FullUserLogs | Out-GridView -Title "Full Logs"
Write-Host "Analyst viewed User SignIn Full Logs"
}
#Prompts analyst for save location and file name before saving full logs.
$buttonDownloadUserFullLogs_Click = {
$UserSaveFileDialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
$UserSaveFileDialog.InitialDirectory = ".\"
$UserSaveFileDialog.FileName = "$($script:UserUPN)_FullLogs_.csv"
$UserSaveFileDialog.DefaultExt = "csv"
$UserSaveFileDialog.Filter = "CSV (*.csv)|*.csv|All files (*.*)|*.*"
if ($UserSaveFileDialog.ShowDialog() -eq 'OK')
{
$script:FullUserLogs | Export-Csv -Path $UserSaveFileDialog.FileName -NoTypeInformation
Write-Host "Analyst downloaded User SignIn Full Logs"
}
}
#Allows analyst to chose IP from "Logins by User" tab to use for their search.
$buttonUseExistingIP_Click = {
$IP = $script:FullUserLogs | Select-Object IPAddress | Group-Object IpAddress -NoElement | Select-Object Name | Out-GridView -Title "Select an IP" -PassThru
If ($IP.Count -eq 0)
{
$IpStatsTextBox.Text = "No IPs found in User Tab or user canceled choice."
}
else
{
$script:ExistingIP = $IP.Name
$IPSearchTextBox.Text = $script:ExistingIP
SearchAzureADSignInLogsExistingIP
}
}
#Searches Message Trace upon analyst clicking button. Will also check IP value provided to ensure it is valid. This IP check works for IPv4 and IPv6 addresses.
$buttonSearchMessageTrace_Click = {
Write-Host "Analyst began search of Message Trace logs"
$script:MessageTraceFromIP = $null
$script:SenderAddress = $null
$script:RecipientAddress = $null
if ($SenderAddressTextbox.TextLength -ne 0)
{
$script:SenderAddress = $SenderAddressTextbox.Text
}
if ($RecipientTextbox.TextLength -ne 0)
{
$script:RecipientAddress = $RecipientTextbox.Text
}
if ($FromIPTextbox.TextLength -ne 0)
{
$text = $FromIPTextbox.Text
$IP = [ipaddress]$text
if ($IP.IPAddressToString)
{
$script:MessageTraceFromIP = $IP.IPAddressToString
}
}
If (!$IP.IPAddressToString -and $FromIPTextbox.TextLength -ne 0)
{
$MessageTraceStatsTextBox.Text = "Error - Please enter a valid IP."
}
elseif (!$script:MessageTraceFromIP -and !$script:SenderAddress -and !$script:RecipientAddress)
{
$MessageTraceStatsTextBox.Text = "Please Enter at least one search parameter."
}
else
{
SearchMessageTrace
}
}
#Allows analyst to view MessageTrace Logs upon clicking button.
$buttonViewMessageTraceLogs_Click = {
$script:MessageTraceLogs | Out-GridView
Write-Host "Analyst viewed Message Trace Logs"
}
#Prompts analyst for file name and location before saving MessageTrace Logs.
$buttonDownloadMessageTrace_Click = {
$UserSaveFileDialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
$UserSaveFileDialog.InitialDirectory = ".\"
$UserSaveFileDialog.FileName = "MessageTraceOutput_.csv"
$UserSaveFileDialog.DefaultExt = "csv"
$UserSaveFileDialog.Filter = "CSV (*.csv)|*.csv|All files (*.*)|*.*"
if($UserSaveFileDialog.ShowDialog() -eq 'OK'){
$script:MessageTraceLogs | Export-Csv -Path $UserSaveFileDialog.FileName -NoTypeInformation
Write-Host "Analyst downloaded Message Trace Logs"
}
}
#Allows analyst to remove Inbox rule if deemed malicious. Will prompt analyst for confirmation first before removing rule.
$buttonRemoveInboxRule_Click = {
Write-Host "Analyst clicked button to remove Inbox Rule "$script:InboxRules[$script:RuleIndex].Name
$response = [System.Windows.Forms.MessageBox]::Show('Remove Inbox Rule: ' + $script:InboxRules[$script:RuleIndex].Name, 'Remove Inbox Rule?', 'YesNo')
if ($response -eq 'Yes')
{
Remove-InboxRule -Mailbox $script:UserUPN -Identity $script:InboxRules[$script:RuleIndex].RuleIdentity -Force
[System.Windows.Forms.MessageBox]::Show('Rule Removed: ' + $script:InboxRules[$script:RuleIndex].Name)
Write-Host "Analyst Removed Inbox Rule "$script:InboxRules[$script:RuleIndex].Name
$RuleSelectionListbox.Items.Clear()
$script:InboxRuleSummary = @()
InvestigateInboxRules
}
if ($response -eq 'No')
{
[System.Windows.Forms.MessageBox]::Show('No Action Taken')
Write-Host "Analyst canceled action of removing Inbox Rule."
}
}
#Allows analyst to revoke AzureAD Refresh Token for a user. Will prompt analyst for confirmation. Please see Microsoft documentation for more information.
$buttonRevokeAzureADRefreshToken_Click = {
Write-Host "Analyst clicked Revoke AzureAD token button"
$response = [System.Windows.Forms.MessageBox]::Show('Revoke AzureAD Refresh Token for User:' + $script:UserUPN, 'Revoke AzureAD Refresh Token?', 'YesNo')
if ($response -eq 'Yes')
{
$User = Get-AzureADUser -ObjectId $script:UserUPN
try
{
Revoke-AzureADUserAllRefreshToken -ObjectId $User.ObjectId
[System.Windows.Forms.MessageBox]::Show('Token Revoked for user: ' + $script:UserUPN)
}
catch
{
[System.Windows.Forms.MessageBox]::Show('An Error Occured. Please try again later.')
}
Write-Host "Analyst revoked AzureAD Refresh Token"
}
if ($response -eq 'No')
{
[System.Windows.Forms.MessageBox]::Show('No Action Taken')
Write-Host "Analyst did not revoke AzureAD Refresh Token for $script:UserUPN"
}
}
#Allows analyst to search for a user that was found through an "Logins by IP" search.
$buttonUseExistingUsername_Click = {
$User = $script:FullIPLogs | Select-Object UserPrincipalName | Group-Object UserPrincipalName -NoElement | Select-Object Name | Out-GridView -Title "Select a User" -PassThru
If ($User.Count -eq 0)
{
$SignInStatsTextBox.Text = "No Users found in IP Tab or user canceled choice."
}
else
{
$TextBoxUserUPN.Text = $User.Name
$RuleSelectionListbox.Items.Clear()
$script:InboxRuleSummary = @()
$script:UserUPN = $TextBoxUserUPN.Text
GetUserInformation
InvestigateInboxRules
PullAzureADAuditSignInLogs
}
}
#Set earliest date searched to reflect value changed in the "Start Date" TimePicker for Message Trace. Also restricts the Min Date in the "End Date" TimePicker to reflect the analyst choice.
$MessageTraceTimePickerMin_ValueChanged = {
$script:MessageTraceStartDate = $MessageTraceTimePickerMin.Value.ToString("yyyy-MM-dd")
$MessageTraceTimePickerMax.MinDate = $MessageTraceTimePickerMin.Value
}
#Set latest date searched to reflect value changed in the "End Date" TimePicker for Message Trace. Also restricts the Max Date in the "Start Date" TimePicker to reflect the analyst choice.
$MessageTraceTimePickerMax_ValueChanged = {
$script:MessageTraceEndDate = $MessageTraceTimePickerMax.Value.ToString("yyyy-MM-dd")
$MessageTraceTimePickerMin.MaxDate = $MessageTraceTimePickerMax.Value
}
#Allows analyst to search "Logins by IP" using a fresh IP. This will do a check to ensure the IP is valid. It accepts IPv4 and IPv6 addresses.
$buttonUseNewIP_Click = {
$script:NewIP = $null
If ($IPSearchTextBox.TextLength -eq 0)
{
$IpStatsTextBox.Text = "Please enter an IP"
}
Else
{
$text = $IPSearchTextBox.Text
Write-Host $IP
try
{
$IP = [ipaddress]$text
$script:NewIP = $IP.IPAddressToString
SearchAzureADSignInLogsNewIP
}
catch
{
$IpStatsTextBox.Text = "Please enter a valid IP"
}
}
}
#Performs a Get-MsolUser -SearchString using analyst input. This will search for users matching the analyst input.
$buttonSearch_Click= {
Write-Host "Analyst attempted search using"$TextBoxUserUPN.Text
$RuleSelectionListbox.Items.Clear()
$UserLicenseOutputTextbox.Text = ""
$RuleOutputTextbox.Text = ""
$SignInStatsTextBox.Text = ""
$script:InboxRuleSummary = @()
$script:UserUPN = $TextBoxUserUPN.Text
$Users = Get-MsolUser -SearchString "$script:UserUPN"
#If no matches are found, alert the analyst.
if ($Users.Count -eq 0)
{
$UserOutputTextbox.Text = "No Users found for this search term."
$UserProgressTextbox.Text = "No Users found for this search term."
Write-Host "No users found for Analyst search term"
}
#If one match is found, begin pulling user information immediately.
elseif ($Users.Count -eq 1)
{
Write-Host "1 user match found. Beginning pull of user information for $script:UserUPN"
$script:UserUPN = $Users.UserPrincipalName
$TextBoxUserUPN.Text = "$script:UserUPN"
GetUserInformation
InvestigateInboxRules
PullAzureADAuditSignInLogs
}
#If two or more users match the input, allow the analyst to choose which one before procededing.
elseif ($Users.Count -ge 2)
{