| 1 | package events_stream |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/bettercap/bettercap/network" |
| 10 | "github.com/bettercap/bettercap/session" |
| 11 | |
| 12 | "github.com/bettercap/bettercap/modules/net_sniff" |
| 13 | "github.com/bettercap/bettercap/modules/syn_scan" |
| 14 | |
| 15 | "github.com/google/go-github/github" |
| 16 | |
| 17 | "github.com/evilsocket/islazy/tui" |
| 18 | ) |
| 19 | |
| 20 | func (mod *EventsStream) viewLogEvent(output io.Writer, e session.Event) { |
| 21 | fmt.Fprintf(output, "[%s] [%s] [%s] %s\n", |
| 22 | e.Time.Format(mod.timeFormat), |
| 23 | tui.Green(e.Tag), |
| 24 | e.Label(), |
| 25 | e.Data.(session.LogMessage).Message) |
| 26 | } |
| 27 | |
| 28 | func (mod *EventsStream) viewEndpointEvent(output io.Writer, e session.Event) { |
| 29 | t := e.Data.(*network.Endpoint) |
| 30 | vend := "" |
| 31 | name := "" |
| 32 | |
| 33 | if t.Vendor != "" { |
| 34 | vend = fmt.Sprintf(" (%s)", t.Vendor) |
| 35 | } |
| 36 | |
| 37 | if t.Alias != "" { |
| 38 | name = fmt.Sprintf(" (%s)", t.Alias) |
| 39 | } else if t.Hostname != "" { |
| 40 | name = fmt.Sprintf(" (%s)", t.Hostname) |
| 41 | } |
| 42 | |
| 43 | if e.Tag == "endpoint.new" { |
| 44 | fmt.Fprintf(output, "[%s] [%s] endpoint %s%s detected as %s%s.\n", |
| 45 | e.Time.Format(mod.timeFormat), |
| 46 | tui.Green(e.Tag), |
| 47 | tui.Bold(t.IpAddress), |
| 48 | tui.Dim(name), |
| 49 | tui.Green(t.HwAddress), |
| 50 | tui.Dim(vend)) |
| 51 | } else if e.Tag == "endpoint.lost" { |
| 52 | fmt.Fprintf(output, "[%s] [%s] endpoint %s%s %s%s lost.\n", |
| 53 | e.Time.Format(mod.timeFormat), |
| 54 | tui.Green(e.Tag), |
| 55 | tui.Red(t.IpAddress), |
| 56 | tui.Dim(name), |
| 57 | tui.Green(t.HwAddress), |
| 58 | tui.Dim(vend)) |
| 59 | } else { |
| 60 | fmt.Fprintf(output, "[%s] [%s] %s\n", |
| 61 | e.Time.Format(mod.timeFormat), |
| 62 | tui.Green(e.Tag), |
| 63 | t.String()) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (mod *EventsStream) viewModuleEvent(output io.Writer, e session.Event) { |
| 68 | if *mod.Session.Options.Debug { |
| 69 | fmt.Fprintf(output, "[%s] [%s] %s\n", |
| 70 | e.Time.Format(mod.timeFormat), |
| 71 | tui.Green(e.Tag), |
| 72 | e.Data) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (mod *EventsStream) viewSnifferEvent(output io.Writer, e session.Event) { |
| 77 | if strings.HasPrefix(e.Tag, "net.sniff.http.") { |
| 78 | mod.viewHttpEvent(output, e) |
| 79 | } else { |
| 80 | fmt.Fprintf(output, "[%s] [%s] %s\n", |
| 81 | e.Time.Format(mod.timeFormat), |
| 82 | tui.Green(e.Tag), |
| 83 | e.Data.(net_sniff.SnifferEvent).Message) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func (mod *EventsStream) viewSynScanEvent(output io.Writer, e session.Event) { |
| 88 | se := e.Data.(syn_scan.SynScanEvent) |
| 89 | fmt.Fprintf(output, "[%s] [%s] found open port %d for %s\n", |
| 90 | e.Time.Format(mod.timeFormat), |
| 91 | tui.Green(e.Tag), |
| 92 | se.Port, |
| 93 | tui.Bold(se.Address)) |
| 94 | } |
| 95 | |
| 96 | func (mod *EventsStream) viewUpdateEvent(output io.Writer, e session.Event) { |
| 97 | update := e.Data.(*github.RepositoryRelease) |
| 98 | |
| 99 | fmt.Fprintf(output, "[%s] [%s] an update to version %s is available at %s\n", |
| 100 | e.Time.Format(mod.timeFormat), |
| 101 | tui.Bold(tui.Yellow(e.Tag)), |
| 102 | tui.Bold(*update.TagName), |
| 103 | *update.HTMLURL) |
| 104 | } |
| 105 | |
| 106 | func (mod *EventsStream) Render(output io.Writer, e session.Event) { |
| 107 | var err error |
| 108 | if err, mod.timeFormat = mod.StringParam("events.stream.time.format"); err != nil { |
| 109 | fmt.Fprintf(output, "%v", err) |
| 110 | mod.timeFormat = "15:04:05" |
| 111 | } |
| 112 | |
| 113 | if e.Tag == "sys.log" { |
| 114 | mod.viewLogEvent(output, e) |
| 115 | } else if strings.HasPrefix(e.Tag, "endpoint.") { |
| 116 | mod.viewEndpointEvent(output, e) |
| 117 | } else if strings.HasPrefix(e.Tag, "wifi.") { |
| 118 | mod.viewWiFiEvent(output, e) |
| 119 | } else if strings.HasPrefix(e.Tag, "ble.") { |
| 120 | mod.viewBLEEvent(output, e) |
| 121 | } else if strings.HasPrefix(e.Tag, "hid.") { |
| 122 | mod.viewHIDEvent(output, e) |
| 123 | } else if strings.HasPrefix(e.Tag, "gps.") { |
| 124 | mod.viewGPSEvent(output, e) |
| 125 | } else if strings.HasPrefix(e.Tag, "mod.") { |
| 126 | mod.viewModuleEvent(output, e) |
| 127 | } else if strings.HasPrefix(e.Tag, "net.sniff.") { |
| 128 | mod.viewSnifferEvent(output, e) |
| 129 | } else if e.Tag == "syn.scan" { |
| 130 | mod.viewSynScanEvent(output, e) |
| 131 | } else if e.Tag == "update.available" { |
| 132 | mod.viewUpdateEvent(output, e) |
| 133 | } else if e.Tag == "gateway.change" { |
| 134 | mod.viewGatewayEvent(output, e) |
| 135 | } else if e.Tag != "tick" && e.Tag != "session.started" && e.Tag != "session.stopped" { |
| 136 | fmt.Fprintf(output, "[%s] [%s] %v\n", e.Time.Format(mod.timeFormat), tui.Green(e.Tag), e) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func (mod *EventsStream) View(e session.Event, refresh bool) { |
| 141 | mod.Render(mod.output, e) |
| 142 | |
| 143 | if refresh && mod.output == os.Stdout { |
| 144 | mod.Session.Refresh() |
| 145 | } |
| 146 | |
| 147 | mod.doRotation() |
| 148 | } |