1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| package main
import ( "bytes" "encoding/binary" "fmt" "log" "net" "strings" )
type DNSHeader struct { ID uint16 Flags uint16 Questions uint16 AnswerRRS uint16 AuthorityRRS uint16 AdditionalRRS uint16 }
func (header *DNSHeader) SetFlag(qr, opcode, aa, tc, rd, ra, rcode uint16) { header.Flags = qr<<15 + opcode<<11 + aa<<10 + tc<<9 + rd<<8 + ra<<7 + rcode }
type Query struct { Type uint16 Class uint16 }
func domainToBytes(domain string) []byte { var ( buf bytes.Buffer segments = strings.Split(domain, ".") ) for _, s := range segments { binary.Write(&buf, binary.BigEndian, byte(len(s))) binary.Write(&buf, binary.BigEndian, []byte(s)) } binary.Write(&buf, binary.BigEndian, byte(0x00)) return buf.Bytes() }
func DigDomain(dnsServerAddr, domain string) (queries, answers []string) { header := DNSHeader{} header.ID = 0xffff header.SetFlag(0, 0, 0, 0, 1, 0, 0) header.Questions = 1 header.AnswerRRS = 0 header.AuthorityRRS = 0 header.AdditionalRRS = 0
query := Query{} query.Type = 1 query.Class = 1
var ( conn net.Conn err error buf bytes.Buffer ) binary.Write(&buf, binary.BigEndian, header) binary.Write(&buf, binary.BigEndian, domainToBytes(domain)) binary.Write(&buf, binary.BigEndian, query)
if conn, err = net.Dial("udp", dnsServerAddr); err != nil { log.Println(err) return } defer conn.Close() if _, err = conn.Write(buf.Bytes()); err != nil { log.Println(err) return } bytes := make([]byte, 1024) n, err := conn.Read(bytes) if err != nil { log.Println(err) return } return dNSResponseDecode(bytes[:n]) }
func dNSResponseDecode(res []byte) (queries, answers []string) { header := res[:12] queryNum := uint16(header[4])<<8 + uint16(header[5]) answerNum := uint16(header[6])<<8 + uint16(header[7]) data := res[12:] index := 0 queriesBytes := make([][]byte, queryNum) answerBytes := make([][]byte, answerNum)
for i := 0; i < int(queryNum); i++ { start := index l := 0 for { l = int(data[index]) if l == 0 { break } index += 1 + l } queriesBytes[i] = data[start:index] index += 5 } if answerNum != 0 { for i := 0; i < int(answerNum); i++ { start := index nums := 2 + 2 + 2 + 4 + 2 dataLenIndex := start + nums - 2 dataLen := int(uint16(data[dataLenIndex])<<8 + uint16(data[dataLenIndex+1])) index = start + nums + dataLen - 1 answerBytes[i] = data[start:index] index += 1 } } queries = make([]string, queryNum) for i, bytes := range queriesBytes { queries[i] = getQuery(bytes) } answers = make([]string, answerNum) for i, bytes := range answerBytes { answers[i] = getAnswer(bytes) } return queries, answers }
func getQuery(queryBytes []byte) string { return getDomain(queryBytes) }
func getDomain(domainBytes []byte) string { domain := "" index := 0 l := 0 for { if index >= len(domainBytes) { break } l = int(domainBytes[index]) if l == 0 { break } if index+1+l > len(domainBytes) { domain += string(domainBytes[index+1:]) + "." } else { domain += string(domainBytes[index+1:index+1+l]) + "." } index += 1 + l } domain = strings.Trim(domain, ".") return domain }
func getAnswer(answerBytes []byte) string { typ := uint16(answerBytes[2])<<8 + uint16(answerBytes[3]) dataLenIndex := 2 + 2 + 2 + 4 dataLen := int(uint16(answerBytes[dataLenIndex])<<8 + uint16(answerBytes[dataLenIndex+1])) address := answerBytes[dataLenIndex+2 : dataLenIndex+2+dataLen] if typ == 1 { return fmt.Sprintf("%d.%d.%d.%d", address[0], address[1], address[2], address[3]) } else if typ == 5 { return getDomain(address) } return "" }
func main() { dnsServerAddr := "114.114.114.114:53" domain := "www.baidu.com" fmt.Println(DigDomain(dnsServerAddr, domain)) }
|