fix: events.include and events.ignore now filter for both events.stream and api.rest

This commit is contained in:
evilsocket 2019-03-21 11:20:48 +01:00
parent f23c780eee
commit 2e3e4f453b
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
5 changed files with 54 additions and 42 deletions

View file

@ -174,7 +174,10 @@ func (mod *RestAPI) Configure() error {
router.Methods("OPTIONS").HandlerFunc(mod.corsRoute) router.Methods("OPTIONS").HandlerFunc(mod.corsRoute)
router.HandleFunc("/api/file", mod.fileRoute)
router.HandleFunc("/api/events", mod.eventsRoute) router.HandleFunc("/api/events", mod.eventsRoute)
router.HandleFunc("/api/session", mod.sessionRoute) router.HandleFunc("/api/session", mod.sessionRoute)
router.HandleFunc("/api/session/ble", mod.sessionRoute) router.HandleFunc("/api/session/ble", mod.sessionRoute)
router.HandleFunc("/api/session/ble/{mac}", 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/started-at", mod.sessionRoute)
router.HandleFunc("/api/session/wifi", mod.sessionRoute) router.HandleFunc("/api/session/wifi", mod.sessionRoute)
router.HandleFunc("/api/session/wifi/{mac}", mod.sessionRoute) router.HandleFunc("/api/session/wifi/{mac}", mod.sessionRoute)
router.HandleFunc("/api/file", mod.fileRoute)
mod.server.Handler = router mod.server.Handler = router

View file

@ -176,7 +176,13 @@ func (mod *RestAPI) showEvents(w http.ResponseWriter, r *http.Request) {
if mod.useWebsocket { if mod.useWebsocket {
mod.startStreamingEvents(w, r) mod.startStreamingEvents(w, r)
} else { } 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) nevents := len(events)
nmax := nevents nmax := nevents
n := nmax n := nmax

View file

@ -29,7 +29,6 @@ type EventsStream struct {
outputName string outputName string
output *os.File output *os.File
rotation rotation rotation rotation
ignoreList *IgnoreList
triggerList *TriggerList triggerList *TriggerList
waitFor string waitFor string
waitChan chan *session.Event waitChan chan *session.Event
@ -47,7 +46,6 @@ func NewEventsStream(s *session.Session) *EventsStream {
quit: make(chan bool), quit: make(chan bool),
waitChan: make(chan *session.Event), waitChan: make(chan *session.Event),
waitFor: "", waitFor: "",
ignoreList: NewIgnoreList(),
triggerList: NewTriggerList(), triggerList: NewTriggerList(),
} }
@ -127,7 +125,7 @@ func NewEventsStream(s *session.Session) *EventsStream {
ignore := session.NewModuleHandler("events.ignore FILTER", "events.ignore ([^\\s]+)", 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).", "Events with an identifier matching this filter will not be shown (use multiple times to add more filters).",
func(args []string) error { func(args []string) error {
return mod.ignoreList.Add(args[0]) return mod.Session.EventsIgnoreList.Add(args[0])
}) })
ignore.Complete("events.ignore", s.EventsCompleter) 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]+)", include := session.NewModuleHandler("events.include FILTER", "events.include ([^\\s]+)",
"Used to remove filters passed with the events.ignore command.", "Used to remove filters passed with the events.ignore command.",
func(args []string) error { func(args []string) error {
return mod.ignoreList.Remove(args[0]) return mod.Session.EventsIgnoreList.Remove(args[0])
}) })
include.Complete("events.include", s.EventsCompleter) include.Complete("events.include", s.EventsCompleter)
@ -147,13 +145,13 @@ func NewEventsStream(s *session.Session) *EventsStream {
mod.AddHandler(session.NewModuleHandler("events.filters", "", mod.AddHandler(session.NewModuleHandler("events.filters", "",
"Print the list of filters used to ignore events.", "Print the list of filters used to ignore events.",
func(args []string) error { func(args []string) error {
if mod.ignoreList.Empty() { if mod.Session.EventsIgnoreList.Empty() {
fmt.Printf("Ignore filters list is empty.\n") fmt.Printf("Ignore filters list is empty.\n")
} else { } else {
mod.ignoreList.RLock() mod.Session.EventsIgnoreList.RLock()
defer mod.ignoreList.RUnlock() defer mod.Session.EventsIgnoreList.RUnlock()
for _, filter := range mod.ignoreList.Filters() { for _, filter := range mod.Session.EventsIgnoreList.Filters() {
fmt.Printf(" '%s'\n", string(filter)) fmt.Printf(" '%s'\n", string(filter))
} }
} }
@ -163,7 +161,7 @@ func NewEventsStream(s *session.Session) *EventsStream {
mod.AddHandler(session.NewModuleHandler("events.filters.clear", "", mod.AddHandler(session.NewModuleHandler("events.filters.clear", "",
"Clear the list of filters passed with the events.ignore command.", "Clear the list of filters passed with the events.ignore command.",
func(args []string) error { func(args []string) error {
mod.ignoreList = NewIgnoreList() mod.Session.EventsIgnoreList.Clear()
return nil return nil
})) }))
@ -281,7 +279,7 @@ func (mod *EventsStream) Start() error {
mod.waitChan <- &e mod.waitChan <- &e
} }
if !mod.ignoreList.Ignored(e) { if !mod.Session.EventsIgnoreList.Ignored(e) {
mod.View(e, true) mod.View(e, true)
} }
@ -303,7 +301,7 @@ func (mod *EventsStream) Show(limit int) error {
selected := []session.Event{} selected := []session.Event{}
for i := range events { for i := range events {
e := events[num-1-i] e := events[num-1-i]
if !mod.ignoreList.Ignored(e) { if !mod.Session.EventsIgnoreList.Ignored(e) {
selected = append(selected, e) selected = append(selected, e)
if len(selected) == limit { if len(selected) == limit {
break break

View file

@ -1,4 +1,4 @@
package events_stream package session
import ( import (
"errors" "errors"
@ -6,8 +6,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/bettercap/bettercap/session"
"github.com/evilsocket/islazy/str" "github.com/evilsocket/islazy/str"
) )
@ -15,24 +13,24 @@ var (
ErrEmptyExpression = errors.New("expression can not be empty") 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)) return string(f) == s || strings.HasPrefix(s, string(f))
} }
type IgnoreList struct { type EventsIgnoreList struct {
sync.RWMutex sync.RWMutex
filters []IgnoreFilter filters []filter
} }
func NewIgnoreList() *IgnoreList { func NewEventsIgnoreList() *EventsIgnoreList {
return &IgnoreList{ return &EventsIgnoreList{
filters: make([]IgnoreFilter, 0), filters: make([]filter, 0),
} }
} }
func (l *IgnoreList) checkExpression(expr string) (string, error) { func (l *EventsIgnoreList) checkExpression(expr string) (string, error) {
expr = str.Trim(expr) expr = str.Trim(expr)
if expr == "" { if expr == "" {
return "", ErrEmptyExpression return "", ErrEmptyExpression
@ -41,7 +39,7 @@ func (l *IgnoreList) checkExpression(expr string) (string, error) {
return expr, nil 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 { if expr, err = l.checkExpression(expr); err != nil {
return err return err
} }
@ -57,12 +55,12 @@ func (l *IgnoreList) Add(expr string) (err error) {
} }
// all good // all good
l.filters = append(l.filters, IgnoreFilter(expr)) l.filters = append(l.filters, filter(expr))
return nil 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 { if expr, err = l.checkExpression(expr); err != nil {
return err return err
} }
@ -71,8 +69,8 @@ func (l *IgnoreList) Remove(expr string) (err error) {
defer l.Unlock() defer l.Unlock()
// build a new list with everything that does not match // build a new list with everything that does not match
toRemove := IgnoreFilter(expr) toRemove := filter(expr)
newList := make([]IgnoreFilter, 0) newList := make([]filter, 0)
for _, filter := range l.filters { for _, filter := range l.filters {
if !toRemove.Matches(string(filter)) { if !toRemove.Matches(string(filter)) {
newList = append(newList, filter) newList = append(newList, filter)
@ -89,7 +87,13 @@ func (l *IgnoreList) Remove(expr string) (err error) {
return nil 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() l.RLock()
defer l.RUnlock() defer l.RUnlock()
@ -102,12 +106,12 @@ func (l *IgnoreList) Ignored(e session.Event) bool {
return false return false
} }
func (l *IgnoreList) Empty() bool { func (l *EventsIgnoreList) Empty() bool {
l.RLock() l.RLock()
defer l.RUnlock() defer l.RUnlock()
return len(l.filters) == 0 return len(l.filters) == 0
} }
func (l *IgnoreList) Filters() []IgnoreFilter { func (l *EventsIgnoreList) Filters() []filter {
return l.filters return l.filters
} }

View file

@ -73,6 +73,7 @@ type Session struct {
Prompt Prompt Prompt Prompt
CoreHandlers []CommandHandler CoreHandlers []CommandHandler
Events *EventPool Events *EventPool
EventsIgnoreList *EventsIgnoreList
UnkCmdCallback UnknownCommandCallback UnkCmdCallback UnknownCommandCallback
Firewall firewall.FirewallManager Firewall firewall.FirewallManager
} }
@ -98,6 +99,7 @@ func New() (*Session, error) {
CoreHandlers: make([]CommandHandler, 0), CoreHandlers: make([]CommandHandler, 0),
Modules: make([]Module, 0), Modules: make([]Module, 0),
Events: nil, Events: nil,
EventsIgnoreList: NewEventsIgnoreList(),
UnkCmdCallback: nil, UnkCmdCallback: nil,
} }