Skip to content

Instantly share code, notes, and snippets.

@jvns

jvns/dns-2.rb Secret

Created November 6, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvns/3587ea0b4a2a6c20dcfd8bf653fc11d9 to your computer and use it in GitHub Desktop.
Save jvns/3587ea0b4a2a6c20dcfd8bf653fc11d9 to your computer and use it in GitHub Desktop.
require 'socket'
sock = UDPSocket.new
sock.bind('0.0.0.0', 12345)
sock.connect('8.8.8.8', 53)
def make_question_header(query_id)
# id, flags, num questions, num answers, num auth, num additional
[query_id, 0x0100, 0x0001, 0x0000, 0x0000, 0x0000].pack('nnnnnn')
end
def encode_domain_name(domain)
domain
.split(".")
.map { |x| x.length.chr + x }
.join + "\0"
end
def make_dns_query(domain, type)
query_id = rand(65535)
header = make_question_header(query_id)
question = encode_domain_name(domain) + [type, 1].pack('nn')
header + question
end
sock.send(make_dns_query("example.com", 1), 0)
reply, _ = sock.recvfrom(1024)
puts reply.unpack('H*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment