Skip to content

Commit 52b8f40

Browse files
committed
Refactor change host methods, remove debug output
1 parent 629f79e commit 52b8f40

File tree

5 files changed

+18
-54
lines changed

5 files changed

+18
-54
lines changed

lib/metasploit/framework/data_service/proxy/host_data_proxy.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def report_hosts(hosts)
4242

4343
def update_host(opts)
4444
begin
45-
$stderr.puts "HostDataProxy.update_host(): opts = #{opts}" # TODO: remove
4645
data_service = self.get_data_service()
4746
data_service.update_host(opts)
4847
rescue Exception => e

lib/metasploit/framework/data_service/remote/http/remote_host_data_service.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def report_hosts(hosts)
2424
end
2525

2626
def update_host(opts)
27-
$stderr.puts "RemoteHostDataService.update_host(): opts = #{opts}" # TODO: remove
2827
path = HOST_API_PATH
2928
if opts && opts[:id]
3029
id = opts.delete(:id)

lib/msf/core/db_manager/host.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ def has_host?(wspace,addr)
143143

144144
# Returns a list of all hosts in the database
145145
def hosts(opts)
146-
$stderr.puts "DBManager::Host.hosts(): opts = #{opts}" # TODO: remove
147146
wspace = opts[:workspace] || opts[:wspace] || workspace
148147
if wspace.kind_of? String
149148
wspace = find_workspace(wspace)
@@ -155,7 +154,6 @@ def hosts(opts)
155154
conditions[:state] = [Msf::HostState::Alive, Msf::HostState::Unknown] if opts[:non_dead]
156155
conditions[:address] = opts[:address] if opts[:address] && !opts[:address].empty?
157156
conditions[:id] = opts[:id] if opts[:id] && !opts[:id].empty?
158-
$stderr.puts "DBManager::Host.hosts(): conditions = #{conditions}" # TODO: remove
159157

160158
if opts[:search_term] && !opts[:search_term].empty?
161159
column_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Mdm::Host, opts[:search_term])
@@ -272,14 +270,11 @@ def report_host(opts)
272270
end
273271

274272
def update_host(opts)
275-
$stderr.puts "DBManager::Host.update_host(): opts = #{opts}" # TODO: remove
276273
# TODO: remove unneeded workspace from opts until it's not automatically added to remote requests
277-
wspace = opts.delete(:workspace)
278-
$stderr.puts "DBManager::Host.update_host(): delete workspace from opts; wspace = #{wspace}" # TODO: remove
274+
opts.delete(:workspace)
279275

280276
::ActiveRecord::Base.connection_pool.with_connection {
281277
id = opts.delete(:id)
282-
$stderr.puts "DBManager::Host.update_host(): id = #{id}, opts = #{opts}" # TODO: remove
283278
Mdm::Host.update(id, opts)
284279
}
285280
end

lib/msf/core/db_manager/http/servlet/host_servlet.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def self.registered(app)
2222
def self.get_host
2323
lambda {
2424
begin
25-
$stderr.puts "HostServlet.get_host(): params[:id] = #{params[:id]}" if params[:id] # TODO: remove
2625
opts = parse_json_request(request, false)
2726
data = get_db().hosts(params.symbolize_keys)
2827
includes = [:loots]
@@ -50,12 +49,8 @@ def self.update_host
5049
lambda {
5150
begin
5251
opts = parse_json_request(request, false)
53-
$stderr.puts "HostServlet.update_host(): opts = #{opts}" # TODO: remove
54-
$stderr.puts "HostServlet.update_host(): params = #{params}" # TODO: remove
55-
# opts_and_params = params ? opts.merge(params.symbolize_keys) : opts
5652
tmp_params = params.symbolize_keys
5753
opts[:id] = tmp_params[:id] if tmp_params[:id]
58-
$stderr.puts "HostServlet.update_host(): opts = #{opts}" # TODO: remove
5954
data = get_db().update_host(opts)
6055
set_json_response(data)
6156
rescue Exception => e

lib/msf/ui/console/command_dispatcher/db.rb

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -231,52 +231,28 @@ def cmd_hosts_help
231231
cmd_hosts("-h")
232232
end
233233

234-
def change_host_info(host_ranges, data)
234+
# Changes the specified host data
235+
#
236+
# @param host_ranges - range of hosts to process
237+
# @param host_data - hash of host data to be updated
238+
def change_host_data(host_ranges, host_data)
239+
if !host_data || host_data.length != 1
240+
print_error("A single key-value data hash is required to change the host data")
241+
return
242+
end
243+
attribute = host_data.keys[0]
244+
235245
if host_ranges == [nil]
236-
print_error("In order to change the host info, you must provide a range of hosts")
246+
print_error("In order to change the host #{attribute}, you must provide a range of hosts")
237247
return
238248
end
239249

240-
$stderr.puts "Msf::Ui::Console::CommandDispatcher::Db.change_host_info(): host_ranges = #{host_ranges}"
241250
each_host_range_chunk(host_ranges) do |host_search|
242-
$stderr.puts "Msf::Ui::Console::CommandDispatcher::Db.change_host_info(): host_search = #{host_search}\n"
243251
break if !host_search.nil? && host_search.empty?
244252

245253
framework.db.hosts(framework.db.workspace, false, host_search).each do |host|
246-
$stderr.puts "Msf::Ui::Console::CommandDispatcher::Db.change_host_info(): host = #{host}, host.id = #{host.id}, host.address = #{host.address}, host.workspace_id = #{host.workspace_id}"
247-
framework.db.update_host(id: host.id, info: data)
248-
framework.db.report_note(:host => host.address, :type => 'host.info', :data => data)
249-
$stderr.puts "************************************\n"
250-
end
251-
end
252-
end
253-
254-
def change_host_name(rws, data)
255-
if rws == [nil]
256-
print_error("In order to change the host name, you must provide a range of hosts")
257-
return
258-
end
259-
260-
rws.each do |rw|
261-
rw.each do |ip|
262-
id = framework.db.get_host(:address => ip).id
263-
framework.db.hosts.update(id, :name => data)
264-
framework.db.report_note(:host => ip, :type => 'host.name', :data => data)
265-
end
266-
end
267-
end
268-
269-
def change_host_comment(rws, data)
270-
if rws == [nil]
271-
print_error("In order to change the comment, you must provide a range of hosts")
272-
return
273-
end
274-
275-
rws.each do |rw|
276-
rw.each do |ip|
277-
id = framework.db.get_host(:address => ip).id
278-
framework.db.hosts.update(id, :comments => data)
279-
framework.db.report_note(:host => ip, :type => 'host.comments', :data => data)
254+
framework.db.update_host(host_data.merge(id: host.id))
255+
framework.db.report_note(host: host.address, type: "host.#{attribute}", data: host_data[attribute])
280256
end
281257
end
282258
end
@@ -497,13 +473,13 @@ def cmd_hosts(*args)
497473

498474
case
499475
when mode == [:new_info]
500-
change_host_info(host_ranges, info_data)
476+
change_host_data(host_ranges, info: info_data)
501477
return
502478
when mode == [:new_name]
503-
change_host_name(host_ranges, name_data)
479+
change_host_data(host_ranges, name: name_data)
504480
return
505481
when mode == [:new_comment]
506-
change_host_comment(host_ranges, comment_data)
482+
change_host_data(host_ranges, comments: comment_data)
507483
return
508484
when mode == [:tag]
509485
begin

0 commit comments

Comments
 (0)