Fix UDP detection when DNS resolution is not on

This commit is contained in:
Ashley Donaldson 2023-11-23 09:54:47 +11:00
parent ef9a165d22
commit 473ded345b
No known key found for this signature in database
GPG Key ID: D4BCDC8C892F7477
3 changed files with 31 additions and 28 deletions

View File

@ -160,6 +160,8 @@ class DNS
print_dns
when "help"
cmd_dns_help
else
print_error("Invalid command. To view help: dns -h")
end
rescue ::ArgumentError => e
print_error(e.message)
@ -298,7 +300,7 @@ class DNS
def print_dns_set(heading, result_set)
return if result_set.length == 0
if result_set[0][:wildcard_rules].any?
columns = ['ID', 'Rules(s)', 'DNS Server', 'Commm channel']
columns = ['ID', 'Rules(s)', 'DNS Server', 'Comm channel']
else
columns = ['ID', 'DNS Server', 'Commm channel']
end

View File

@ -975,7 +975,7 @@ module Net # :nodoc:
end
end
ans = self.old_send(method,packet,packet_data)
ans = self.old_send(method,packet,packet_data, nameservers.map {|ns| [ns, {}]})
unless ans
@logger.fatal "No response from nameservers list: aborting"
@ -1027,7 +1027,8 @@ module Net # :nodoc:
answers = []
soa = 0
self.old_send(method, packet, packet_data) do |ans|
nameservers_and_hash = nameservers.map {|ns| [ns, {}]}
self.old_send(method, packet, packet_data, nameservers_and_hash) do |ans|
@logger.info "Received #{ans[0].size} bytes from #{ans[1][2]+":"+ans[1][1].to_s}"
begin
@ -1161,12 +1162,12 @@ module Net # :nodoc:
end
def send_tcp(packet,packet_data)
def send_tcp(packet,packet_data, nameservers)
ans = nil
length = [packet_data.size].pack("n")
@config[:nameservers].each do |ns|
nameservers.each do |ns, _unused|
begin
socket = Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)
socket.bind(Socket.pack_sockaddr_in(@config[:source_port],@config[:source_address].to_s))
@ -1233,13 +1234,13 @@ module Net # :nodoc:
return nil
end
def send_udp(packet,packet_data)
def send_udp(packet, packet_data, nameservers)
socket = UDPSocket.new
socket.bind(@config[:source_address].to_s,@config[:source_port])
ans = nil
response = ""
@config[:nameservers].each do |ns|
nameservers.each do |ns, _unused|
begin
@config[:udp_timeout].timeout do
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"

View File

@ -117,7 +117,7 @@ module DNS
# @return [Array<Array>] A list of nameservers, each with Rex::Socket options
#
def nameservers_for_packet(_dns_message)
@config[:nameservers].map {|ns| [ns, {}]}
@config[:nameservers].map {|ns| [ns.to_s, {}]}
end
#
@ -213,30 +213,30 @@ module DNS
nameservers.each do |ns, socket_options|
begin
socket = nil
config = {
'PeerHost' => ns.to_s,
'PeerPort' => @config[:port].to_i,
'Proxies' => prox,
'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
suffix = " over session #{@config['Comm'].sid}" unless @config['Comm'].nil?
if @config[:source_port] > 0
config['LocalPort'] = @config[:source_port]
end
if @config[:source_host].to_s != '0.0.0.0'
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
end
@config[:tcp_timeout].timeout do
catch(:next_ns) do
suffix = ''
begin
config = {
'PeerHost' => ns.to_s,
'PeerPort' => @config[:port].to_i,
'Proxies' => prox,
'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
suffix = " over session #{@config['Comm'].sid}" unless @config['Comm'].nil?
if @config[:source_port] > 0
config['LocalPort'] = @config[:source_port]
end
if @config[:source_host].to_s != '0.0.0.0'
config['LocalHost'] = @config[:source_host] unless @config[:source_host].nil?
end
socket = Rex::Socket::Tcp.create(config)
rescue
@logger.warn "TCP Socket could not be established to #{ns}:#{@config[:port]} #{@config[:proxies]}#{suffix}"