-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
56 lines (46 loc) · 1.43 KB
/
main.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
package main
import (
"log"
"net"
"github.com/google/gopacket/pcap"
"github.com/jackpal/gateway"
"github.com/mostlygeek/arp"
)
// performDNSRequest creates a DNS query message, creates a UDP frame with the DNS query message
// as the payload, and sends the UDP frame to the default gateway.
func performDNSRequest(query DNSMessage) {
messageBytes := query.ToByteSlice()
log.Printf("messageBytes: %x\n", messageBytes)
defaultGatewayIP, _ := gateway.DiscoverGateway()
log.Printf("defaultGatewayIP: %#v\n", defaultGatewayIP.String())
defaultGatewayMac, _ := net.ParseMAC(StandardizeMACFormat(arp.Search(defaultGatewayIP.String())))
log.Printf("defaultGatewayMac: %#v\n", defaultGatewayMac)
udpFrameOptions := UdpFrameOptions{
sourceIP: net.IPv4(127, 0, 0, 1),
destIP: net.IPv4(1, 1, 1, 1),
sourcePort: 4000,
destPort: 53,
sourceMac: GetMacAddrForInterface("en0"),
destMac: defaultGatewayMac,
isIPv6: false,
payloadBytes: query.ToByteSlice(),
}
frameBytes, err := CreateSerializedUDPFrame(udpFrameOptions)
if err != nil {
log.Fatal(err)
}
handle, err := pcap.OpenLive("en0", 1024, false, pcap.BlockForever)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
if err := handle.WritePacketData(frameBytes); err != nil {
log.Fatal(err)
}
log.Println("DNS request sent.")
}
func main() {
query := "fb.me"
queryMessage := GenerateDNSMessage(query)
performDNSRequest(queryMessage)
}