events_view.go 4.0 KB

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