-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoohbear.go
125 lines (88 loc) · 3.02 KB
/
poohbear.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
package main
import (
"os"
"os/signal"
"syscall"
"log"
"flag"
"time"
)
type AppGlobals struct {
sniffer PcapSniffer
etherAddr string
iface string
isRunning bool
alertConfig AlertConfig
EphemeralStart uint16
}
func main() {
log.Printf(PoohBanner())
iface := flag.String("iface", "eth0", "interface to sniff. Defaults to eth0")
sesEmailConfig := flag.String("email", "", "email address used for alerting")
sesRegionConfig := flag.String("SES region", "us-west-2", "[OPTIONAL] AWS region for SES service, defaults to us-west-2")
alertIntervalConfig := flag.String("interval", "10m", "[OPTIONAL] interval for batching alerts together, (eg. 30s, 60m). Defaults to 10m")
whiteListPortsConfig := flag.String("whitelist-ports", "", "[OPTIONAL] CSV string of ports to never alert on (eg: 22,80,443,8888)")
whiteListMACsConfig := flag.String("whitelist-macs", "", "[OPTIONAL] CSV string of MAC addresses to never alert on (eg: aa:bb:cc:dd:ee:ff,a2:b2:c2:d2:e2:f2)")
whiteListMACPortCombosConfig := flag.String("whitelist-macsports", "", "[OPTIONAL] CSV string of MAC address|port combinations to never alert on (eg: aa:bb:cc:dd:ee:ff|8888)")
flag.Parse()
// TODO - get these as args
ai, couldParse := time.ParseDuration(*alertIntervalConfig)
if couldParse != nil{
log.Fatalf("Could not parse duration: %v\n%v", ai, couldParse)
}
wlp := parseWhiteListPorts(whiteListPortsConfig)
wlm := parseWhiteListMACs(whiteListMACsConfig)
wlmp := parseWhiteListMACPortCombos(whiteListMACPortCombosConfig)
// confirm the configs
log.Printf("")
log.Printf("======================================")
log.Printf("Batch alert duration: %v", ai)
log.Printf("Using white list of ports: %v", wlp)
log.Printf("Using white list of MACs: %v", wlm)
log.Printf("Using white list of MAC|port combinations: %v", wlmp)
log.Printf("======================================")
log.Printf("")
sniffer := &PcapSniffer{}
etherAddr := getMacAddr(*iface)
if err := sniffer.Open(*iface); err != nil {
log.Fatal("Failed to open the sniffer: ", err)
}
// initialize an empty alert
initialAlert := NewAlert()
// Construct alerting config from the arguments
ac := AlertConfig {
alertInterval: ai,
whiteListPorts: wlp,
whiteListMACPortCombos: wlmp,
whiteListMACs: wlm,
alertHandler: SesAlertHandler,
currentAlert: initialAlert,
previousAlert: nil,
sesEmail: *sesEmailConfig,
sesRegion: *sesRegionConfig,
}
// Global structures all wired together to run
globals := AppGlobals {
sniffer: *sniffer,
etherAddr: etherAddr,
isRunning: true,
alertConfig: ac,
EphemeralStart: 32768,
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigc
log.Print("Received sigterm/sigint, stopping")
globals.isRunning = false
}()
// start alerting loop async
go func(appGlobals *AppGlobals) {
for appGlobals.isRunning {
time.Sleep(appGlobals.alertConfig.alertInterval)
log.Printf("Alert Cycling")
appGlobals.alertConfig.Cycle()
}
}(&globals)
Listen(&globals)
}