Skip to content

Instantly share code, notes, and snippets.

@eze-kiel
Last active February 16, 2021 08:26
Show Gist options
  • Save eze-kiel/12c4efbdc82cf74dbbda757c721932f5 to your computer and use it in GitHub Desktop.
Save eze-kiel/12c4efbdc82cf74dbbda757c721932f5 to your computer and use it in GitHub Desktop.
Hand-crafted CNAME query
package main
import (
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func main() {
// Select the interace to use
handle, err := pcap.OpenLive("lo", 1500, false, pcap.BlockForever)
if err != nil {
panic(err)
}
// Create ethernet layer
eth := layers.Ethernet{
SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeIPv4,
}
// Create ip layer
ip := layers.IPv4{
Version: 4,
TTL: 64,
SrcIP: net.IP{1, 3, 3, 7},
DstIP: net.IP{127, 0, 0, 1},
Protocol: layers.IPProtocolUDP,
}
// Create udp layer
udp := layers.UDP{
SrcPort: 62003,
DstPort: 53,
}
udp.SetNetworkLayerForChecksum(&ip)
// Create the DNS question
qst := layers.DNSQuestion{
Name: []byte{'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', '.'},
Type: layers.DNSTypeCNAME,
Class: layers.DNSClassIN,
}
// Create the DNS layer
dns := layers.DNS{
BaseLayer: layers.BaseLayer{},
ID: 0,
QR: true,
OpCode: 0,
AA: false,
TC: false,
RD: true,
RA: true,
Z: 0,
ResponseCode: 0,
QDCount: 1,
ANCount: 0,
NSCount: 0,
ARCount: 0,
Questions: []layers.DNSQuestion{qst},
}
// Create the data buffer and its options
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
// Assemble the layers to create a packet
if err = gopacket.SerializeLayers(buffer, options,
&eth,
&ip,
&udp,
&dns,
); err != nil {
panic(err)
}
outgoingPacket := buffer.Bytes()
// Send the packet
if err = handle.WritePacketData(outgoingPacket); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment