-
Notifications
You must be signed in to change notification settings - Fork 8
/
SysmonLoadTest.ps1
827 lines (655 loc) · 38.9 KB
/
SysmonLoadTest.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
<#
$DebugPreference = "Continue" # Debug Mode
$DebugPreference = "SilentlyContinue" # Normal Mode
#>
$TestDurationTotalSeconds = 120
$SysinternalsSuitePath = "C:\Users\Administrator\Downloads\SysinternalsSuite"
$LogFile = "C:\ProgramData\SysmonBetaTest\activity.log"
$ConstrainSysmonCollection = $True
# creates Sysmon config with all inputs except specified type
function make-sysmon-config ($sysmonPath, $name)
{
Write-Debug "creating sysmon config"
# Get sysmon schema into xml
$sysmonSchemaPrint = & $sysmonPath -s 2> $null | Select-String -Pattern "<"
$sysmonSchemaPrintXml = [xml]$sysmonSchemaPrint
# spit out a new template file
$events = $sysmonSchemaPrintXml.manifest.events.event | Where-Object {$_.name -notmatch "(SYSMON_ERROR|SYSMON_SERVICE_STATE_CHANGE|SYSMON_SERVICE_CONFIGURATION_CHANGE)"}
$xmlConfig = @()
$xmlConfig += "<Sysmon schemaversion=`"$($sysmonSchemaPrintXml.manifest.schemaversion)`">"
if ($sysmonSchemaPrintXml.manifest.configuration.options.option.name -match "HashAlgorithms") { $xmlConfig += "`t<HashAlgorithms>*</HashAlgorithms>" }
if ($sysmonSchemaPrintXml.manifest.configuration.options.option.name -match "CheckRevocation") { $xmlConfig += "`t<CheckRevocation/>" }
# if ($sysmonSchemaPrintXml.manifest.configuration.options.option.name -match "DnsLookup") { $xmlConfig += "`t<DnsLookup>False</DnsLookup>" }
# if ($sysmonSchemaPrintXml.manifest.configuration.options.option.name -match "ArchiveDirectory") { $xmlConfig += "`t<ArchiveDirectory/>" }
$xmlConfig += "`t<EventFiltering>"
foreach ($event in $events) {
$printConfig = $true
# print the section hearder listing ID (value), Description (template), and config file section id (rulename)
$xmlConfig += ""
$xmlConfig += "`t`t<!--SYSMON EVENT ID $($event.value) : $($event.template) [$($event.rulename)]-->"
# print the section hearder data elements of event
$items = ""
foreach ($item in $event.data | Select Name) {
if ($items -eq "") {
$items = "$($item.name)"
} else {
$items += ", $($item.name)"
}
}
$xmlConfig += "`t`t<!--DATA: $($items)-->"
#
if ($event.value -match "12|13|17|19|20") { $printConfig = $false}
if ($name -match "SYSMON_REG_KEY|SYSMON_REG_SETVALUE" -and $event) {
$name = "SYSMON_REG_NAME"
}
if ($name -match "SYSMON_CREATE_NAMEDPIPE|SYSMON_CONNECT_NAMEDPIPE" -and $event) {
$name = "SYSMON_CONNECT_NAMEDPIPE"
}
if ($name -match "SYSMON_WMI_FILTER|SYSMON_WMI_CONSUMER|SYSMON_WMI_BINDING" -and $event) {
$name = "SYSMON_WMI_BINDING"
}
$matchtype = "include"
if ($event.name -ieq $name) {
Write-Debug "setting $($event.name) match level to exclude"
$matchtype = "exclude"
}
if ($printConfig -eq $true) {
$xmlConfig += ""
$xmlConfig += "`t`t<RuleGroup name=`"`" groupRelation=`"or`">"
$xmlConfig += "`t`t`t<$($event.rulename) onmatch=`"$($matchtype)`">"
$xmlConfig += "`t`t`t</$($event.rulename)>"
$xmlConfig += "`t`t</RuleGroup>"
}
}
$xmlConfig += ""
$xmlConfig += "`t</EventFiltering>"
$xmlConfig += ""
$xmlConfig += "</Sysmon>"
$ConfigFile = "$($env:TEMP)\$($name).xml"
if (Test-Path -Path $ConfigFile) { Remove-Item -Path $ConfigFile -Force }
write-debug "writing config to file: $($configfile)"
Set-Content -Path $ConfigFile -Value $xmlConfig
return $ConfigFile
}
# merges new config, starts-sysmon
function reset-sysmon ($sysmonPath, $configpath)
{
Write-Debug "configuring sysmon"
Start-Process -FilePath $sysmonPath -ArgumentList @("-c --") -NoNewWindow
Start-Sleep -Seconds 1
Start-Process -FilePath $sysmonPath -ArgumentList @("-c",$configpath) -NoNewWindow
Start-Sleep -Seconds 1
}
function format-splunktime {
param (
[parameter(Mandatory=$false)][datetime]$inputDate=(Get-Date)
)
$inputDateString = $inputDate.ToString('MM-dd-yyyy HH:mm:ss.fff zzzz')
$inputDateParts = $inputDateString -split " "
$inputDateZone = $inputDateParts[2] -replace ":",""
$outputDateString = "$($inputDateParts[0]) $($inputDateParts[1]) $($inputDateZone)"
return $outputDateString
}
# gets event logs of specified type
function get-eventlog ($logname)
{
$events = Get-WInEvent -log $logname
# Parse out the event message data
ForEach ($Event in $Events) {
# Convert the event to XML
$eventXML = [xml]$Event.ToXml()
# Iterate through each one of the XML message properties
For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++) {
# Append these as object properties
Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name $eventXML.Event.EventData.Data[$i].name -Value $eventXML.Event.EventData.Data[$i].'#text'
}
}
return $Events
}
# Establish path to Sysmon
$sysmonPath = "$($env:windir)\sysmon.exe"
if (!(Test-Path -Path $sysmonPath)) {
write-host "Sysmon.exe not present in $($sysmonPath). Exiting."
exit
}
# Ensure PowerSploit is present
$ModulePath = "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PowerSploit"
<#
# Make sure .NET 2.0 is present
if ((get-WindowsOptionalFeature -FeatureName "NetFX3" -Online).State -ne "Enabled") {
Enable-WindowsOptionalFeature -Online -FeatureName "NetFX3" -All
}
#Disable AV
if ((Get-MpPreference).DisableRealtimeMonitoring -eq $false) {
Read-Host -Prompt "Please disable A/V and press ENTER to continue.."
}
$url = "https://github.com/PowerShellMafia/PowerSploit/archive/master.zip"
$download = "$($env:temp)\master.zip"
if (Test-Path -Path $download) { Remove-Item -Path $download -Force -Recurse }
write-host "-downloading latest project from $($url)."
$Response = Invoke-WebRequest -Uri $url -OutFile $download
# extract the compressed content (if local copy older than 20 hours)
write-host "-extracting archive."
$extracted = "$($env:temp)\extracted"
if (Test-Path -Path $extracted) { Remove-Item -Path $extracted -Force -Recurse }
Expand-Archive -LiteralPath $download -DestinationPath $extracted -Force
# copy the module
Rename-Item -Path "$($extracted)\PowerSploit-master" -NewName "PowerSploit"
Copy-Item -Path "$($extracted)\PowerSploit" -Destination $ModulePath -Recurse -Force
#Disable AV
if ((Get-MpPreference).DisableRealtimeMonitoring -eq $false) {
Read-Host -Prompt "Please disable A/V and press ENTER to continue.."
}
#Set PowerShell ExecutionPolicy is top allow execution of PowerSploit
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force
#>
# remove any marks of the web/streams
Get-ChildItem -path $ModulePath -Recurse | Unblock-File
$x = 0
do
{
################################################################################
# SYSMON_CREATE_PROCESS: EventCode=1 RuleName=ProcessCreate
################################################################################
$TestName = "SYSMON_CREATE_PROCESS"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$Process = start-process -FilePath "notepad.exe" -WindowStyle Hidden -PassThru
Stop-Process -Id $Process.Id -ErrorAction stop
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_FILE_TIME: EventCode=2 RuleName=FileCreateTime
################################################################################
$TestName = "SYSMON_FILE_TIME"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$TemporaryFile = New-TemporaryFile
(Get-Item -path $TemporaryFile.FullName).CreationTime=("08 March 2016 18:00:00")
Remove-Item -Path $TemporaryFile.FullName
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_NETWORK_CONNECT: EventCode=3 RuleName=NetworkConnect
################################################################################
$TestName = "SYSMON_NETWORK_CONNECT"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$blah = Invoke-WebRequest -Uri "192.168.1.1" -DisableKeepAlive -UseBasicParsing
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_PROCESS_TERMINATE: EventCode=5 RuleName=ProcessTerminate
################################################################################
$TestName = "SYSMON_PROCESS_TERMINATE"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$Process = start-process -FilePath "notepad.exe" -WindowStyle Hidden -PassThru
Stop-Process -Id $Process.Id -ErrorAction stop
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_DRIVER_LOAD: EventCode=6 RuleName=DriverLoad
################################################################################
$TestName = "SYSMON_DRIVER_LOAD"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$ProcessPath = "$($SysinternalsSuitePath)\notmyfault64.exe"
$Process = start-process -FilePath $ProcessPath -ArgumentList @("/AcceptEula") -WindowStyle Hidden -PassThru
Start-Sleep -Seconds 1
Stop-Process -Id $Process.Id -ErrorAction stop -Force
Get-Service myfault | Stop-Service
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_IMAGE_LOAD: EventCode=7 RuleName=ImageLoad
################################################################################
$TestName = "SYSMON_IMAGE_LOAD"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$ProcessPath = "c:\windows\notepad.exe"
$Process = start-process -FilePath $ProcessPath -WindowStyle Hidden -PassThru
Stop-Process -Id $Process.Id -ErrorAction stop
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_CREATE_REMOTE_THREAD: EventCode=8 RuleName=CreateRemoteThread
################################################################################
# The CreateRemoteThread event detects when a process creates a thread in
# another process. This technique is used by malware to inject code and hide
# in other processes. The event indicates the source and target process. It
# gives information on the code that will be run in the new thread:
# StartAddress, StartModule and StartFunction. Note that StartModule and
# StartFunction fields are inferred, they might be empty if the starting
# address is outside loaded modules or known exported functions.
################################################################################
$TestName = "SYSMON_CREATE_REMOTE_THREAD"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$ScriptPath = "$($env:temp)\$($TestName).ps1"
if (Test-Path -Path $ScriptPath) { Remove-Item -Path $ScriptPath -Force }
$Content = @()
$Content += "param([string]`$processid,[string]`$dll)"
$Content += "Import-Module PowerSploit"
$Content += "if (Get-Process | ?{`$_.id -eq `$processid}) {"
$Content += " write-host `"Process id `$(`$processid)`" exists"
$Content += " if (Test-Path -Path `$dll) {"
$Content += " write-host `"file `$(`$dll)`" exists"
$Content += " Invoke-DllInjection -ProcessID `$processid -Dll `$dll"
$Content += " }"
$Content += "}"
Set-Content -PassThru $ScriptPath -Value $Content | Out-Null
$dll = "C:\Windows\System32\advapi32.dll"
$ProcessPath = "c:\windows\notepad.exe"
$Process = start-process -FilePath $ProcessPath -WindowStyle Hidden -PassThru
Start-Process -FilePath "Powershell.exe" -ArgumentList @("-version 2.0","-file $($ScriptPath)","-processid $($process.id)","-dll $($dll)") -Wait -WindowStyle Hidden
Stop-Process -Id $process.id -Force
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_RAWACCESS_READ: EventCode=9 RuleName=RawAccessRead
################################################################################
# The RawAccessRead event detects when a process conducts reading operations
# from the drive using the \\.\ denotation. This technique is often used by
# malware for data exfiltration of files that are locked for reading, as well
# as to avoid file access auditing tools. The event indicates the source
# process and target device.
# https://devblogs.microsoft.com/scripting/use-powershell-to-interact-with-the-windows-api-part-1/
###############################################################################
$TestName = "SYSMON_RAWACCESS_READ"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
# build the script to call
$ScriptPath = "$($env:temp)\$($TestName).ps1"
if (Test-Path -Path $ScriptPath) { Remove-Item -Path $ScriptPath -Force }
$Content = @()
$Content += "Import-Module PowerSploit -Force"
$Content += "Invoke-NinjaCopy -Path `"$($env:windir)\system32\calc.exe`" -LocalDestination `"$($env:temp)\calc.exe`""
Set-Content -Path $ScriptPath -Value $Content
$Process = Start-Process -FilePath "Powershell.exe" -ArgumentList @("-version 2.0","-file $($ScriptPath)") -Wait -WindowStyle Hidden -PassThru
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_FILE_CREATE: EventCode=11 RuleName=FileCreate
################################################################################
# File create operations are logged when a file is created or overwritten.
# This event is useful for monitoring autostart locations, like the Startup
# folder, as well as temporary and download directories, which are common
# places malware drops during initial infection.
###############################################################################
$TestName = "SYSMON_FILE_CREATE"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$TemporaryFile = New-TemporaryFile
Remove-Item $TemporaryFile -Force
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_REG_KEY: EventCode=12 RuleName=RegistryEvent (Object create and delete)
################################################################################
$TestName = "SYSMON_REG_KEY"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
New-Item -Path HKLM:\Software\DeleteMe | Out-Null
remove-item -Path HKLM:\Software\DeleteMe | Out-Null
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_REG_SETVALUE: EventCode=13 RuleName=RegistryEvent (Value Set)
################################################################################
$TestName = "SYSMON_REG_SETVALUE"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
New-Item -Path HKLM:\Software\DeleteMe | out-null
New-ItemProperty -Path HKLM:\Software\DeleteMe -Name Test -PropertyType String -Value "Hello World!" | out-null
remove-item -Path HKLM:\Software\DeleteMe | out-null
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_FILE_CREATE_STREAM_HASH: EventCode=15 RuleName=FileCreateStreamHash
################################################################################
$TestName = "SYSMON_FILE_CREATE_STREAM_HASH"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$TempFile = New-TemporaryFile
$StreamName = "StreamMessage"
$StreamText = "Hello World"
Set-Content -Path $TempFile.FullName -Stream $StreamName -Value $StreamText
$TempFile | Remove-Item -Force
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_CREATE_NAMEDPIPE: EventCode=17 RuleName=PipeEvent
# https://stackoverflow.com/questions/24096969/powershell-named-pipe-no-connection
################################################################################
$TestName = "SYSMON_CREATE_NAMEDPIPE"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
# create a named pipe
$pipeName = "testpipe$($i)"
$pipe = new-object System.IO.Pipes.NamedPipeServerStream $pipeName,'Out'
$pipe.Dispose()
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_CONNECT_NAMEDPIPE: EventCode=18 RuleName=PipeEvent
################################################################################
$TestName = "SYSMON_CONNECT_NAMEDPIPE"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
# build the script to run the pipe server
$ScriptPath = "$($env:temp)\$($TestName).ps1"
if (Test-Path -Path $ScriptPath) { Remove-Item -Path $ScriptPath -Force }
$Content = @()
$Content += "`$pipe = new-object System.IO.Pipes.NamedPipeServerStream 'testpipe','Out'"
$Content += "`$pipe.WaitForConnection()"
$Content += "`$sw = new-object System.IO.StreamWriter `$pipe"
$Content += "`$sw.AutoFlush = `$true"
$Content += "`$sw.WriteLine(`"Server pid is `$pid`")"
$Content += "`$sw.Dispose()"
$Content += "`$pipe.Dispose()"
Set-Content -Path $ScriptPath -Value $Content
$Process = Start-Process -FilePath "Powershell.exe" -ArgumentList @("-file $($ScriptPath)") -PassThru -WindowStyle Hidden
# create a named pipe
$pipe = new-object System.IO.Pipes.NamedPipeClientStream '.','testpipe','In'
$pipe.Connect()
$sr = new-object System.IO.StreamReader $pipe
while (($data = $sr.ReadLine()) -ne $null) { write-debug "Received: $data"}
$sr.Dispose()
$pipe.Dispose()
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_WMI_FILTER: EventCode=19 RuleName=WmiEvent
# SYSMON_WMI_CONSUMER: EventCode=20 RuleName=WmiEvent
# SYSMON_WMI_BINDING: EventCode=21 RuleName=WmiEvent
################################################################################
$TestName = "SYSMON_WMI_CONSUMER"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$command = 'powershell.exe -Command {write-host "hello world!"}'
$Filter = Set-WmiInstance -Namespace root/subscription -Class __EventFilter -Arguments @{
EventNamespace = 'root/cimv2'
Name = "TestFilter"
Query = "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance ISA 'Win32_Process' AND Name='calc.exe'"
QueryLanguage = 'WQL'
}
$Consumer = Set-WmiInstance -Namespace root/subscription -Class CommandLineEventConsumer -Arguments @{
Name = "TestConsumer"
CommandLineTemplate = $Command
}
$Binding = Set-WmiInstance -Namespace root/subscription -Class __FilterToConsumerBinding -Arguments @{
Filter = $Filter
Consumer = $Consumer
}
#Cleanup
Get-WmiObject __EventFilter -namespace root\subscription | ?{$_.Name -eq "TestFilter"} | Remove-WmiObject
Get-WmiObject CommandLineEventConsumer -Namespace root\subscription | ?{$_.Name -eq "TestConsumer"} | Remove-WmiObject
Get-WmiObject __FilterToConsumerBinding -Namespace root\subscription | ?{$_.filter -match "TestFilter"} | Remove-WmiObject
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_DNS_QUERY: EventCode=22 RuleName=DnsQuery
################################################################################
$TestName = "SYSMON_DNS_QUERY"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
Resolve-DnsName -Name "www.google.com" | Out-Null
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_FILE_DELETE: EventCode=23 RuleName=FileDelete
################################################################################
$TestName = "SYSMON_FILE_DELETE"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
$TemporaryFile = New-TemporaryFile
Add-Content -Value "Hello World!" -Path $TemporaryFile.FullName
Remove-Item -Path $TemporaryFile.FullName -Force
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
################################################################################
# SYSMON_CLIPBOARD: EventCode=24 RuleName=ClipboardChange
################################################################################
$TestName = "SYSMON_CLIPBOARD"
if ($ConstrainSysmonCollection -eq $True) {
$configpath = make-sysmon-config -name $TestName -sysmonPath $sysmonPath
reset-sysmon -sysmonPath $sysmonpath -configpath $configpath
}
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"Begin`""
write-host $Message ; add-content -Path $LogFile -Value $Message
$TestStart = (get-date) ; $TestCount = 0
do
{
$TestCount++
###########################################################################
# Payload:
Set-Clipboard -Value "Hello World!"
###########################################################################
} until ((New-TimeSpan -Start $TestStart).TotalSeconds -ge $TestDurationTotalSeconds)
$Message = "$(format-splunktime) TestName=`"$($TestName)`" TestDurationTotalSeconds=`"$($TestDurationTotalSeconds)`" TestStatus=`"End`" TestCount=`"$($TestCount)`""
write-host $Message ; add-content -Path $LogFile -Value $Message
Start-Sleep -Seconds 2
}
until ($x -gt 0)