Skip to content

Commit

Permalink
implement questions in dns payload
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Apr 9, 2022
1 parent f70074a commit 41d35f2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/core/packetgen/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,49 @@ func buildICMPV4Packet(c ICMPV4PacketConfig) *layers.ICMPv4 {
}
}

// DNSQuestion wraps a single request (question) within a DNS query.
type DNSQuestion struct {
Name string
Type layers.DNSType
Class layers.DNSClass
}

type DNSPacketConfig struct {
ID uint16
Qr bool
OpCode uint8
QDCount uint16
ID uint16
Qr bool
OpCode uint8

AA bool // Authoritative answer
TC bool // Truncated
RD bool // Recursion desired
RA bool // Recursion available
Z uint8 // Reserved for future use

ResponseCode uint8
QDCount *uint16 // Number of questions to expect

// Entries
Questions []DNSQuestion
}

func buildDNSPacket(c DNSPacketConfig) *layers.DNS {
questions := make([]layers.DNSQuestion, 0, len(c.Questions))
for _, question := range c.Questions {
questions = append(questions, layers.DNSQuestion{Name: []byte(question.Name), Type: question.Type, Class: question.Class})
}

return &layers.DNS{
ID: c.ID,
QR: c.Qr,
OpCode: layers.DNSOpCode(c.OpCode),
QDCount: c.QDCount,
ID: c.ID,
QR: c.Qr,
OpCode: layers.DNSOpCode(c.OpCode),

AA: c.AA,
TC: c.TC,
RD: c.RD,
RA: c.RA,
Z: c.Z,

QDCount: utils.NonNilOrDefault(c.QDCount, uint16(len(c.Questions))),
Questions: questions,
}
}
57 changes: 57 additions & 0 deletions src/core/packetgen/serialization_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package packetgen

import (
"bytes"
"encoding/binary"
"fmt"
"testing"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/miekg/dns"
"go.uber.org/zap"

"github.com/Arriven/db1000n/src/utils"
Expand Down Expand Up @@ -133,3 +135,58 @@ func extractPayload(p gopacket.Packet, c map[string]any) error {

return nil
}

func TestDNS(t *testing.T) {
t.Parallel()

configTPL := map[string]any{
"payload": map[string]any{
"type": "dns",
"data": map[string]any{
"id": 1234,
"op_code": 0,
"rd": true,
"questions": []map[string]any{
{
"name": "google.com",
"type": "1",
"class": "1",
},
},
},
},
}

logger := zap.NewExample()
config := templates.ParseAndExecuteMapStruct(logger, configTPL, nil)

var packetConfig PacketConfig
if err := utils.Decode(config, &packetConfig); err != nil {
t.Fatal(err)
}

logger.Debug("deserialized packet config", zap.Any("packet", packetConfig))

packet, err := packetConfig.Build()
if err != nil {
t.Fatal(err)
}

buf := gopacket.NewSerializeBuffer()
if err = packet.Serialize(buf); err != nil {
t.Fatal(err)
}

question := new(dns.Msg).
SetQuestion(dns.Fqdn("google.com"), 1)
question.Id = 1234

out, err := question.Pack()
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(buf.Bytes(), out) {
t.Fatal(fmt.Errorf("not equal %v to %v", buf.Bytes(), out))
}
}

0 comments on commit 41d35f2

Please sign in to comment.