This repository has been archived by the owner on May 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
PtHTools.psm1
1056 lines (896 loc) · 46.3 KB
/
PtHTools.psm1
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
#requires -version 3.0
Import-Module multithreading -force
Import-module Windows\DomainInfo -force
import-module Windows\AccountInfo -force
import-module Windows\General -force
import-module Windows\ADSI -force
import-module password -force
import-module windows\securestring -force
import-module windows\filesystem -force
function Find-PotentialPtHEvents(){
<#
.SYNOPSIS
Determines if a network logon meets specific criteria that may have been a PtH login.
.DESCRIPTION
Find-PotentialPtHEvents iterates over all non-domain controllers registered within active directory and queries their event logs for specific network logon criteria that PtH logins meet. The criteria is as follows:
network logon type == 3
domain != domain name
authentication mechanism == NTLM
username != ANONYMOUS
Each computer is contacted in parallel. The current model pushes all of the work to the individual domain-joined machines from the invoking process. The invoker waits until the invokee finishes and returns the results. The length of time the process should take is dependent on the size of the event logs and whether or not some of the machines that are domain-joined are down. In the case that a machine is down, the program will wait until a timeout occurs.
.PARAMETER ndays
The number of days back to go in the event log.
.PARAMETER interactive
Returns the results to the pipeline interactively instead of pretty printing them to the screen.
.EXAMPLE
Find-PotentialPtHEvents
Looks for PtH behavior in the event logs for the last 7 days.
.EXAMPLE
Find-PotentialPtHEvents -ndays 30
Looks for PtH behavior in the event logs for the last 30 days.
#>
[CmdletBinding(DefaultParameterSetName="All")]
param(
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="Single")]
[int]$ndays=7,
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="Single")]
[switch]$interactive,
[Parameter(Mandatory=$TRUE, ParameterSetName="Single")]
[switch]$single,
[Parameter(Mandatory=$TRUE, ParameterSetName="Single", ValueFromPipeline=$TRUE, position=1)]
[string]$computerName,
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="Single")]
[string]$channelName="Security"
)
BEGIN{
Test-Assert {(Test-ModuleExists multithreading) -eq $TRUE}
$script:NSECS_IN_1_HOUR = 3600000
$script:NSECS_IN_1_DAY = $script:NSECS_IN_1_HOUR * 24
}
PROCESS{
$mgr = New-ParallelTaskManager
try {
$results = @{}
$servers = $NULL
if($PSCmdlet.ParameterSetName -eq "All"){
$servers = @(Get-DomainComputersSansDomainControllers)
}
else{
$servers = $computerName
}
$task = {
param(
[Parameter(Mandatory=$TRUE)]
[string]$server,
[Parameter(Mandatory=$TRUE)]
[string]$channelName,
[Parameter(Mandatory=$TRUE)]
[int64]$nsecs
)
#$script:winXPLogonEvents = @{540="Success";528="Success";529="Failure"; 530="Failure"; 531="Failure"; 532="Failure"; 533="Failure"; 534="Failure"; 535="Failure";536="Failure";537="Failure";539="Failure"; }
$script:winVistaPlusLogonEvents = @{4624="Success";4625="Failure"}
function New-Result([string]$hostname, [string]$os, [DateTime]$TimeCreated, [int]$id, [string]$message, $event){
$result = "" | Select host, os, TimeCreated, Id, Message, Event
$result.host = $hostname
$result.os = $os
$result.timecreated = $timecreated
$result.id = $id
$result.message = $message
$result.event = $event
return $result
}
$domain = (([System.DirectoryServices.ActiveDirectory.domain]::GetCurrentDomain()).name).split(".")[0].toupper() #get ~NETBIOS name, which is how the event log stores the domain name
$results = @()
$os = $NULL
try{
$os = Get-WmiObject -class Win32_OperatingSystem -cn $server
if($os){
#os will be null if Get-WMI fails...for any reason
Switch -regex ($os.Version){
#used to support xp classic event logs.
#moving away from it in further development
"^6`.*"
{
#vista+
#silently continue to suppress the error that is thrown
#if no events were found meeting our criteria
#$events = @(Get-WinEvent -ErrorAction SilentlyCOntinue -computerName $server -FilterXML "<QueryList><Query Id='0' Path='Security'><Select Path='Security'>*[(System[Provider[@Name='Microsoft-Windows-Security-Auditing']]) and (System[TimeCreated[timediff(@SystemTime) <= $nsecs]]) and (System[EventID=4624] or System[EventID=4625]) and (EventData[Data[@Name='LogonType']='3' and Data[@Name='AuthenticationPackageName'] ='NTLM' and Data[@Name='TargetDomainName'] != '$domain' and Data[@Name='TargetUserName'] != 'ANONYMOUS LOGON' and Data[@Name='TargetUserName'] != '-']) ]</Select></Query></QueryList>")
$events = @(Get-WinEvent -ErrorAction SilentlyCOntinue -computerName $server -FilterXML "<QueryList><Query Id='0' Path='$channelName'><Select Path='$channelName'>*[(System[Provider[@Name='Microsoft-Windows-Security-Auditing']]) and (System[TimeCreated[timediff(@SystemTime) <= $nsecs]]) and (System[EventID=4624] or System[EventID=4625]) and (EventData[Data[@Name='LogonType']='3' and Data[@Name='AuthenticationPackageName'] ='NTLM' and Data[@Name='TargetDomainName'] != '$domain' and Data[@Name='TargetUserName'] != 'ANONYMOUS LOGON']) ]</Select></Query></QueryList>")
foreach($event in $events){
$results += New-Result $server $os.version $event.timecreated $event.id $event.message $event
}
break
}
DEFAULT
{
Write-Error "UNSUPPORTED Operating system: $($os.version)"
}
}
}
}
catch{
Write-Error "Could not connect to server: $server"
}
Write-Output $results
}
$nsecs = $script:NSECS_IN_1_DAY * $ndays
foreach ($server in $servers){
[void]$mgr.new_task($task, @($server, $channelName, $nsecs))
}
$results = $mgr.receive_alltasks()
if($interactive){
#return the results to the user
Write-Output $results
}
else{
#print out the results to the screen (Default)
$results | Sort-Object TimeCreated -descending | format-table host, os, timecreated, id, message
}
}
catch [System.Exception]
{
Write-Host $_.Exception.Message
}
finally{
$mgr = $NULL
}
}
END{}
}
function Disable-NetworkLogonsForGroup(){
param(
[CmdletBinding()]
[Parameter(Mandatory=$FALSE, ValueFromPipeline=$TRUE, position=1)]
[string]$group,
[Parameter(Mandatory=$FALSE)]
[string]$computerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$FALSE)]
[AllowEmptyString()]
[string]$description=""
)
BEGIN{}
PROCESS{
if(Test-GroupExists -local -name $group -computerName $computerName){
#could also do it this way.
#Get-Group -local $group | Get-GroupMembers -local | foreach{Remove-GroupMember -local -group $group -user $_.name}
Remove-Group -local -name $group -computerName $computerName
}
$adsiGroup = (New-Group -local -name $group -computerName $computerName -description $description)
if((Test-ADSISuccess $adsiGroup) -eq $TRUE){
$adminGroupName = Get-LocalAdminGroupName -computerName $computerName
Get-Group -local -computerName $computerName $adminGroupName | Get-GroupMembers -local -computerName $computerName | foreach{(Add-GroupMember -local -group $group -user $_.name -computerName $computerName)} | Out-Null
Write-Host "Successfully added local administrators to group $group on $computerName"
}
}
END{}
}
function Invoke-DenyNetworkAccess(){
<#
.SYNOPSIS
Adds all local administrators to a specific group that can be used to deny them network access.
.DESCRIPTION
Interfaces with the cmdlet Disable-NetworkLogonsForGroup to provide the guidance given in the PtH paper. If the user wants a different group name, then they can call Disable-NetworkLogonsForGroup expclitly and provide a custom group name.
.PARAMETER group
[string] The group name to put all local admins in.
.PARAMETER computername
[string] The local computername to create the new group on.
.PARAMETER description
[string] The description of the group.
.INPUTS
[string] See <Computername>
.OUTPUTS
None.
.EXAMPLE
Invoke-DenyNetworkAccess -group "DenyNetworkAccess" -computerName "host1" -description "Group used to remove network access to all local administrator."
#>
param(
[CmdletBinding()]
[Parameter(Mandatory=$FALSE)]
[string]$group="DenyNetworkAccess",
[Parameter(Mandatory=$FALSE, ValueFromPipeline=$TRUE, position=1)]
[string]$computerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$FALSE)]
[AllowEmptyString()]
[string]$description=""
)
BEGIN{}
PROCESS{
Disable-NetworkLogonsForGroup -group $group -computerName $computerName -description $description
}
END{}
}
#script variables for Edit-AllLocalAccountPasswords
$script:MIN_REQUIRED_DISK_SPACE = 25mb
function Edit-AllLocalAccountPasswords(){
<#
.SYNOPSIS
Changes a local account password for an inputted list of domain-joined machines <machinesFilePath> or by automatically detecting all of the registered machines on the domain.
.DESCRIPTION
This program changes a local account password for an inputted list of domain-joined machines <machinesFilePath> or by automatically detecting all of the registered machines on the domain. The length of the password is configurable by the <minPasswordLength> and <maxPasswordLength> parameters. If the -machinesFilePath switch is used, the program expects a file listing the hostnames, one on each line. Passwords are generated using the RNGCryptoServiceProvider .Net object. The local account (denoted by <localAccountName>) password is changed via the IADsUser interface. If for any reason, the password change is unsuccessful, then the program will notify the user which machine names failed to have the account's password changed. Upon completion, the machine will have written a tab delimited file to the path of <outFilePath> with each line consisting of: <machineName> <localAccountName> <newPassword>. By default, the output must be saved to a USB drive, but this can be disabled with setting forceUSBKeyUsage to $FALSE. It is recommended to run this script with a domain account.
.PARAMETER machinesFilePath
The path to a list of domain-joined machines to use for the password changing. The file is line delimited with each line consisting of a domain-joined machinename.
.PARAMETER minPasswordLength
The minimum password length (default value of 14) to use for the creation of the new password. Only used if random password is selected.
.PARAMETER maxPasswordLength
The maximum password length (default value of 25) to use for the creation of the new password. Only used if random password is selected.
.PARAMETER maxThreads
The maximum number of threads to attach to the runspace.
.PARAMETER forceUSBKeyUsage
Have the program ensure that the <outFilePath> location is on a USB drive (default value of true).
.PARAMETER localAccountName
The username for which the user desires to change the password.
.PARAMETER outFilePath
The filename to use for writing the output (both successful and unsuccessful password changes).
.NOTES
Running this cmdlet in multithreaded mode will not get you realtime feedback to stdout. Check the passwords.out and machinesNotChanged.out files to see the progress. machinesNotChanged.out does not contain any machines that are unavailable, which are filtered out by Test-IsComputerAlive or Get-AllAliveComputers.
.EXAMPLE
Edit-AllLocalAccountPasswords -localAccountName test -outFilePath .\passwords.out
Change the password for the local account name "test", in single threaded mode, and writing the output to the file passwords.out in the current working directory, where the current working directory must be on a usb drive.
.EXAMPLE
Edit-AllLocalAccountPasswords -localAccountName test -outFilePath .\passwords.out -forceUSBKeyUsage $FALSE
Change the password for the local accountname test and write the file to passwords.out in the current working directory, where the current workign directory does not require to be on a usb drive.
#>
[CmdletBinding(DefaultParameterSetName="All")]
param(
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="multithreading")]
[switch]$fast,
[Parameter(Mandatory=$TRUE, ParameterSetName="multithreading")]
[switch]$multithreaded,
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="multithreading")]
[string]$machinesFilePath,
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="multithreading")]
[int]$minPasswordLength = 14,
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="multithreading")]
[int]$maxPasswordLength = 25,
[Parameter(Mandatory=$FALSE, ParameterSetName="multithreading")]
[int]$maxThreads = 20,
[Parameter(Mandatory=$FALSE, ParameterSetName="All")]
[Parameter(Mandatory=$FALSE, ParameterSetName="multithreading")]
[bool]$forceUSBKeyUsage = $TRUE,
[Parameter(Mandatory=$TRUE, ParameterSetName="All")]
[Parameter(Mandatory=$TRUE, ParameterSetName="multithreading")]
[string]$localAccountName,
[Parameter(Mandatory=$TRUE, ParameterSetName="All")]
[Parameter(Mandatory=$TRUE, ParameterSetName="multithreading")]
[string]$outFilePath
)
BEGIN{
$machinesArray = $NULL
$available = $NULL
$unavailable = $NULL
$errors = $NULL
if($forceUSBKeyUsage -eq $TRUE){
if((Test-ForceUSBKeyUsage $outFilePath) -eq $FALSE){
#abort program. The outfile path is not on a removable device
throw "The outfile path is not on a removable device"
}
}
if((Test-SufficientDiskSpace $outFilePath $script:MIN_REQUIRED_DISK_SPACE) -eq $FALSE){
#see if volume contains enough space
throw "disk does not have enough free space to save password file"
}
if(-not ([System.String]::IsNullOrEmpty($machinesFilePath))){
if((Test-Path $machinesFilePath) -eq $FALSE){
throw "$machinesFilePath does not exist"
}
$machinesArray = Get-Content $machinesFilePath
}
else{
$machinesArray = @(Get-DomainComputersSansDomainControllers)
}
if($fast){
if($multithreaded){
$available = Get-AllComputersAlive -multithreaded -fast $machinesArray
$unavailable = Get-SetDifference $machinesArray $available
}
else{
$unavailable = @()
$tempMachinesArray = @()
foreach($m in $machinesArray){
if(Test-IsComputerAlive -fast $m){
$tempMachinesArray += $m
}
else{
$unavailable += $m
}
}
$available = $tempMachinesArray
$tempMachinesArray = $NULL
#===================================================================
# Write-Host "************Unavailable**************"
# $unavailable | foreach{Write-Host $_}
# Write-Host "*************************************"
#===================================================================
}
}
else{
$available = $machinesArray
}
#this should resolve to the locally defined Get-FunctionParameters
$fargs = Get-FunctionParametersPTHTools "Invoke-PasswordChange"
$fargs["multithreaded"] = $multithreaded.ispresent
$fargs["machineNames"] = $available
}
PROCESS{
$menu =
@"
`t1. Random passwords
`t2. Salt passwords.
`t3. Quit
"@
while($TRUE){
Write-Host $menu
$command = Read-Host "Enter command number"
switch($command){
1{
$fargs["random"] = [switch]$TRUE
[array]$errors = Invoke-PasswordChange @fargs
break
}
2{
$fargs["salted"] = [switch]$TRUE
[array]$errors = Invoke-PasswordChange @fargs
break
}
3{
break
}
default{
Write-Host "Invalid command $command"
break
}
}
if($unavailable -gt 0){
#unavailable hosts are only determined if the -fast option is on, which filters boxes that aren't responding to conventional probes
Write-Host "`r`n*********Unavailable*************"
$unavailable | foreach{Write-Host $_}
Write-Host "****************************`r`n"
}
if($errors.count -gt 0){
#anything that fails during run-time
Write-Host "`r`n*********Errors*************"
$errors | foreach {Write-Host $_}
Write-Host "****************************`r`n"
}
break
}
}
END{}
}
function Invoke-PasswordChange(){
[CmdletBinding()]
param(
[Parameter(Mandatory=$TRUE, ParameterSetName="Random")]
[switch]$random,
[Parameter(Mandatory=$TRUE, ParameterSetName="Salted")]
[switch]$salted,
[Parameter(Mandatory=$FALSE)]
[switch]$multithreaded,
[Parameter(Mandatory=$TRUE)]
[int]$minPasswordLength,
[Parameter(Mandatory=$TRUE)]
[int]$maxPasswordLength,
[Parameter(Mandatory=$FALSE)]
[int]$maxThreads,
[Parameter(Mandatory=$TRUE)]
[array]$machineNames,
[Parameter(Mandatory=$TRUE)]
[string]$localAccountName,
[Parameter(Mandatory=$TRUE)]
[string]$outFilePath
)
$passwordsArray = @()
if($random){
#build up password array for random passwords
foreach($m in $machineNames){
$passwordLength = Get-Random -Minimum $minPasswordLength -Maximum ($maxPasswordLength+1);
$passwordsArray += (New-RandomPassword -length $passwordLength)
}
}
else{
#build up password array for salted passwords
#read in the password from the user
$password = Read-PasswordFromUser
#prompt the user to see if they wanted the salt prepended or appended
do{
$cmd = (Read-Host "Enter location to place salt (p)repend or (a)ppend.").toLower()
}while(($cmd -ne "a") -and ($cmd -ne "p"))
#iterate over all machines and change the password for $machineName\$localAccountName
foreach($name in $machineNames){
$salt = $name.toLower()
if($cmd -eq "p"){
$saltedPassword = New-SecureStringPrependedWithString $salt $password
}
else{
$saltedPassword = New-SecureStringAppendedWithString $salt $password
}
$passwordsArray += $saltedPassword
}
}
Test-Assert {($passwordsArray).count -eq ($machineNames).count}
if($multithreaded){
Update-LocalAccountPasswordForAllHosts -multithreaded -localAccountName $localAccountName -machineNames $machineNames -passwords $passwordsArray -outFilePath $outFilePath
}
else{
Update-LocalAccountPasswordForAllHosts -localAccountName $localAccountName -machineNames $machineNames -passwords $passwordsArray -outFilePath $outFilePath
}
}
function Update-LocalAccountPasswordForAllHosts(){
param(
[Parameter(Mandatory=$FALSE)]
[switch]$multiThreaded,
[Parameter(Mandatory=$TRUE)]
[array]$machineNames,
[Parameter(Mandatory=$TRUE)]
[array]$passwords,
[Parameter(Mandatory=$TRUE)]
[string]$localAccountName,
[Parameter(Mandatory=$TRUE)]
[string]$outFilePath
)
$passwordFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($outFilePath)
$errorFile = Join-Path (Split-Path $passwordFile -Parent) "machinesNotChanged.out"
Out-File -FilePath $passwordFile > $NULL #clear file
Out-File -FilePath $errorFile > $NULL #clear file
if($multiThreaded){
Update-LocalAccountPasswordForAllHostsMT -machineNames $machineNames -passwords $passwords -localAccountName $localAccountName -passwordFile $passwordFile -errorFile $errorFile
}
else{
Update-LocalAccountPasswordForAllHostsST -machineNames $machineNames -passwords $passwords -localAccountName $localAccountName -passwordFile $passwordFile -errorFile $errorFile
}
}
function Update-LocalAccountPasswordForAllHostsMT(){
param(
[Parameter(Mandatory=$TRUE)]
[array]$machineNames,
[Parameter(Mandatory=$TRUE)]
[array]$passwords,
[Parameter(Mandatory=$TRUE)]
[string]$localAccountName,
[Parameter(Mandatory=$TRUE)]
[string]$passwordFile,
[Parameter(Mandatory=$TRUE)]
[string]$errorFile
)
try{
$mgr = New-ParallelTaskManager $maxThreads
$setPasswordSB = {
#script block for threaded password changing
#get local information through WinNT provider and change password
#through iADSuser interface. Output successful pw changes to
#$outFilePath and unsuccessful changes to $errorPath and console
param(
[string]$machineName,
[System.Security.SecureString]$password,
[string]$localAccountName,
[string]$passwordFile,
[string]$errorFile
)
Import-module Windows\DomainInfo -force
import-module Windows\AccountInfo -force
import-module Windows\General -force
import-module Windows\ADSI -force
import-module password -force
import-module windows\securestring -force
import-module windows\filesystem -force
function Append-FileThreadSafe($filepath, $data){
#appends $data to the file located at $filepath in a threadsafe
#manner. This script uses a global semaphore for mutual exclusion
$sem = New-Object -TypeName System.Threading.Semaphore(1,1,"Global\PWSem")
[void]$sem.waitOne()
try{
Out-File -FilePath $filepath -InputObject $data -Append > $NULL
}
finally{
[void]$sem.release()
$sem = $NULL
}
}
function Format-ErrorMessage($errorMessage){
if($errorMessage){
$msg = $errorMessage.split(":")
Write-Output (($errormessage.split(":"))[1].replace('"', "").trim())
}
}
function Edit-LocalAccountPassword(){
param(
[Parameter(Mandatory=$TRUE)]
[AllowNull()]
[ADSI]$account,
[Parameter(Mandatory=$TRUE)]
[System.Security.SecureString]$password
)
if(Test-ADSISuccess $account){
$account.setPassword((ConvertFrom-SecureStringToBStr $password))
if($?){
$account.setInfo()
if($?){
return $TRUE
}
else{
throw "could not set the password for account $account"
}
}
else{
throw "could not set the password for account $account"
}
}
return $FALSE
}
try{
$success = Edit-LocalAccountPassword (Get-User -local -name $localAccountName -computerName $machineName) ($password)
if($success){
$output = "$machineName`t`t$localAccountName`t`t$(ConvertFrom-SecureStringToBStr $password)"
Write-Output $outpu
Append-FileThreadSafe $passwordFile $output
}
else{
throw [System.InvalidOperationException]
}
}
catch [Exception] {
Append-FileThreadSafe $errorFile $machineName
$error | foreach{Format-ErrorMessage $($_.Exception.message)} | foreach{Write-Output "Error changing password for $machineName`: $_"}
}
}
for($i=0; $i -lt $machineNames.count; $i++ ){
#iterate over all machine names and change password
[void]$mgr.new_task($setPasswordSB, @($machineNames[$i], $passwords[$i], $localAccountName, $passwordFile, $errorFile))
}
$output = $mgr.receive_alltasks()
[array]$errors = $output | where {$_ -ne $NULL}
return $errors
}
finally{
$jobs = $NULL
}
}
function Update-LocalAccountPasswordForAllHostsST(){
param(
[Parameter(Mandatory=$TRUE)]
[array]$machineNames,
[Parameter(Mandatory=$TRUE)]
[array]$passwords,
[Parameter(Mandatory=$TRUE)]
[string]$localAccountName,
[Parameter(Mandatory=$TRUE)]
[string]$passwordFile,
[Parameter(Mandatory=$TRUE)]
[string]$errorFile
)
for($i=0; $i -lt $machineNames.count; $i++){
try{
$success = Edit-LocalAccountPassword (Get-User -local -name $localAccountName -computerName $machineNames[$i]) ($passwords[$i])
if($success){
$output = "$($machineNames[$i])`t`t$localAccountName`t`t$(ConvertFrom-SecureStringToBStr ($passwords[$i]))"
Out-File -FilePath $passwordFile -InputObject $output -Append | Out-Null
Write-Host "Password successfully changed on $($machineNames[$i]) for $($machineNames[$i])\$localAccountName"
}
else{
throw [System.InvalidOperationException]
}
}
catch [Exception] {
$message = "Error changing password on $($machineNames[$i]) for $($machineNames[$i])\$localAccountName"
Write-Output $message
Out-File -FilePath $errorFile -InputObject $machineNames[$i] -Append
}
}
}
function Edit-LocalAccountPassword(){
param(
[Parameter(Mandatory=$TRUE)]
[AllowNull()]
[ADSI]$account,
[Parameter(Mandatory=$TRUE)]
[System.Security.SecureString]$password
)
if(Test-ADSISuccess $account){
$account.setPassword((ConvertFrom-SecureStringToBStr $password))
if($?){
$account.setInfo()
if($?){
return $TRUE
}
else{
throw "could not set the password for account $account"
}
}
else{
throw "could not set the password for account $account"
}
}
return $FALSE
}
function Test-ForceUSBKeyUsage(){
param(
[Parameter(Mandatory=$TRUE, position=1)]
[string]$outFilePath
)
if($(Test-isPathOnUSBDrive $outFilePath) -eq $FALSE){
return $FALSE
}
return $TRUE
}
function Get-FunctionParametersPTHTools(){
param(
[string]$_function
)
$result = New-Object "System.collections.Generic.Dictionary[System.String, System.Object]"
$cmd = Get-command $_function
foreach($key in $cmd.Parameters.keys){
$metadata = $cmd.parameters.item($key)
try{
$value = (Get-Variable -ErrorAction Stop -errorvariable "fpErr" -scope 1 $key).value
if($value -or $metadata.attributes.typeid.name -eq $script:ALLOW_EMPTY_STRING_ATTR){
$result.add($key, $value)
}
}
catch [System.Management.Automation.ItemNotFoundException]{
# Write-Host "CANNOT FIND PARAMETER: $key for function $_function"
}
}
return $result
}
function Get-SetDifference(){
param(
[array]$lhs,
[array]$rhs
)
#convert $rhs to a dictionary and then test if something in $lhs is not in $rhs.
$table = New-Object System.Collections.Hashtable
$rhs | foreach{$table.add($_, $TRUE)}
$difference = @()
foreach($ele in $lhs){
if(-not ($table.containsKey($ele))){
$difference += $ele
}
}
return $difference
}
function Get-LocalAccountSummaryOnDomain(){
[CmdletBinding()]
param()
BEGIN{
}
PROCESS{
$localAccountMgr = New-ParallelTaskManager 25
$adminAccountMgr = New-ParallelTaskManager 25
try{
$machinesArray = @(Get-DomainComputersSansDomainControllers)
$localAccountSB = {
param(
[string]$computername=$env:COMPUTERNAME
)
import-module Windows\AccountInfo -force
$localAccount = New-Object PSObject
$localAccount | add-member -membertype noteproperty "host" $computername
try{
$accountNames = (Get-Group -local -computerName $computername | Get-GroupMembers -local -computerName $computername).name
$localAccount | add-member -membertype noteproperty "accountNames" $($accountNames | Sort-Object)
}
catch{
Write-Output "Host $computername is unavailable"
Write-output $error
Write-Output "`r`nException $($_.exception.toString()) occurred getting Local Admin Accounts on host: $machineName"
}
finally{
$localAccount
}
}
$adminAccountSB = {
param(
[string]$computername=$env:COMPUTERNAME
)
import-module Windows\AccountInfo -force
$adminAccounts = New-Object PSObject
$adminAccounts | add-member -membertype noteproperty "host" $computername
try{
$accountNames = ((Get-Group -local -name $(Get-LocalAdminGroupName) -computerName $computername) | Get-GroupMembers -local -computerName $computername).name
$adminAccounts | add-member -membertype noteproperty "accountNames" $($accountNames | Sort-Object)
}
catch{
Write-Output "Host $computername is unavailable"
Write-output $error
Write-Output "`r`nException $($_.exception.toString()) occurred getting Local Admin Accounts on host: $machineName"
}
finally{
$adminAccounts
}
}
foreach($m in $machinesArray){
[void]$localAccountMgr.new_task($localAccountSB, @($m))
[void]$adminAccountMgr.new_task($adminAccountSB, @($m))
}
$accountOutput = $localAccountMgr.receive_alltasks()
$adminOutput = $adminAccountMgr.receive_allTasks()
$currFormatEnumerationLimit = $global:formatEnumerationLimit
$global:formatEnumerationLimit = -1
Write-Host "*****Summary of Local Accounts*****"
#$accountOutput | where {$_.accountnames -ne $NULL} | Select host, accountnames | sort host | group -property accountnames | Sort -descending count | format-table @{Label="Count";Expression={$_.count};Width=15;Alignment="Left"}, @{label='Host';Expression={$_.group.host};Width=50}, @{label='Local Accounts';Expression={$_.group.accountnames | Sort -unique }} -wrap
$accountOutput | where {$_.accountnames -ne $NULL} | Select host, accountnames | group -property accountnames | Sort -descending count | format-table @{label='Host';Expression={$_.group[0].host};Alignment="Left";Width=50}, @{label='Local Accounts';Expression={$_.group[0].accountnames | Sort -unique }} -wrap | Out-Host
Write-Host "***********************************"
Write-Host "*****Summary of Local Admin Accounts*****"
$adminOutput | where {$_.accountnames -ne $NULL} | Select host, accountnames | group -property accountnames | Sort -descending count | format-table @{label='Host';Expression={$_.group | foreach{$_.host}};Alignment="Left";Width=50}, @{label='Admin Accounts';e={$_.group[0].accountnames | Sort -unique}} -wrap | Out-Host
Write-Host "*****************************************"
Write-Host "*****Unique Local Accounts*****"
$accountOutput | foreach{$_.accountNames} | Sort -unique | Out-Host
Write-Host "*******************************"
Write-Host "*****Unique Admin Accounts*****"
$adminOutput | foreach{$_.accountNames} | Sort -unique | Out-Host
Write-Host "*******************************"
Write-Host "*****Unavailable Hosts*****"
$accountOutput | Where {$_.accountnames -eq $NULL} | foreach{$_.host} | Out-Host
Write-Host "***************************"
$global:formatEnumerationLimit = $currFormatEnumerationLimit
}
finally{
$localAccountMgr = $NULL
$adminAccountMgr = $NULL
}
}
END{}
}
function Invoke-SmartcardHashRefresh() {
<#
.SYNOPSIS
Changes the hash for any Active Directory accounts that require smartcards for login.
.DESCRIPTION
Changes the hash for any Active Directory accounts that require smartcards for login.
.PARAMETER User
The samAccountName or distinguishedName of the user(s) to reset smartcard hashes for.
.PARAMETER Group
The samAccountName or distinguishedName of the group(s) containing users to reset smartcard hashes for.
.PARAMETER OU
The distinguishedName of the organizational unit(s) containing hashes to reset smartcard hashes for.
.PARAMETER Container
The distinguishedName of the container(s) containing hashes to reset smartcard hashes for.
.PARAMETER AlwaysUpdatePasswordLastSet
Always update the password last set property's timestamp even if the account has the password never expires flag enabled.
.PARAMETER Evict
Refresh the password hash two times to force active accounts to get locked out for an eviction operation.
.EXAMPLE
Invoke-SmartcardHashRefresh
.EXAMPLE
Invoke-SmartcardHashRefresh -Verbose -WhatIf
.EXAMPLE
Invoke-SmartcardHashRefresh -User 'scuser1',scuser2' -Verbose
.EXAMPLE
Invoke-SmartcardHashRefresh -User 'scuser1' -AlwaysUpdatePasswordLastSet -Verbose
.EXAMPLE
Invoke-SmartcardHashRefresh -Group 'scgroup1' -Verbose
.EXAMPLE
Invoke-SmartcardHashRefresh -OU 'OU=Smartcard Users,OU=Domain Users,DC=test,DC=net' -Verbose
.EXAMPLE
Invoke-SmartcardHashRefresh -User 'CN=Users,DC=test,DC=net' -Verbose
#>
[CmdletBinding(SupportsShouldProcess=$true)]
[OutputType([void])]
Param(
[Parameter(Position=0, Mandatory=$false, HelpMessage='The samAccountName or distinguishedName of the user(s) to reset smartcard hashes for')]
[ValidateNotNullOrEmpty()]
[string[]]$User,
[Parameter(Position=1, Mandatory=$false, HelpMessage='The samAccountName or distinguishedName of the group(s) containing users to reset smartcard hashes for')]
[ValidateNotNullOrEmpty()]
[string[]]$Group,
[Parameter(Position=2, Mandatory=$false, HelpMessage='The distinguishedName of the organizational unit(s) containing hashes to reset smartcard hashes for')]
[ValidateNotNullOrEmpty()]
[string[]]$OU,
[Parameter(Position=3, Mandatory=$false, HelpMessage='The distinguishedName of the container(s) containing hashes to reset smartcard hashes for')]
[ValidateNotNullOrEmpty()]
[string[]]$Container,
[Parameter(Position=4, Mandatory=$false, HelpMessage='Always update the password last set property even if the account has the password never expires flag enabled')]
[ValidateNotNullOrEmpty()]
[switch]$AlwaysUpdatePasswordLastSet,
[Parameter(Position=5, Mandatory=$false, HelpMessage='Refresh the password hash two times to force active accounts to get locked out for an eviction operation')]
[ValidateNotNullOrEmpty()]
[switch]$Evict
)
Import-Module ActiveDirectory -Verbose:$false
$parameters = $PSBoundParameters
$properties = [string[]]@('PasswordLastSet','PasswordNeverExpires','SamAccountName','SmartcardLogonRequired')
if ($parameters.ContainsKey('User')) {
$validUsers = New-Object System.Collections.Generic.List[string]
$User | Select-Object -Unique | ForEach-Object { try { Get-ADUser -Identity $_ | Out-Null; $validUsers.Add($_) } catch {} }
$users = $validUsers | ForEach-Object { Get-ADUser -Identity $_ -Properties $properties } # can't use -Filter and -Identity together
} elseif ($parameters.ContainsKey('Group')) {
$validGroups = New-Object System.Collections.Generic.List[string]
$Group | Select-Object -Unique | ForEach-Object { try { Get-ADGroup -Identity $_ | Out-Null; $validGroups.Add($_) } catch {} }
$users = $validGroups | ForEach-Object { Get-ADGroupMember -Identity $_ -Recursive | ForEach-Object { Get-ADUser -Identity $_ -Properties $properties } } # can't use -Filter and -Identity together
} elseif ($parameters.ContainsKey('OU')) { # could collapse OU and Container into same case since Test-Path works for either one
$validOUs = New-Object System.Collections.Generic.List[string]
$OU | Select-Object -Unique | ForEach-Object { try { Get-ADOrganizationalUnit -Identity $_ | Out-Null; $validOUs.Add($_) } catch {} }
$users = $validOUs | ForEach-Object { Get-ADUser -Filter 'SmartcardLogonRequired -eq $true' -Properties $properties -SearchScope Subtree -SearchBase $_ }
} elseif ($parameters.ContainsKey('Container')) {
$validContainers = New-Object System.Collections.Generic.List[string]
$Container | Select-Object -Unique | ForEach-Object { if (Test-Path "AD:\$_"){ $validContainers.Add($_) } } | Out-Null # could collapse OU and Container into same case since Test-Path works for either one
$users = $validContainers | ForEach-Object { Get-ADUser -Filter 'SmartcardLogonRequired -eq $true' -Properties $properties -SearchScope Subtree -SearchBase $_ }
} else {
$users = Get-ADUser -Filter 'SmartcardLogonRequired -eq $true' -Properties $properties
}
# the Group case uses -Recursive so it is possible for a user to be repeated so use Select-Object -Unique to filter out repeats
# not all cases (User and Group) can use -Filter so use Where-Object to include only smartcard enabled logins
$users | Select-Object -Unique | Where-Object { $_.SmartcardLogonRequired -eq $true } | ForEach-Object {
# ChangePasswordAtLogon cannot be set to $true or 1 for an account that also has the PasswordNeverExpires property set to true.
if (-not($_.PasswordNeverExpires) -or $AlwaysUpdatePasswordLastSet) {
#$originalValue = $_.PasswordLastSet -eq 0
if ($AlwaysUpdatePasswordLastSet) {
Set-ADUser -Identity $_ -PasswordNeverExpires $false
}
Set-ADUser -Identity $_ -ChangePasswordAtLogon $true
Set-ADUser -Identity $_ -ChangePasswordAtLogon $false
if ($AlwaysUpdatePasswordLastSet) {
Set-ADUser -Identity $_ -PasswordNeverExpires $true
}
} else {
Write-Warning -Message ('Skipped updating the password last set property for user with login name of {0} because their password never expires. Use the AlwaysUpdatePasswordLastSet option to force an update to the password last set property' -f $_.SamAccountName)
}
Set-ADUser -Identity $_ -SmartcardLogonRequired $false
Set-ADUser -Identity $_ -SmartcardLogonRequired $true
if ($Evict) {
Set-ADUser -Identity $_ -SmartcardLogonRequired $true
}
Write-Verbose -Message ('Refreshed smartcard hash for user with login name {0}' -f $_.SamAccountName)
}
}
function Find-OldSmartcardHash() {
<#
.SYNOPSIS
Finds smartcard users whose Active Directory account's hash is over a certain age.
.DESCRIPTION
Outputs smartcard users and the number of days when their password was last set,
where the password was last set more than a specified number of days ago
(or 60 days by default).
.PARAMETER limit