Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: internet intents discovered with CNAME instead of the actual domain name #223

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/sniffer/pkg/collectors/dnssniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/otterize/network-mapper/src/sniffer/pkg/config"
"github.com/otterize/network-mapper/src/sniffer/pkg/ipresolver"
"github.com/otterize/nilable"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net"
Expand Down Expand Up @@ -136,13 +137,21 @@ func (s *DNSSniffer) HandlePacket(packet gopacket.Packet) {
ip, _ := ipLayer.(*layers.IPv4)
dns, _ := dnsLayer.(*layers.DNS)
if dns.OpCode == layers.DNSOpCodeQuery && dns.ResponseCode == layers.DNSResponseCodeNoErr {
cnameToA := getCNameTranslation(dns)

for _, answer := range dns.Answers {
// This is the DNS Answer, so the Dst IP is the pod IP
if answer.Type != layers.DNSTypeA && answer.Type != layers.DNSTypeAAAA {
continue
}
hostName := string(answer.Name)
if nameFromCNAME, ok := cnameToA[hostName]; ok {
logrus.Debugf("Found CNAME record for %s: %s", hostName, nameFromCNAME)
hostName = nameFromCNAME
}

if !s.isRunningOnAWS {
s.addCapturedRequest(ip.DstIP.String(), "", string(answer.Name), answer.IP.String(), captureTime, nilable.From(int(answer.TTL)), nil)
s.addCapturedRequest(ip.DstIP.String(), "", hostName, answer.IP.String(), captureTime, nilable.From(int(answer.TTL)), nil)
continue
}
hostname, err := s.resolver.ResolveIP(ip.DstIP.String())
Expand All @@ -153,7 +162,7 @@ func (s *DNSSniffer) HandlePacket(packet gopacket.Packet) {
s.pending = append(s.pending, pendingCapture{
srcIp: ip.DstIP.String(),
srcHostname: hostname,
destHostnameOrIP: string(answer.Name),
destHostnameOrIP: hostName,
destIPFromDNS: answer.IP.String(),
time: captureTime,
ttl: nilable.From(int(answer.TTL)),
Expand All @@ -164,6 +173,25 @@ func (s *DNSSniffer) HandlePacket(packet gopacket.Packet) {
}
}

func getCNameTranslation(dns *layers.DNS) map[string]string {
// Probably there is one CNAME record per DNS packet, so we could just use the first one we find
// But, since the implementation uses a list of answers, we will support multiple CNAME records with one
// caveat: it won't work with multiple domains for the same CNAME which is really unlikely in the same packet
cnameAnswer := lo.Filter(dns.Answers, func(answer layers.DNSResourceRecord, _ int) bool {
return answer.Type == layers.DNSTypeCNAME && len(answer.CNAME) > 0 && len(answer.Name) > 0
})

cnameToA := make(map[string]string)
for _, answer := range cnameAnswer {
existing, found := cnameToA[string(answer.CNAME)]
if found && existing != string(answer.Name) {
logrus.Debugf("Multiple CNAME records for the same CNAME, overwriting %s with %s", existing, string(answer.Name))
}
cnameToA[string(answer.CNAME)] = string(answer.Name)
}
return cnameToA
}

func (s *DNSSniffer) RefreshHostsMapping() error {
if !s.isRunningOnAWS {
return nil
Expand Down
Loading