Skip to content

Commit 473ded3

Browse files
committed
Fix UDP detection when DNS resolution is not on
1 parent ef9a165 commit 473ded3

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def cmd_dns(*args)
160160
print_dns
161161
when "help"
162162
cmd_dns_help
163+
else
164+
print_error("Invalid command. To view help: dns -h")
163165
end
164166
rescue ::ArgumentError => e
165167
print_error(e.message)
@@ -298,7 +300,7 @@ def prettify_comm(comm, dns_server)
298300
def print_dns_set(heading, result_set)
299301
return if result_set.length == 0
300302
if result_set[0][:wildcard_rules].any?
301-
columns = ['ID', 'Rules(s)', 'DNS Server', 'Commm channel']
303+
columns = ['ID', 'Rules(s)', 'DNS Server', 'Comm channel']
302304
else
303305
columns = ['ID', 'DNS Server', 'Commm channel']
304306
end

lib/net/dns/resolver.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ def send(argument,type=Net::DNS::A,cls=Net::DNS::IN)
975975
end
976976
end
977977

978-
ans = self.old_send(method,packet,packet_data)
978+
ans = self.old_send(method,packet,packet_data, nameservers.map {|ns| [ns, {}]})
979979

980980
unless ans
981981
@logger.fatal "No response from nameservers list: aborting"
@@ -1027,7 +1027,8 @@ def axfr(name,cls=Net::DNS::IN)
10271027

10281028
answers = []
10291029
soa = 0
1030-
self.old_send(method, packet, packet_data) do |ans|
1030+
nameservers_and_hash = nameservers.map {|ns| [ns, {}]}
1031+
self.old_send(method, packet, packet_data, nameservers_and_hash) do |ans|
10311032
@logger.info "Received #{ans[0].size} bytes from #{ans[1][2]+":"+ans[1][1].to_s}"
10321033

10331034
begin
@@ -1161,12 +1162,12 @@ def make_query_packet(string,type,cls)
11611162

11621163
end
11631164

1164-
def send_tcp(packet,packet_data)
1165+
def send_tcp(packet,packet_data, nameservers)
11651166

11661167
ans = nil
11671168
length = [packet_data.size].pack("n")
11681169

1169-
@config[:nameservers].each do |ns|
1170+
nameservers.each do |ns, _unused|
11701171
begin
11711172
socket = Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)
11721173
socket.bind(Socket.pack_sockaddr_in(@config[:source_port],@config[:source_address].to_s))
@@ -1233,13 +1234,13 @@ def send_tcp(packet,packet_data)
12331234
return nil
12341235
end
12351236

1236-
def send_udp(packet,packet_data)
1237+
def send_udp(packet, packet_data, nameservers)
12371238
socket = UDPSocket.new
12381239
socket.bind(@config[:source_address].to_s,@config[:source_port])
12391240

12401241
ans = nil
12411242
response = ""
1242-
@config[:nameservers].each do |ns|
1243+
nameservers.each do |ns, _unused|
12431244
begin
12441245
@config[:udp_timeout].timeout do
12451246
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"

lib/rex/proto/dns/resolver.rb

+21-21
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def proxies=(prox, timeout_added = 250)
117117
# @return [Array<Array>] A list of nameservers, each with Rex::Socket options
118118
#
119119
def nameservers_for_packet(_dns_message)
120-
@config[:nameservers].map {|ns| [ns, {}]}
120+
@config[:nameservers].map {|ns| [ns.to_s, {}]}
121121
end
122122

123123
#
@@ -213,30 +213,30 @@ def send_tcp(packet, packet_data, nameservers, prox = @config[:proxies])
213213
nameservers.each do |ns, socket_options|
214214
begin
215215
socket = nil
216+
config = {
217+
'PeerHost' => ns.to_s,
218+
'PeerPort' => @config[:port].to_i,
219+
'Proxies' => prox,
220+
'Context' => @config[:context],
221+
'Comm' => @config[:comm]
222+
}
223+
config.update(socket_options)
224+
unless config['Comm'].nil? || config['Comm'].alive?
225+
@logger.warn("Session #{config['Comm'].sid} not active, and cannot be used to resolve DNS")
226+
throw :next_ns
227+
end
228+
229+
suffix = " over session #{@config['Comm'].sid}" unless @config['Comm'].nil?
230+
if @config[:source_port] > 0
231+
config['LocalPort'] = @config[:source_port]
232+
end
233+
if @config[:source_host].to_s != '0.0.0.0'
234+
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
235+
end
216236
@config[:tcp_timeout].timeout do
217237
catch(:next_ns) do
218238
suffix = ''
219239
begin
220-
config = {
221-
'PeerHost' => ns.to_s,
222-
'PeerPort' => @config[:port].to_i,
223-
'Proxies' => prox,
224-
'Context' => @config[:context],
225-
'Comm' => @config[:comm]
226-
}
227-
config.update(socket_options)
228-
unless config['Comm'].nil? || config['Comm'].alive?
229-
@logger.warn("Session #{config['Comm'].sid} not active, and cannot be used to resolve DNS")
230-
throw :next_ns
231-
end
232-
233-
suffix = " over session #{@config['Comm'].sid}" unless @config['Comm'].nil?
234-
if @config[:source_port] > 0
235-
config['LocalPort'] = @config[:source_port]
236-
end
237-
if @config[:source_host].to_s != '0.0.0.0'
238-
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
239-
end
240240
socket = Rex::Socket::Tcp.create(config)
241241
rescue
242242
@logger.warn "TCP Socket could not be established to #{ns}:#{@config[:port]} #{@config[:proxies]}#{suffix}"

0 commit comments

Comments
 (0)