Skip to content

Commit aeee3bb

Browse files
authored
Merge pull request #15 from clee-r7/MS-2941-remote-host-update
MS-2941: Remote Host Update
2 parents f176e33 + de0c4c0 commit aeee3bb

File tree

5 files changed

+74
-41
lines changed

5 files changed

+74
-41
lines changed

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

Lines changed: 10 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,15 @@ def report_hosts(hosts)
4040
end
4141
end
4242

43+
def update_host(opts)
44+
begin
45+
data_service = self.get_data_service()
46+
data_service.update_host(opts)
47+
rescue Exception => e
48+
elog "Problem updating host: #{e.message}"
49+
end
50+
end
51+
4352
def delete_host(opts)
4453
begin
4554
data_service = self.get_data_service()

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

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

26+
def update_host(opts)
27+
path = HOST_API_PATH
28+
if opts && opts[:id]
29+
id = opts.delete(:id)
30+
path = "#{HOST_API_PATH}/#{id}"
31+
end
32+
json_to_mdm_object(self.put_data(path, opts), HOST_MDM_CLASS, [])
33+
end
34+
2635
def delete_host(opts)
2736
json_to_mdm_object(self.delete_data(HOST_API_PATH, opts), HOST_MDM_CLASS, [])
2837
end

lib/msf/core/db_manager/host.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def hosts(opts)
152152

153153
conditions = {}
154154
conditions[:state] = [Msf::HostState::Alive, Msf::HostState::Unknown] if opts[:non_dead]
155-
conditions[:address] = opts[:addresses] if opts[:addresses] && !opts[:addresses].empty?
155+
conditions[:address] = opts[:address] if opts[:address] && !opts[:address].empty?
156+
conditions[:id] = opts[:id] if opts[:id] && !opts[:id].empty?
156157

157158
if opts[:search_term] && !opts[:search_term].empty?
158159
column_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Mdm::Host, opts[:search_term])
@@ -268,6 +269,20 @@ def report_host(opts)
268269
}
269270
end
270271

272+
def update_host(opts)
273+
# process workspace string for update if included in opts
274+
wspace = opts.delete(:workspace)
275+
if wspace.kind_of? String
276+
wspace = find_workspace(wspace)
277+
opts[:workspace] = wspace
278+
end
279+
280+
::ActiveRecord::Base.connection_pool.with_connection {
281+
id = opts.delete(:id)
282+
Mdm::Host.update(id, opts)
283+
}
284+
end
285+
271286
def split_windows_os_name(os_name)
272287
return [] if os_name.nil?
273288
flavor_match = os_name.match(/Windows\s+(.*)/)

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

Lines changed: 21 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

@@ -18,7 +23,7 @@ def self.get_host
1823
lambda {
1924
begin
2025
opts = parse_json_request(request, false)
21-
data = get_db().hosts(params)
26+
data = get_db().hosts(params.symbolize_keys)
2227
includes = [:loots]
2328
set_json_response(data, includes)
2429
rescue Exception => e
@@ -40,6 +45,20 @@ def self.report_host
4045
}
4146
end
4247

48+
def self.update_host
49+
lambda {
50+
begin
51+
opts = parse_json_request(request, false)
52+
tmp_params = params.symbolize_keys
53+
opts[:id] = tmp_params[:id] if tmp_params[:id]
54+
data = get_db().update_host(opts)
55+
set_json_response(data)
56+
rescue Exception => e
57+
set_error_on_response(e)
58+
end
59+
}
60+
end
61+
4362
def self.delete_host
4463
lambda {
4564
begin

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

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

234-
def change_host_info(rws, data)
235-
if rws == [nil]
236-
print_error("In order to change the host info, you must provide a range of hosts")
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")
237241
return
238242
end
243+
attribute = host_data.keys[0]
239244

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)
245-
end
246-
end
247-
end
248-
249-
def change_host_name(rws, data)
250-
if rws == [nil]
251-
print_error("In order to change the host name, you must provide a range of hosts")
245+
if host_ranges == [nil]
246+
print_error("In order to change the host #{attribute}, you must provide a range of hosts")
252247
return
253248
end
254249

255-
rws.each do |rw|
256-
rw.each do |ip|
257-
id = framework.db.get_host(:address => ip).id
258-
framework.db.hosts.update(id, :name => data)
259-
framework.db.report_note(:host => ip, :type => 'host.name', :data => data)
260-
end
261-
end
262-
end
263-
264-
def change_host_comment(rws, data)
265-
if rws == [nil]
266-
print_error("In order to change the comment, you must provide a range of hosts")
267-
return
268-
end
250+
each_host_range_chunk(host_ranges) do |host_search|
251+
break if !host_search.nil? && host_search.empty?
269252

270-
rws.each do |rw|
271-
rw.each do |ip|
272-
id = framework.db.get_host(:address => ip).id
273-
framework.db.hosts.update(id, :comments => data)
274-
framework.db.report_note(:host => ip, :type => 'host.comments', :data => data)
253+
framework.db.hosts(framework.db.workspace, false, host_search).each do |host|
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])
275256
end
276257
end
277258
end
@@ -492,13 +473,13 @@ def cmd_hosts(*args)
492473

493474
case
494475
when mode == [:new_info]
495-
change_host_info(host_ranges, info_data)
476+
change_host_data(host_ranges, info: info_data)
496477
return
497478
when mode == [:new_name]
498-
change_host_name(host_ranges, name_data)
479+
change_host_data(host_ranges, name: name_data)
499480
return
500481
when mode == [:new_comment]
501-
change_host_comment(host_ranges, comment_data)
482+
change_host_data(host_ranges, comments: comment_data)
502483
return
503484
when mode == [:tag]
504485
begin

0 commit comments

Comments
 (0)