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.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

View file

@ -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

View file

@ -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

View file

@ -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
}

View file

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