-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAHelper.ps1
1180 lines (998 loc) · 46.4 KB
/
DAHelper.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
<#
.DESCRIPTION
[+] Sets a series of baselines.
[+] Creates a WMI event to monitor newly created processes.
-> Runs pe-sieve over all newly created processes.
[+] Creates filesystem watcher events.
-> Copies created files to a directory.
[+] Sets a second series of baselines
[+] Checks for diffs between baselines, prints and logs results.
.LINK
Tools used:
- https://github.com/hasherezade/pe-sieve/releases/tag/v0.3.9
- https://github.com/jschicht/ExtractUsnJrnl?tab=readme-ov-file
- https://github.com/EricZimmerman/MFTECmd
.NOTES
[!] If hitting [Enter] once doesn't trigger the closing of events and the
start of the second round of baselines, hit [Enter] again.
[!] Option given on first run to download tools and setup sysmon with a trace config.
If using without network connection, make a copy of the tools
and match or change the paths at the bottom of the script.
.USAGE
[>] ./DAHelper.ps1 [command] <args>
[Ex] ./DAHelper.ps1 Run-All
[Ex] ./DAHelper.ps1 Compare C:\Temp\baselines_<time> C:\Temp\baselines_<time>
.OPTIONS
[Run-All]
\__ Runs everything in the script
\__ Baselines > Watchers/Process Monitoring > Baselines > Compare
[Watch]
\__ Starts up the only the process monitoring and file creation functions
[Single] <OutputPath>
\__ Runs a single baseline set
[Compare] <Dir 1> <Dir 2>
\__ Compares two previously exported baseline sets
[Help] Print help
#>
param(
[Parameter(Position=0)]
[string]$command,
[Parameter(Position=1)]
[string]$argPathOne,
[Parameter(Position=2)]
[string]$argPathTwo
)
$compareBaselines = @"
using System;
using System.IO;
using System.Collections.Generic;
public class CompareBaselines
{
public static Tuple<List<string>, List<string>> Compare(string blOne, string blTwo)
{
int index = 0;
var result = GetDifferences(blOne, blTwo, index);
return result;
}
private static Tuple<List<string>, List<string>> GetDifferences(string blOne, string blTwo, int index)
{
var fullRowsInFirst = new Dictionary<string, string>();
var fullRowsInSecond = new Dictionary<string, string>();
string[] firstFileLines = File.ReadAllLines(blOne);
string[] secondFileLines = File.ReadAllLines(blTwo);
string headers = firstFileLines[0];
for (int i = 1; i < firstFileLines.Length; i++)
{
var columns = firstFileLines[i].Split(',');
if (columns.Length > index)
fullRowsInFirst[columns[index]] = firstFileLines[i];
}
for (int i = 1; i < secondFileLines.Length; i++)
{
var columns = secondFileLines[i].Split(',');
if (columns.Length > index)
fullRowsInSecond[columns[index]] = secondFileLines[i];
}
var newKeys = new List<string> { headers };
foreach (var key in fullRowsInSecond.Keys)
{
if (!fullRowsInFirst.ContainsKey(key))
newKeys.Add(fullRowsInSecond[key]);
}
var removedKeys = new List<string> { headers };
foreach (var key in fullRowsInFirst.Keys)
{
if (!fullRowsInSecond.ContainsKey(key))
removedKeys.Add(fullRowsInFirst[key]);
}
return Tuple.Create(newKeys, removedKeys);
}
}
"@
function Compare-BaseLines {
Param(
[Parameter(Position=0, Mandatory=$true)]
[string]$blDirOne,
[Parameter(Position=1, Mandatory=$true)]
[string]$blDirTwo,
[Parameter(Position=2, Mandatory=$true)]
[string]$reportPath
)
BEGIN {
Add-Type -TypeDefinition $compareBaselines -Language CSharp
$dirOneFiles = Get-ChildItem -Path $blDirOne -Filter *.csv
$dirTwoFiles = Get-ChildItem -Path $blDirTwo -Filter *.csv
$total = $dirOneFiles.Count
$dirTwoHT = @{}
foreach ($file in $dirTwoFiles) { $dirTwoHT[$file.Name] = $file.FullName }
}
PROCESS {
foreach ($file in $dirOneFiles) {
if ($dirTwoHT.ContainsKey($file.Name)) {
$bl1 = $file.FullName
$bl2 = $dirTwoHT[$file.Name]
$blType = switch -regex ($file) {
'^AccessFeatures' { 'Win Accessability Features' }
'^Addresses' { 'Addresses' }
'^BTJobs' { 'Background Intelligent Transfer Jobs' }
'^Certs' { 'Certificates' }
'^COMexe' { 'COM exe' }
'^COMdll' { 'COM dll'}
'^DnsCache' { 'DNS Cache' }
'^DLILCOM' { 'Disabled LowIL Isolation COM' }
'^Drivers' { 'Drivers' }
'^EvtConsumers' { 'Event Consumers' }
'^USNJrnl' { 'Change Journal' }
'^FirewallRules' { 'Firewall Rules' }
'^Files' { 'Files' }
'^FWLog' { 'Firewall Logs' }
'^Links' { 'Links' }
'^Pipes' { 'Named Pipes' }
'^NSPipes' { 'NullSession Pipes' }
'^NSShares' { 'NullSession Shares' }
'^PendingRenames' { 'Pending Renames' }
'^Processes' { 'Processes' }
'^RootTPs' { 'Root Thumbprints' }
'^SchTasks' { 'Sch Tasks' }
'^Services' { 'Services' }
'^Shims' { 'Shims' }
'^Software' { 'Software' }
'^StartUp' { 'Start Up' }
'^StartUpCmd' { 'Start Up Cmd' }
'^Streams' { 'Streams' }
'^SMCmdLine' { 'Proc Creation CmdLine' }
'^SMDNS' { 'DNS Query Evts' }
'^SMImgLoad' { 'Image Load Evts' }
'^TcpConnections' { 'Network Traffic' }
'^UrlCache' { 'INet Cache' }
default { 'Type Unknown' }
}
$diffs = [CompareBaselines]::Compare($bl1, $bl2)
$newEntries = $diffs.Item1
$removedEntries = $diffs.Item2
$title = "`n=======================> [ $blType ] <======================="
$new = "------------------------------ [ New ] ------------------------------"
$removed = "`n--------------------------- [ Removed ] ---------------------------"
$printNew = $newEntries.Count -gt 1
$printRemoved = $removedEntries.Count -gt 1
if ($printNew -or $printRemoved) {
Write-Host $title -Fore Cyan
$title | Out-File -FilePath $reportPath -Append
}
if ($printNew) {
Write-Host $new -Fore Green
$headers = $newEntries[0] -split ','
$counter = 0
foreach ($entry in $newEntries) {
if ($counter -gt 0) {
$color = if ($counter % 2 -eq 0) { "White" } else { "Gray" }
$columns = $entry -split ','
for ($i = 0; $i -lt $columns.Length; $i++) {
Write-Host "$($headers[$i]): $($columns[$i])" -Fore $color
}
Write-Host ""
}
$counter++
}
$new | Out-File -FilePath $reportPath -Append
foreach ($entry in $newEntries) { Add-Content -Path $reportPath $entry }
}
if ($printRemoved) {
Write-Host $removed -Fore Red
$headers = $removedEntries[0] -split ','
$counter = 0
foreach ($entry in $removedEntries) {
if ($counter -gt 0) {
$color = if ($counter % 2 -eq 0) { "White" } else { "Gray" }
$columns = $entry -split ','
for ($i = 0; $i -lt $columns.Length; $i++) {
Write-Host "$($headers[$i]): $($columns[$i])" -Fore $color
}
Write-Host ""
}
$counter++
}
$removed | Out-File -FilePath $reportPath -Append
foreach ($entry in $removedEntries) { Add-Content -Path $reportPath $entry }
}
} else {
Write-Warning "[!] File $($file.Name) does not exist in second directory."
}
}
}
END {
Write-Host "[>] Comparisons complete!" -Fore Cyan
}
}
$kernel32 = @"
using System;
using System.Runtime.InteropServices;
public class Kernel32 {
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetNamedPipeServerProcessId(
IntPtr Pipe, out uint ServerProcessId);
}
"@
function Export-Baselines {
param (
[Parameter(Position=0)]
[string]$blDirectory
)
BEGIN {
Write-Host "[>] Starting baseline export.." -Fore Cyan
Add-Type -TypeDefinition $kernel32
}
PROCESS {
<#
Write-Host " [>] Getting streams.." -Fore Magenta
$streams = Get-ChildItem -Path C:\ -Recurse -Force |
ForEach-Object { Get-Item -Path $_.FullName -Stream * } |
Where-Object { ($_.Stream -notlike "*DATA") -and ($_.Stream -ne "Zone.Identifier") }
$streamResults = @()
foreach ($stream in $streams) {
$file = Get-Item -Path $stream.FileName
$content = Get-Content -Path $stream.FileName -Stream $stream.Stream
$streamResults += [PSCustomObject]@{
File = $file
StreamContent = $content
}
}
$streamResults | Export-Csv -Path "$blDirectory\Streams.csv" -NoTypeInformation
#>
Write-Host " [>] Getting files.." -Fore Magenta
Get-ChildItem -Path C:\Windows -Recurse -Force |
Where-Object { $_.FullName -notlike '*\System32\*' -and $_.FullName -notlike '*\SysWOW64\*' -and $_.FullName -notlike '*\WinSxS\*' } |
Select-Object FullName | Export-Csv -Path "$blDirectory\Files.csv" -NoTypeInformation
Get-ChildItem -Path C:\ -Force | Select-Object FullName | Export-Csv -Path "$blDirectory\Files.csv" -Append -NoTypeInformation
Get-ChildItem -Path "C:\Program Files" -Recurse | Select-Object FullName | Export-Csv -Path "$blDirectory\Files.csv" -Append -NoTypeInformation
Get-ChildItem -Path "C:\Program Files (x86)" -Recurse | Select-Object FullName | Export-Csv -Path "$blDirectory\Files.csv" -Append -NoTypeInformation
Write-Host " [>] Getting COM bin file hashes.." -Fore Magenta
Get-COMHashes $blDirectory
Write-Host " [>] Getting USNJournal.." -Fore Magenta
Extract-USNJournal $blDirectory
Write-Host " [>] Getting root thumbprints and certs.." -Fore Magenta
Get-RootThumprints | Export-Csv -Path "$blDirectory\RootTPs.csv" -NoTypeInformation
#>
Get-ChildItem -Path cert:\ -Recurse | Select-Object ThumbPrint, FriendlyName, Subject |
Export-Csv -Path "$blDirectory\Certs.csv" -NoTypeInformation
Write-Host " [>] Getting process information.." -Fore Magenta
Get-CimInstance -Class Win32_Process | Select-Object ExecutablePath, ProcessId, CommandLine |
Export-Csv -Path "$blDirectory\Processes.csv" -NoTypeInformation
Get-Addresses | Export-Csv -Path "$blDirectory\Addresses.csv" -NoTypeInformation
Write-Host " [>] Getting network information.." -Fore Magenta
Get-NetTCPConnection | Select-Object RemoteAddress, RemotePort, LocalAddress, LocalPort, OwningProcess, `
@{ Name="Path"; Expression={ (Get-Process -Id $_.OwningProcess).Path } } |
Export-Csv -Path "$blDirectory\TcpConnections.csv" -NoTypeInformation
Get-DnsClientCache | Select-Object Name, Data | Export-Csv -Path "$blDirectory\DnsCache.csv" -NoTypeInformation
Get-Content $env:WINDIR\System32\LogFiles\Firewall\*.log | Select-String "ALLOW TCP" | out-string |
Export-Csv -Path "$blDirectory\FWLog.csv" -NoTypeInformation
Get-NetFirewallRule | Select-Object Name, Direction, Action |
Export-Csv -Path "$blDirectory\FirewallRules.csv" -NoTypeInformation
Get-ChildItem "$env:LOCALAPPDATA\Microsoft\Windows\INetCache" -Recurse -Force | Select-Object Name |
Export-Csv -Path "$blDirectory\UrlCache.csv" -NoTypeInformation
Write-Host " [>] Getting event consumers.." -Fore Magenta
Get-WmiObject -NameSpace root\Subscription -Class __EventConsumer | Select-Object __PATH, __NAMESPACE, options, ClassPath |
Export-Csv -Path "$blDirectory\EvtConsumers.csv" -NoTypeInformation
Write-Host " [>] Getting pending rename operations.." -Fore Magenta
Get-ItemProperty ("HKLM:\System\CurrentControlSet\Control\Session Manager").FileRenameOperations |
Export-Csv -Path "$blDirectory\PendingRenames.csv" -NoTypeInformation
Write-Host " [>] Getting drivers.." -Fore Magenta
Get-WmiObject Win32_SystemDriver | Select Name, DisplayName, PathName |
Export-Csv -Path "$blDirectory\Drivers.csv" -NoTypeInformation
Write-Host " [>] Getting services.." -Fore Magenta
Get-ChildItem -Path "HKLM:\System\CurrentControlSet\Services\*" | Select-Object Name |
Export-Csv -Path "$blDirectory\Services.csv" -NoTypeInformation
Write-Host " [>] Getting software.." -Fore Magenta
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, Publisher, InstallLocation |
Export-Csv -Path "$blDirectory\Software.csv" -NoTypeInformation
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, Publisher, InstallLocation |
Export-Csv -Path "$blDirectory\Software.csv" -NoTypeInformation
Write-Host " [>] Getting shims.." -Fore Magenta
Get-ItemProperty HKLM:"\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Custom\*" |
Export-Csv "$blDirectory\Shims.csv" -NoTypeInformation
Get-ItemProperty HKLM:"\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\InstalledSDB\*" |
Export-Csv "$blDirectory\Shims.csv" -Append -NoTypeInformation
Write-Host " [>] Getting scheduled tasks.." -Fore Magenta
Get-ScheduledTask | Select-Object TaskPath, TaskName, Source |
Export-Csv -Path "$blDirectory\SchTasks.csv" -NoTypeInformation
Write-Host " [>] Getting start up.." -Fore Magenta
Get-ChildItem "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\startup" -Recurse -Attributes !Directory -Force |
Select-Object PSPath | Export-Csv -Path "$blDirectory\StartUp.csv" -NoTypeInformation
Get-ChildItem "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\startup" -Recurse -Attributes !Directory -Force |
Select-Object PSPath | Export-Csv -Path "$blDirectory\StartUp.csv" -Append -NoTypeInformation
Get-CimInstance Win32_StartupCommand | Select-Object Command, Location, Name |
Export-Csv -Path "$blDirectory\StartUpCmd.csv" -NoTypeInformation
Write-Host " [>] Getting accessibility features.." -Fore Magenta
$accessibilityFeatures = @(
"$env_homedrive\Program Files\Common Files\microsoft shared\ink\HID.dll"
"$env_homedrive\Windows\System32\AtBroker.exe",
"$env_homedrive\Windows\System32\DisplaySwitch.exe",
"$env_homedrive\Windows\System32\Magnify.exe",
"$env_homedrive\Windows\System32\Narrator.exe",
"$env_homedrive\Windows\System32\osk.exe",
"$env_homedrive\Windows\System32\sethc.exe",
"$env_homedrive\Windows\System32\utilman.exe"
)
foreach ($feature in $accessibilityFeatures) {
$info = Get-Item $feature | Select-Object CreationTime, LastWriteTime
$features = [PSCustomObject]@{
WriteTime = $info.LastWriteTime
CreationTime = $info.CreationTime
Feature = $feature
}
$features | Export-Csv -Path "$blDirectory\AccessFeatures.csv" -Append -NoTypeInformation
}
Write-Host " [>] Getting BITS jobs.." -Fore Magenta
$btJobs = Get-BitsTransfer -AllUsers | Select-Object *
foreach ($job in $btJobs) {
$btJob = [PSCustomObject]@{
JobId = $job.JobId
Method = $job.HttpMethod
Type = $job.TransferType
Bytes = $job.BytesTotal
FileList = $job.FileList
}
$btJob | Export-Csv -Path "$blDirectory\BTJobs.csv" -Append -NoTypeInformation
}
Write-Host " [>] Getting pipes.." -Fore Magenta
$pipes = try { [System.IO.Directory]::GetFiles("\\.\pipe\") } catch { @() }
$pipesResult = @()
foreach ($pipe in $pipes) {
$hPipe = [Kernel32]::CreateFile($pipe, 0x80000000, 0, [System.IntPtr]::Zero, 3, 0x80, [System.IntPtr]::Zero)
if ($hPipe -eq [System.IntPtr]::Zero) { continue }
$owner = 0
if (-not [Kernel32]::GetNamedPipeServerProcessId($hPipe, [ref]$owner)) {
[Kernel32]::CloseHandle($hPipe)
continue
}
$proc = Get-WmiObject -Query "SELECT Caption FROM Win32_Process WHERE ProcessId = $owner" | Select -ExpandProperty Caption
$pipesResult += [PSCustomObject]@{
Pipe = $pipe
Proc = $proc
Owner = $owner
}
[Kernel32]::CloseHandle($hPipe)
}
$pipesResult | Export-Csv -Path "$blDirectory\Pipes.csv" -NoTypeInformation
(Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parametesr).NullSessionPipes |
Export-Csv -Path "$blDirectory\NSPipes.csv" -NoTypeInformation
(Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters).NullSessionShares |
Export-Csv -Path "$blDirectory\NSShares.csv" -NoTypeInformation
Write-Host " [>] Getting .lnks.." -Fore Magenta
$wScript = New-Object -ComObject WScript.Shell
$lnks = Get-ChildItem -Path "C:\Users\benzr\Desktop" -File -Recurse | Where-Object { $_.extension -in ".lnk" } | select-Object *
foreach ($lnk in $lnks) {
$target = $wScript.CreateShortcut($lnk.FullName).TargetPath
if ($target -notlike $("$PSScriptRoot\*")) {
$link = [PSCustomObject]@{
TargetPath = $target
LnkFile = $lnk.FullName
WriteTime = $lnk.LastWriteTime
}
}
$link | Export-Csv -Path "$blDirectory\Links.csv" -Append -NoTypeInformation
}
Write-Host " [>] Getting DisableLowILProcessIsolation COM objects.." -Fore Magenta
try {
$hkcrCLSID = "Registry::HKEY_CLASSES_ROOT\CLSID"
$clsid = Get-ChildItem -LiteralPath $hkcrCLSID | Select-Object * -ExcludeProperty PSPath, PSParentPath, PSChildName, PSProvider
foreach ($item in $clsid) {
$path = "Registry::"+$item.Name
$data = Get-ItemProperty -LiteralPath $path | Select-Object * -ExcludeProperty PSPath, PSParentPath, PSChildName, PSProvider
$data.PSObject.Properties | ForEach-Object {
if ($_.Name -eq 'DisableLowILProcessIsolation' -and $_.Value -eq 1) {
$lowILCOM = [PSCustomObject]@{
Key = $item.Name
Name = $data.DisplayName
}
$lowILCOM | Export-Csv -Path "$blDirectory\DLILCOM.csv" -Append -NoTypeInformation
}
}
}
} catch {
$errMsg = "[X] $($_.Exception.Message)"
Write-Host $errMsg -Fore Red
}
Write-Host " [>] Getting sysmon events.." -Fore Magenta
Extract-Sysmon $blDirectory
}
END {
Write-Host "[>] Baselines exported!" -Fore Cyan
}
}
# --------------------------------- [ Monitoring Functions ] ---------------------------------
function Monitor-CreationEvents {
<#
.DESCRIPTION
[+] Starts a file system watcher for temp directories; copies created files to script root.
[+] Registers a WMI event to monitor new processes that open.
[+] Runs pe-sieve on the process and checks it's loaded modules.
.NOTES
- Edit the exclusions in the event query.
- Edit pe-sieve args.
#>
param (
[Parameter(Position=0)]
[string]$resultsDir
)
BEGIN {
if (-not(Test-Path -Path "$PSScriptRoot\Tools\pe-sieve64.exe")) {
Write-Warning "[!] pe-sieve not found, skipping proc watch"
return
}
}
PROCESS {
Write-Host "[>] Monitoring processes! Run sample then hit [Enter] when ready continue!" -Fore Cyan
Copy-OnCreate
$eventQuery = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process' " +
"AND TargetInstance.Name != 'pe-sieve64.exe' " +
"AND TargetInstance.Name != 'SearchProtocolHost.exe' " +
"AND TargetInstance.Name != 'SearchFilterHost.exe' " +
"AND TargetInstance.Name != 'dllhost.exe' " +
"AND TargetInstance.Name != 'svchost.exe'" +
"AND TargetInstance.Name != 'smartscreen.exe'" +
"AND TargetInstance.Name != 'RuntimeBroker.exe'" +
"AND TargetInstance.Name != 'ApplicationFrameHost.exe'" +
"AND TargetInstance.Name != 'backgroundTaskHost.exe'"
Register-WmiEvent -Query $eventQuery -SourceIdentifier ProcessCreation -Action {
$process = $event.SourceEventArgs.NewEvent.TargetInstance
Write-Host "[>] New process started: $($process.Name), PID: $($process.ProcessId)" -Fore Cyan
cmd /c "$PSScriptRoot\Tools\pe-sieve64.exe" /pid $process.ProcessId /dir "$PSScriptRoot\sieve_output" `
/quiet /iat 3 /obfusc 3 /shellc 3 /threads /dmode 3 /imp 1 /minidmp
$modules = Get-Process -id $process.ProcessId | Select-Object -ExpandProperty Modules |
Select-Object ModuleName, FileName, Company
foreach ($module in $modules) {
$authSig = Get-AuthenticodeSignature $module.FileName
if ($authSig.Status -ne 'Valid') {
Write-Host " [!] Invalid Module: $($module.FileName)" -Fore Red
} else {
Write-Host " [!] Module: $($module.FileName)" -Fore Green
}
}
}
$continue = $true
while ($continue) {
if ([console]::KeyAvailable) {
$key = [System.Console]::ReadKey($true)
if ($key.Key -eq 'Enter') {
$continue = $false
}
}
else { Start-Sleep -Seconds 1 }
}
}
END {
Get-EventSubscriber | Unregister-Event
Get-Job | Remove-Job
Move-Item -Path "$PSScriptRoot\sieve_output" -Destination $resultsDir -Force
Move-Item -Path "$PSScriptRoot\copied_files" -Destination $resultsDir -Force
}
}
function Copy-OnCreate {
$filter = '*.*'
$tempPath = "$env:WINDIR\Temp"
$appDataPath = [System.IO.Path]::Combine($env:USERPROFILE, 'AppData')
$publicPath = "$env:PUBLIC"
$programDataPath = "$env:PROGRAMDATA"
$tempFSW = New-Object IO.FileSystemWatcher $tempPath, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$appDataFSW = New-Object IO.FileSystemWatcher $appDataPath, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$publicFSW = New-Object IO.FileSystemWatcher $publicPath, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$programDataFSW = New-Object IO.FileSystemWatcher $programDataPath, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$action = {
$fName = $Event.SourceEventArgs.Name
$fullPath = $Event.SourceEventArgs.FullPath
Write-Host "[>] File created: '$fullPath'" -Fore Magenta
try {
Copy-Item -Path $fullPath -Destination "$PSScriptRoot\copied_files"
Write-Host " [+] Copy success" -Fore Green
} catch {
Write-Host " [!] Failed copy - $_" -Fore DarkRed
}
}
Register-ObjectEvent $tempFSW Created -SourceIdentifier TempFileCreated -Action $action
Register-ObjectEvent $appDataFSW Created -SourceIdentifier AppDataFileCreated -Action $action
Register-ObjectEvent $publicFSW Created -SourceIdentifier PublicFileCreated -Action $action
Register-ObjectEvent $programDataFSW Created -SourceIdentifier ProgramDataFileCreated -Action $action
}
# --------------------------------- [ Functions for Export-Baselines ] ---------------------------------
function Get-COMHashes {
param (
[Parameter(Position=0)]
[string]$blDirectory
)
$clsidDLL = reg query HKLM\SOFTWARE\Classes\CLSID\ /s /f ".dll" |
ForEach-Object { if ($_ -match "([A-Z]:\\.*\.(dll))") { $matches[1] }}
$clsidEXE = reg query HKLM\SOFTWARE\Classes\CLSID\ /s /f ".exe" |
ForEach-Object { if ($_ -match "([A-Z]:\\.*\.(exe))") { $matches[1] }}
$clsidDLLResult = @()
foreach ($path in $clsidDLL) {
if (Test-Path $path) {
try {
$fileHash = (Get-FileHash -Path $path -Algorithm SHA256).Hash
} catch {
$fileHash = "Error calculating hash"
}
} else {
$fileHash = "File not found"
}
$clsidDLLResult += [pscustomobject]@{
FileHash = $fileHash
FilePath = $path
}
}
$clsidDLLResult | Export-Csv -Path "$blDirectory\COMdll.csv" -NoTypeInformation
$clsidEXEResult = @()
foreach ($path in $clsidEXE) {
if (Test-Path $path) {
try {
$fileHash = (Get-FileHash -Path $path -Algorithm SHA256).Hash
} catch {
$fileHash = "Error calculating hash"
}
} else {
$fileHash = "File not found"
}
$clsidEXEResult += [pscustomobject]@{
FileHash = $fileHash
FilePath = $path
}
}
$clsidEXEResult | Export-Csv -Path "$blDirectory\COMexe.csv" -NoTypeInformation
}
function Extract-USNJournal {
param (
[Parameter(Position=0)]
[string]$blDirectory
)
$dotnetExec = "C:\Program Files\dotnet\dotnet.exe"
$dotNetRuntimes = & $dotnetExec --list-runtimes
$dn6Installed = $dotNetRuntimes -like "*Microsoft.NETCore.App 6.*"
if (-not $dn6Installed) {
try {
Write-Host "[>] Installing .NET 6 Runtime" -Fore Green
winget install --id=Microsoft.DotNet.Runtime.6 -e
} catch {
Write-Error "[!] Error installing .NET 6 Runtime for MFTECmd $_"
Write-Host "[>] Skipping USNJournal extraction" -Fore Yellow
return
}
}
if (Test-Path $exUsnJrnl) {
& $exUsnJrnl /DevicePath:c: /OutputName:usnjrnl.bin | Out-Null
} else { Write-Warning "[!] Missing ExtractUsnJrnl64.exe, skipping"}
if (Test-Path $mfteCmd) {
& $mfteCmd -f $usnJrnlBin --csv $blDirectory --csvf journal.csv | Out-Null
$jrnl = Import-Csv -Path "$blDirectory\journal.csv"
$jrnl | Select-Object Name, FileAttributes | Export-Csv -Path "$blDirectory\USNJrnl.csv" -NoTypeInformation
Remove-Item -Path "$blDirectory\journal.csv" -Force
} else { Write-Warning "[!] Missing MFTECmd.exe, skipping"}
Remove-Item -Path $usnJrnlBin -Force
}
function Extract-Sysmon {
param (
[Parameter(Position=0)]
[string]$blDirectory
)
$isRules = Get-ChildItem -Path HKLM:\SYSTEM\CurrentControlSet\Services -Recurse -Include 'Parameters' |
Where-Object { $_.Property -contains 'Rules' }
if ($isRules -eq $null) {
Write-Warning "[!] Sysmon not detected, skipping"
return
}
$cmdLineResults = @()
$procCreateEvents = Get-WinEvent -LogName 'Microsoft-Windows-Sysmon/Operational' | Where-Object { $_.Id -eq 1 }
foreach ($event in $procCreateEvents) {
$processXML = [xml]$event.ToXml()
$properties = @{
ParentCmdLine = $processXML.Event.EventData.Data[21].'#text'
CMDLine = $processXML.Event.EventData.Data[10].'#text'
}
$cmdLineResults += New-Object -Type PSObject -Property $properties
}
$cmdLineResults | Export-Csv -Path "$blDirectory\SMCmdLine.csv" -NoTypeInformation
$dnsResults = @()
$dnsEvents = Get-WinEvent -LogName 'Microsoft-Windows-Sysmon/Operational' | Where-Object { $_.Id -eq 22 }
foreach ($event in $dnsEvents) {
$processXML = [xml]$event.ToXml()
$properties = @{
Query = $processXML.Event.EventData.Data[4].'#text'
}
$dnsResults += New-Object -Type PSObject -Property $properties
}
$dnsResults | Export-Csv -Path "$blDirectory\SMDNS.csv" -NoTypeInformation
$imgLoadResults = @()
$imgLoadEvents = Get-WinEvent -LogName 'Microsoft-Windows-Sysmon/Operational' | Where-Object { $_.Id -eq 7 }
foreach ($event in $imgLoadEvents) {
$processXML = [xml]$event.ToXml()
$properties = @{
Image = $processXML.Event.EventData.Data[4].'#text'
ImageLoaded = $processXML.Event.EventData.Data[5].'#text'
OrigFile = $processXML.Event.EventData.Data[10].'#text'
}
$imgLoadResults += New-Object -Type PSObject -Property $properties
}
$imgLoadResults | Export-Csv -Path "$blDirectory\SMImgLoad.csv" -NoTypeInformation
}
function Get-Addresses {
$unicodeExp = [Regex] "[\u0020-\u007E]{7,}"
$asciiExp = [Regex] "[\x20-\x7E]{7,}"
$ipExp = [Regex] '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$'
$urlExp = [Regex] '^(http|https)://.*$'
$ftpsExp = [Regex] "/^(ftps?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/"
$procs = Get-CimInstance -Class Win32_Process | Where-Object { $null -ne $_.Path } | Select-Object -Unique Path
$results = @()
try {
foreach ($proc in $procs) {
$path = $proc.Path
$unicodeContent = Get-Content -Encoding Unicode -Path $path
if ($unicodeContent) {
$matches = $unicodeExp.Matches($unicodeContent).Value
foreach ($match in $matches) {
if ($ipExp.IsMatch($match)) {
$results += [PSCustomObject]@{
MatchedString = $match
ProcessPath = $path
Type = "IP Address"
}
} elseif ($urlExp.IsMatch($match)) {
$results += [PSCustomObject]@{
MatchedString = $match
ProcessPath = $path
Type = "URL"
}
} elseif ($ftpsExp.IsMatch($match)) {
$results += [PSCustomObject]@{
MatchedString = $match
ProcessPath = $path
Type = "FTPS"
}
}
}
}
$asciiContent = Get-Content -Encoding UTF7 -Path $path | Out-Null
if ($asciiContent) {
$matches = $asciiExp.Matches($asciiContent).Value
foreach ($match in $matches) {
if ($ipExp.IsMatch($match)) {
$results += [PSCustomObject]@{
MatchedString = $match
ProcessPath = $path
Type = "IP Address"
}
} elseif ($urlExp.IsMatch($match)) {
$results += [PSCustomObject]@{
MatchedString = $match
ProcessPath = $path
Type = "URL"
}
} elseif ($ftpsExp.IsMatch($match)) {
$results += [PSCustomObject]@{
MatchedString = $match
ProcessPath = $path
Type = "FTPS"
}
}
}
}
}
} catch {
$errMsg = "[X] $($_.Exception.Message)"
Write-Host $errMsg -Fore Red
}
return $results
}
function Get-RootThumprints {
# https://posts.specterops.io/what-is-it-that-makes-a-microsoft-executable-a-microsoft-executable-b43ac612195e
$rootThumbprints = @()
$bins = Get-ChildItem -Path "C:\Windows\System32\*.exe" -Recurse -Force
$bins += Get-ChildItem -Path "C:\Windows\SysWOW64\*.dll" -Recurse -Force
$bins += Get-ChildItem -Path "C:\Windows\SysWOW64\*.exe" -Recurse -Force
foreach ($bin in $bins) {
$path = $bin.FullName
$fileInfo = Get-Item -Path $path
$verInfo = $fileInfo.VersionInfo
$originalName = $verInfo.OriginalFilename
$signatureInfo = Get-AuthenticodeSignature $path
$rootThumprint = $null
if ($signatureInfo.SignerCertificate) {
$signerCert = $signatureInfo.SignerCertificate
$signerChain = New-Object -TypeName Security.Cryptography.X509Certificates.X509Chain
$null = $signerChain.Build($signerCert)
$rootCert = $signerChain.ChainElements[$signerChain.ChainElements.Count - 1].Certificate
$rootTumbprint = $rootCert.Thumbprint
if ($rootThumbprint -eq $null) { $rootThumbprint = "None" }
}
$rootThumbprints += [PSCustomObject]@{
RootThumbprint = $rootTumbprint
FileName = $fileInfo.Name
OriginalName = $originalName
}
}
return $rootThumbprints
}
# --------------------------------- [ Helper Functions ] ---------------------------------
function Get-Tools {
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function Get-LatestReleaseAssetUrl {
param (
[string]$repo,
[string]$assetPattern
)
$releaseInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases/latest" `
-Headers @{ "User-Agent" = "PowerShell" }
$asset = $releaseInfo.assets | Where-Object { $_.name -like $assetPattern }
return $asset.browser_download_url
}
$mfteCmdUrl = "https://download.mikestammer.com/MFTECmd.zip"
Invoke-WebRequest -Uri $mfteCmdUrl -OutFile "$tools\MFTECmd.zip"
$peSieveUrl = Get-LatestReleaseAssetUrl "hasherezade/pe-sieve" "*pe-sieve64.exe"
Invoke-WebRequest -Uri $peSieveUrl -OutFile "$tools\pe-sieve64.exe"
$exUsnJrnlUrl = "https://github.com/jschicht/ExtractUsnJrnl/archive/refs/heads/master.zip"
Invoke-WebRequest -Uri $exUsnJrnlUrl -OutFile "$tools\ExtractUsnJrnl.zip"
$sysmonUrl = "https://download.sysinternals.com/files/Sysmon.zip"
Invoke-WebRequest -Uri $sysmonUrl -OutFile "$tools\sysmon.zip"
$smTraceConfigUrl = "https://github.com/bakedmuffinman/Neo23x0-sysmon-config/archive/refs/heads/main.zip"
Invoke-WebRequest -Uri $smTraceConfigUrl -OutFile "$tools\sysmon-configs.zip"
Expand-Archive -Path "$tools\MFTECmd.zip" -Destination "$tools\MFTECmd" -Force
Expand-Archive -Path "$tools\ExtractUsnJrnl.zip" -Destination "$tools\ExtractUsnJrnl" -Force
Expand-Archive -Path "$tools\sysmon.zip" -Destination "$tools\sysmon" -Force
Expand-Archive -Path "$tools\sysmon-configs.zip" -Destination "$tools\sysmon-configs" -Force
Remove-Item "$tools\MFTECmd.zip" -Force
Remove-Item "$tools\ExtractUsnJrnl.zip" -Force
Remove-Item "$tools\sysmon.zip" -Force
Remove-Item "$tools\sysmon-configs.zip" -Force
Write-Host "[>] Tools downloaded" -Fore Green
} catch {
$errMsg = "[X] $($_.Exception.Message)"
Write-Host $errMsg -Fore Red
}
}
function Kill-Edge {
$kEdge = Read-Host "[?] Kill Edge processes? [y/n]"
if ($kEdge -eq 'y') {
foreach ($service in (Get-Service -Name "*edge*" | Where-Object { $_.DisplayName -like "*Microsoft Edge*" }).Name) {
Stop-Service -Name $service -Force
}
foreach ($proc in (Get-Process | Where-Object {($_.Path -like "$([Environment]::GetFolderPath('ProgramFilesX86'))\Microsoft\*") `
-or ($_.Name -like "*msedge*")}).Id) {
Stop-Process -Id $proc -Force
}
} else {
return
}
}
function Check-Sysmon {
$isRules = Get-ChildItem -Path HKLM:\SYSTEM\CurrentControlSet\Services -Recurse -Include 'Parameters' |
Where-Object { $_.Property -contains 'Rules' }
if ($isRules -ne $null) {
Write-Host "[>] Sysmon detected" -Fore Green
$clearSysmon = Read-Host "[?] Clear sysmon logs before starting? [y/n]"
if ($clearSysmon -eq 'y') {
wevtutil cl "Microsoft-Windows-Sysmon/Operational"
return
} else {
return
}
} else {
Write-Host "[>] Sysmon not detedcted" -Fore Yellow
$installSysmon = Read-Host "[?] Install with trace config? [y/n]"
if ($installSysmon -eq 'y') {
& $sysmon64 -accepteula -i $sysmonConfig
return
} else {
return
}
}
}
function Check-FirstRun {
$runFile = Join-Path -Path $PSScriptRoot -ChildPath "has_run.txt"
if (-not(Test-Path -Path $runFile)) {
"qwerty123" | Out-File -FilePath $runFile
Write-Host "[>] First run detected" -Fore Green
$install = Read-Host "[?] Install tools? [y/n]"
if ($install -eq 'y') {
Get-Tools
} else {
return
}
} else {
return
}
}
function Check-EventsAndJobs {
$subscribers = Get-EventSubscriber
$jobs = Get-Job
if ($subscribers -gt 0) {
Write-Host "[>] Found event subscribers: " -Fore Yellow
$subscribers | fl *
$unregister = Read-Host "[?] Unregister all? [y/n]"
if ($unregister -eq 'y') {
$subscribers | ForEach-Object {
Unregister-Event -SubscriptionId $_.SubscriptionId
}
}
}
if ($jobs.Count -gt 0) {
Write-Host "[>] Found open jobs" -Fore Yellow
$jobs | fl *
$remove = Read-Host "[?] Remove all? [y/n]"
if ($remove -eq 'y') {
$jobs | ForEach-Object { Remove-Job -Id $_.Id }
}
}
}