From 2e3e4f453b438272a972987df5f101592bcfccf6 Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 21 Mar 2019 11:20:48 +0100 Subject: [PATCH] fix: events.include and events.ignore now filter for both events.stream and api.rest --- modules/api_rest/api_rest.go | 4 +- modules/api_rest/api_rest_controller.go | 8 +++- modules/events_stream/events_stream.go | 20 ++++----- .../events_ignore_list.go | 42 ++++++++++--------- session/session.go | 22 +++++----- 5 files changed, 54 insertions(+), 42 deletions(-) rename modules/events_stream/ignore_list.go => session/events_ignore_list.go (63%) diff --git a/modules/api_rest/api_rest.go b/modules/api_rest/api_rest.go index 444e422d..04cb455d 100644 --- a/modules/api_rest/api_rest.go +++ b/modules/api_rest/api_rest.go @@ -174,7 +174,10 @@ func (mod *RestAPI) Configure() error { router.Methods("OPTIONS").HandlerFunc(mod.corsRoute) + router.HandleFunc("/api/file", mod.fileRoute) + router.HandleFunc("/api/events", mod.eventsRoute) + router.HandleFunc("/api/session", mod.sessionRoute) router.HandleFunc("/api/session/ble", mod.sessionRoute) router.HandleFunc("/api/session/ble/{mac}", mod.sessionRoute) @@ -191,7 +194,6 @@ func (mod *RestAPI) Configure() error { router.HandleFunc("/api/session/started-at", mod.sessionRoute) router.HandleFunc("/api/session/wifi", mod.sessionRoute) router.HandleFunc("/api/session/wifi/{mac}", mod.sessionRoute) - router.HandleFunc("/api/file", mod.fileRoute) mod.server.Handler = router diff --git a/modules/api_rest/api_rest_controller.go b/modules/api_rest/api_rest_controller.go index af087179..16738d89 100644 --- a/modules/api_rest/api_rest_controller.go +++ b/modules/api_rest/api_rest_controller.go @@ -176,7 +176,13 @@ func (mod *RestAPI) showEvents(w http.ResponseWriter, r *http.Request) { if mod.useWebsocket { mod.startStreamingEvents(w, r) } else { - events := session.I.Events.Sorted() + events := make([]session.Event, 0) + for _, e := range session.I.Events.Sorted() { + if mod.Session.EventsIgnoreList.Ignored(e) == false { + events = append(events, e) + } + } + nevents := len(events) nmax := nevents n := nmax diff --git a/modules/events_stream/events_stream.go b/modules/events_stream/events_stream.go index 517e4db1..8fc56466 100644 --- a/modules/events_stream/events_stream.go +++ b/modules/events_stream/events_stream.go @@ -29,7 +29,6 @@ type EventsStream struct { outputName string output *os.File rotation rotation - ignoreList *IgnoreList triggerList *TriggerList waitFor string waitChan chan *session.Event @@ -47,7 +46,6 @@ func NewEventsStream(s *session.Session) *EventsStream { quit: make(chan bool), waitChan: make(chan *session.Event), waitFor: "", - ignoreList: NewIgnoreList(), triggerList: NewTriggerList(), } @@ -127,7 +125,7 @@ func NewEventsStream(s *session.Session) *EventsStream { ignore := session.NewModuleHandler("events.ignore FILTER", "events.ignore ([^\\s]+)", "Events with an identifier matching this filter will not be shown (use multiple times to add more filters).", func(args []string) error { - return mod.ignoreList.Add(args[0]) + return mod.Session.EventsIgnoreList.Add(args[0]) }) ignore.Complete("events.ignore", s.EventsCompleter) @@ -137,7 +135,7 @@ func NewEventsStream(s *session.Session) *EventsStream { include := session.NewModuleHandler("events.include FILTER", "events.include ([^\\s]+)", "Used to remove filters passed with the events.ignore command.", func(args []string) error { - return mod.ignoreList.Remove(args[0]) + return mod.Session.EventsIgnoreList.Remove(args[0]) }) include.Complete("events.include", s.EventsCompleter) @@ -147,13 +145,13 @@ func NewEventsStream(s *session.Session) *EventsStream { mod.AddHandler(session.NewModuleHandler("events.filters", "", "Print the list of filters used to ignore events.", func(args []string) error { - if mod.ignoreList.Empty() { + if mod.Session.EventsIgnoreList.Empty() { fmt.Printf("Ignore filters list is empty.\n") } else { - mod.ignoreList.RLock() - defer mod.ignoreList.RUnlock() + mod.Session.EventsIgnoreList.RLock() + defer mod.Session.EventsIgnoreList.RUnlock() - for _, filter := range mod.ignoreList.Filters() { + for _, filter := range mod.Session.EventsIgnoreList.Filters() { fmt.Printf(" '%s'\n", string(filter)) } } @@ -163,7 +161,7 @@ func NewEventsStream(s *session.Session) *EventsStream { mod.AddHandler(session.NewModuleHandler("events.filters.clear", "", "Clear the list of filters passed with the events.ignore command.", func(args []string) error { - mod.ignoreList = NewIgnoreList() + mod.Session.EventsIgnoreList.Clear() return nil })) @@ -281,7 +279,7 @@ func (mod *EventsStream) Start() error { mod.waitChan <- &e } - if !mod.ignoreList.Ignored(e) { + if !mod.Session.EventsIgnoreList.Ignored(e) { mod.View(e, true) } @@ -303,7 +301,7 @@ func (mod *EventsStream) Show(limit int) error { selected := []session.Event{} for i := range events { e := events[num-1-i] - if !mod.ignoreList.Ignored(e) { + if !mod.Session.EventsIgnoreList.Ignored(e) { selected = append(selected, e) if len(selected) == limit { break diff --git a/modules/events_stream/ignore_list.go b/session/events_ignore_list.go similarity index 63% rename from modules/events_stream/ignore_list.go rename to session/events_ignore_list.go index 036142e2..05f23212 100644 --- a/modules/events_stream/ignore_list.go +++ b/session/events_ignore_list.go @@ -1,4 +1,4 @@ -package events_stream +package session import ( "errors" @@ -6,8 +6,6 @@ import ( "strings" "sync" - "github.com/bettercap/bettercap/session" - "github.com/evilsocket/islazy/str" ) @@ -15,24 +13,24 @@ var ( ErrEmptyExpression = errors.New("expression can not be empty") ) -type IgnoreFilter string +type filter string -func (f IgnoreFilter) Matches(s string) bool { +func (f filter) Matches(s string) bool { return string(f) == s || strings.HasPrefix(s, string(f)) } -type IgnoreList struct { +type EventsIgnoreList struct { sync.RWMutex - filters []IgnoreFilter + filters []filter } -func NewIgnoreList() *IgnoreList { - return &IgnoreList{ - filters: make([]IgnoreFilter, 0), +func NewEventsIgnoreList() *EventsIgnoreList { + return &EventsIgnoreList{ + filters: make([]filter, 0), } } -func (l *IgnoreList) checkExpression(expr string) (string, error) { +func (l *EventsIgnoreList) checkExpression(expr string) (string, error) { expr = str.Trim(expr) if expr == "" { return "", ErrEmptyExpression @@ -41,7 +39,7 @@ func (l *IgnoreList) checkExpression(expr string) (string, error) { return expr, nil } -func (l *IgnoreList) Add(expr string) (err error) { +func (l *EventsIgnoreList) Add(expr string) (err error) { if expr, err = l.checkExpression(expr); err != nil { return err } @@ -57,12 +55,12 @@ func (l *IgnoreList) Add(expr string) (err error) { } // all good - l.filters = append(l.filters, IgnoreFilter(expr)) + l.filters = append(l.filters, filter(expr)) return nil } -func (l *IgnoreList) Remove(expr string) (err error) { +func (l *EventsIgnoreList) Remove(expr string) (err error) { if expr, err = l.checkExpression(expr); err != nil { return err } @@ -71,8 +69,8 @@ func (l *IgnoreList) Remove(expr string) (err error) { defer l.Unlock() // build a new list with everything that does not match - toRemove := IgnoreFilter(expr) - newList := make([]IgnoreFilter, 0) + toRemove := filter(expr) + newList := make([]filter, 0) for _, filter := range l.filters { if !toRemove.Matches(string(filter)) { newList = append(newList, filter) @@ -89,7 +87,13 @@ func (l *IgnoreList) Remove(expr string) (err error) { return nil } -func (l *IgnoreList) Ignored(e session.Event) bool { +func (l *EventsIgnoreList) Clear() { + l.RLock() + defer l.RUnlock() + l.filters = make([]filter, 0) +} + +func (l *EventsIgnoreList) Ignored(e Event) bool { l.RLock() defer l.RUnlock() @@ -102,12 +106,12 @@ func (l *IgnoreList) Ignored(e session.Event) bool { return false } -func (l *IgnoreList) Empty() bool { +func (l *EventsIgnoreList) Empty() bool { l.RLock() defer l.RUnlock() return len(l.filters) == 0 } -func (l *IgnoreList) Filters() []IgnoreFilter { +func (l *EventsIgnoreList) Filters() []filter { return l.filters } diff --git a/session/session.go b/session/session.go index 647eb684..2dd8a04f 100644 --- a/session/session.go +++ b/session/session.go @@ -69,12 +69,13 @@ type Session struct { GPS GPS Modules ModuleList - Input *readline.Instance - Prompt Prompt - CoreHandlers []CommandHandler - Events *EventPool - UnkCmdCallback UnknownCommandCallback - Firewall firewall.FirewallManager + Input *readline.Instance + Prompt Prompt + CoreHandlers []CommandHandler + Events *EventPool + EventsIgnoreList *EventsIgnoreList + UnkCmdCallback UnknownCommandCallback + Firewall firewall.FirewallManager } func New() (*Session, error) { @@ -95,10 +96,11 @@ func New() (*Session, error) { Active: false, Queue: nil, - CoreHandlers: make([]CommandHandler, 0), - Modules: make([]Module, 0), - Events: nil, - UnkCmdCallback: nil, + CoreHandlers: make([]CommandHandler, 0), + Modules: make([]Module, 0), + Events: nil, + EventsIgnoreList: NewEventsIgnoreList(), + UnkCmdCallback: nil, } if *s.Options.CpuProfile != "" {