Skip to content

Commit 629f79e

Browse files
committed
WIP remote host update
1 parent f176e33 commit 629f79e

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def hosts(wspace = workspace, non_dead = false, addresses = nil, search_term = n
66
opts = {}
77
opts[:wspace] = wspace
88
opts[:non_dead] = non_dead
9-
opts[:addresses] = addresses
9+
opts[:address] = addresses
1010
opts[:search_term] = search_term
1111
data_service.hosts(opts)
1212
rescue Exception => e
@@ -40,6 +40,16 @@ def report_hosts(hosts)
4040
end
4141
end
4242

43+
def update_host(opts)
44+
begin
45+
$stderr.puts "HostDataProxy.update_host(): opts = #{opts}" # TODO: remove
46+
data_service = self.get_data_service()
47+
data_service.update_host(opts)
48+
rescue Exception => e
49+
elog "Problem updating host: #{e.message}"
50+
end
51+
end
52+
4353
def delete_host(opts)
4454
begin
4555
data_service = self.get_data_service()

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def report_hosts(hosts)
2323
self.post_data(HOST_API_PATH, hosts)
2424
end
2525

26+
def update_host(opts)
27+
$stderr.puts "RemoteHostDataService.update_host(): opts = #{opts}" # TODO: remove
28+
path = HOST_API_PATH
29+
if opts && opts[:id]
30+
id = opts.delete(:id)
31+
path = "#{HOST_API_PATH}/#{id}"
32+
end
33+
json_to_mdm_object(self.put_data(path, opts), HOST_MDM_CLASS, [])
34+
end
35+
2636
def delete_host(opts)
2737
json_to_mdm_object(self.delete_data(HOST_API_PATH, opts), HOST_MDM_CLASS, [])
2838
end

lib/msf/core/db_manager/host.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ 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
146147
wspace = opts[:workspace] || opts[:wspace] || workspace
147148
if wspace.kind_of? String
148149
wspace = find_workspace(wspace)
@@ -152,7 +153,9 @@ def hosts(opts)
152153

153154
conditions = {}
154155
conditions[:state] = [Msf::HostState::Alive, Msf::HostState::Unknown] if opts[:non_dead]
155-
conditions[:address] = opts[:addresses] if opts[:addresses] && !opts[:addresses].empty?
156+
conditions[:address] = opts[:address] if opts[:address] && !opts[:address].empty?
157+
conditions[:id] = opts[:id] if opts[:id] && !opts[:id].empty?
158+
$stderr.puts "DBManager::Host.hosts(): conditions = #{conditions}" # TODO: remove
156159

157160
if opts[:search_term] && !opts[:search_term].empty?
158161
column_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Mdm::Host, opts[:search_term])
@@ -268,6 +271,19 @@ def report_host(opts)
268271
}
269272
end
270273

274+
def update_host(opts)
275+
$stderr.puts "DBManager::Host.update_host(): opts = #{opts}" # TODO: remove
276+
# 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
279+
280+
::ActiveRecord::Base.connection_pool.with_connection {
281+
id = opts.delete(:id)
282+
$stderr.puts "DBManager::Host.update_host(): id = #{id}, opts = #{opts}" # TODO: remove
283+
Mdm::Host.update(id, opts)
284+
}
285+
end
286+
271287
def split_windows_os_name(os_name)
272288
return [] if os_name.nil?
273289
flavor_match = os_name.match(/Windows\s+(.*)/)

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ def self.api_path
44
'/api/v1/hosts'
55
end
66

7+
def self.api_path_with_id
8+
"#{HostServlet.api_path}/?:id?"
9+
end
10+
711
def self.registered(app)
8-
app.get HostServlet.api_path, &get_host
12+
app.get HostServlet.api_path_with_id, &get_host
913
app.post HostServlet.api_path, &report_host
14+
app.put HostServlet.api_path_with_id, &update_host
1015
app.delete HostServlet.api_path, &delete_host
1116
end
1217

@@ -17,8 +22,9 @@ def self.registered(app)
1722
def self.get_host
1823
lambda {
1924
begin
25+
$stderr.puts "HostServlet.get_host(): params[:id] = #{params[:id]}" if params[:id] # TODO: remove
2026
opts = parse_json_request(request, false)
21-
data = get_db().hosts(params)
27+
data = get_db().hosts(params.symbolize_keys)
2228
includes = [:loots]
2329
set_json_response(data, includes)
2430
rescue Exception => e
@@ -40,6 +46,24 @@ def self.report_host
4046
}
4147
end
4248

49+
def self.update_host
50+
lambda {
51+
begin
52+
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
56+
tmp_params = params.symbolize_keys
57+
opts[:id] = tmp_params[:id] if tmp_params[:id]
58+
$stderr.puts "HostServlet.update_host(): opts = #{opts}" # TODO: remove
59+
data = get_db().update_host(opts)
60+
set_json_response(data)
61+
rescue Exception => e
62+
set_error_on_response(e)
63+
end
64+
}
65+
end
66+
4367
def self.delete_host
4468
lambda {
4569
begin

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

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

234-
def change_host_info(rws, data)
235-
if rws == [nil]
234+
def change_host_info(host_ranges, data)
235+
if host_ranges == [nil]
236236
print_error("In order to change the host info, you must provide a range of hosts")
237237
return
238238
end
239239

240-
rws.each do |rw|
241-
rw.each do |ip|
242-
id = framework.db.get_host(:address => ip).id
243-
framework.db.hosts.update(id, :info => data)
244-
framework.db.report_note(:host => ip, :type => 'host.info', :data => data)
240+
$stderr.puts "Msf::Ui::Console::CommandDispatcher::Db.change_host_info(): host_ranges = #{host_ranges}"
241+
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"
243+
break if !host_search.nil? && host_search.empty?
244+
245+
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"
245250
end
246251
end
247252
end

0 commit comments

Comments
 (0)