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 support for resolving multiple host ips #18499

Closed
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 29 additions & 17 deletions lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,30 @@ def initialize(client)
self.client = client
end

def resolve_host(hostname, family=AF_INET)
def resolve_host(hostname, family = AF_INET)
request = Packet.create_request(COMMAND_ID_STDAPI_NET_RESOLVE_HOST)
request.add_tlv(TLV_TYPE_HOST_NAME, hostname)
request.add_tlv(TLV_TYPE_ADDR_TYPE, family)

response = client.send_request(request)

raw = response.get_tlv_value(TLV_TYPE_IP)
ips = []
if response.has_tlv?(TLV_TYPE_RESOLVE_HOST_ENTRY)
response.each(TLV_TYPE_RESOLVE_HOST_ENTRY) do |tlv|
tlv.each(TLV_TYPE_IP) do |ip|
ips << raw_to_host_ip_pair(hostname, ip.value)[:ip]
end
end
elsif response.has_tlv?(TLV_TYPE_IP)
ip = response.get_tlv_value(TLV_TYPE_IP)
ips << raw_to_host_ip_pair(hostname, ip)[:ip]
end

return raw_to_host_ip_pair(hostname, raw)
{ hostname: hostname, ip: ips.first, ips: ips }
end

def resolve_hosts(hostnames, family=AF_INET)
def resolve_hosts(hostnames, family = AF_INET)
result = []
request = Packet.create_request(COMMAND_ID_STDAPI_NET_RESOLVE_HOSTS)
request.add_tlv(TLV_TYPE_ADDR_TYPE, family)

Expand All @@ -53,21 +64,22 @@ def resolve_hosts(hostnames, family=AF_INET)

response = client.send_request(request)

hosts = []
raws = []

response.each(TLV_TYPE_IP) do |raw|
raws << raw
end

0.upto(hostnames.length - 1) do |i|
raw = raws[i]
host = hostnames[i]

hosts << raw_to_host_ip_pair(host, raw&.value)
if response.has_tlv?(TLV_TYPE_RESOLVE_HOST_ENTRY)
response.each_with_index(TLV_TYPE_RESOLVE_HOST_ENTRY) do |tlv, index|
ips = []
tlv.each(TLV_TYPE_IP) do |ip|
ips << raw_to_host_ip_pair(hostnames[index], ip.value)[:ip]
end
result << { hostname: hostnames[index], ip: ips.first, ips: ips }
end
elsif response.has_tlv?(TLV_TYPE_IP)
response.each_with_index(TLV_TYPE_IP) do |tlv, index|
ips = [raw_to_host_ip_pair(hostnames[index], tlv.value)[:ip]]
result << { hostname: hostnames[index], ip: ips.first, ips: ips }
end
end

return hosts
result
end

def raw_to_host_ip_pair(host, raw)
Expand Down
4 changes: 3 additions & 1 deletion lib/rex/post/meterpreter/extensions/stdapi/tlv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ module Stdapi

TLV_TYPE_SHUTDOWN_HOW = TLV_META_TYPE_UINT | 1530

# Resolve hosts/host
TLV_TYPE_RESOLVE_HOST_ENTRY = TLV_META_TYPE_GROUP | 1550

##
#
# Sys
Expand Down Expand Up @@ -290,4 +293,3 @@ module Stdapi
TLV_TYPE_AUDIO_INTERFACE_NAME = TLV_META_TYPE_STRING | (TLV_EXTENSIONS + 13)

end; end; end; end; end