main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strings"
  7. "runtime"
  8. "github.com/bettercap/bettercap/core"
  9. "github.com/bettercap/bettercap/log"
  10. "github.com/bettercap/bettercap/modules"
  11. "github.com/bettercap/bettercap/session"
  12. "github.com/evilsocket/islazy/str"
  13. "github.com/evilsocket/islazy/tui"
  14. )
  15. func main() {
  16. sess, err := session.New()
  17. if err != nil {
  18. fmt.Println(err)
  19. os.Exit(1)
  20. }
  21. defer sess.Close()
  22. if !tui.Effects() {
  23. if *sess.Options.NoColors {
  24. fmt.Printf("\n\nWARNING: Terminal colors have been disabled, view will be very limited.\n\n")
  25. } else {
  26. fmt.Printf("\n\nWARNING: This terminal does not support colors, view will be very limited.\n\n")
  27. }
  28. }
  29. if *sess.Options.PrintVersion {
  30. fmt.Printf("%s v%s (built for %s %s with %s)\n", core.Name, core.Version, runtime.GOOS, runtime.GOARCH, runtime.Version())
  31. return
  32. }
  33. appName := fmt.Sprintf("%s v%s", core.Name, core.Version)
  34. appBuild := fmt.Sprintf("(built for %s %s with %s)", runtime.GOOS, runtime.GOARCH, runtime.Version())
  35. fmt.Printf("%s %s [type '%s' for a list of commands]\n\n", tui.Bold(appName), tui.Dim(appBuild), tui.Bold("help"))
  36. // Load all modules
  37. modules.LoadModules(sess)
  38. if err = sess.Start(); err != nil {
  39. log.Fatal("%s", err)
  40. }
  41. // Some modules are enabled by default in order
  42. // to make the interactive session useful.
  43. for _, modName := range str.Comma(*sess.Options.AutoStart) {
  44. if err = sess.Run(modName + " on"); err != nil {
  45. log.Fatal("error while starting module %s: %s", modName, err)
  46. }
  47. }
  48. // Commands sent with -eval are used to set specific
  49. // caplet parameters (i.e. arp.spoof.targets) via command
  50. // line, therefore they need to be executed first otherwise
  51. // modules might already be started.
  52. for _, cmd := range session.ParseCommands(*sess.Options.Commands) {
  53. if err = sess.Run(cmd); err != nil {
  54. log.Error("error while running '%s': %s", tui.Bold(cmd), tui.Red(err.Error()))
  55. }
  56. }
  57. // Then run the caplet if specified.
  58. if *sess.Options.Caplet != "" {
  59. if err = sess.RunCaplet(*sess.Options.Caplet); err != nil {
  60. log.Error("error while running caplet %s: %s", tui.Bold(*sess.Options.Caplet), tui.Red(err.Error()))
  61. }
  62. }
  63. // Eventually start the interactive session.
  64. for sess.Active {
  65. line, err := sess.ReadLine()
  66. if err != nil {
  67. if err == io.EOF || err.Error() == "Interrupt" {
  68. if exitPrompt() {
  69. sess.Run("exit")
  70. os.Exit(0)
  71. }
  72. continue
  73. } else {
  74. log.Fatal("%s", err)
  75. }
  76. }
  77. for _, cmd := range session.ParseCommands(line) {
  78. if err = sess.Run(cmd); err != nil {
  79. log.Error("%s", err)
  80. }
  81. }
  82. }
  83. }
  84. func exitPrompt() bool {
  85. var ans string
  86. fmt.Printf("Are you sure you want to quit this session? y/n ")
  87. fmt.Scan(&ans)
  88. return strings.ToLower(ans) == "y"
  89. }