Skip to content

Commit

Permalink
fix dns decoder for response without question (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard authored Oct 12, 2024
1 parent f21ed94 commit 10e4e1e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p align="center">
<img src="https://goreportcard.com/badge/github.com/dmachard/go-dns-collector" alt="Go Report"/>
<img src="https://img.shields.io/badge/go%20version-min%201.21-green" alt="Go version"/>
<img src="https://img.shields.io/badge/go%20tests-507-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20tests-508-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20bench-21-green" alt="Go bench"/>
<img src="https://img.shields.io/badge/go%20lines-31679-green" alt="Go lines"/>
</p>
Expand Down
5 changes: 4 additions & 1 deletion dnsutils/dns_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func DecodePayload(dm *DNSMessage, header *DNSHeader, config *pkgconfig.Config)
dm.DNS.Qtype = RdatatypeToString(dnsRRtype)
dm.DNS.Qclass = ClassToString(dnsQclass)
payloadOffset = offsetrr
} else {
payloadOffset = DNSLen
}

// decode DNS answers
Expand Down Expand Up @@ -242,8 +244,9 @@ func DecodePayload(dm *DNSMessage, header *DNSHeader, config *pkgconfig.Config)
return &decodingError{part: "authority records", err: err}
}
}

// decode additional answers
if header.Arcount > 0 {
// decode additional answers
answers, _, err := DecodeAnswer(header.Arcount, payloadOffset, dm.DNS.Payload)
if err == nil { // nolint
dm.DNS.DNSRRs.Records = answers
Expand Down
44 changes: 44 additions & 0 deletions dnsutils/dns_parser_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,3 +1042,47 @@ func TestDecodePayload_UpdateQuery(t *testing.T) {
t.Error("expected no error on decode", err)
}
}

func TestDecodePayload_MDNSResponseWithNoQuestion(t *testing.T) {
payload := []byte{
0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x0b, 0x5f, 0x67, 0x6f,
0x6f, 0x67, 0x6c, 0x65, 0x63, 0x61, 0x73, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f,
0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x35, 0x32, 0x53,
0x48, 0x49, 0x45, 0x4c, 0x44, 0x2d, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x2d, 0x54, 0x56,
0x2d, 0x30, 0x66, 0x33, 0x31, 0x30, 0x63, 0x32, 0x63, 0x39, 0x66, 0x62, 0x62, 0x64, 0x61, 0x64,
0x64, 0x31, 0x39, 0x39, 0x30, 0x30, 0x38, 0x31, 0x61, 0x61, 0x66, 0x35, 0x37, 0x65, 0x66, 0x38,
0x33, 0xc0, 0x0c, 0xc0, 0x2e, 0x00, 0x10, 0x80, 0x01, 0x00, 0x00, 0x11, 0x94, 0x00, 0xd2, 0x23,
0x69, 0x64, 0x3d, 0x30, 0x66, 0x33, 0x31, 0x30, 0x63, 0x32, 0x63, 0x39, 0x66, 0x62, 0x62, 0x64,
0x61, 0x64, 0x64, 0x31, 0x39, 0x39, 0x30, 0x30, 0x38, 0x31, 0x61, 0x61, 0x66, 0x35, 0x37, 0x65,
0x66, 0x38, 0x33, 0x23, 0x63, 0x64, 0x3d, 0x42, 0x35, 0x35, 0x36, 0x39, 0x31, 0x37, 0x42, 0x30,
0x36, 0x33, 0x38, 0x46, 0x46, 0x32, 0x43, 0x35, 0x30, 0x45, 0x42, 0x43, 0x33, 0x34, 0x34, 0x41,
0x45, 0x35, 0x31, 0x46, 0x39, 0x43, 0x37, 0x03, 0x72, 0x6d, 0x3d, 0x05, 0x76, 0x65, 0x3d, 0x30,
0x35, 0x14, 0x6d, 0x64, 0x3d, 0x53, 0x48, 0x49, 0x45, 0x4c, 0x44, 0x20, 0x41, 0x6e, 0x64, 0x72,
0x6f, 0x69, 0x64, 0x20, 0x54, 0x56, 0x12, 0x69, 0x63, 0x3d, 0x2f, 0x73, 0x65, 0x74, 0x75, 0x70,
0x2f, 0x69, 0x63, 0x6f, 0x6e, 0x2e, 0x70, 0x6e, 0x67, 0x09, 0x66, 0x6e, 0x3d, 0x53, 0x48, 0x49,
0x45, 0x4c, 0x44, 0x09, 0x63, 0x61, 0x3d, 0x34, 0x36, 0x33, 0x33, 0x36, 0x35, 0x04, 0x73, 0x74,
0x3d, 0x31, 0x0f, 0x62, 0x73, 0x3d, 0x46, 0x41, 0x38, 0x46, 0x46, 0x36, 0x46, 0x30, 0x45, 0x30,
0x38, 0x38, 0x04, 0x6e, 0x66, 0x3d, 0x31, 0x09, 0x63, 0x74, 0x3d, 0x30, 0x39, 0x31, 0x43, 0x44,
0x34, 0x13, 0x72, 0x72, 0x3d, 0x41, 0x6e, 0x64, 0x72, 0x6f, 0x69, 0x64, 0x4e, 0x61, 0x74, 0x69,
0x76, 0x65, 0x41, 0x70, 0x70, 0x0b, 0x72, 0x73, 0x3d, 0x4a, 0x65, 0x6c, 0x6c, 0x79, 0x66, 0x69,
0x6e, 0xc0, 0x2e, 0x00, 0x21, 0x80, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x2d, 0x00, 0x00, 0x00,
0x00, 0x1f, 0x49, 0x24, 0x30, 0x66, 0x33, 0x31, 0x30, 0x63, 0x32, 0x63, 0x2d, 0x39, 0x66, 0x62,
0x62, 0x2d, 0x64, 0x61, 0x64, 0x64, 0x2d, 0x31, 0x39, 0x39, 0x30, 0x2d, 0x30, 0x38, 0x31, 0x61,
0x61, 0x66, 0x35, 0x37, 0x65, 0x66, 0x38, 0x33, 0xc0, 0x1d, 0xc1, 0x53, 0x00, 0x1c, 0x80, 0x01,
0x00, 0x00, 0x00, 0x78, 0x00, 0x10, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x3e,
0x51, 0x15, 0x8e, 0x6d, 0xa2, 0x96,
}

dm := DNSMessage{}
dm.DNS.Payload = payload
dm.DNS.Length = len(payload)

header, err := DecodeDNS(payload)
if err != nil {
t.Errorf("unexpected error when decoding header: %v", err)
}

if err = DecodePayload(&dm, &header, pkgconfig.GetDefaultConfig()); err != nil {
t.Error("expected no error on decode", err)
}
}

0 comments on commit 10e4e1e

Please sign in to comment.