new: implemented events.stream.output (closes #169). new: implemented -no-colors argument.

This commit is contained in:
evilsocket 2018-03-13 13:20:21 +01:00
parent b63c20b757
commit e40219976c
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
9 changed files with 97 additions and 56 deletions

View file

@ -7,6 +7,7 @@ type Options struct {
Caplet *string Caplet *string
Debug *bool Debug *bool
Silent *bool Silent *bool
NoColors *bool
NoHistory *bool NoHistory *bool
EnvFile *string EnvFile *string
Commands *string Commands *string
@ -20,6 +21,7 @@ func ParseOptions() (Options, error) {
Caplet: flag.String("caplet", "", "Read commands from this file and execute them in the interactive session."), Caplet: flag.String("caplet", "", "Read commands from this file and execute them in the interactive session."),
Debug: flag.Bool("debug", false, "Print debug messages."), Debug: flag.Bool("debug", false, "Print debug messages."),
Silent: flag.Bool("silent", false, "Suppress all logs which are not errors."), Silent: flag.Bool("silent", false, "Suppress all logs which are not errors."),
NoColors: flag.Bool("no-colors", false, "Disable output color effect,s."),
NoHistory: flag.Bool("no-history", false, "Disable interactive session history file."), NoHistory: flag.Bool("no-history", false, "Disable interactive session history file."),
EnvFile: flag.String("env-file", "", "Load environment variables from this file if found, set to empty to disable environment persistance."), EnvFile: flag.String("env-file", "", "Load environment variables from this file if found, set to empty to disable environment persistance."),
Commands: flag.String("eval", "", "Run one or more commands separated by ; in the interactive session, used to set variables via command line."), Commands: flag.String("eval", "", "Run one or more commands separated by ; in the interactive session, used to set variables via command line."),

View file

@ -26,14 +26,17 @@ var (
RESET = "\033[0m" RESET = "\033[0m"
NoColors = false HasColors = true
) )
func init() { func isDumbTerminal() bool {
NoColors = os.Getenv("TERM") == "dumb" || return os.Getenv("TERM") == "dumb" ||
os.Getenv("TERM") == "" || os.Getenv("TERM") == "" ||
(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))
if NoColors { }
func InitSwag(disableColors bool) {
if disableColors || isDumbTerminal() {
BOLD = "" BOLD = ""
DIM = "" DIM = ""
RED = "" RED = ""
@ -48,6 +51,7 @@ func init() {
BG_YELLOW = "" BG_YELLOW = ""
BG_LBLUE = "" BG_LBLUE = ""
RESET = "" RESET = ""
HasColors = false
} }
} }

19
main.go
View file

@ -27,9 +27,13 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if core.NoColors == true { if core.HasColors == false {
if *sess.Options.NoColors == true {
fmt.Printf("\n\nWARNING: Terminal colors have been disabled, view will be very limited.\n\n")
} else {
fmt.Printf("\n\nWARNING: This terminal does not support colors, view will be very limited.\n\n") fmt.Printf("\n\nWARNING: This terminal does not support colors, view will be very limited.\n\n")
} }
}
appName := fmt.Sprintf("%s v%s", core.Name, core.Version) appName := fmt.Sprintf("%s v%s", core.Name, core.Version)
@ -59,12 +63,6 @@ func main() {
log.Fatal("%s", err) log.Fatal("%s", err)
} }
for _, modName := range autoEnableList {
if err = sess.Run(modName + " on"); err != nil {
log.Fatal("Error while starting module %s: %", modName, err)
}
}
/* /*
* Commands sent with -eval are used to set specific * Commands sent with -eval are used to set specific
* caplet parameters (i.e. arp.spoof.targets) via command * caplet parameters (i.e. arp.spoof.targets) via command
@ -77,6 +75,13 @@ func main() {
} }
} }
// Start modules that are enabled by default.
for _, modName := range autoEnableList {
if err = sess.Run(modName + " on"); err != nil {
log.Fatal("Error while starting module %s: %", modName, err)
}
}
// Then run the caplet if specified. // Then run the caplet if specified.
if *sess.Options.Caplet != "" { if *sess.Options.Caplet != "" {
if err = sess.RunCaplet(*sess.Options.Caplet); err != nil { if err = sess.RunCaplet(*sess.Options.Caplet); err != nil {

View file

@ -2,6 +2,7 @@ package modules
import ( import (
"fmt" "fmt"
"os"
"strconv" "strconv"
"time" "time"
@ -12,6 +13,7 @@ import (
type EventsStream struct { type EventsStream struct {
session.SessionModule session.SessionModule
output *os.File
ignoreList *IgnoreList ignoreList *IgnoreList
waitFor string waitFor string
waitChan chan *session.Event waitChan chan *session.Event
@ -22,6 +24,7 @@ type EventsStream struct {
func NewEventsStream(s *session.Session) *EventsStream { func NewEventsStream(s *session.Session) *EventsStream {
stream := &EventsStream{ stream := &EventsStream{
SessionModule: session.NewSessionModule("events.stream", s), SessionModule: session.NewSessionModule("events.stream", s),
output: os.Stdout,
quit: make(chan bool), quit: make(chan bool),
waitChan: make(chan *session.Event), waitChan: make(chan *session.Event),
waitFor: "", waitFor: "",
@ -104,6 +107,11 @@ func NewEventsStream(s *session.Session) *EventsStream {
return nil 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 return stream
} }
@ -119,11 +127,25 @@ func (s EventsStream) Author() string {
return "Simone Margaritelli <evilsocket@protonmail.com>" return "Simone Margaritelli <evilsocket@protonmail.com>"
} }
func (s *EventsStream) Configure() error { func (s *EventsStream) Configure() (err error) {
return nil 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 { func (s *EventsStream) Start() error {
if err := s.Configure(); err != nil {
return err
}
return s.SetRunning(true, func() { return s.SetRunning(true, func() {
s.eventListener = s.Session.Events.Listen() s.eventListener = s.Session.Events.Listen()
for { for {
@ -196,5 +218,8 @@ func (s *EventsStream) startWaitingFor(tag string, timeout int) error {
func (s *EventsStream) Stop() error { func (s *EventsStream) Stop() error {
return s.SetRunning(false, func() { return s.SetRunning(false, func() {
s.quit <- true s.quit <- true
if s.output != os.Stdout {
s.output.Close()
}
}) })
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings" "strings"
"github.com/bettercap/bettercap/core" "github.com/bettercap/bettercap/core"
@ -13,15 +14,15 @@ import (
const eventTimeFormat = "15:04:05" const eventTimeFormat = "15:04:05"
func (s EventsStream) viewLogEvent(e session.Event) { func (s *EventsStream) viewLogEvent(e session.Event) {
fmt.Printf("[%s] [%s] [%s] %s\n", fmt.Fprintf(s.output, "[%s] [%s] [%s] %s\n",
e.Time.Format(eventTimeFormat), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
e.Label(), e.Label(),
e.Data.(session.LogMessage).Message) 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.") { if strings.HasPrefix(e.Tag, "wifi.ap.") {
ap := e.Data.(*network.AccessPoint) ap := e.Data.(*network.AccessPoint)
@ -35,7 +36,7 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
} }
if e.Tag == "wifi.ap.new" { 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
core.Bold(ap.ESSID()), core.Bold(ap.ESSID()),
@ -43,13 +44,13 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
core.Green(ap.BSSID()), core.Green(ap.BSSID()),
core.Dim(vend)) core.Dim(vend))
} else if e.Tag == "wifi.ap.lost" { } 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
core.Red(ap.ESSID()), core.Red(ap.ESSID()),
ap.BSSID()) ap.BSSID())
} else { } else {
fmt.Printf("[%s] [%s] %s\n", fmt.Fprintf(s.output, "[%s] [%s] %s\n",
e.Time.Format(eventTimeFormat), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
ap.String()) ap.String())
@ -67,7 +68,7 @@ func (s EventsStream) viewWiFiEvent(e session.Event) {
rssi = fmt.Sprintf(" (%d dBm)", probe.RSSI) 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
probe.FromAddr.String(), 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) t := e.Data.(*network.Endpoint)
vend := "" vend := ""
name := "" name := ""
@ -93,7 +94,7 @@ func (s EventsStream) viewEndpointEvent(e session.Event) {
} }
if e.Tag == "endpoint.new" { 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
core.Bold(t.IpAddress), core.Bold(t.IpAddress),
@ -101,27 +102,27 @@ func (s EventsStream) viewEndpointEvent(e session.Event) {
core.Green(t.HwAddress), core.Green(t.HwAddress),
core.Dim(vend)) core.Dim(vend))
} else if e.Tag == "endpoint.lost" { } 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
core.Red(t.IpAddress), core.Red(t.IpAddress),
core.Dim(vend)) core.Dim(vend))
} else { } else {
fmt.Printf("[%s] [%s] %s\n", fmt.Fprintf(s.output, "[%s] [%s] %s\n",
e.Time.Format(eventTimeFormat), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
t.String()) t.String())
} }
} }
func (s EventsStream) viewModuleEvent(e session.Event) { func (s *EventsStream) viewModuleEvent(e session.Event) {
fmt.Printf("[%s] [%s] %s\n", fmt.Fprintf(s.output, "[%s] [%s] %s\n",
e.Time.Format(eventTimeFormat), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
e.Data) e.Data)
} }
func (s EventsStream) viewSnifferEvent(e session.Event) { func (s *EventsStream) viewSnifferEvent(e session.Event) {
se := e.Data.(SnifferEvent) se := e.Data.(SnifferEvent)
misc := "" misc := ""
@ -150,16 +151,16 @@ func (s EventsStream) viewSnifferEvent(e session.Event) {
misc = fmt.Sprintf("%s", se.Data) 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
se.Message, se.Message,
misc) misc)
} }
func (s EventsStream) viewSynScanEvent(e session.Event) { func (s *EventsStream) viewSynScanEvent(e session.Event) {
se := e.Data.(SynScanEvent) 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
se.Port, se.Port,
@ -182,10 +183,10 @@ func (s *EventsStream) View(e session.Event, refresh bool) {
} else if strings.HasPrefix(e.Tag, "syn.scan.") { } else if strings.HasPrefix(e.Tag, "syn.scan.") {
s.viewSynScanEvent(e) s.viewSynScanEvent(e)
} else { } 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() s.Session.Refresh()
} }
} }

View file

@ -11,7 +11,7 @@ import (
"github.com/bettercap/bettercap/session" "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" { if e.Tag == "ble.device.new" {
dev := e.Data.(*network.BLEDevice) dev := e.Data.(*network.BLEDevice)
name := dev.Device.Name() name := dev.Device.Name()
@ -23,7 +23,7 @@ func (s EventsStream) viewBLEEvent(e session.Event) {
vend = fmt.Sprintf(" (%s)", core.Yellow(vend)) 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
name, name,
@ -41,14 +41,14 @@ func (s EventsStream) viewBLEEvent(e session.Event) {
vend = fmt.Sprintf(" (%s)", core.Yellow(vend)) 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), e.Time.Format(eventTimeFormat),
core.Green(e.Tag), core.Green(e.Tag),
name, name,
dev.Device.ID(), dev.Device.ID(),
vend) vend)
} /* else { } /* else {
fmt.Printf("[%s] [%s]\n", fmt.Fprintf(s.output,"[%s] [%s]\n",
e.Time.Format(eventTimeFormat), e.Time.Format(eventTimeFormat),
core.Green(e.Tag)) core.Green(e.Tag))
} */ } */

View file

@ -6,6 +6,6 @@ import (
"github.com/bettercap/bettercap/session" "github.com/bettercap/bettercap/session"
) )
func (s EventsStream) viewBLEEvent(e session.Event) { func (s *EventsStream) viewBLEEvent(e session.Event) {
} }

View file

@ -15,23 +15,6 @@ const (
DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {reset}" DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {reset}"
) )
var PromptEffects = map[string]string{
"{bold}": core.BOLD,
"{dim}": core.DIM,
"{r}": core.RED,
"{g}": core.GREEN,
"{b}": core.BLUE,
"{y}": core.YELLOW,
"{fb}": core.FG_BLACK,
"{fw}": core.FG_WHITE,
"{bdg}": core.BG_DGRAY,
"{br}": core.BG_RED,
"{bg}": core.BG_GREEN,
"{by}": core.BG_YELLOW,
"{blb}": core.BG_LBLUE, // Ziggy this is for you <3
"{reset}": core.RESET,
}
var PromptCallbacks = map[string]func(s *Session) string{ var PromptCallbacks = map[string]func(s *Session) string{
"{cidr}": func(s *Session) string { "{cidr}": func(s *Session) string {
return s.Interface.CIDR() return s.Interface.CIDR()
@ -71,7 +54,26 @@ func (p Prompt) Render(s *Session) string {
prompt = DefaultPrompt prompt = DefaultPrompt
} }
for tok, effect := range PromptEffects { // these are here because if colors are disabled,
// we need the updated core.* variables
var effects = map[string]string{
"{bold}": core.BOLD,
"{dim}": core.DIM,
"{r}": core.RED,
"{g}": core.GREEN,
"{b}": core.BLUE,
"{y}": core.YELLOW,
"{fb}": core.FG_BLACK,
"{fw}": core.FG_WHITE,
"{bdg}": core.BG_DGRAY,
"{br}": core.BG_RED,
"{bg}": core.BG_GREEN,
"{by}": core.BG_YELLOW,
"{blb}": core.BG_LBLUE, // Ziggy this is for you <3
"{reset}": core.RESET,
}
for tok, effect := range effects {
prompt = strings.Replace(prompt, tok, effect, -1) prompt = strings.Replace(prompt, tok, effect, -1)
} }

View file

@ -138,6 +138,8 @@ func New() (*Session, error) {
return nil, err return nil, err
} }
core.InitSwag(*s.Options.NoColors)
if *s.Options.CpuProfile != "" { if *s.Options.CpuProfile != "" {
if f, err := os.Create(*s.Options.CpuProfile); err != nil { if f, err := os.Create(*s.Options.CpuProfile); err != nil {
return nil, err return nil, err