mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 13:33:21 -07:00
new: implemented events.stream.output (closes #169). new: implemented -no-colors argument.
This commit is contained in:
parent
b63c20b757
commit
e40219976c
9 changed files with 97 additions and 56 deletions
|
@ -2,6 +2,7 @@ package modules
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -12,6 +13,7 @@ import (
|
|||
|
||||
type EventsStream struct {
|
||||
session.SessionModule
|
||||
output *os.File
|
||||
ignoreList *IgnoreList
|
||||
waitFor string
|
||||
waitChan chan *session.Event
|
||||
|
@ -22,6 +24,7 @@ type EventsStream struct {
|
|||
func NewEventsStream(s *session.Session) *EventsStream {
|
||||
stream := &EventsStream{
|
||||
SessionModule: session.NewSessionModule("events.stream", s),
|
||||
output: os.Stdout,
|
||||
quit: make(chan bool),
|
||||
waitChan: make(chan *session.Event),
|
||||
waitFor: "",
|
||||
|
@ -104,6 +107,11 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
|||
return nil
|
||||
}))
|
||||
|
||||
stream.AddParam(session.NewStringParameter("events.stream.output",
|
||||
"",
|
||||
"",
|
||||
"If not empty, events will be written to this file instead of the standard output."))
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
|
@ -119,11 +127,25 @@ func (s EventsStream) Author() string {
|
|||
return "Simone Margaritelli <evilsocket@protonmail.com>"
|
||||
}
|
||||
|
||||
func (s *EventsStream) Configure() error {
|
||||
return nil
|
||||
func (s *EventsStream) Configure() (err error) {
|
||||
var output string
|
||||
|
||||
if err, output = s.StringParam("events.stream.output"); err == nil {
|
||||
if output == "" {
|
||||
s.output = os.Stdout
|
||||
} else if output, err = core.ExpandPath(output); err == nil {
|
||||
s.output, err = os.OpenFile(output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *EventsStream) Start() error {
|
||||
if err := s.Configure(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.SetRunning(true, func() {
|
||||
s.eventListener = s.Session.Events.Listen()
|
||||
for {
|
||||
|
@ -196,5 +218,8 @@ func (s *EventsStream) startWaitingFor(tag string, timeout int) error {
|
|||
func (s *EventsStream) Stop() error {
|
||||
return s.SetRunning(false, func() {
|
||||
s.quit <- true
|
||||
if s.output != os.Stdout {
|
||||
s.output.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/bettercap/bettercap/core"
|
||||
|
@ -13,15 +14,15 @@ import (
|
|||
|
||||
const eventTimeFormat = "15:04:05"
|
||||
|
||||
func (s EventsStream) viewLogEvent(e session.Event) {
|
||||
fmt.Printf("[%s] [%s] [%s] %s\n",
|
||||
func (s *EventsStream) viewLogEvent(e session.Event) {
|
||||
fmt.Fprintf(s.output, "[%s] [%s] [%s] %s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
e.Label(),
|
||||
e.Data.(session.LogMessage).Message)
|
||||
}
|
||||
|
||||
func (s EventsStream) viewWiFiEvent(e session.Event) {
|
||||
func (s *EventsStream) viewWiFiEvent(e session.Event) {
|
||||
|
||||
if strings.HasPrefix(e.Tag, "wifi.ap.") {
|
||||
ap := e.Data.(*network.AccessPoint)
|
||||
|
@ -35,7 +36,7 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
|
|||
}
|
||||
|
||||
if e.Tag == "wifi.ap.new" {
|
||||
fmt.Printf("[%s] [%s] WiFi access point %s%s detected as %s%s.\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] WiFi access point %s%s detected as %s%s.\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
core.Bold(ap.ESSID()),
|
||||
|
@ -43,13 +44,13 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
|
|||
core.Green(ap.BSSID()),
|
||||
core.Dim(vend))
|
||||
} else if e.Tag == "wifi.ap.lost" {
|
||||
fmt.Printf("[%s] [%s] WiFi access point %s (%s) lost.\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] WiFi access point %s (%s) lost.\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
core.Red(ap.ESSID()),
|
||||
ap.BSSID())
|
||||
} else {
|
||||
fmt.Printf("[%s] [%s] %s\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] %s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
ap.String())
|
||||
|
@ -67,7 +68,7 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
|
|||
rssi = fmt.Sprintf(" (%d dBm)", probe.RSSI)
|
||||
}
|
||||
|
||||
fmt.Printf("[%s] [%s] Station %s%s is probing for SSID %s%s\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] Station %s%s is probing for SSID %s%s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
probe.FromAddr.String(),
|
||||
|
@ -77,7 +78,7 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s EventsStream) viewEndpointEvent(e session.Event) {
|
||||
func (s *EventsStream) viewEndpointEvent(e session.Event) {
|
||||
t := e.Data.(*network.Endpoint)
|
||||
vend := ""
|
||||
name := ""
|
||||
|
@ -93,7 +94,7 @@ func (s EventsStream) viewEndpointEvent(e session.Event) {
|
|||
}
|
||||
|
||||
if e.Tag == "endpoint.new" {
|
||||
fmt.Printf("[%s] [%s] Endpoint %s%s detected as %s%s.\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] Endpoint %s%s detected as %s%s.\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
core.Bold(t.IpAddress),
|
||||
|
@ -101,27 +102,27 @@ func (s EventsStream) viewEndpointEvent(e session.Event) {
|
|||
core.Green(t.HwAddress),
|
||||
core.Dim(vend))
|
||||
} else if e.Tag == "endpoint.lost" {
|
||||
fmt.Printf("[%s] [%s] Endpoint %s%s lost.\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] Endpoint %s%s lost.\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
core.Red(t.IpAddress),
|
||||
core.Dim(vend))
|
||||
} else {
|
||||
fmt.Printf("[%s] [%s] %s\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] %s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
t.String())
|
||||
}
|
||||
}
|
||||
|
||||
func (s EventsStream) viewModuleEvent(e session.Event) {
|
||||
fmt.Printf("[%s] [%s] %s\n",
|
||||
func (s *EventsStream) viewModuleEvent(e session.Event) {
|
||||
fmt.Fprintf(s.output, "[%s] [%s] %s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
e.Data)
|
||||
}
|
||||
|
||||
func (s EventsStream) viewSnifferEvent(e session.Event) {
|
||||
func (s *EventsStream) viewSnifferEvent(e session.Event) {
|
||||
se := e.Data.(SnifferEvent)
|
||||
misc := ""
|
||||
|
||||
|
@ -150,16 +151,16 @@ func (s EventsStream) viewSnifferEvent(e session.Event) {
|
|||
misc = fmt.Sprintf("%s", se.Data)
|
||||
}
|
||||
|
||||
fmt.Printf("[%s] [%s] %s %s\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] %s %s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
se.Message,
|
||||
misc)
|
||||
}
|
||||
|
||||
func (s EventsStream) viewSynScanEvent(e session.Event) {
|
||||
func (s *EventsStream) viewSynScanEvent(e session.Event) {
|
||||
se := e.Data.(SynScanEvent)
|
||||
fmt.Printf("[%s] [%s] Found open port %d for %s\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] Found open port %d for %s\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
se.Port,
|
||||
|
@ -182,10 +183,10 @@ func (s *EventsStream) View(e session.Event, refresh bool) {
|
|||
} else if strings.HasPrefix(e.Tag, "syn.scan.") {
|
||||
s.viewSynScanEvent(e)
|
||||
} else {
|
||||
fmt.Printf("[%s] [%s] %v\n", e.Time.Format(eventTimeFormat), core.Green(e.Tag), e)
|
||||
fmt.Fprintf(s.output, "[%s] [%s] %v\n", e.Time.Format(eventTimeFormat), core.Green(e.Tag), e)
|
||||
}
|
||||
|
||||
if refresh {
|
||||
if refresh && s.output == os.Stdout {
|
||||
s.Session.Refresh()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"github.com/bettercap/bettercap/session"
|
||||
)
|
||||
|
||||
func (s EventsStream) viewBLEEvent(e session.Event) {
|
||||
func (s *EventsStream) viewBLEEvent(e session.Event) {
|
||||
if e.Tag == "ble.device.new" {
|
||||
dev := e.Data.(*network.BLEDevice)
|
||||
name := dev.Device.Name()
|
||||
|
@ -23,7 +23,7 @@ func (s EventsStream) viewBLEEvent(e session.Event) {
|
|||
vend = fmt.Sprintf(" (%s)", core.Yellow(vend))
|
||||
}
|
||||
|
||||
fmt.Printf("[%s] [%s] New BLE device%s detected as %s%s %s.\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] New BLE device%s detected as %s%s %s.\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
name,
|
||||
|
@ -41,14 +41,14 @@ func (s EventsStream) viewBLEEvent(e session.Event) {
|
|||
vend = fmt.Sprintf(" (%s)", core.Yellow(vend))
|
||||
}
|
||||
|
||||
fmt.Printf("[%s] [%s] BLE device%s %s%s lost.\n",
|
||||
fmt.Fprintf(s.output, "[%s] [%s] BLE device%s %s%s lost.\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag),
|
||||
name,
|
||||
dev.Device.ID(),
|
||||
vend)
|
||||
} /* else {
|
||||
fmt.Printf("[%s] [%s]\n",
|
||||
fmt.Fprintf(s.output,"[%s] [%s]\n",
|
||||
e.Time.Format(eventTimeFormat),
|
||||
core.Green(e.Tag))
|
||||
} */
|
||||
|
|
|
@ -6,6 +6,6 @@ import (
|
|||
"github.com/bettercap/bettercap/session"
|
||||
)
|
||||
|
||||
func (s EventsStream) viewBLEEvent(e session.Event) {
|
||||
func (s *EventsStream) viewBLEEvent(e session.Event) {
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue