Skip to content

Commit 34bd661

Browse files
committed
Fall back to other server if first one fails
1 parent 1a07ab5 commit 34bd661

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

lib/msf/core/framework.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ def initialize(options={})
9595
events.add_ui_subscriber(subscriber)
9696
end
9797

98-
def dns_resolver
99-
self.dns_resolver
100-
end
101-
10298
def inspect
10399
"#<Framework (#{sessions.length} sessions, #{jobs.length} jobs, #{plugins.length} plugins#{db.active ? ", #{db.driver} database active" : ""})>"
104100
end

lib/rex/proto/dns/resolver.rb

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -314,41 +314,43 @@ def send_udp(packet,packet_data)
314314
response = ""
315315
nameservers = nameservers_for_packet(packet)
316316
nameservers.each do |ns, socket_options|
317-
begin
318-
@config[:udp_timeout].timeout do
319-
begin
320-
config = {
321-
'PeerHost' => ns.to_s,
322-
'PeerPort' => @config[:port].to_i,
323-
'Context' => @config[:context],
324-
'Comm' => @config[:comm]
325-
}
326-
config.update(socket_options)
327-
unless config['Comm'].nil? || config['Comm'].alive?
328-
@logger.warn("Session #{config['Comm'].sid} not active, and cannot be used to resolve DNS")
329-
throw :next_ns
330-
end
317+
catch(:next_ns) do
318+
begin
319+
@config[:udp_timeout].timeout do
320+
begin
321+
config = {
322+
'PeerHost' => ns.to_s,
323+
'PeerPort' => @config[:port].to_i,
324+
'Context' => @config[:context],
325+
'Comm' => @config[:comm]
326+
}
327+
config.update(socket_options)
328+
unless config['Comm'].nil? || config['Comm'].alive?
329+
@logger.warn("Session #{config['Comm'].sid} not active, and cannot be used to resolve DNS")
330+
throw :next_ns
331+
end
331332

332-
if @config[:source_port] > 0
333-
config['LocalPort'] = @config[:source_port]
334-
end
335-
if @config[:source_host] != IPAddr.new('0.0.0.0')
336-
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
333+
if @config[:source_port] > 0
334+
config['LocalPort'] = @config[:source_port]
335+
end
336+
if @config[:source_host] != IPAddr.new('0.0.0.0')
337+
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
338+
end
339+
socket = Rex::Socket::Udp.create(config)
340+
rescue
341+
@logger.warn "UDP Socket could not be established to #{ns}:#{@config[:port]}"
342+
throw :next_ns
337343
end
338-
socket = Rex::Socket::Udp.create(config)
339-
rescue
340-
@logger.warn "UDP Socket could not be established to #{ns}:#{@config[:port]}"
341-
throw :next_ds
344+
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
345+
#socket.sendto(packet_data, ns.to_s, @config[:port].to_i, 0)
346+
socket.write(packet_data)
347+
ans = socket.recvfrom(@config[:packet_size])
342348
end
343-
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
344-
#socket.sendto(packet_data, ns.to_s, @config[:port].to_i, 0)
345-
socket.write(packet_data)
346-
ans = socket.recvfrom(@config[:packet_size])
349+
break if ans
350+
rescue Timeout::Error
351+
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
352+
throw :next_ds
347353
end
348-
break if ans
349-
rescue Timeout::Error
350-
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
351-
next
352354
end
353355
end
354356
return ans

0 commit comments

Comments
 (0)