-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
172 lines (149 loc) · 3.7 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
package main
import (
"fmt"
"os"
"time"
"github.com/chazlever/rickybobby/iohandlers"
"github.com/chazlever/rickybobby/parser"
"github.com/pkg/profile"
log "github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1"
)
func getOutputFormats() []string {
marshalers := make([]string, 0, len(iohandlers.Marshalers))
for m := range iohandlers.Marshalers {
marshalers = append(marshalers, m)
}
return marshalers
}
func loadGlobalOptions(c *cli.Context) error {
parser.DoParseTcp = c.GlobalBool("tcp")
parser.DoParseQuestions = c.GlobalBool("questions")
parser.DoParseQuestionsEcs = c.GlobalBool("questions-ecs")
parser.Source = c.GlobalString("source")
parser.Sensor = c.GlobalString("sensor")
outputFormat := c.GlobalString("format")
parser.OutputFormat = outputFormat
outputFormats := make(map[string]bool)
for _, format := range getOutputFormats() {
outputFormats[format] = true
}
if _, ok := outputFormats[outputFormat]; !ok {
return cli.NewExitError(
fmt.Sprintf("ERROR: Invalid output format: \"%s\" not in %v",
outputFormat,
getOutputFormats()),
1)
}
return nil
}
func pcapCommand(c *cli.Context) error {
if c.NArg() < 1 {
return cli.NewExitError("ERROR: must provide at least one filename", 1)
}
if c.GlobalBool("profile") {
defer profile.Start().Stop()
}
if err := loadGlobalOptions(c); err != nil {
return err
}
for _, f := range c.Args() {
parser.ParseFile(f)
}
return nil
}
func liveCommand(c *cli.Context) error {
if c.NArg() != 1 {
return cli.NewExitError("ERROR: must supply exactly one interface", 1)
}
if c.GlobalBool("profile") {
defer profile.Start().Stop()
}
if err := loadGlobalOptions(c); err != nil {
return err
}
// Load command specific flags
snapshotLen := int32(c.Int("snaplen"))
promiscuous := c.Bool("promiscuous")
timeout := time.Duration(c.Int("timeout")) * time.Second
parser.ParseDevice(c.Args().First(), snapshotLen, promiscuous, timeout)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "rickybobby"
app.Usage = "Parsing DNS packets when you wanna GO fast!"
app.Version = "1.0.5"
app.Compiled = time.Now()
app.Authors = []cli.Author{
{
Name: "Chaz Lever",
Email: "chazlever@users.noreply.github.com",
},
}
app.Commands = []cli.Command{
{
Name: "pcap",
Usage: "read packets from a PCAP file",
Action: pcapCommand,
ArgsUsage: "[file...]",
},
{
Name: "live",
Usage: "read packets from a live interface",
Action: liveCommand,
ArgsUsage: "[interface]",
Flags: []cli.Flag{
cli.IntFlag{
Name: "snaplen",
Usage: "set snapshot length for PCAP collection",
Value: 4096,
},
cli.BoolFlag{
Name: "promiscuous",
Usage: "set promiscuous mode for traffic collection",
},
cli.IntFlag{
Name: "timeout",
Usage: "set timeout value for traffic collection",
Value: 30,
},
},
},
}
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "tcp",
Usage: "attempt to parse TCP packets",
},
cli.BoolFlag{
Name: "questions",
Usage: "parse questions in addition to responses",
},
cli.BoolFlag{
Name: "questions-ecs",
Usage: "parse questions only if they contain ECS information",
},
cli.BoolFlag{
Name: "profile",
Usage: "toggle performance profiler",
},
cli.StringFlag{
Name: "sensor",
Usage: "name of sensor DNS traffic was collected from",
},
cli.StringFlag{
Name: "source",
Usage: "name of source DNS traffic was collected from",
},
cli.StringFlag{
Name: "format",
Usage: fmt.Sprintf("specify the output formatter to use %+q", getOutputFormats()),
Value: "json",
},
}
app.Action = cli.ShowAppHelp
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}