2010-12-31 02:38:16 +11:00
|
|
|
package main
|
2011-02-09 06:22:43 +11:00
|
|
|
|
2010-12-31 02:38:16 +11:00
|
|
|
import (
|
|
|
|
"dns"
|
|
|
|
"os"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2011-02-25 02:13:23 +11:00
|
|
|
"strconv"
|
2010-12-31 04:48:29 +11:00
|
|
|
"strings"
|
2010-12-31 02:38:16 +11:00
|
|
|
)
|
|
|
|
|
2011-07-05 07:57:26 +10:00
|
|
|
func q(w dns.RequestWriter, m *dns.Msg) {
|
2011-07-25 16:58:34 +10:00
|
|
|
w.Send(m)
|
2011-08-08 21:10:35 +10:00
|
|
|
r, err := w.Receive()
|
2011-09-19 23:16:05 +10:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s\n", err.String())
|
|
|
|
}
|
2011-07-25 16:58:34 +10:00
|
|
|
w.Write(r)
|
2011-07-05 07:57:26 +10:00
|
|
|
}
|
|
|
|
|
2010-12-31 02:38:16 +11:00
|
|
|
func main() {
|
2011-09-19 23:16:05 +10:00
|
|
|
dnssec := flag.Bool("dnssec", false, "request DNSSEC records")
|
|
|
|
query := flag.Bool("question", false, "show question")
|
|
|
|
short := flag.Bool("short", false, "abbreviate long DNSKEY and RRSIG RRs")
|
|
|
|
aa := flag.Bool("aa", false, "set AA flag in query")
|
|
|
|
ad := flag.Bool("ad", false, "set AD flag in query")
|
|
|
|
cd := flag.Bool("cd", false, "set CD flag in query")
|
|
|
|
rd := flag.Bool("rd", true, "unset RD flag in query")
|
|
|
|
tcp := flag.Bool("tcp", false, "TCP mode")
|
|
|
|
nsid := flag.Bool("nsid", false, "ask for NSID")
|
|
|
|
fp := flag.Bool("fingerprint", false, "enable server detection")
|
2010-12-31 04:48:29 +11:00
|
|
|
flag.Usage = func() {
|
2011-03-30 23:54:43 +11:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [@server(:port)] [qtype] [qclass] [name ...]\n", os.Args[0])
|
2010-12-31 04:48:29 +11:00
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
2010-12-31 03:15:59 +11:00
|
|
|
|
2011-03-30 20:14:36 +11:00
|
|
|
// Need to think about it... Config
|
2011-07-05 07:57:26 +10:00
|
|
|
conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
|
|
|
|
nameserver := "@" + conf.Servers[0]
|
|
|
|
qtype := uint16(0)
|
2010-12-31 02:38:16 +11:00
|
|
|
qclass := uint16(dns.ClassINET) // Default qclass
|
|
|
|
var qname []string
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2011-02-24 01:25:11 +11:00
|
|
|
Flags:
|
2010-12-31 02:38:16 +11:00
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
|
|
|
// If it starts with @ it is a nameserver
|
|
|
|
if flag.Arg(i)[0] == '@' {
|
|
|
|
nameserver = flag.Arg(i)
|
2011-02-24 01:25:11 +11:00
|
|
|
continue Flags
|
2010-12-31 02:38:16 +11:00
|
|
|
}
|
2011-02-25 02:13:23 +11:00
|
|
|
// First class, then type, to make ANY queries possible
|
2011-01-19 01:44:30 +11:00
|
|
|
// And if it looks like type, it is a type
|
|
|
|
for k, v := range dns.Rr_str {
|
2010-12-31 02:44:32 +11:00
|
|
|
if v == strings.ToUpper(flag.Arg(i)) {
|
2011-01-19 01:44:30 +11:00
|
|
|
qtype = k
|
2011-02-24 01:25:11 +11:00
|
|
|
continue Flags
|
2010-12-31 02:38:16 +11:00
|
|
|
}
|
|
|
|
}
|
2011-01-19 01:44:30 +11:00
|
|
|
// If it looks like a class, it is a class
|
|
|
|
for k, v := range dns.Class_str {
|
2010-12-31 02:44:32 +11:00
|
|
|
if v == strings.ToUpper(flag.Arg(i)) {
|
2011-01-19 01:44:30 +11:00
|
|
|
qclass = k
|
2011-02-24 01:25:11 +11:00
|
|
|
continue Flags
|
2010-12-31 02:38:16 +11:00
|
|
|
}
|
|
|
|
}
|
2011-02-25 02:13:23 +11:00
|
|
|
// If it starts with TYPExxx it is unknown rr
|
|
|
|
if strings.HasPrefix(flag.Arg(i), "TYPE") {
|
|
|
|
i, e := strconv.Atoi(string([]byte(flag.Arg(i))[4:]))
|
|
|
|
if e == nil {
|
|
|
|
qtype = uint16(i)
|
|
|
|
continue Flags
|
|
|
|
}
|
|
|
|
}
|
2011-02-22 02:00:30 +11:00
|
|
|
|
2010-12-31 02:38:16 +11:00
|
|
|
// Anything else is a qname
|
|
|
|
qname = append(qname, flag.Arg(i))
|
|
|
|
}
|
2011-07-25 16:58:34 +10:00
|
|
|
if len(qname) == 0 {
|
|
|
|
qname = make([]string, 1)
|
|
|
|
qname[0] = "."
|
|
|
|
qtype = dns.TypeNS
|
|
|
|
}
|
|
|
|
if qtype == 0 {
|
|
|
|
qtype = dns.TypeA
|
|
|
|
}
|
2011-03-29 01:13:34 +11:00
|
|
|
|
2010-12-31 04:48:29 +11:00
|
|
|
nameserver = string([]byte(nameserver)[1:]) // chop off @
|
2011-03-30 23:54:43 +11:00
|
|
|
if !strings.HasSuffix(nameserver, ":53") {
|
|
|
|
nameserver += ":53"
|
2010-12-31 03:15:59 +11:00
|
|
|
}
|
2011-01-17 20:51:33 +11:00
|
|
|
|
2011-09-19 23:16:05 +10:00
|
|
|
// ipv6 todo
|
|
|
|
// We use the async query handling, just to show how
|
|
|
|
// it is to be used.
|
2011-07-25 16:58:34 +10:00
|
|
|
dns.HandleQueryFunc(".", q)
|
|
|
|
dns.ListenAndQuery(nil, nil)
|
|
|
|
c := dns.NewClient()
|
2011-03-30 20:14:36 +11:00
|
|
|
if *tcp {
|
2011-07-05 07:57:26 +10:00
|
|
|
c.Net = "tcp"
|
2011-03-30 20:14:36 +11:00
|
|
|
}
|
2011-03-29 01:51:29 +11:00
|
|
|
|
2011-07-05 07:57:26 +10:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.MsgHdr.Authoritative = *aa
|
|
|
|
m.MsgHdr.AuthenticatedData = *ad
|
|
|
|
m.MsgHdr.CheckingDisabled = *cd
|
|
|
|
m.MsgHdr.RecursionDesired = *rd
|
|
|
|
m.Question = make([]dns.Question, 1)
|
|
|
|
if *dnssec || *nsid {
|
2011-09-20 20:52:23 +10:00
|
|
|
o := new(dns.RR_OPT)
|
|
|
|
o.Hdr.Name = "."
|
|
|
|
o.Hdr.Rrtype = dns.TypeOPT
|
|
|
|
if *dnssec {
|
|
|
|
o.SetDo()
|
|
|
|
o.SetUDPSize(dns.DefaultMsgSize)
|
|
|
|
}
|
|
|
|
if *nsid {
|
|
|
|
o.SetNsid("")
|
|
|
|
}
|
|
|
|
m.Extra = append(m.Extra, o)
|
|
|
|
//m.SetEdns0(dns.DefaultMsgSize, true)
|
2011-07-05 07:57:26 +10:00
|
|
|
}
|
2011-09-20 20:52:23 +10:00
|
|
|
|
2011-09-20 06:58:36 +10:00
|
|
|
if *fp {
|
|
|
|
startParse(nameserver)
|
|
|
|
return
|
|
|
|
}
|
2011-07-25 16:58:34 +10:00
|
|
|
for _, v := range qname {
|
|
|
|
m.Question[0] = dns.Question{v, qtype, qclass}
|
|
|
|
m.Id = dns.Id()
|
2011-09-19 23:16:05 +10:00
|
|
|
if *query {
|
2011-09-20 06:27:56 +10:00
|
|
|
fmt.Printf("%s\n", msgToFingerprint(m))
|
2011-09-19 23:16:05 +10:00
|
|
|
fmt.Printf("%s\n", m.String())
|
|
|
|
}
|
2011-07-25 16:58:34 +10:00
|
|
|
c.Do(m, nameserver)
|
|
|
|
}
|
2011-03-29 01:51:29 +11:00
|
|
|
|
2011-03-30 23:54:43 +11:00
|
|
|
i := 0
|
|
|
|
forever:
|
2011-03-30 20:14:36 +11:00
|
|
|
for {
|
|
|
|
select {
|
2011-07-05 07:57:26 +10:00
|
|
|
case r := <-dns.DefaultReplyChan:
|
2011-09-11 05:37:57 +10:00
|
|
|
if r.Reply != nil {
|
2011-09-19 23:16:05 +10:00
|
|
|
if r.Reply.Rcode == dns.RcodeSuccess {
|
|
|
|
if r.Request.Id != r.Reply.Id {
|
|
|
|
fmt.Printf("Id mismatch\n")
|
|
|
|
}
|
|
|
|
}
|
2011-03-30 20:14:36 +11:00
|
|
|
if *short {
|
2011-09-11 05:37:57 +10:00
|
|
|
r.Reply = shortMsg(r.Reply)
|
2011-03-30 20:14:36 +11:00
|
|
|
}
|
2011-09-19 23:16:05 +10:00
|
|
|
if *fp {
|
2011-09-20 06:27:56 +10:00
|
|
|
fmt.Printf("%s\n", msgToFingerprint(r.Reply))
|
2011-09-19 23:16:05 +10:00
|
|
|
}
|
2011-09-11 05:37:57 +10:00
|
|
|
fmt.Printf("%v", r.Reply)
|
2011-02-25 02:13:23 +11:00
|
|
|
}
|
2011-03-30 23:54:43 +11:00
|
|
|
i++
|
|
|
|
if i == len(qname) {
|
|
|
|
break forever
|
|
|
|
}
|
2011-02-25 02:13:23 +11:00
|
|
|
}
|
2010-12-31 02:38:16 +11:00
|
|
|
}
|
|
|
|
}
|
2011-03-02 00:57:21 +11:00
|
|
|
|
|
|
|
// Walk trough message and short Key data and Sig data
|
|
|
|
func shortMsg(in *dns.Msg) *dns.Msg {
|
|
|
|
for i := 0; i < len(in.Answer); i++ {
|
|
|
|
in.Answer[i] = shortRR(in.Answer[i])
|
|
|
|
}
|
|
|
|
for i := 0; i < len(in.Ns); i++ {
|
|
|
|
in.Ns[i] = shortRR(in.Ns[i])
|
|
|
|
}
|
|
|
|
for i := 0; i < len(in.Extra); i++ {
|
|
|
|
in.Extra[i] = shortRR(in.Extra[i])
|
|
|
|
}
|
2011-03-28 19:01:10 +11:00
|
|
|
return in
|
2011-03-02 00:57:21 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
func shortRR(r dns.RR) dns.RR {
|
|
|
|
switch t := r.(type) {
|
2011-03-28 19:01:10 +11:00
|
|
|
case *dns.RR_NSEC3:
|
|
|
|
t.Salt = "-" // nobody cares
|
2011-10-06 23:47:49 +11:00
|
|
|
case *dns.RR_DS:
|
|
|
|
t.Digest = "..."
|
|
|
|
case *dns.RR_DNSKEY:
|
|
|
|
t.PublicKey = "..."
|
|
|
|
case *dns.RR_RRSIG:
|
|
|
|
t.Signature = "..."
|
|
|
|
t.Inception = 0 // For easy grepping
|
|
|
|
t.Expiration = 0
|
|
|
|
case *dns.RR_NSEC3:
|
|
|
|
if len(t.TypeBitMap) > 5 {
|
|
|
|
t.TypeBitMap = t.TypeBitMap[1:5]
|
|
|
|
}
|
2011-03-02 00:57:21 +11:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|