forked from sourque/louis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlouis.go
235 lines (207 loc) · 5.91 KB
/
louis.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
package main
import (
"fmt"
"os"
"os/signal"
"github.com/sourque/louis/analysis"
"github.com/sourque/louis/events"
"github.com/sourque/louis/output"
"github.com/sourque/louis/techs"
"github.com/spf13/cobra"
)
var (
active bool
mitigate bool
duplicates bool
interactive bool
ignoreList []string
)
const (
version = "0.0.5"
)
func main() {
cmdMonitor := &cobra.Command{
Use: "monitor",
Aliases: []string{"m", "mon", "eyes"},
Short: "actively monitor for malicious action",
Run: func(cmd *cobra.Command, args []string) {
louisMonitor()
},
}
cmdMonitor.Flags().BoolVarP(&mitigate, "mitigate", "m", false, "attempt to mitigate detected techniques")
cmdMonitor.Flags().BoolVarP(&duplicates, "duplicates", "d", false, "show duplicate detections")
cmdMonitor.Flags().StringSliceVarP(&ignoreList, "ignore", "i", []string{}, "don't show certain event types in verbose mode (ex. -i open)")
cmdHunt := &cobra.Command{
Use: "hunt",
Aliases: []string{"h", "uwu"},
Short: "hunt for existing malicious activity",
Run: func(cmd *cobra.Command, args []string) {
louisHunt()
},
}
cmdHunt.Flags().BoolVarP(&mitigate, "mitigate", "m", false, "attempt to mitigate detected techniques")
cmdMitigate := &cobra.Command{
Use: "mitigate",
Aliases: []string{"mit", "cybpat"},
Short: "mitigate all known vulnerabilities",
Run: func(cmd *cobra.Command, args []string) {
louisMitigate()
},
}
cmdVersion := &cobra.Command{
Use: "version",
Short: "print louis version",
Run: func(cmd *cobra.Command, args []string) {
output.Notice("louis version", version)
},
}
rootCmd := &cobra.Command{
Use: "louis",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
output.Init()
},
}
rootCmd.PersistentFlags().BoolVarP(&active, "active", "a", false, "counter detected malicious activity (dangerous, may clobber)")
rootCmd.PersistentFlags().BoolVarP(&output.Verbose, "verbose", "v", false, "enable verbose output")
rootCmd.PersistentFlags().BoolVarP(&output.Syslog, "syslog", "s", false, "output to syslog")
rootCmd.AddCommand(cmdMonitor, cmdHunt, cmdMitigate, cmdVersion)
rootCmd.Execute()
}
func louisMonitor() {
output.Info("Welcome to louis :)")
// Quit when program receives CTRL-C.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
// Events context (contains output/error channels and *sync.WaitGroup)
evCtx := events.NewContext()
evChan := make(chan events.Event)
// List of implemented sources
sourceList := []func(chan events.Event, events.Ctx){
events.ExecBPF,
events.ListenBPF,
events.OpenBPF,
events.ReadlineBPF,
}
// Load each eBPF module
output.Info("Loading eBPF modules...")
evCtx.LoadWg.Add(len(sourceList))
for _, sourceFunc := range sourceList {
go sourceFunc(evChan, evCtx)
}
evLoaded := make(chan bool)
go func() {
evCtx.LoadWg.Wait()
evLoaded <- true
}()
// Handle output from BPF modules
go func() {
output.Info("Beginning monitoring loop...")
for {
var detections []*analysis.Detection
var err error
select {
case module := <-evCtx.Load:
output.Info("Loaded module:", module)
case <-evLoaded:
output.Info("All modules loaded!")
case err := <-evCtx.Error:
output.Negative("Error:", err)
case ev := <-evChan:
events.Log(ev)
switch ev.(type) {
case *events.Exec:
detections, err = analysis.Exec(ev.(*events.Exec))
case *events.Listen:
detections, err = analysis.Listen(ev.(*events.Listen))
case *events.Open:
detections, err = analysis.Open(ev.(*events.Open))
case *events.Readline:
detections, err = analysis.Readline(ev.(*events.Readline))
}
if typeHeader := events.TypeHeader(ev); !output.IsIgnored(ignoreList, typeHeader) {
output.Event(typeHeader, fmt.Sprintf("%s {ret: %d} (uid: %d) [pid: %d]", ev.Print(), ev.FetchRetVal(), ev.FetchUid(), ev.FetchPid()))
}
}
// Handle detection results
if err != nil {
output.Err(err)
continue
}
for _, det := range detections {
analysis.Log(*det)
if det.Dupe.Tech != nil {
if duplicates {
output.Leveled(det.Level, "DUPLICATE!", det.Print())
}
} else {
output.Leveled(det.Level, det.Print())
output.Tabber(1)
output.Negative(det.Brief())
for i := len(det.Artifacts) - 1; i >= 0; i-- {
art := det.Artifacts[i]
output.EventLog(art.Time, events.TypeHeader(art.Ev), art.Ev.Print())
}
}
if active {
// Clean most recent artifact
if len(det.Artifacts) > 0 {
output.Positive("Cleaning:", det.Tech.Name())
if err := det.Tech.Clean(det.Artifacts[0].Ev); err != nil {
output.Negative("Cleaning failed:", err.Error())
}
}
if mitigate {
output.Positive("Mitigating", det.Tech)
det.Tech.Mitigate()
}
}
output.Tabber(0)
}
}
}()
<-sig
output.Info("Waiting for monitoring routines to quit...")
evCtx.Quit <- true
}
func louisHunt() {
ts := techs.All()
for _, t := range ts {
output.Info("Hunting:", t.Name())
if res, err := t.Hunt(); err != nil {
output.Negative("Error in hunting:", t.Name()+":", err.Error())
} else if res.Found {
output.Positive("Found:", t.Name(), res.Ev.Print())
if active {
t.Clean(res.Ev)
if mitigate {
t.Mitigate()
}
}
}
}
}
func louisMitigate() {
ts := techs.All()
for _, t := range ts {
output.Info("Checking:", t.Name())
if res, err := t.Check(); err != nil {
output.Negative("Error in checking for mitigation:", t.Name()+":", err.Error())
} else if res.Found {
if !active {
output.Positive("Mitigation possible:", t.Name())
if res.Ev != nil {
output.Tabber(1)
output.Negative(res.Ev.Print)
output.Tabber(0)
}
} else {
output.Info("Mitigating:", t.Name())
if err := t.Mitigate(); err != nil {
output.Negative("Error in mitigating:", t.Name()+":", err.Error())
} else {
output.Positive("Mitigated:", t.Name())
}
}
}
}
}