-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (167 loc) · 4.79 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
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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
var (
devName string
esIndex string
esDocType string
esServer string
esUser string
esPassword string
destNet string
verbosity bool
err error
handle *pcap.Handle
SrcIP string
DstIP string
httpTransport *http.Transport
)
type DnsMsg struct {
Timestamp string
SourceIP string
DestinationIP string
DnsQuery string
DnsAnswer []string
DnsAnswerTTL []string
NumberOfAnswers string
DnsResponseCode string
DnsOpCode string
}
func sendToElastic(dnsMsg DnsMsg, wg *sync.WaitGroup) {
defer wg.Done()
var jsonMsg, jsonErr = json.Marshal(dnsMsg)
if jsonErr != nil {
panic(jsonErr)
}
// getting ready for elasticsearch
request, reqErr := http.NewRequest("POST", "https://"+esServer+":9200/"+esIndex+"/"+esDocType,
bytes.NewBuffer(jsonMsg))
if reqErr != nil {
panic(reqErr)
}
request.SetBasicAuth(esUser, esPassword)
request.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: 60 * time.Second,
Transport: httpTransport,
}
resp, elErr := client.Do(request)
if elErr != nil {
panic(elErr)
}
defer resp.Body.Close()
}
func main() {
flag.StringVar(&devName, "i", "eth0", "Listening on interface")
flag.StringVar(&esServer, "e", "127.0.0.1", "Hostname of Elastic service")
flag.StringVar(&esUser, "u", "logstash", "Elastic username")
flag.StringVar(&esPassword, "p", "logstash", "Elastic user password")
flag.StringVar(&esIndex, "s", "dns_index", "Elastic index name")
flag.StringVar(&esDocType, "t", "syslog", "Elastic document type")
flag.StringVar(&destNet, "x", "0.0.0.0/0", "Set destination network")
flag.BoolVar(&verbosity, "v", false, "Show requests in console")
flag.Parse()
httpTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var eth layers.Ethernet
var ip4 layers.IPv4
var ip6 layers.IPv6
var tcp layers.TCP
var udp layers.UDP
var dns layers.DNS
var payload gopacket.Payload
wg := new(sync.WaitGroup)
// Open device
handle, err = pcap.OpenLive(devName, 1600, false, pcap.BlockForever)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
var filter = fmt.Sprintf("udp and port 53 and dst net %s", destNet)
fmt.Println(" Filter: ", filter)
err := handle.SetBPFFilter(filter)
if err != nil {
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 {
fmt.Println("Error reading packet data: ", err)
continue
}
err = parser.DecodeLayers(data, &decodedLayers)
for _, typ := range decodedLayers {
switch typ {
case layers.LayerTypeIPv4:
SrcIP = ip4.SrcIP.String()
DstIP = ip4.DstIP.String()
case layers.LayerTypeIPv6:
SrcIP = ip6.SrcIP.String()
DstIP = ip6.DstIP.String()
case layers.LayerTypeDNS:
dnsOpCode := int(dns.OpCode)
dnsResponseCode := int(dns.ResponseCode)
dnsANCount := int(dns.ANCount)
if (dnsANCount == 0 && dnsResponseCode > 0) || (dnsANCount > 0) {
if verbosity {
fmt.Println("------------------------")
fmt.Println(" DNS Record Detected")
}
for _, dnsQuestion := range dns.Questions {
t := time.Now()
timestamp := t.Format(time.RFC3339)
// Add a document to the index
d := DnsMsg{
Timestamp: timestamp,
SourceIP: SrcIP,
DestinationIP: DstIP,
DnsQuery: string(dnsQuestion.Name),
DnsOpCode: strconv.Itoa(dnsOpCode),
DnsResponseCode: strconv.Itoa(dnsResponseCode),
NumberOfAnswers: strconv.Itoa(dnsANCount),
}
if verbosity {
fmt.Println(" DNS OpCode: ", strconv.Itoa(int(dns.OpCode)))
fmt.Println(" DNS ResponseCode: ", dns.ResponseCode.String())
fmt.Println(" DNS # Answers: ", strconv.Itoa(dnsANCount))
fmt.Println(" DNS Question: ", string(dnsQuestion.Name))
fmt.Println(" DNS Endpoints: ", SrcIP, DstIP)
}
if dnsANCount > 0 {
for _, dnsAnswer := range dns.Answers {
d.DnsAnswerTTL = append(d.DnsAnswerTTL, fmt.Sprint(dnsAnswer.TTL))
if dnsAnswer.IP.String() != "<nil>" {
if verbosity {
fmt.Println(" DNS Answer: ", dnsAnswer.IP.String())
}
d.DnsAnswer = append(d.DnsAnswer, dnsAnswer.IP.String())
}
}
}
wg.Add(1)
sendToElastic(d, wg)
}
}
}
}
if err != nil {
fmt.Println(" Error encountered:", err)
}
}
}