-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathInvoke-O365Ninja.ps1
1076 lines (819 loc) · 42.6 KB
/
Invoke-O365Ninja.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
#====================================#
# Office 365 Ninja #
# LogRhythm Security Operations #
# greg . foss @ logrhythm . com #
# v2.0 -- August, 2018 #
#====================================#
# Copyright 2018 LogRhythm Inc.
# Licensed under the MIT License. See LICENSE file in the project root for full license information.
function Invoke-O365Ninja {
<#
SYNOPSIS:
Collection of useful commands for easy integration with Office 365 and the LogRhythm SIEM
Automate the full response to phishing attacks by dynamically blocking and quarantining delivered mail
USAGE:
Run the following command for a list of options associated with this script:
PS C:\> Invoke-O365Ninja -help
#>
[CmdLetBinding()]
param(
[string]$username,
[string]$password,
[string]$encodedXMLCredentials,
[string]$socMailbox,
[string]$LogRhythmHost,
[string]$caseAPItoken,
[string]$caseNumber,
[string]$addCaseUser,
[string]$targetUser,
[string]$fromIP,
[string]$sender,
[string]$recipient,
[string]$subject,
[string]$past,
[string]$spammerList,
[string]$defaultCaseTag = "phishing", # default tag for case management
[switch]$help,
[switch]$searchMail,
[switch]$scrapeMail,
[switch]$getMail,
[switch]$auditLog,
[switch]$resetPassword,
[switch]$blockSender,
[switch]$unblockSender,
[switch]$appendToList,
[switch]$checkForwards,
[switch]$checkMemberships,
[switch]$auditMailboxes,
[switch]$bypass,
[switch]$nuke = $false
)
$banner = @"
___ ____ __ ___ _ _ _ _
/ _ \__ / / /| __| | \| (_)_ _ (_)__ _
| (_) |_ \/ _ \__ \ | .' | | ' \ | / _' |
\___/___/\___/___/ |_|\_|_|_||_|/ \__,_|
|__/
"@
$usage = @"
USAGE:
Search through mail logs:
PS C:\> Invoke-O365Ninja -searchMail
Available switches for mail search:
-sender, -recipient, -fromIP
Capture A Specific Email:
PS C:\> Invoke-O365Ninja -getMail -targetUser "<user.name>" -sender "<spammer>"
Quarantine A Specific Email:
PS C:\> Invoke-O365Ninja -getMail -targetUser "<user.name>" -sender "<spammer>" -nuke
Available switches for targeted mail capture:
-sender, -subject, -recipient
Capture All Emails:
PS C:\> Invoke-O365Ninja -scrapeMail -sender "<spammer>"
Quarantine All Emails Matching Defined Criteria:
PS C:\> Invoke-O365Ninja -scrapeMail -sender "<spammer>" -nuke
Available switches for quarantine / extraction:
-sender, -subject, -recipient
Block Sender for specific user:
PS C:\> Invoke-O365Ninja -blockSender -sender "<spammer>" -recipient "<recipient>"
Block Sender for the whole company:
PS C:\> Invoke-O365Ninja -blockSender -sender "<spammer>"
Remove Sender from block list for specific user:
PS C:\> Invoke-O365Ninja -unblockSender -sender "<not spammer>" -recipient "<recipient>"
Remove Sender from block list for the whole company:
PS C:\> Invoke-O365Ninja -unblockSender -sender "<not spammer>"
Reset End User's Password:
PS C:\> Invoke-O365Ninja -resetPassword -targetMailbox "User.Name"
Add Spammer to Threat List:
PS C:\> Invoke-O365Ninja -appendToList -sender "<sender@email>" -spammerList "<LogRhythm List Name>"
Check Auto Forwarding Rules:
PS C:\> Invoke-O365Ninja -checkForwards
Obtain Group Memberships:
PS C:\> Invoke-O365Ninja -checkMemberships
Audit Mailboxes for new O365 Accounts:
PS C:\> Invoke-O365Ninja -auditMailboxes
************************************************************
All arguments require administrative access to Office 365, and must include the following parameters / supply them at runtime
-username, -password, -socMailbox
-encodedXMLCredentials "C:\File-location.xml"
This value can be used if you would like to store your credentials in an encoded XML file
To take advantage of the LogRhythm SIEM integrations, the following parameters are required
-LogRhythmHost, -appendToList, -spammerList -caseAPIToken, -caseNumber (optional - if not supplied a new case will be created)
"@
if ( $help ) {
Write-Host $banner -ForegroundColor Green
Write-Host $usage
Write-Host ""
break;
}
# ================================================================================
# DEFINE GLOBAL PARAMETERS AND CAPTURE CREDENTIALS
# ================================================================================
# Mask errors
$ErrorActionPreference = "silentlycontinue"
$warningPreference = "silentlyContinue"
# date and time
$today = "{0:MM-dd-yyyy}" -f (Get-Date).ToUniversalTime()
$yesterday = "{0:MM-dd-yyyy}" -f ((Get-Date).ToUniversalTime()).AddDays(-1)
$dayBefore = "{0:MM-dd-yyyy}" -f ((Get-Date).ToUniversalTime()).AddDays(-2)
$date = (Get-Date).ToUniversalTime()
$48Hours = ((Get-Date).ToUniversalTime()).AddHours(-48)
$24Hours = ((Get-Date).ToUniversalTime()).AddHours(-24)
$12Hours = ((Get-Date).ToUniversalTime()).AddHours(-12)
# folder structure and global parameters
$companyDomain = $socMailbox.Split("@")[1]
$currentFolder = (Get-Item -Path ".\" -Verbose).FullName
$tmPIEfolder = "$currentFolder\TemporaryPIE\"
mkdir $tmPIEfolder > $null
$traceLog = "$tmPIEfolder\ongoing-trace-log.csv"
$phishLog = "$tmPIEfolder\ongoing-phish-log.csv"
$analysisLog = "$tmPIEfolder\analysis.csv"
$tmpLog = "$tmPIEfolder\srp-tmp.csv"
$tmpFolder = "$tmPIEfolder\tmp\"
Write-Host $banner -ForegroundColor Green
Write-Host ""
# ================================================================================
# Office 365 API Authentication
# ================================================================================
if ( $encodedXMLCredentials ) {
# XML Configuration - store credentials in an encoded XML file
# This file will need to be re-generated whenever your system reboots!
#
# To generate the XML:
# PS C:\> Get-Credential | Export-Clixml Service-Account_cred.xml
$CredentialsFile = "$encodedXMLCredentials"
try {
$cred = Import-Clixml -Path $CredentialsFile
$Username = $cred.Username
$Password = $cred.GetNetworkCredential().Password
} catch {
Write-Error ("Could not find credentials file: " + $CredentialsFile)
Break;
}
}
try {
if (-Not ($password)) {
$cred = Get-Credential
} Else {
$securePass = ConvertTo-SecureString -string $password -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $securePass
}
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber > $null
} Catch {
Write-Host "Access Denied..."
Write-Host ""
break;
}
# ================================================================================
# TARGETED MAIL CAPTURE AND DELETION
# ================================================================================
if ( $getMail ) {
if ( -Not $socMailbox ) {
Write-Host "Target mailbox -socMailbox is required for this option" -ForegroundColor Red
Break;
}
if ( $past ) {
$day = $past
} else {
$day = "{0:MM-dd-yyyy}" -f (Get-Date).ToUniversalTime()
}
if ( $targetUser ) {
if ( $subject ) {
$messageQuery = "Subject:" + '"' + $subject + '"' + " Sent:" + $day
}
if ( $sender ) {
$messageQuery = "from:" + '"' + $sender + '"' + " Sent:" + $day
}
if ( $recipient ) {
$messageQuery = "to:" + '"' + $recipient + '"' + " Sent:" + $day
}
if ( $nuke -eq $true ) {
$searchMailboxResults = Search-Mailbox $targetUser -SearchQuery $messageQuery -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -DeleteContent -Force
} else {
$searchMailboxResults = Search-Mailbox $targetUser -SearchQuery $messageQuery -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -LogLevel Full
}
$searchMailboxResults
} else {
Write-Host "Target User not specified (-targetUser)" -ForegroundColor Red
Write-Host ""
break;
}
}
if ( $searchMail ) {
if ( $fromIP ) {
Get-MessageTrace -FromIP $fromIP -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Out-GridView
Write-Host "Displaying available results..."
break;
}
if ( $sender ) {
Get-MessageTrace -SenderAddress $sender -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Out-GridView
Write-Host "Displaying available results..."
break;
}
if ( $recipient ) {
Get-MessageTrace -RecipientAddress $recipient -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Out-GridView
Write-Host "Displaying available results..."
break;
}
}
if ( $scrapeMail ) {
if ( -Not $socMailbox ) {
Write-Host "Target mailbox -socMailbox is required for this option" -ForegroundColor Red
Break;
}
if ( $fromIP ) {
Get-MessageTrace -FromIP $fromIP -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Export-Csv $tmpLog -NoTypeInformation
type $tmpLog | findstr -v "MessageTraceId" > $analysisLog
$messageCount = type $analysisLog | findstr -i $sender | Measure-Object | Select-Object Count | findstr -v "Count -"
$messageCount = $messageCount.Trim() -match "[0-9+]"
type $analysisLog | ForEach-Object { $_.Split(",")[3] } | Sort | Get-Unique | findstr "@" > $tmPIEfolder\recipients.txt
#$messageQuery1 = "from:" + '"' + $sender + '"' + " Sent:" + $today
#$messageQuery2 = "from:" + '"' + $sender + '"' + " Sent:" + $yesterday
#$messageQuery3 = "from:" + '"' + $sender + '"' + " Sent:" + $dayBefore
$caseQuery = "Malicious Emails Sent from IP Address ($fromIP)"
if ( $messageCount -lt 10 ) {
$timeframe = "48 hours"
} else {
$timeframe = "12 hours"
}
}
if ( $sender ) {
$senderDomain = $sender.Split("@")[1]
if ( $senderDomain -eq $companyDomain ) {
if ( $bypass ) {
Write-Host "Warning - Quarantining mail from internal employee ($sender)!" -ForegroundColor Yellow
Write-Host ""
} else {
Write-Host "Error - unable to quarantine mail from $sender as they appear to be an internal employee!" -ForegroundColor Red
Write-Host "If you are sure that you would like to proceed, run the script with the -bypass flag set" -ForegroundColor Red
Write-Host ""
break;
}
} else {
Get-MessageTrace -SenderAddress $sender -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Export-Csv $tmpLog -NoTypeInformation
type $tmpLog | findstr -v "MessageTraceId" > $analysisLog
$messageCount = type $analysisLog | findstr -i $sender | Measure-Object | Select-Object Count | findstr -v "Count -"
$messageCount = $messageCount.Trim() -match "[0-9+]"
$spammer = $sender
type $analysisLog | ForEach-Object { $_.Split(",")[3] } | Sort | Get-Unique | findstr "@" > $tmPIEfolder\recipients.txt
$messageQuery1 = "from:" + '"' + $sender + '"' + " Sent:" + $today
$messageQuery2 = "from:" + '"' + $sender + '"' + " Sent:" + $yesterday
$messageQuery3 = "from:" + '"' + $sender + '"' + " Sent:" + $dayBefore
$caseQuery = "The sender of the email is ($sender)."
if ( $messageCount -lt 10 ) {
$timeframe = "48 hours"
} else {
$timeframe = "12 hours"
}
}
}
if ( $recipient ) {
Get-MessageTrace -RecipientAddress $recipient -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Export-Csv $tmpLog -NoTypeInformation
type $tmpLog | findstr -v "MessageTraceId" > $analysisLog
$spammer = type $analysisLog | ForEach-Object { $_.Split(",")[2] } | Sort | Get-Unique | findstr "@"
$senderDomain = $spammer.Split("@")[1]
if ( $senderDomain -eq $companyDomain ) {
if ( $bypass ) {
Write-Host "Warning - Quarantining mail from internal employee ($sender)!" -ForegroundColor Yellow
Write-Host ""
} else {
Write-Host "Error - unable to quarantine mail from $sender as they appear to be an internal employee!" -ForegroundColor Red
Write-Host "If you are sure that you would like to proceed, run the script with the -bypass flag set" -ForegroundColor Red
Write-Host ""
break;
}
} else {
type $analysisLog | ForEach-Object { $_.Split(",")[3] } | Sort | Get-Unique | findstr "@" > $tmPIEfolder\recipients.txt
$messageCount = type $analysisLog | findstr -i $recipient | Measure-Object | Select-Object Count | findstr -v "Count -"
$messageCount = $messageCount.Trim() -match "[0-9+]"
$messageQuery1 = "to:" + '"' + $recipient + '"' + " Sent:" + $today
$messageQuery2 = "to:" + '"' + $recipient + '"' + " Sent:" + $yesterday
$messageQuery3 = "to:" + '"' + $recipient + '"' + " Sent:" + $dayBefore
$caseQuery = "The recipient of the email is ($recipient)."
if ( $messageCount -lt 10 ) {
$timeframe = "48 hours"
} else {
$timeframe = "12 hours"
}
}
}
if ( $subject ) {
for( $c=1; $c -lt 1001; $c++ ) {
if((Get-MessageTrace -StartDate $12Hours -EndDate $date -PageSize 5000 -Page $c).count -gt 0) {
Get-MessageTrace -StartDate $12Hours -EndDate $date -PageSize 5000 -Page $c | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Export-Csv $tmpLog -NoTypeInformation -Append
} else {
break;
}
}
$subjectMatches = type $tmpLog | findstr -i "$subject"
type $tmpLog | findstr -i "$subject" > $analysisLog
$spammer = type $analysisLog | ForEach-Object { $_.Split(",")[2] } | Sort | Get-Unique | findstr "@"
$senderDomain = $spammer.Split("@")[1]
if ( $senderDomain -eq $companyDomain ) {
if ( $bypass ) {
Write-Host "Warning - Quarantining mail from internal employee ($sender)!" -ForegroundColor Yellow
Write-Host ""
} else {
Write-Host "Error - unable to quarantine mail from $sender as they appear to be an internal employee!" -ForegroundColor Red
Write-Host "If you are sure that you would like to proceed, run the script with the -bypass flag set" -ForegroundColor Red
Write-Host ""
break;
}
} else {
type $analysisLog | ForEach-Object { $_.Split(",")[3] } | Sort | Get-Unique | findstr "@" > $tmPIEfolder\recipients.txt
$messageCount = type $analysis | findstr -i $subject | Measure-Object | Select-Object Count | findstr -v "Count -"
$messageCount = $messageCount.Trim() -match "[0-9+]"
$messageQuery1 = "Subject:" + '"' + $subject + '"' + " Sent:" + $today
$messageQuery2 = "Subject:" + '"' + $subject + '"' + " Sent:" + $yesterday
$messageQuery3 = "Subject:" + '"' + $subject + '"' + " Sent:" + $dayBefore
$caseQuery = "The subject of the email is ($subject)."
if ( $messageCount -lt 10 ) {
$timeframe = "48 hours"
} else {
$timeframe = "12 hours"
}
}
}
if ( $nuke -eq $true ) {
$getUsers = type $tmPIEfolder\recipients.txt
foreach ($phishRecipient in $getUsers) {
$phishRecipient = $phishRecipient.Split('"')[1]
$endUserName = $phishRecipient.Split("@")[0]
$searchMailboxResults1 = Search-Mailbox $endUserName -SearchQuery $messageQuery1 -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -DeleteContent -Force
if ( $messageCount -lt 10 ) {
$searchMailboxResults2 = Search-Mailbox $endUserName -SearchQuery $messageQuery2 -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -DeleteContent -Force
$searchMailboxResults3 = Search-Mailbox $endUserName -SearchQuery $messageQuery3 -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -DeleteContent -Force
}
}
$quarantine = "YES"
$caseStatus = "Phishing messages have been quarantined and samples have been extracted to the Phishing Case Inbox ($socMailbox) for further analysis..."
$messageStatus = "Quarantined"
echo ""
echo "Phishing messages have been quarantined and samples have been extracted to the Phishing Case Inbox for further analysis..."
echo ""
echo "$messageCount - Total Recipients:"
echo ""
echo "$getUsers"
echo ""
} else {
$getUsers = type $tmPIEfolder\recipients.txt
foreach ($phishRecipient in $getUsers) {
$phishRecipient = $phishRecipient.Split('"')[1]
$endUserName = $phishRecipient.Split("@")[0]
$searchMailboxResults1 = Search-Mailbox $endUserName -SearchQuery $messageQuery1 -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -LogLevel Full
if ( $messageCount -lt 10 ) {
$searchMailboxResults2 = Search-Mailbox $endUserName -SearchQuery $messageQuery2 -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -LogLevel Full
$searchMailboxResults3 = Search-Mailbox $endUserName -SearchQuery $messageQuery3 -TargetMailbox $socMailbox -TargetFolder "PROCESSING" -LogLevel Full
}
}
$quarantine = "NO"
$caseStatus = "Email messages have been extracted to the Phishing Case Inbox ($socMailbox) for further analysis..."
$messageStatus = "Extracted"
echo ""
echo "Email messages have been extracted to the Phishing Case Inbox for further analysis..."
echo "To delete the emails run the following PowerShell command:"
echo ""
echo 'PS C:\> Search-Mailbox <end username> -SearchQuery <message query> -TargetMailbox <soc mailbox> -TargetFolder "PROCESSING" -DeleteContent -Force'echo ""
echo "$messageCount - Total Recipients:"
echo ""
echo "$getUsers"
echo ""
}
}
# ================================================================================
# OFFICE365 AUDIT LOG SEARCHING
# ================================================================================
if ( $auditLog ) {
if ( $targetUser ) {
Write-Host ""
Write-Host "Audit Log Search for ($targetUser)"
Search-MailboxAuditLog -identity $targetUser -logontypes admin,delegate,owner -startdate $48Hours -enddate $today -resultsize 5000 -ShowDetails | Out-GridView
} else {
Write-Host ""
Write-Host "Target User Account Required ( -targetUser )" -ForegroundColor Red
break;
}
}
# ================================================================================
# OFFICE365 ENABLE AUDITING ON MAILBOXES
# ================================================================================
if ( $auditMailboxes ) {
Write-Host ""
Write-Host "Checking for unaudited mailboxes, limited to 1000 results, then will attempt to enable auditing."
# We want to leave this at the default of 1000, and re-run auditMailboxes if needed
$UnauditedMailboxes=(Get-Mailbox -Filter {AuditEnabled -eq $false}).Identity
$UAMBCount=$UnauditedMailboxes.Count
if ($UAMBCount -gt 0){
Write-Host "Attempting to enable auditing on $UAMBCount mailboxes, please wait..." -ForegroundColor Cyan
$UnauditedMailboxes | % { Set-Mailbox -Identity $_ -AuditDelegate SendAs,SendOnBehalf,Create,Update,SoftDelete,HardDelete -AuditEnabled $true }
Write-Host "Finished attempting to enable auditing on $UAMBCount mailboxes." -ForegroundColor Yellow
}
if ($UAMBCount -eq 1000){
Write-Host "Since the count was $UAMBCount mailboxes, execute auditMailboxes again until auditing is enabled on all mailboxes" -ForegroundColor Yellow
}
if ($UAMBCount -eq 0){Write-Host "No auditing actions to perform, all mailboxes have auditing enabled!"}
}
# ================================================================================
# OFFICE365 ADMINISTRATIVE ACTIONS
# ================================================================================
if ( $resetPassword ) {
if ( $targetUser ) {
$newPassword = ([System.Web.Security.Membership]::GeneratePassword(16,2))
Set-MsolUserPassword –UserPrincipalName $targetUser –NewPassword $newPassword -ForceChangePassword $True
Write-Output "We've set the password for the account $targetUser to be $newPassword. Make sure you record this and share with the user, or be ready to reset the password again. They will have to reset their password on the next logon."
#Set-MsolUser -UserPrincipalName $targetMailbox -StrongPasswordRequired $True
#Write-Output "We've also set this user's account to require a strong password."
$caseStatus = "We've set the password for the account $targetUser to be $newPassword. Ensure that they change this immediately!"
} else {
Write-Host ""
Write-Host "Target User Account Required ( -targetUser )" -ForegroundColor Red
break;
}
}
if ( $blockSender ) {
if ( $sender ) {
if ( $recipient ) {
Write-Host ""
Write-Host "Blocking ($sender) from sending mail to ($recipient)."
Write-Host ""
try {
Set-MailboxJunkEmailConfiguration -Identity $recipient -BlockedSendersAndDomains @{Add="$sender"}
Write-Host " [ + ] $recipient || $sender Blocked Successfully" -ForegroundColor Cyan
Write-Host ""
} catch {
Write-Host " [ - ] $recipient || Unable to block $sender..." -ForegroundColor Cyan
Write-Host ""
}
} else {
Get-MessageTrace -SenderAddress $sender -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Export-Csv $tmpLog -NoTypeInformation
type $tmpLog | findstr -v "MessageTraceId" > $analysisLog
$messageCount = type $analysisLog | findstr -i $sender | Measure-Object | Select-Object Count | findstr -v "Count -"
$messageCount = $messageCount.Trim() -match "[0-9+]"
$spammer = $sender
type $analysisLog | ForEach-Object { $_.Split(",")[3] } | Sort | Get-Unique | findstr "@" > $tmPIEfolder\recipients.txt
Write-Host ""
Write-Host "Blocking ($sender) from sending mail to $messageCount recipients. This may take a few minutes..."
Write-Host ""
$getUsers = type $tmPIEfolder\recipients.txt
$recipients = $getUsers.Split('"')[1]
$messageRecipients = (Get-Content "$tmPIEfolder\recipients.txt") -join ", "
$messageRecipients = $messageRecipients -replace '"', ""
foreach ($phishRecipient in $getUsers) {
$phishRecipient = $phishRecipient.Split('"')[1]
$endUserName = $recipient.Split("@")[0]
try {
Set-MailboxJunkEmailConfiguration -Identity $phishRecipient -BlockedSendersAndDomains @{Add="$sender"}
Write-Host " [ + ] $phishRecipient || $sender Blocked Successfully" -ForegroundColor Cyan
} catch {
Write-Host " [ - ] $phishRecipient || Unable to block $sender..." -ForegroundColor Red
Write-Host ""
}
}
Write-Host ""
Write-Host "Sender ($sender) successfully blocked for $messageCount users!"
Write-Host ""
}
} else {
Write-Host ""
Write-Host "Sender Email Address Required ( -sender )" -ForegroundColor Red
break;
}
}
if ( $unblockSender ) {
if ( $sender ) {
if ( $recipient ) {
Write-Host ""
Write-Host "Unblocking ($sender) for recipient ($recipient)."
Write-Host ""
$phishRecipient = $recipient.Split('"')[1]
$endUserName = $recipient.Split("@")[0]
try {
Set-MailboxJunkEmailConfiguration -Identity $phishRecipien -BlockedSendersAndDomains @{Remove="$sender"}
Write-Host " [ + ] $recipient || $sender Unblocked Successfully" -ForegroundColor Cyan
Write-Host ""
break;
} catch {
Write-Host " [ - ] $recipient || Unable to unblock $sender..." -ForegroundColor Red
Write-Host ""
break;
}
} else {
Get-MessageTrace -SenderAddress $sender -StartDate $48Hours -EndDate $date | Select MessageTraceID,Received,*Address,*IP,Subject,Status,Size,MessageID | Export-Csv $tmpLog -NoTypeInformation
type $tmpLog | findstr -v "MessageTraceId" > $analysisLog
$messageCount = type $analysisLog | findstr -i $sender | Measure-Object | Select-Object Count | findstr -v "Count -"
$messageCount = $messageCount.Trim() -match "[0-9+]"
$spammer = $sender
type $analysisLog | ForEach-Object { $_.Split(",")[3] } | Sort | Get-Unique | findstr "@" > $tmPIEfolder\recipients.txt
Write-Host ""
Write-Host "Unblocking ($sender) to allow mail to be sent to $messageCount recipients. This may take a few minutes..."
Write-Host ""
$getUsers = type $tmPIEfolder\recipients.txt
$recipients = $getUsers.Split('"')[1]
$messageRecipients = (Get-Content "$tmPIEfolder\recipients.txt") -join ", "
$messageRecipients = $messageRecipients -replace '"', ""
foreach ($phishRecipient in $getUsers) {
$phishRecipient = $phishRecipient.Split('"')[1]
$endUserName = $phishRecipient.Split("@")[0]
try {
Set-MailboxJunkEmailConfiguration -Identity $endUserName -BlockedSendersAndDomains @{Remove="$sender"}
Write-Host " [ + ] $phishRecipient || $sender Unblocked Successfully" -ForegroundColor Cyan
} catch {
Write-Host " [ - ] $phishRecipient || Unable to unblock $sender..." -ForegroundColor Red
Write-Host ""
}
}
Write-Host ""
Write-Host "Sender ($sender) successfully unblocked from sending mail to $messageCount users"
Write-Host ""
break;
}
} else {
Write-Host ""
Write-Host "Sender Email Address Required ( -sender )" -ForegroundColor Red
break;
}
}
if ( $checkForwards ) {
Get-MailBox |?{$_.ForwardingAddress -ne $null}| Select-Object PrimarySmtpAddress,ForwardingAddress,ForwardingSmtpAddress,Office | Out-Gridview
break;
}
if ( $checkMemberships ) {
Get-UnifiedGroup | Sort-Object GroupMemberCount -Descending | Select-Object DisplayName,PrimarySmtpAddress,ManagedBy,GroupMemberCount,GroupExternalMemberCount,WhenCreated,WhenChanged,Notes | Out-GridView
$Groups = Get-UnifiedGroup -Filter {GroupExternalMemberCount -gt 0}
if ( $groups -gt 0 ) {
Write-Host "External Group Memberships Detected" -ForegroundColor Red
ForEach ($member in $groups) {
$ext = Get-UnifiedGroupLinks -Identity $member.Identity -LinkType Members
ForEach ($e in $ext) {
If ($e.Name -match "#EXT#")
{ Write-Host "Group " $member.DisplayName "includes guest user" $member.Name -ForegroundColor Cyan }
}
}
Write-Host ""
} else {
Write-Host "No External Group Memberships Detected" -ForegroundColor Cyan
Write-Host ""
}
break;
}
# ================================================================================
# LOGRHYTHM CASE AND LIST MANAGEMENT
# ================================================================================
if ( $caseAPItoken ) {
#force TLS v1.2 required by caseAPI
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Ignore invalid SSL certification warning
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$token = "Bearer $caseAPItoken"
$caseURL = "https://$LogRhythmHost/lr-case-api/cases/"
$listUrl = "https://$LogRhythmHost/lr-admin-api/lists/"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-type", "application/json")
$headers.Add("Authorization", $token)
$headers.Add("Count", "100000")
$headers.Add("maxItemsThreshold", "10000")
if ( $appendToList ) {
if ( $spammerList ) {
if ( $sender ) {
Write-Host ""
Write-Host "Append to List " -ForegroundColor Green
Write-Host "================================"
$output = irm -Uri $listURL -Headers $headers -Method GET
$listGuid = @($output | Where-Object name -EQ "$spammerList").guid
$listType = @($output | Where-Object name -EQ "$spammerList").listType
if ( $listType -eq "GeneralValue" ) {
$listType = "StringValue"
}
$listUpdate = $listUrl + $listGuid + "/items/"
# ListItemType: List,KnownService,Classification,CommonEvent,KnownHost,IP,IPRange,Location,MsgSource,
# MsgSourceType,MPERule,Network,StringValue,Port,PortRange,Protocol,HostName,ADGroup,Entity,RootEntity,
# DomainOrigin,Hash,Policy,VendorInfo,Result,ObjectType,CVE,UserAgent,ParentProcessId,ParentProcessName,
# ParentProcessPath,SerialNumber,Reason,Status,ThreatId,ThreatName,SessionType,Action,ResponseCode,Identity
$payload = @('{ "items":
[
{
"displayValue": "List",
"expirationDate": null,
"isExpired": false,
"isListItem": false,
"isPattern": false,
"listItemDataType": "List",
"listItemType": "' + $listType + '",
"value": "' + $sender + '",
"valueAsListReference": {}
}
]}')
try {
$output = Invoke-RestMethod -Uri $listUpdate -Headers $headers -Method POST -Body $payload
Write-Host "Successfully Appended " -NoNewline
Write-Host "$sender" -NoNewline -ForegroundColor Cyan
Write-Host " To List " -NoNewline
Write-Host "$spammerList"-ForegroundColor Cyan
} catch {
Write-Host "Failed To Append " -ForegroundColor Red -NoNewline
Write-Host "$sender" -NoNewline
Write-Host " To List " -NoNewline -ForegroundColor Red
Write-Host "$spammerList"
}
Write-Host "================================"
Write-Host ""
} else {
Write-Host "-sender variable is required for the blocklist" -ForegroundColor Red
}
} else {
Write-Host "-spammerList variable is required for the blocklist" -ForegroundColor Red
}
}
Write-Host "LogRhythm Case Management" -ForegroundColor Green
Write-Host "========================="
if ( $scrapeMail ) {
$casePriority = "2"
$spammerName = $spammer.Split("@")[0]
$spammerDomain = $spammer.Split("@")[1]
# Define the case summary
$caseName = "Email $messageStatus : $spammerName [at] $spammerDomain"
$caseSummary = "Email from $spammer has been quarantined and extracted for analysis via LogRhythm SmartResponse. $caseQuery Initial analysis shows that $messageCount user(s) received this email in the past $timeframe."
# Create Case if one doesn't already exist
if ( -Not $caseNumber ) {
# CREATE CASE
$payload = "{ `"name`": `"$caseName`", `"priority`": $casePriority, `"summary`": `"$caseSummary`" }"
$output = Invoke-RestMethod -Uri $caseURL -Headers $headers -Method POST -Body $payload
$caseNumber = $output.number
$noteurl = $caseURL + "number/$caseNumber/evidence/note"
sleep 5
Write-Host "Creating LogRhythm Case:"
Write-Host "URL:: " -NoNewline
Write-Host "$noteurl" -ForegroundColor Cyan
Write-Host "Name: " -NoNewline
Write-Host "$caseName" -ForegroundColor Cyan
Write-Host "Pri:: " -NoNewline
Write-Host "$casePriority" -ForegroundColor Cyan
Write-Host "Summary:: " -NoNewline
Write-Host "$caseSummary" -ForegroundColor Cyan
# Update Case with raw logs
$caseNote = type $analysisLog
$caseNote = $caseNote -replace '"', ""
$note = "Raw Phishing Logs: $caseNote"
Write-Host "Adding Case Note:"
Write-Host "$note" -ForegroundColor Cyan
$payload = "{ `"text`": `"$note`" }"
$output = Invoke-RestMethod -uri $noteurl -headers $headers -Method POST -body $payload
# Append List of Email Recipients
$messageRecipients = (Get-Content "$tmPIEfolder\recipients.txt") -join ", "
$messageRecipients = $messageRecipients -replace '"', ""
$note = "Email Recipients: $messageRecipients"
Write-Host "Appending List of Recipients to the case:"
Write-Host "$note" -ForegroundColor Cyan
$payload = "{ `"text`": `"$note`" }"
$output = Invoke-RestMethod -uri $noteurl -headers $headers -Method POST -body $payload
# Send the datas
$output = Invoke-RestMethod -Uri $caseURL -Headers $headers -Method GET
$output = $output | Select-Object number, id | Where-Object number -EQ $caseNumber
$caseUUID = $output.id
# Tag The Case
$tagUrl = "https://$LogRhythmHost/lr-case-api/tags/"
$output = Invoke-RestMethod -Uri $tagUrl -Headers $headers -Method GET
$tagNumber = @($output | Select-Object number, text | Where-Object text -EQ "$defaultCaseTag").number
$tagUrl = $caseUrl + "/$caseUUID/actions/addTags"
Write-Host "Tagging Case $caseNumber:: " -NoNewline
Write-Host "`"$defaultCaseTag`"" -ForegroundColor Cyan
$payload = "{ `"numbers`": `[$tagNumber`] }"
$output = Invoke-RestMethod -Uri $tagUrl -Headers $headers -Method PUT -Body $payload
}
# Update Case status
$note = "$caseStatus"
$noteurl = $caseURL + "number/$caseNumber/evidence/note"
Write-Host "Adding Case Note:"
Write-Host "$note" -ForegroundColor Cyan
$payload = "{ `"text`": `"$note`" }"
$output = Invoke-RestMethod -uri $noteurl -headers $headers -Method POST -body $payload
}
if ( $resetPassword ) {
$casePriority = "3"
# Define the case summary
$caseName = "Office 365 Account Credentials Reset: $targetMailbox"
$caseSummary = "We've set the password for the account $targetMailbox to be $newPassword. Make sure you record this and share with the user, or be ready to reset the password again. They will have to reset their password on the next logon."
# Create Case if one doesn't already exist
if ( -Not $caseNumber ) {
# CREATE CASE
$payload = "{ `"name`": `"$caseName`", `"priority`": $casePriority, `"summary`": `"$caseSummary`" }"
$output = Invoke-RestMethod -uri $caseURL -headers $headers -Method POST -body $payload
$caseNumber = $output.number
sleep 5
Write-Host "Creating LogRhythm Case:"
Write-Host "URL:: " -NoNewline
Write-Host "$noteurl" -ForegroundColor Cyan
Write-Host "Name: " -NoNewline
Write-Host "$caseName" -ForegroundColor Cyan
Write-Host "Pri:: " -NoNewline
Write-Host "$casePriority" -ForegroundColor Cyan
Write-Host "Summary:: " -NoNewline
Write-Host "$caseSummary" -ForegroundColor Cyan
}
# Update Case status
$note = "($recipient) Credentials Reset - Please communicate this change with the affected user!"
$noteurl = $caseURL + "number/$caseNumber/evidence/note"
Write-Host "Adding Case Note:"
Write-Host "$note" -ForegroundColor Cyan
$payload = "{ `"text`": `"$note`" }"
$output = Invoke-RestMethod -uri $noteurl -headers $headers -Method POST -body $payload
# Tag The Case
$tagUrl = "https://$LogRhythmHost/lr-case-api/tags/"
$output = Invoke-RestMethod -Uri $tagUrl -Headers $headers -Method GET
$tagNumber = @($output | Select-Object number, text | Where-Object text -EQ "password reset").number
$tagUrl = $caseUrl + "/$caseUUID/actions/addTags"
Write-Host "Tagging Case $caseNumber:: " -NoNewline
Write-Host "`"password reset`"" -ForegroundColor Cyan
$payload = "{ `"numbers`": `[$tagNumber`] }"
$output = Invoke-RestMethod -Uri $tagUrl -Headers $headers -Method PUT -Body $payload
}
if ( $blockSender ) {
$casePriority = "5"
# Define the case summary
$caseName = "Blacklisted Sender : $sender"
$caseSummary = "($sender) has been banned from sending further mail to the organization. Review the black list within the Office 365 Management Interface."
# Create Case if one doesn't already exist
if ( -Not $caseNumber ) {
# CREATE CASE
$payload = "{ `"name`": `"$caseName`", `"priority`": $casePriority, `"summary`": `"$caseSummary`" }"
$output = Invoke-RestMethod -uri $caseURL -headers $headers -Method POST -body $payload
$caseNumber = $output.number
sleep 5
Write-Host "Creating LogRhythm Case:"
Write-Host "URL:: " -NoNewline
Write-Host "$caseURL" -ForegroundColor Cyan
Write-Host "Name: " -NoNewline
Write-Host "$caseName" -ForegroundColor Cyan