Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RPORT tab completion crash when connected to remote dataservice #15194

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions lib/msf/core/db_manager/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,6 @@ def get_host(opts)
}
end

# Look for an address across all comms
def has_host?(wspace,addr)
::ApplicationRecord.connection_pool.with_connection {
address, scope = addr.split('%', 2)
wspace.hosts.find_by_address(addr)
agalway-r7 marked this conversation as resolved.
Show resolved Hide resolved
}
end
agalway-r7 marked this conversation as resolved.
Show resolved Hide resolved

# Returns a list of all hosts in the database
def hosts(opts)
::ApplicationRecord.connection_pool.with_connection {
Expand Down
25 changes: 14 additions & 11 deletions lib/msf/ui/console/module_option_tab_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ def option_values_dispatch(mod, o, str, words)
res << port
end
end
if res.empty?
res << rand(1..65534).to_s
end
Comment on lines -216 to -218
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can anyone explain why we were returning a random port suggestion when we couldn't find one in the DB?

when Msf::OptEnum
o.enums.each do |val|
res << val
Expand Down Expand Up @@ -338,19 +335,25 @@ def option_values_target_addrs(mod)
# Provide the target ports
#
def option_values_target_ports(mod)
res = [ ]
return res if !framework.db.active
return res if !mod.datastore['RHOST']
return [] unless framework.db.active
return [] if mod.datastore['RHOST'].nil?

host_addresses = mod.datastore['RHOST'].split.map do |addr|
address, _scope = addr.split('%', 2)
address
end

host = framework.db.has_host?(framework.db.workspace, mod.datastore['RHOST'])
return res if !host
hosts = framework.db.hosts({:address => host_addresses, :workspace => framework.db.workspace})
return [] if hosts.empty?

framework.db.services.each do |service|
if service.host_id == host.id
res = []
hosts.each do |host|
host.services.each do |service|
res << service.port.to_s
end
end
return res

res.uniq
end
end
end
Expand Down
19 changes: 15 additions & 4 deletions lib/rex/ui/text/input/readline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def initialize(tab_complete_proc = nil)

self.extend(::Readline)

if (tab_complete_proc)
if tab_complete_proc
::Readline.basic_word_break_characters = ""
::Readline.completion_proc = tab_complete_proc
@rl_saved_proc = tab_complete_proc
@rl_saved_proc = with_error_handling(tab_complete_proc)
::Readline.completion_proc = @rl_saved_proc
end
end

Expand All @@ -36,7 +36,7 @@ def initialize(tab_complete_proc = nil)
#
def reset_tab_completion(tab_complete_proc = nil)
::Readline.basic_word_break_characters = "\x00"
::Readline.completion_proc = tab_complete_proc || @rl_saved_proc
::Readline.completion_proc = tab_complete_proc ? with_error_handling(tab_complete_proc) : @rl_saved_proc
end


Expand Down Expand Up @@ -187,6 +187,17 @@ def readline_with_output(prompt, add_history=false)
end
end

private

def with_error_handling(proc)
proc do |*args|
proc.call(*args)
rescue StandardError => e
elog("tab_complete_proc has failed with args #{args}", error: e)
[]
end
end

end
rescue LoadError
end
Expand Down
3 changes: 1 addition & 2 deletions spec/support/shared/examples/msf/db_manager/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
unless ENV['REMOTE_DB']
it { is_expected.to respond_to :each_host }
it { is_expected.to respond_to :del_host }
it { is_expected.to respond_to :has_host? }
end

it { is_expected.to respond_to :find_or_create_host }
it { is_expected.to respond_to :get_host }
it { is_expected.to respond_to :hosts }
it { is_expected.to respond_to :report_host }
end
end