Skip to content

Commit f88840e

Browse files
committed
Move normaliize_host to a library method
This method was in Msf::DbManager class but doesn't actually use the DB. This required you to have a DB connection just to do the check. Moved it out to a helper library so we have access to it without forcing a DB connection.
1 parent 0b29408 commit f88840e

File tree

8 files changed

+14
-65
lines changed

8 files changed

+14
-65
lines changed

lib/msf/core/auxiliary/report.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def store_loot(ltype, ctype, host, data, filename=nil, info=nil, service=nil)
389389
ext = "txt"
390390
end
391391
# This method is available even if there is no database, don't bother checking
392-
host = framework.db.normalize_host(host)
392+
host = Msf::Util::Host.normalize_host(host)
393393

394394
ws = (db ? myworkspace.name[0,16] : 'default')
395395
name =

lib/msf/core/db_manager/host.rb

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def get_host(opts)
107107
wspace = find_workspace(wspace)
108108
end
109109

110-
address = normalize_host(address)
110+
address = Msf::Util::Host.normalize_host(address)
111111
return wspace.hosts.find_by_address(address)
112112
}
113113
end
@@ -135,60 +135,6 @@ def hosts(opts)
135135
}
136136
end
137137

138-
#
139-
# Returns something suitable for the +:host+ parameter to the various report_* methods
140-
#
141-
# Takes a Host object, a Session object, an Msf::Session object or a String
142-
# address
143-
#
144-
def normalize_host(host)
145-
return host if defined?(::Mdm) && host.kind_of?(::Mdm::Host)
146-
norm_host = nil
147-
148-
if (host.kind_of? String)
149-
150-
if Rex::Socket.is_ipv4?(host)
151-
# If it's an IPv4 addr with a port on the end, strip the port
152-
if host =~ /((\d{1,3}\.){3}\d{1,3}):\d+/
153-
norm_host = $1
154-
else
155-
norm_host = host
156-
end
157-
elsif Rex::Socket.is_ipv6?(host)
158-
# If it's an IPv6 addr, drop the scope
159-
address, scope = host.split('%', 2)
160-
norm_host = address
161-
else
162-
norm_host = Rex::Socket.getaddress(host, true)
163-
end
164-
elsif defined?(::Mdm) && host.kind_of?(::Mdm::Session)
165-
norm_host = host.host
166-
elsif host.respond_to?(:session_host)
167-
# Then it's an Msf::Session object
168-
norm_host = host.session_host
169-
end
170-
171-
# If we got here and don't have a norm_host yet, it could be a
172-
# Msf::Session object with an empty or nil tunnel_host and tunnel_peer;
173-
# see if it has a socket and use its peerhost if so.
174-
if (
175-
norm_host.nil? &&
176-
host.respond_to?(:sock) &&
177-
host.sock.respond_to?(:peerhost) &&
178-
host.sock.peerhost.to_s.length > 0
179-
)
180-
norm_host = session.sock.peerhost
181-
end
182-
# If We got here and still don't have a real host, there's nothing left
183-
# to try, just log it and return what we were given
184-
if !norm_host
185-
dlog("Host could not be normalized: #{host.inspect}")
186-
norm_host = host
187-
end
188-
189-
norm_host
190-
end
191-
192138
#
193139
# Report a host's attributes such as operating system and service pack
194140
#
@@ -225,7 +171,7 @@ def report_host(opts)
225171
ret = { }
226172

227173
if !addr.kind_of? ::Mdm::Host
228-
addr = normalize_host(addr)
174+
addr = Msf::Util::Host.normalize_host(addr)
229175

230176
unless ipv46_validator(addr)
231177
raise ::ArgumentError, "Invalid IP address in report_host(): #{addr}"
@@ -331,7 +277,7 @@ def update_host_via_sysinfo(opts)
331277
end
332278

333279
if !addr.kind_of? ::Mdm::Host
334-
addr = normalize_host(addr)
280+
addr = Msf::Util::Host.normalize_host(addr)
335281
addr, scope = addr.split('%', 2)
336282
opts[:scope] = scope if scope
337283

lib/msf/core/db_manager/loot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def report_loot(opts)
5454
host = opts[:host]
5555
else
5656
host = report_host({:workspace => wspace, :host => opts[:host]})
57-
addr = normalize_host(opts[:host])
57+
addr = Msf::Util::Host.normalize_host(opts[:host])
5858
end
5959
end
6060

lib/msf/core/db_manager/note.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def report_note(opts)
6868
if opts[:host].kind_of? ::Mdm::Host
6969
host = opts[:host]
7070
else
71-
addr = normalize_host(opts[:host])
71+
addr = Msf::Util::Host.normalize_host(opts[:host])
7272
host = report_host({:workspace => wspace, :host => addr})
7373
end
7474
# Do the same for a service if that's also included.

lib/msf/core/db_manager/session.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_session(opts)
5050
# created, but the Mdm::Host will have been. (There is no transaction
5151
# to rollback the Mdm::Host creation.)
5252
# @see #find_or_create_host
53-
# @see #normalize_host
53+
# @see #Msf::Util::Host.normalize_host
5454
# @see #report_exploit_success
5555
# @see #report_vuln
5656
#
@@ -234,7 +234,7 @@ def create_mdm_session_from_session(opts)
234234

235235
wspace = opts[:workspace] || find_workspace(session.workspace)
236236
h_opts = { }
237-
h_opts[:host] = normalize_host(session)
237+
h_opts[:host] = Msf::Util::Host.normalize_host(session)
238238
h_opts[:arch] = session.arch if session.respond_to?(:arch) and session.arch
239239
h_opts[:workspace] = wspace
240240
host = find_or_create_host(h_opts)

lib/msf/core/db_manager/vuln.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def report_vuln(opts)
118118
host = opts[:host]
119119
else
120120
host = report_host({:workspace => wspace, :host => opts[:host]})
121-
addr = normalize_host(opts[:host])
121+
addr = Msf::Util::Host.normalize_host(opts[:host])
122122
end
123123

124124
ret = {}

lib/msf/util.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ module Util
2222
# Executable generation and encoding
2323
require 'msf/util/exe'
2424
require 'msf/util/helper'
25+
26+
# Host helpers
27+
require 'msf/util/host'

spec/support/shared/examples/msf/db_manager/session.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
it 'should pass normalized host from session as :host to #find_or_create_host' do
176176
normalized_host = double('Normalized Host')
177177
expect(db_manager).to receive(:normalize_host).with(session).and_return(normalized_host)
178-
# stub report_vuln so its use of find_or_create_host and normalize_host doesn't interfere.
178+
# stub report_vuln so its use of find_or_create_host and Msf::Util::Host.normalize_host doesn't interfere.
179179
expect(db_manager).to receive(:report_vuln)
180180

181181
expect(db_manager).to receive(:find_or_create_host).with(
@@ -518,7 +518,7 @@
518518
it 'should pass normalized host from session as :host to #find_or_create_host' do
519519
normalized_host = double('Normalized Host')
520520
allow(db_manager).to receive(:normalize_host).with(session).and_return(normalized_host)
521-
# stub report_vuln so its use of find_or_create_host and normalize_host doesn't interfere.
521+
# stub report_vuln so its use of find_or_create_host and Msf::Util::Host.normalize_host doesn't interfere.
522522
allow(db_manager).to receive(:report_vuln)
523523

524524
expect(db_manager).to receive(:find_or_create_host).with(

0 commit comments

Comments
 (0)