Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds mixin to support resolving IPs across different session types #18500

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/msf/core/post/dns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: binary -*-

module Msf::Post::DNS
end
39 changes: 39 additions & 0 deletions lib/msf/core/post/dns/resolve_host.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: binary -*-

module Msf
class Post
module DNS
###
#
# This module resolves session DNS
#
###
module ResolveHost
# Takes the host name and makes use of nslookup to resolve the IP
#
# @param [String] host Hostname
# @return [Array, nil] result[:ips], ips The resolved IPs
def resolve_host(host)
cgranleese-r7 marked this conversation as resolved.
Show resolved Hide resolved
if client.respond_to?(:net) && client.commands.include?(Rex::Post::Meterpreter::Extensions::Stdapi::COMMAND_ID_STDAPI_NET_RESOLVE_HOST)
result = client.net.resolve.resolve_host(host)
result[:ips]
else
ips = []
data = cmd_exec("nslookup #{host}")
if data =~ /Name/
# Remove unnecessary data and get the section with the addresses
returned_data = data.split(/Name:/)[1]
# check each element of the array to see if they are IP
returned_data.gsub(/\r\n\t |\r\n|Aliases:|Addresses:|Address:/, ' ').split(' ').each do |e|
if Rex::Socket.dotted_ip?(e)
ips << e
end
end
end
ips
end
end
end
end
end
end
31 changes: 7 additions & 24 deletions modules/post/windows/gather/enum_computers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class MetasploitModule < Msf::Post
include Msf::Post::File
include Msf::Post::Windows::Accounts
include Msf::Post::Windows::Registry
include Msf::Post::DNS::ResolveHost

def initialize(info = {})
super(
Expand Down Expand Up @@ -60,34 +61,16 @@ def run
#
# @param [String] host Hostname
# @return [String] ip The resolved IP
def resolve_host(host)
vprint_status("Looking up IP for #{host}")
return host if Rex::Socket.dotted_ip?(host)

ip = []
data = cmd_exec("nslookup #{host}")
if data =~ /Name/
# Remove unnecessary data and get the section with the addresses
returned_data = data.split(/Name:/)[1]
# check each element of the array to see if they are IP
returned_data.gsub(/\r\n\t |\r\n|Aliases:|Addresses:|Address:/, ' ').split(' ').each do |e|
if Rex::Socket.dotted_ip?(e)
ip << e
end
end
end

if ip.blank?
'Not resolvable'
else
ip.join(', ')
end
def gethost(hostname)
## get IP for host
vprint_status("Looking up IP for #{hostname}")
resolve_host(hostname).join(', ')
cgranleese-r7 marked this conversation as resolved.
Show resolved Hide resolved
end

def get_domain_computers
computer_list = []
divisor = "-------------------------------------------------------------------------------\r\n"
net_view_response = cmd_exec('net view')
net_view_response = cmd_exec("cmd.exe", "/c net view")
cgranleese-r7 marked this conversation as resolved.
Show resolved Hide resolved
unless net_view_response.include?(divisor)
print_error("The net view command failed with: #{net_view_response}")
return []
Expand Down Expand Up @@ -115,7 +98,7 @@ def list_computers(domain, hosts)
]
)
hosts.each do |hostname|
hostip = resolve_host(hostname)
hostip = gethost(hostname)
tbl << [domain, hostname, hostip]
end

Expand Down