/
dns_snooper.go
294 lines (259 loc) · 8.34 KB
/
dns_snooper.go
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// +build linux,amd64 linux,ppc64le
// Build constraint to use this file for amd64 & ppc64le on Linux
package endpoint
import (
"bytes"
"encoding/binary"
"fmt"
"sync"
"time"
"github.com/bluele/gcache"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
log "github.com/sirupsen/logrus"
)
const (
bufSize = 8 * 1024 * 1024 // 8MB
maxReverseDNSrecords = 10000
maxLogsPerDecodingError = 4
maxDecodingErrorCardinality = 1000
)
// DNSSnooper is a snopper of DNS queries
type DNSSnooper struct {
stop chan struct{}
pcapHandle *pcap.Handle
// gcache is goroutine-safe, but the cached values aren't
reverseDNSMutex sync.RWMutex
reverseDNSCache gcache.Cache
decodingErrorCounts map[string]uint64 // for limiting
}
// NewDNSSnooper creates a new snooper of DNS queries
func NewDNSSnooper() (*DNSSnooper, error) {
pcapHandle, err := newPcapHandle()
if err != nil {
return nil, err
}
reverseDNSCache := gcache.New(maxReverseDNSrecords).LRU().Build()
s := &DNSSnooper{
stop: make(chan struct{}),
pcapHandle: pcapHandle,
reverseDNSCache: reverseDNSCache,
decodingErrorCounts: map[string]uint64{},
}
go s.run()
return s, nil
}
func newPcapHandle() (*pcap.Handle, error) {
inactive, err := pcap.NewInactiveHandle("any")
if err != nil {
return nil, err
}
defer inactive.CleanUp()
// Set a long timeout because "pcap.BlockForever" actually spins on a 10ms timeout
// see https://github.com/weaveworks/weave/commit/025315363d5ea8b8265f1b3ea800f24df2be51a4
// (note the value in microseconds has to fit in a 32-bit signed int)
if err = inactive.SetTimeout(time.Minute * 30); err != nil {
return nil, err
}
if err = inactive.SetImmediateMode(true); err != nil {
// If gopacket is compiled against an older pcap.h that
// doesn't have pcap_set_immediate_mode, it supplies a dummy
// definition that always returns PCAP_ERROR. That becomes
// "Generic error", which is not very helpful. The real
// pcap_set_immediate_mode never returns PCAP_ERROR, so this
// turns it into a more informative message.
if fmt.Sprint(err) == "Generic error" {
return nil, fmt.Errorf("compiled against an old version of libpcap; please compile against libpcap-1.5.0 or later")
}
return nil, err
}
if err = inactive.SetBufferSize(bufSize); err != nil {
return nil, err
}
pcapHandle, err := inactive.Activate()
if err != nil {
return nil, err
}
if err := pcapHandle.SetDirection(pcap.DirectionIn); err != nil {
pcapHandle.Close()
return nil, err
}
if err := pcapHandle.SetBPFFilter("inbound and port 53"); err != nil {
pcapHandle.Close()
return nil, err
}
return pcapHandle, nil
}
// CachedNamesForIP obtains the domains associated to an IP,
// obtained while snooping A-record queries
func (s *DNSSnooper) CachedNamesForIP(ip string) []string {
result := []string{}
if s == nil {
return result
}
domains, err := s.reverseDNSCache.Get(ip)
if err != nil {
return result
}
s.reverseDNSMutex.RLock()
for domain := range domains.(map[string]struct{}) {
result = append(result, domain)
}
s.reverseDNSMutex.RUnlock()
return result
}
// Stop makes the snooper stop inspecting DNS communications
func (s *DNSSnooper) Stop() {
if s != nil {
close(s.stop)
}
}
// Gopacket doesn't provide direct support for DNS over TCP, see https://github.com/google/gopacket/issues/236
type tcpWithDNSSupport struct {
tcp layers.TCP
}
func (m *tcpWithDNSSupport) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
return m.tcp.DecodeFromBytes(data, df)
}
func (m *tcpWithDNSSupport) CanDecode() gopacket.LayerClass { return m.tcp.CanDecode() }
// Determine if a TCP segment contains a full DNS message (i.e. not fragmented)
func (m *tcpWithDNSSupport) hasSelfContainedDNSPayload() bool {
payload := m.tcp.LayerPayload()
if len(payload) < 2 {
return false
}
// Assume it's a self-contained DNS message if the Length field
// matches the length of the TCP segment
dnsLengthField := binary.BigEndian.Uint16(payload)
return int(dnsLengthField) == len(payload)-2
}
func (m *tcpWithDNSSupport) NextLayerType() gopacket.LayerType {
// TODO: deal with TCP fragmentation and out-of-order segments
if (m.tcp.SrcPort == 53 || m.tcp.DstPort == 53) && m.hasSelfContainedDNSPayload() {
return layers.LayerTypeDNS
}
return m.tcp.NextLayerType()
}
func (m *tcpWithDNSSupport) LayerPayload() []byte {
payload := m.tcp.LayerPayload()
if len(payload) > 1 && (m.tcp.SrcPort == 53 || m.tcp.DstPort == 53) {
// Omit the DNS length field, only included
// in TCP, in order to reuse the DNS UDP parser
payload = payload[2:]
}
return payload
}
func (s *DNSSnooper) run() {
var (
decodedLayers []gopacket.LayerType
dns layers.DNS
udp layers.UDP
tcp tcpWithDNSSupport
ip4 layers.IPv4
ip6 layers.IPv6
eth layers.Ethernet
dot1q layers.Dot1Q
sll layers.LinuxSLL
)
// assumes that the "any" interface is being used (see https://wiki.wireshark.org/SLL)
packetParser := gopacket.NewDecodingLayerParser(layers.LayerTypeLinuxSLL, &sll, &dot1q, ð, &ip4, &ip6, &udp, &tcp, &dns)
for {
select {
case <-s.stop:
s.pcapHandle.Close()
return
default:
}
packet, _, err := s.pcapHandle.ZeroCopyReadPacketData()
if err != nil {
// TimeoutExpired is acceptable due to the Timeout black magic
// on the handle.
if err != pcap.NextErrorTimeoutExpired {
log.Errorf("DNSSnooper: error reading packet data: %s", err)
}
continue
}
if err := packetParser.DecodeLayers(packet, &decodedLayers); err != nil {
// LayerTypePayload indicates the TCP payload has non-DNS data, which we are not interested in
if layer, ok := err.(gopacket.UnsupportedLayerType); !ok || gopacket.LayerType(layer) != gopacket.LayerTypePayload {
s.handleDecodingError(err)
}
continue
}
for _, layerType := range decodedLayers {
if layerType == layers.LayerTypeDNS {
s.processDNSMessage(&dns)
}
}
}
}
// handleDecodeError logs errors up to the maximum allowed count
func (s *DNSSnooper) handleDecodingError(err error) {
// prevent potential memory leak
if len(s.decodingErrorCounts) > maxDecodingErrorCardinality {
return
}
str := err.Error()
count := s.decodingErrorCounts[str]
count++
s.decodingErrorCounts[str] = count
switch {
case count == maxLogsPerDecodingError:
log.Errorf("DNSSnooper: error decoding packet: %s (reached %d occurrences, silencing)", str, maxLogsPerDecodingError)
case count < maxLogsPerDecodingError:
log.Errorf("DNSSnooper: error decoding packet: %s", str)
}
}
func (s *DNSSnooper) processDNSMessage(dns *layers.DNS) {
// Only consider responses to singleton, A-record questions
if !dns.QR || dns.ResponseCode != 0 || len(dns.Questions) != 1 {
return
}
question := dns.Questions[0]
if question.Type != layers.DNSTypeA || question.Class != layers.DNSClassIN {
return
}
var (
domainQueried = question.Name
records = append(dns.Answers, dns.Additionals...)
ips = map[string]struct{}{}
aliases = [][]byte{}
)
// Traverse all the CNAME records and the get the aliases. There are cases when the A record is for only one of the
// aliases. We traverse CNAME records first because there is no guarantee that the A records will be the first ones
for _, record := range records {
if record.Type == layers.DNSTypeCNAME && record.Class == layers.DNSClassIN {
aliases = append(aliases, record.CNAME)
}
}
// Finally, get the answer
for _, record := range records {
if record.Type != layers.DNSTypeA || record.Class != layers.DNSClassIN {
continue
}
if bytes.Equal(domainQueried, record.Name) {
ips[record.IP.String()] = struct{}{}
continue
}
for _, alias := range aliases {
if bytes.Equal(alias, record.Name) {
ips[record.IP.String()] = struct{}{}
break
}
}
}
// Update cache
newDomain := string(domainQueried)
log.Debugf("DNSSnooper: caught DNS lookup: %s -> %v", newDomain, ips)
for ip := range ips {
if existingDomains, err := s.reverseDNSCache.Get(ip); err != nil {
s.reverseDNSCache.Set(ip, map[string]struct{}{newDomain: {}})
} else {
// TODO: Be smarter about the expiration of entries with pre-existing associated domains
s.reverseDNSMutex.Lock()
existingDomains.(map[string]struct{})[newDomain] = struct{}{}
s.reverseDNSMutex.Unlock()
}
}
}