Skip to content

Commit 5c38207

Browse files
committed
WIP externalize host data search
1 parent be0ca39 commit 5c38207

File tree

7 files changed

+45
-29
lines changed

7 files changed

+45
-29
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module HostDataProxy
22

3-
def hosts(wspace = workspace, non_dead = false, addresses = nil)
3+
def hosts(wspace = workspace, non_dead = false, addresses = nil, search_term = nil)
44
begin
55
data_service = self.get_data_service()
66
opts = {}
77
opts[:wspace] = wspace
88
opts[:non_dead] = non_dead
99
opts[:addresses] = addresses
10+
opts[:search_term] = search_term
1011
data_service.hosts(opts)
1112
rescue Exception => e
1213
puts "Call to #{data_service.class}#hosts threw exception: #{e.message}"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module RemoteHostDataService
88
HOST_MDM_CLASS = 'Mdm::Host'
99

1010
def hosts(opts)
11-
json_to_mdm_object(self.get_data(HOST_API_PATH, opts), HOST_MDM_CLASS, [])
11+
json_to_mdm_object(self.get_data(HOST_API_PATH, nil, opts), HOST_MDM_CLASS, [])
1212
end
1313

1414
def report_host(opts)

lib/msf/core/db_manager/host.rb

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,6 @@ def add_host_tag(opts)
8181
end
8282
end
8383

84-
def find_hosts_with_tag(opts)
85-
workspace_id = opts[:workspace_id]
86-
host_address = opts[:host_address]
87-
tag_name = opts[:tag_name]
88-
Mdm::Tag.joins(:hosts).where("hosts.workspace_id = ? and hosts.address = ? and tags.name = ?", workspace_id, host_address, tag_name).references(:hosts).order("tags.id DESC")
89-
end
90-
91-
def find_host_tags(opts)
92-
workspace_id = opts[:workspace_id]
93-
host_address = opts[:host_address]
94-
Mdm::Tag.joins(:hosts).where("hosts.workspace_id = ? and hosts.address = ?", workspace.id, host_address).order("tags.id DESC")
95-
end
96-
9784
def delete_host_tag(opts)
9885
workspace = opts[:workspace]
9986
if workspace.kind_of? String
@@ -161,12 +148,18 @@ def hosts(opts)
161148
wspace = find_workspace(wspace)
162149
end
163150

164-
::ActiveRecord::Base.connection_pool.with_connection {
165-
conditions = {}
166-
conditions[:state] = [Msf::HostState::Alive, Msf::HostState::Unknown] if opts[:non_dead]
167-
conditions[:address] = opts[:addresses] if opts[:addresses]
168-
wspace.hosts.where(conditions).order(:address)
169-
}
151+
::ActiveRecord::Base.connection_pool.with_connection {
152+
153+
if opts[:search_term]
154+
conditions = Msf::Util::DBManager.create_all_column_search_conditions(Mdm::Host, opts[:search_term])
155+
wspace.hosts.where(conditions).order(:address)
156+
else
157+
conditions = {}
158+
conditions[:state] = [Msf::HostState::Alive, Msf::HostState::Unknown] if opts[:non_dead]
159+
conditions[:address] = opts[:addresses] if opts[:addresses]
160+
wspace.hosts.where(conditions).order(:address)
161+
end
162+
}
170163
end
171164

172165
#

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def self.get_host
1818
lambda {
1919
begin
2020
opts = parse_json_request(request, false)
21-
data = get_db().hosts(opts)
21+
data = get_db().hosts(params)
2222
includes = [:loots]
2323
set_json_response(data, includes)
2424
rescue Exception => e

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def cmd_hosts(*args)
542542
when '-R', '--rhosts'
543543
set_rhosts = true
544544
when '-S', '--search'
545-
search_term = /#{args.shift}/nmi
545+
search_term = args.shift
546546
when '-i', '--info'
547547
mode << :new_info
548548
info_data = args.shift
@@ -648,12 +648,12 @@ def cmd_hosts(*args)
648648
each_host_range_chunk(host_ranges) do |host_search|
649649
break if !host_search.nil? && host_search.empty?
650650

651-
framework.db.hosts(framework.db.workspace, onlyup, host_search).each do |host|
652-
if search_term
653-
next unless (
654-
host.attribute_names.any? { |a| host[a.intern].to_s.match(search_term) } || !find_hosts_with_tag(framework.db.workspace.id, host.address, search_term.source).empty?
655-
)
656-
end
651+
framework.db.hosts(framework.db.workspace, onlyup, host_search, search_term = search_term).each do |host|
652+
# if search_term
653+
# next unless (
654+
# host.attribute_names.any? { |a| host[a.intern].to_s.match(search_term) } || !find_hosts_with_tag(framework.db.workspace.id, host.address, search_term.source).empty?
655+
# )
656+
# end
657657

658658
columns = col_names.map do |n|
659659
# Deal with the special cases

lib/msf/util.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ module Util
2525

2626
# Host helpers
2727
require 'msf/util/host'
28+
29+
# DBManager helpers
30+
require 'msf/util/db_manager'

lib/msf/util/db_manager.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Msf
2+
module Util
3+
module DBManager
4+
# Creates search conditions to match the specified search string against all of the model's columns.
5+
#
6+
# @param model - An ActiveRecord model object
7+
# @param search - A string regex search
8+
# @return Arel::Nodes::Or object that represents a search of all of the model's columns
9+
def self.create_all_column_search_conditions(model, search)
10+
search = "(?mi)#{search}"
11+
condition_set = model.columns.map do |column|
12+
Arel::Nodes::Regexp.new(Arel::Nodes::NamedFunction.new("CAST", [model.arel_table[column.name].as("TEXT")]),
13+
Arel::Nodes.build_quoted(search))
14+
end
15+
condition_set.reduce { |conditions, condition| conditions.or(condition).expr }
16+
end
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)