forked from bettercap/bettercap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
net_sniff_dns.go
66 lines (55 loc) · 1.27 KB
/
net_sniff_dns.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
package modules
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"strings"
"github.com/evilsocket/islazy/tui"
)
func dnsParser(ip *layers.IPv4, pkt gopacket.Packet, udp *layers.UDP) bool {
dns, parsed := pkt.Layer(layers.LayerTypeDNS).(*layers.DNS)
if !parsed {
return false
}
if dns.OpCode != layers.DNSOpCodeQuery {
return false
}
m := make(map[string][]string)
answers := [][]layers.DNSResourceRecord{
dns.Answers,
dns.Authorities,
dns.Additionals,
}
for _, list := range answers {
for _, a := range list {
if a.IP == nil {
continue
}
hostname := string(a.Name)
if _, found := m[hostname]; !found {
m[hostname] = make([]string, 0)
}
m[hostname] = append(m[hostname], vIP(a.IP))
}
}
if len(m) == 0 && dns.ResponseCode != layers.DNSResponseCodeNoErr {
for _, a := range dns.Questions {
m[string(a.Name)] = []string{tui.Red(dns.ResponseCode.String())}
}
}
for hostname, ips := range m {
NewSnifferEvent(
pkt.Metadata().Timestamp,
"dns",
ip.SrcIP.String(),
ip.DstIP.String(),
nil,
"%s %s > %s : %s is %s",
tui.Wrap(tui.BACKDARKGRAY+tui.FOREWHITE, "dns"),
vIP(ip.SrcIP),
vIP(ip.DstIP),
tui.Yellow(hostname),
tui.Dim(strings.Join(ips, ", ")),
).Push()
}
return true
}