CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb
Views: 1904
1
# -*- coding: binary -*-
2
3
require 'rex/post/meterpreter/extensions/stdapi/tlv'
4
5
module Rex
6
module Post
7
module Meterpreter
8
module Extensions
9
module Stdapi
10
module Net
11
12
###
13
#
14
# This class provides DNS resolution from the perspective
15
# of the remote host.
16
#
17
###
18
class Resolve
19
20
##
21
#
22
# Constructor
23
#
24
##
25
26
#
27
# Initializes a Resolve instance that is used to resolve network addresses
28
# on the remote machine.
29
#
30
def initialize(client)
31
self.client = client
32
end
33
34
def resolve_host(hostname, family=AF_INET)
35
request = Packet.create_request(COMMAND_ID_STDAPI_NET_RESOLVE_HOST)
36
request.add_tlv(TLV_TYPE_HOST_NAME, hostname)
37
request.add_tlv(TLV_TYPE_ADDR_TYPE, family)
38
39
response = client.send_request(request)
40
41
raw = response.get_tlv_value(TLV_TYPE_IP)
42
43
return raw_to_host_ip_pair(hostname, raw)
44
end
45
46
def resolve_hosts(hostnames, family=AF_INET)
47
request = Packet.create_request(COMMAND_ID_STDAPI_NET_RESOLVE_HOSTS)
48
request.add_tlv(TLV_TYPE_ADDR_TYPE, family)
49
50
hostnames.each do |hostname|
51
request.add_tlv(TLV_TYPE_HOST_NAME, hostname)
52
end
53
54
response = client.send_request(request)
55
56
hosts = []
57
raws = []
58
59
response.each(TLV_TYPE_IP) do |raw|
60
raws << raw
61
end
62
63
0.upto(hostnames.length - 1) do |i|
64
raw = raws[i]
65
host = hostnames[i]
66
67
hosts << raw_to_host_ip_pair(host, raw&.value)
68
end
69
70
return hosts
71
end
72
73
def raw_to_host_ip_pair(host, raw)
74
if raw.nil? or host.nil?
75
return nil
76
end
77
78
ip = nil
79
if raw.length == 4 || raw.length == 16
80
ip = Rex::Socket.addr_ntoa(raw)
81
elsif raw.length != 0
82
wlog("hostname resolution failed, the returned address is corrupt (hostname: #{host}, length: #{raw.length})")
83
end
84
85
result = { :hostname => host, :ip => ip }
86
87
return result
88
end
89
90
protected
91
92
attr_accessor :client # :nodoc:
93
94
end
95
96
end; end; end; end; end; end
97
98