Dont wrap object in array when using ID parameter

GSoC/Meterpreter_Web_Console
James Barnett 2018-07-27 16:41:11 -05:00
parent 829b43f743
commit 37706e094d
No known key found for this signature in database
GPG Key ID: 647983861A4EC5EA
13 changed files with 28 additions and 5 deletions

View File

@ -5,7 +5,7 @@ module Msf::DBManager::Cred
::ActiveRecord::Base.connection_pool.with_connection { ::ActiveRecord::Base.connection_pool.with_connection {
# If :id exists we're looking for a specific record, skip the other stuff # If :id exists we're looking for a specific record, skip the other stuff
if opts[:id] && !opts[:id].empty? if opts[:id] && !opts[:id].empty?
return Metasploit::Credential::Core.find(opts[:id]) return Array.wrap(Metasploit::Credential::Core.find(opts[:id]))
end end
wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework) wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)

View File

@ -9,7 +9,7 @@ module CredentialServlet
end end
def self.registered(app) def self.registered(app)
app.get CredentialServlet.api_path, &get_credentials app.get CredentialServlet.api_path_with_id, &get_credentials
app.post CredentialServlet.api_path, &create_credential app.post CredentialServlet.api_path, &create_credential
app.put CredentialServlet.api_path_with_id, &update_credential app.put CredentialServlet.api_path_with_id, &update_credential
app.delete CredentialServlet.api_path, &delete_credentials app.delete CredentialServlet.api_path, &delete_credentials
@ -33,6 +33,8 @@ module CredentialServlet
json = cred.as_json(include: includes).merge(private_class: cred.private.class.to_s) json = cred.as_json(include: includes).merge(private_class: cred.private.class.to_s)
response << json response << json
end end
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
response = format_cred_json(data) response = format_cred_json(data)
set_json_data_response(response: response) set_json_data_response(response: response)
rescue => e rescue => e

View File

@ -31,6 +31,8 @@ module HostServlet
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.hosts(sanitized_params) data = get_db.hosts(sanitized_params)
includes = [:loots] includes = [:loots]
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data, includes: includes) set_json_data_response(response: data, includes: includes)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error getting hosts:', code: 500) print_error_and_create_response(error: e, message: 'There was an error getting hosts:', code: 500)

View File

@ -23,8 +23,10 @@ module LoginServlet
lambda { lambda {
begin begin
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
response = get_db.logins(sanitized_params) data = get_db.logins(sanitized_params)
set_json_response(response) # Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_response(data)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving logins:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving logins:', code: 500)
end end

View File

@ -61,6 +61,8 @@ module LootServlet
tmp_params = sanitize_params(params) tmp_params = sanitize_params(params)
opts[:id] = tmp_params[:id] if tmp_params[:id] opts[:id] = tmp_params[:id] if tmp_params[:id]
data = get_db.update_loot(opts) data = get_db.update_loot(opts)
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data) set_json_data_response(response: data)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error updating the loot:', code: 500) print_error_and_create_response(error: e, message: 'There was an error updating the loot:', code: 500)

View File

@ -51,6 +51,8 @@ module NoteServlet
tmp_params = sanitize_params(params) tmp_params = sanitize_params(params)
opts[:id] = tmp_params[:id] if tmp_params[:id] opts[:id] = tmp_params[:id] if tmp_params[:id]
data = get_db.update_note(opts) data = get_db.update_note(opts)
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data) set_json_data_response(response: data)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error updating the note:', code: 500) print_error_and_create_response(error: e, message: 'There was an error updating the note:', code: 500)

View File

@ -26,6 +26,8 @@ module ServiceServlet
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.services(sanitized_params) data = get_db.services(sanitized_params)
includes = [:host] includes = [:host]
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data, includes: includes) set_json_data_response(response: data, includes: includes)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving services:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving services:', code: 500)

View File

@ -23,6 +23,8 @@ module SessionEventServlet
begin begin
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.session_events(sanitized_params) data = get_db.session_events(sanitized_params)
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data) set_json_data_response(response: data)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving session events:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving session events:', code: 500)

View File

@ -24,6 +24,8 @@ module SessionServlet
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.sessions(sanitized_params) data = get_db.sessions(sanitized_params)
includes = [:host] includes = [:host]
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data, includes: includes) set_json_data_response(response: data, includes: includes)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving sessions:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving sessions:', code: 500)

View File

@ -50,6 +50,8 @@ module UserServlet
tmp_params = sanitize_params(params) tmp_params = sanitize_params(params)
opts[:id] = tmp_params[:id] if tmp_params[:id] opts[:id] = tmp_params[:id] if tmp_params[:id]
data = get_db.update_user(opts) data = get_db.update_user(opts)
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data) set_json_data_response(response: data)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error creating the user:', code: 500) print_error_and_create_response(error: e, message: 'There was an error creating the user:', code: 500)

View File

@ -23,6 +23,8 @@ module VulnAttemptServlet
begin begin
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.vuln_attempts(sanitized_params) data = get_db.vuln_attempts(sanitized_params)
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data) set_json_data_response(response: data)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving vuln attempts:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving vuln attempts:', code: 500)

View File

@ -26,6 +26,8 @@ module VulnServlet
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.vulns(sanitized_params) data = get_db.vulns(sanitized_params)
includes = [:host, :vulns_refs, :refs, :module_refs] includes = [:host, :vulns_refs, :refs, :module_refs]
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data, includes: includes) set_json_data_response(response: data, includes: includes)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving vulns:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving vulns:', code: 500)

View File

@ -27,7 +27,8 @@ module WorkspaceServlet
sanitized_params = sanitize_params(params) sanitized_params = sanitize_params(params)
data = get_db.workspaces(sanitized_params) data = get_db.workspaces(sanitized_params)
# Only return the single object if the user used the resource/ID GET request
data = data.first if data.count == 1 && request.url =~ /\/\d$/
set_json_data_response(response: data, includes: includes) set_json_data_response(response: data, includes: includes)
rescue => e rescue => e
print_error_and_create_response(error: e, message: 'There was an error retrieving workspaces:', code: 500) print_error_and_create_response(error: e, message: 'There was an error retrieving workspaces:', code: 500)