-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
344 lines (292 loc) · 7.9 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"flag"
"fmt"
"log"
"log/syslog"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/google/gopacket/pcap"
// setup aws config
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
)
var cshostPtr *string
var cstokenPtr *string
var csschemePtr *string
var upPtr *bool
var wLocal *bool
var panicPtr *bool
var destdir *string
var hostname string
var maxpackets *int
var config tomlConfig
var awsconfig *aws.Config
var ifmap = map[string]pcap.Interface{}
var almap = make(map[string][]string)
type logWriter struct {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
// return fmt.Print(time.Now().UTC().Format("2006-01-02T15:04:05.999Z") + " [DEBUG] " + string(bytes))
return fmt.Print(string(bytes))
}
func updateInterfaceMap() {
x, ierr := pcap.FindAllDevs()
if ierr != nil {
fmt.Printf("Error loading interfaces: %s", ierr)
log.Printf("Error loading interfaces: %s", ierr)
}
for _, v := range x {
fmt.Println("Found interface " + v.Name + " description: " + v.Description)
log.Println("Found interface " + v.Name + " description: " + v.Description)
ifmap[v.Name] = v
}
}
func validateOptions(c tomlConfig) {
if !c.Aws.Upload && !c.Cs.Upload && !c.Gen.Writelocal {
log.Printf("Error. You must enable at least one of the following: Cloudshark, S3 or Writelocal\n")
os.Exit(1)
}
if !c.AwsSqs.Listen && !c.R.Listen && !c.K.Listen {
log.Printf("Error. You must enable at least one listener: Kafka, Redis or Amazon SQS\n")
os.Exit(1)
}
// S3 configs
if c.Aws.Upload {
if c.Aws.AccessID == nil || c.Aws.AccessKey == nil {
log.Fatal("You must supply an accesskey and accessid to use S3 ")
}
if c.Aws.Endpoint == nil || c.Aws.Bucket == nil {
log.Fatal("You must supply an endpoint and a default bucket to use S3 ")
}
if c.Aws.Region == nil {
log.Fatal("You must supply a Region to use S3 ")
}
if c.Aws.ACL == nil {
// default acl to private
*c.Aws.ACL = "private"
}
if c.Aws.Encryption == nil {
// set encryption default
*c.Aws.Encryption = false
}
dest := new(bool)
*dest = true
ll := new(aws.LogLevelType)
*ll = 1
awsconfig = &aws.Config{
Region: c.Aws.Region,
Endpoint: c.Aws.Endpoint,
S3ForcePathStyle: dest, // <-- without these lines. All will fail! fork you aws!
Credentials: credentials.NewStaticCredentials(*c.Aws.AccessID, *c.Aws.AccessKey, ""),
LogLevel: ll,
}
}
// SQS Configs
if c.AwsSqs.Listen {
if c.AwsSqs.AccessID == nil || c.AwsSqs.AccessKey == nil {
log.Fatal("You must supply an accesskey and accessid to use SQS ")
}
if c.AwsSqs.Region == nil {
log.Fatal("You must supply a Region to use SQS ")
}
if c.AwsSqs.URL == nil {
log.Fatal("You must supply a Queue URL to use SQS ")
}
if c.AwsSqs.Waitseconds == nil {
*c.AwsSqs.Waitseconds = 20
}
if c.AwsSqs.Chunksize == nil {
*c.AwsSqs.Waitseconds = 10
}
}
// Kafka configs
if c.K.Listen {
if len(c.K.Server) < 1 {
log.Fatal("You must supply at least 1 Kafka node")
}
if c.K.Topic == "" {
log.Fatal("You must supply a Kafka topic to subscribe to")
}
}
// Redis configs
if c.R.Listen {
if c.R.Host == "" {
log.Fatal("You must supply a redis host")
}
if c.R.Channel == "" {
log.Fatal("You must supply a redis channel to subscribe to")
}
if c.R.Port == 0 {
c.R.Port = 6379
}
}
// Cloudshark configs
if c.Cs.Upload {
if c.Cs.Host == "" {
log.Fatal("If using Cloudshark, you must supply a host")
}
if c.Cs.Token == "" {
log.Fatal("If using Cloudshark, you must supply an API token")
}
if c.Cs.Scheme == "" {
c.Cs.Scheme = "https"
}
if c.Cs.Port == 0 {
c.Cs.Port = 443
}
} else {
fmt.Println("No cloudshark settings")
log.Println("No cloudshark settings")
}
// General configs
if c.Gen.Maxpackets == 0 {
c.Gen.Maxpackets = 10000
}
if c.Gen.Snap == 0 {
c.Gen.Snap = 512
}
if c.Gen.Writelocal {
if _, err := os.Stat(c.Gen.Localdir); os.IsNotExist(err) {
log.Fatal("localdir: " + c.Gen.Localdir + " does not exist")
}
}
if c.Gen.Deftimeout == 0 {
c.Gen.Deftimeout = 60
}
if c.Gen.Maxtimeout == 0 {
c.Gen.Maxtimeout = 3600 * time.Second
}
if c.Gen.Maxduration == 0 {
c.Gen.Maxduration = 3600 * time.Second
}
if c.Gen.Maxbytes == 0 {
// set to 100 megabytes if max not set
c.Gen.Maxbytes = 100000000
}
for _, v := range c.Ifmap {
if v.Name == "" {
log.Fatal("Interface definition missing name property")
}
if _, ok := ifmap[v.Name]; !ok {
log.Fatal("Interface " + v.Name + " does not exist on host")
}
if len(v.Alias) == 0 {
fmt.Println("Warning: Interface [" + v.Name + "] has no aliases")
log.Println("Warning: Interface [" + v.Name + "] has no aliases")
} else {
for _, av := range v.Alias {
almap[av] = append(almap[av], v.Name)
}
}
}
// validate syslog
if c.Log.Priority == 0 {
c.Log.Priority = 85
}
if c.Log.Tag == "" {
c.Log.Tag = "pcapdaemon"
}
}
func main() {
cshostPtr = flag.String("cshost", "", "cloudshark host")
cstokenPtr = flag.String("cstoken", "", "cloudshark api token")
csschemePtr = flag.String("csscheme", "", "cloudshark scheme http|https")
csportPtr := flag.Int("csport", 0, "cloudshark port")
cstimeoutPtr := flag.Int("cstimeout", 0, "cloudshark timeout")
configfile := flag.String("config", "", "/path/to/configfile")
redisnode := flag.String("redisnode", "", "Hostname|IP of redis server. Default localhost")
redisport := flag.Int("redisport", 0, "Port of redis server. Default 6379")
redischannel := flag.String("redischannel", "", "Redis channel to subscribe to. Default capture")
maxpackets = flag.Int("maxpackets", 0, "Maximum number of packets per capture. Default 50000")
upPtr = flag.Bool("upload", false, "Upload pcap")
panicPtr = flag.Bool("panic", false, "Enable panics on layer parsing")
// flags for writing locally
wLocal = flag.Bool("writelocal", false, "Write files locally. Must set destdir")
destdir = flag.String("destdir", "", "Destination directory locally for pcap files. Requires -writelocal")
// parse the flags
flag.Parse()
if *configfile != "" {
if _, err := os.Stat(*configfile); os.IsNotExist(err) {
log.Fatal(*configfile + " does not exist")
} else {
if _, err := toml.DecodeFile(*configfile, &config); err != nil {
log.Fatal(err)
}
}
}
log.SetFlags(0)
logwriter, e := syslog.New(syslog.Priority(config.Log.Priority), config.Log.Tag)
if e == nil {
log.SetOutput(logwriter)
}
// create interface map
updateInterfaceMap()
// Overrides. Command line parameters, if set, override the config file
if *redisnode != "" {
config.R.Host = *redisnode
}
if *redisport != 0 {
config.R.Port = *redisport
}
if *redischannel != "" {
config.R.Channel = *redischannel
}
if *cshostPtr != "" {
config.Cs.Host = *cshostPtr
}
if *cstokenPtr != "" {
config.Cs.Token = *cstokenPtr
}
if *csschemePtr != "" {
config.Cs.Scheme = *csschemePtr
}
if *csportPtr != 0 {
config.Cs.Port = *csportPtr
}
if *cstimeoutPtr != 0 {
config.Cs.Timeout = *cstimeoutPtr
}
if *upPtr {
config.Cs.Upload = true
}
if *maxpackets != 0 {
config.Gen.Maxpackets = *maxpackets
}
if *wLocal {
config.Gen.Writelocal = true
if *destdir != "" {
config.Gen.Localdir = *destdir
}
}
// Run the validator AFTER defaults and config have been processed
validateOptions(config)
hostname, _ = os.Hostname()
// Channel for thread sync
done := make(chan bool)
if config.R.Listen {
go func() {
log.Println("Starting Redis Thread")
subToRedis(config.R.Host, config.R.Port, config.R.Channel, config.R.Auth)
done <- true
}()
}
if config.K.Listen {
go func() {
log.Println("Starting Kafka Thread")
subToKafka(config.K.Server, config.K.Topic)
done <- true
}()
}
if config.AwsSqs.Listen {
go func() {
log.Println("Starting SQS Thread")
subToSqs()
done <- true
}()
}
// This little channel read prevents main() from ending
<-done
log.Println("Exiting")
}