Skip to content

Commit e57275d

Browse files
Royce DavisRoyce Davis
authored andcommitted
added check cleanup method to command.rb
1 parent 6e257d5 commit e57275d

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

modules/auxiliary/admin/smb/command.rb

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,32 @@ module so thanks very much to the author/s of that great tool. This module is u
5050

5151
# This is the main controle method
5252
def run_host(ip)
53-
cmd = "C:\\WINDOWS\\SYSTEM32\\cmd.exe"
5453
text = "\\WINDOWS\\Temp\\#{Rex::Text.rand_text_alpha(16)}.txt"
55-
bat = "C:\\WINDOWS\\Temp\\#{Rex::Text.rand_text_alpha(16)}.bat"
56-
57-
#Try and Connect to the target
58-
begin
59-
connect()
60-
rescue StandardError => connecterror
61-
print_error("Unable to connect to the target: #{connecterror}")
62-
return
63-
end
54+
bat = "%WINDIR%\\Temp\\#{Rex::Text.rand_text_alpha(16)}.bat"
55+
smbshare = datastore['SMBSHARE']
6456

6557
#Try and authenticate with given credentials
66-
begin
67-
smb_login()
68-
rescue StandardError => autherror
69-
print_error("Unable to authenticate with given credentials: #{autherror}")
70-
return
71-
end
72-
73-
smbshare = datastore['SMBSHARE']
74-
75-
if execute_command(smbshare, ip, cmd, text, bat)
76-
get_output(smbshare, ip, text)
58+
if connect
59+
begin
60+
smb_login
61+
rescue StandardError => autherror
62+
print_error("Unable to authenticate with given credentials: #{autherror}")
63+
return
64+
end
65+
if execute_command(smbshare, ip, text, bat)
66+
o = get_output(smbshare, ip, text)
67+
end
68+
cleanup_after(smbshare, ip, text, bat)
7769
end
78-
cleanup_after(smbshare, ip, cmd, text, bat)
7970
end
8071

8172

8273

8374
# Executes specified Windows Command
84-
def execute_command(smbshare, ip, cmd, text, bat)
75+
def execute_command(smbshare, ip, text, bat)
8576
begin
8677
#Try and execute the provided command
87-
execute = "#{cmd} /C echo #{datastore['COMMAND']} ^> C:#{text} > #{bat} & #{cmd} /C start cmd.exe /C #{bat}"
78+
execute = "%COMSPEC% /C echo #{datastore['COMMAND']} ^> %SYSTEMDRIVE%#{text} > #{bat} & %COMSPEC% /C start cmd.exe /C #{bat}"
8879
simple.connect(smbshare)
8980
print_status("Executing your command on host: #{ip}")
9081
psexec(smbshare, execute)
@@ -110,22 +101,26 @@ def get_output(smbshare, ip, file)
110101
return
111102
end
112103
print_good("Command completed successfuly! Output from: #{ip}\r\n#{output}")
104+
return output
113105
rescue StandardError => output_error
114106
print_error("#{ip} - Error getting command output. #{output_error.class}. #{output_error}.")
115-
return output_error
107+
return nil
116108
end
117109
end
118110

119111

120112

121113
# This is the cleanup method, removes .txt and .bat file/s created during execution-
122-
def cleanup_after(smbshare, ip, cmd, text, bat)
114+
def cleanup_after(smbshare, ip, text, bat)
123115
begin
124116
# Try and do cleanup command
125-
cleanup = "#{cmd} /C del C:#{text} & del #{bat}"
117+
cleanup = "%COMSPEC% /C del %SYSTEMDRIVE%#{text} & del #{bat}"
126118
simple.connect(smbshare)
127119
print_status("Executing cleanup on host: #{ip}")
128120
psexec(smbshare, cleanup)
121+
#if !check_cleanup(smbshare, ip, text)
122+
# print_error("#{ip} - Unable to cleanup. Need to manually remove #{text} and #{bat} from the target.")
123+
#end
129124
rescue StandardError => cleanuperror
130125
print_error("Unable to processes cleanup commands: #{cleanuperror}")
131126
return cleanuperror
@@ -134,6 +129,19 @@ def cleanup_after(smbshare, ip, cmd, text, bat)
134129

135130

136131

132+
def check_cleanup(smbshare, ip, text)
133+
simple.connect("\\\\#{ip}\\#{smbshare}")
134+
if checktext = simple.open(text, 'ro')
135+
check = false
136+
else
137+
check = true
138+
end
139+
simple.disconnect("\\\\#{ip}\\#{smbshare}")
140+
return check
141+
end
142+
143+
144+
137145
# This code was stolen straight out of psexec.rb. Thanks very much HDM and all who contributed to that module!!
138146
# Instead of uploading and runing a binary. This method runs a single windows command fed into the #{command} paramater
139147
def psexec(smbshare, command)

0 commit comments

Comments
 (0)