-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbadcapt.go
330 lines (280 loc) · 7.26 KB
/
badcapt.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package badcapt
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"log"
"net"
"strings"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/olivere/elastic"
)
const (
indexName = "badcapt"
docType = "bcrecord"
)
// Marker represents a routine that identifies the raw packet.
type Marker func(gopacket.Packet) []string
var defaultMarkers = []Marker{
MiraiIdentifier,
ZmapIdentifier,
MasscanIdentifier,
LowMSSIdentifier,
}
// Badcapt defines badcapt configuration
type Badcapt struct {
elasticClient *elastic.Client
indexName string
docType string
markers []Marker
exportFunc func(context.Context, *Record) error
portsDescription NmapServices
}
// TaggedPacket represents a packet that went through markers.
type TaggedPacket struct {
Packet gopacket.Packet
Tags []string
}
// Record contains packet data, that is ready to be exported
type Record struct {
SrcIP net.IP `json:"src_ip,omitempty"`
Layers []string `json:"layers,omitempty"`
SrcPort uint16 `json:"src_port,omitempty"`
DstIP net.IP `json:"dst_ip,omitempty"`
DstPort uint16 `json:"dst_port,omitempty"`
DstService string `json:"dst_service,omitempty"`
Timestamp time.Time `json:"date"`
Tags []string `json:"tags"`
Payload []byte `json:"payload,omitempty"`
PayloadString string `json:"payload_str,omitempty"`
}
func unpackIPv4(p gopacket.Packet) *layers.IPv4 {
ip4Layer := p.Layer(layers.LayerTypeIPv4)
if ip4Layer == nil {
return nil
}
ip4 := ip4Layer.(*layers.IPv4)
return ip4
}
func unpackTCP(p gopacket.Packet) *layers.TCP {
tcpLayer := p.Layer(layers.LayerTypeTCP)
if tcpLayer == nil {
return nil
}
tcp := tcpLayer.(*layers.TCP)
return tcp
}
// NewRecord constructs a record for export.
func NewRecord(tp *TaggedPacket) (*Record, error) {
var layers []string
for _, l := range tp.Packet.Layers() {
layers = append(layers, l.LayerType().String())
}
var srcIP, dstIP net.IP
if netLayer := tp.Packet.NetworkLayer(); netLayer != nil {
srcIP = net.IP(netLayer.NetworkFlow().Src().Raw())
dstIP = net.IP(netLayer.NetworkFlow().Dst().Raw())
}
var srcPort, dstPort uint16
if trLayer := tp.Packet.TransportLayer(); trLayer != nil {
srcPort = binary.BigEndian.Uint16(trLayer.TransportFlow().Src().Raw())
dstPort = binary.BigEndian.Uint16(trLayer.TransportFlow().Dst().Raw())
}
var payload []byte
if appLayer := tp.Packet.ApplicationLayer(); appLayer != nil {
payload = appLayer.Payload()
}
return &Record{
SrcIP: srcIP,
DstIP: dstIP,
SrcPort: srcPort,
DstPort: dstPort,
Timestamp: tp.Packet.Metadata().CaptureInfo.Timestamp,
Payload: payload,
PayloadString: string(payload),
Tags: tp.Tags,
Layers: layers,
}, nil
}
func (b *Badcapt) export(ctx context.Context, tp *TaggedPacket) error {
record, err := NewRecord(tp)
if err != nil {
return err
}
if b.portsDescription != nil {
var proto string
for _, p := range record.Layers {
if p == "TCP" || p == "UDP" || p == "SCTP" {
proto = strings.ToLower(p)
break
}
}
if proto != "" {
record.DstService = b.portsDescription[fmt.Sprintf("%d/%s", record.DstPort, proto)]
}
}
return b.exportFunc(ctx, record)
}
func (b *Badcapt) exportElastic(ctx context.Context, record *Record) error {
_, err := b.elasticClient.Index().
Index(b.indexName).
Type(b.docType).
BodyJson(record).
Do(ctx)
return err
}
func (b *Badcapt) exportScreen(_ context.Context, record *Record) error {
data, err := json.Marshal(record)
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
// New bootstraps badcapt configuration.
func New(opts ...func(*Badcapt) error) (*Badcapt, error) {
conf := &Badcapt{
elasticClient: nil,
indexName: indexName,
docType: docType,
markers: defaultMarkers,
}
conf.exportFunc = conf.exportScreen
for _, f := range opts {
err := f(conf)
if err != nil {
return nil, err
}
}
if conf.elasticClient == nil {
return conf, nil
}
exists, err := conf.elasticClient.IndexExists(indexName).Do(context.Background())
if err != nil {
return nil, err
}
if !exists {
_, err := conf.elasticClient.CreateIndex(indexName).Do(context.Background())
if err != nil {
return nil, err
}
}
return conf, nil
}
// AddPacketMarker adds a packet marking routine.
func AddPacketMarker(m Marker) func(*Badcapt) error {
return func(b *Badcapt) error {
b.markers = append(b.markers, m)
return nil
}
}
// SetElastic sets elasticsearch client to export events to.
func SetElastic(client *elastic.Client) func(*Badcapt) error {
return func(b *Badcapt) error {
b.elasticClient = client
b.exportFunc = b.exportElastic
return nil
}
}
// SetElasticIndexName sets an index name where events are going to be written.
func SetElasticIndexName(name string) func(*Badcapt) error {
return func(b *Badcapt) error {
b.indexName = name
return nil
}
}
// SetElasticDocType sets the events documents type.
func SetElasticDocType(doc string) func(*Badcapt) error {
return func(b *Badcapt) error {
b.docType = doc
return nil
}
}
// SetExportFunc to export events the way user want.
func SetExportFunc(fn func(ctx context.Context, rec *Record) error) func(*Badcapt) error {
return func(b *Badcapt) error {
b.exportFunc = fn
return nil
}
}
// SetNmapServicesPath to translate port number to a service name.
func SetNmapServicesPath(path string) func(*Badcapt) error {
var err error
return func(b *Badcapt) error {
b.portsDescription, err = ParseNmapServices(path)
if err != nil {
return fmt.Errorf("parsing nmap-services file: %w", err)
}
log.Printf("parsed descriptions for %d ports", len(b.portsDescription))
return nil
}
}
// NewConfig bootstraps badcapt configuration.
// Deprecated. Use New instead.
func NewConfig(elasticLoc string, markers ...Marker) (*Badcapt, error) {
client, err := elastic.NewClient(
elastic.SetURL(elasticLoc),
elastic.SetSniff(false),
)
if err != nil {
return nil, err
}
conf := &Badcapt{
elasticClient: client,
indexName: indexName,
docType: docType,
}
exists, err := client.IndexExists(indexName).Do(context.Background())
if err != nil {
return nil, err
}
if !exists {
_, err := client.CreateIndex(indexName).Do(context.Background())
if err != nil {
return nil, err
}
}
if len(markers) == 0 {
conf.markers = defaultMarkers
}
return conf, nil
}
// Listen starts packet sniffing and processing
func (b *Badcapt) Listen(iface string) error {
handle, err := pcap.OpenLive(iface, 1600, true, pcap.BlockForever)
if err != nil {
return err
}
err = handle.SetDirection(pcap.DirectionIn)
if err != nil {
return nil
}
defer handle.Close()
log.Printf("Started capturing on iface %s", iface)
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
p, err := packetSource.NextPacket()
if err == io.EOF {
break
} else if err != nil {
log.Println(err)
continue
}
var tags []string
for _, fn := range b.markers {
tags = append(tags, fn(p)...)
}
if len(tags) == 0 {
continue
}
if err := b.export(context.Background(), &TaggedPacket{p, tags}); err != nil {
log.Println(err)
}
}
return nil
}