Skip to content

Commit

Permalink
Fall back to other server if first one fails
Browse files Browse the repository at this point in the history
  • Loading branch information
smashery committed Nov 21, 2023
1 parent 1a07ab5 commit 34bd661
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
4 changes: 0 additions & 4 deletions lib/msf/core/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ def initialize(options={})
events.add_ui_subscriber(subscriber)
end

def dns_resolver
self.dns_resolver
end

def inspect
"#<Framework (#{sessions.length} sessions, #{jobs.length} jobs, #{plugins.length} plugins#{db.active ? ", #{db.driver} database active" : ""})>"
end
Expand Down
64 changes: 33 additions & 31 deletions lib/rex/proto/dns/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,41 +314,43 @@ def send_udp(packet,packet_data)
response = ""
nameservers = nameservers_for_packet(packet)
nameservers.each do |ns, socket_options|
begin
@config[:udp_timeout].timeout do
begin
config = {
'PeerHost' => ns.to_s,
'PeerPort' => @config[:port].to_i,
'Context' => @config[:context],
'Comm' => @config[:comm]
}
config.update(socket_options)
unless config['Comm'].nil? || config['Comm'].alive?
@logger.warn("Session #{config['Comm'].sid} not active, and cannot be used to resolve DNS")
throw :next_ns
end
catch(:next_ns) do
begin
@config[:udp_timeout].timeout do
begin
config = {
'PeerHost' => ns.to_s,
'PeerPort' => @config[:port].to_i,
'Context' => @config[:context],
'Comm' => @config[:comm]
}
config.update(socket_options)
unless config['Comm'].nil? || config['Comm'].alive?
@logger.warn("Session #{config['Comm'].sid} not active, and cannot be used to resolve DNS")
throw :next_ns
end

if @config[:source_port] > 0
config['LocalPort'] = @config[:source_port]
end
if @config[:source_host] != IPAddr.new('0.0.0.0')
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
if @config[:source_port] > 0
config['LocalPort'] = @config[:source_port]
end
if @config[:source_host] != IPAddr.new('0.0.0.0')
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
end
socket = Rex::Socket::Udp.create(config)
rescue
@logger.warn "UDP Socket could not be established to #{ns}:#{@config[:port]}"
throw :next_ns
end
socket = Rex::Socket::Udp.create(config)
rescue
@logger.warn "UDP Socket could not be established to #{ns}:#{@config[:port]}"
throw :next_ds
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
#socket.sendto(packet_data, ns.to_s, @config[:port].to_i, 0)
socket.write(packet_data)
ans = socket.recvfrom(@config[:packet_size])
end
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
#socket.sendto(packet_data, ns.to_s, @config[:port].to_i, 0)
socket.write(packet_data)
ans = socket.recvfrom(@config[:packet_size])
break if ans
rescue Timeout::Error
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
throw :next_ds
end
break if ans
rescue Timeout::Error
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
next
end
end
return ans
Expand Down

0 comments on commit 34bd661

Please sign in to comment.