caplets.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package caplets
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "github.com/bettercap/bettercap/caplets"
  8. "github.com/bettercap/bettercap/session"
  9. "github.com/dustin/go-humanize"
  10. "github.com/evilsocket/islazy/fs"
  11. "github.com/evilsocket/islazy/tui"
  12. "github.com/evilsocket/islazy/zip"
  13. )
  14. type CapletsModule struct {
  15. session.SessionModule
  16. }
  17. func NewCapletsModule(s *session.Session) *CapletsModule {
  18. mod := &CapletsModule{
  19. SessionModule: session.NewSessionModule("caplets", s),
  20. }
  21. mod.AddHandler(session.NewModuleHandler("caplets.show", "",
  22. "Show a list of installed caplets.",
  23. func(args []string) error {
  24. return mod.Show()
  25. }))
  26. mod.AddHandler(session.NewModuleHandler("caplets.paths", "",
  27. "Show a list caplet search paths.",
  28. func(args []string) error {
  29. return mod.Paths()
  30. }))
  31. mod.AddHandler(session.NewModuleHandler("caplets.update", "",
  32. "Install/updates the caplets.",
  33. func(args []string) error {
  34. return mod.Update()
  35. }))
  36. return mod
  37. }
  38. func (mod *CapletsModule) Name() string {
  39. return "caplets"
  40. }
  41. func (mod *CapletsModule) Description() string {
  42. return "A module to list and update caplets."
  43. }
  44. func (mod *CapletsModule) Author() string {
  45. return "Simone Margaritelli <evilsocket@gmail.com>"
  46. }
  47. func (mod *CapletsModule) Configure() error {
  48. return nil
  49. }
  50. func (mod *CapletsModule) Stop() error {
  51. return nil
  52. }
  53. func (mod *CapletsModule) Start() error {
  54. return nil
  55. }
  56. func (mod *CapletsModule) Show() error {
  57. caplets := caplets.List()
  58. if len(caplets) == 0 {
  59. return fmt.Errorf("no installed caplets on this system, use the caplets.update command to download them")
  60. }
  61. colNames := []string{
  62. "Name",
  63. "Path",
  64. "Size",
  65. }
  66. rows := [][]string{}
  67. for _, caplet := range caplets {
  68. rows = append(rows, []string{
  69. tui.Bold(caplet.Name),
  70. caplet.Path,
  71. tui.Dim(humanize.Bytes(uint64(caplet.Size))),
  72. })
  73. }
  74. tui.Table(mod.Session.Events.Stdout, colNames, rows)
  75. return nil
  76. }
  77. func (mod *CapletsModule) Paths() error {
  78. colNames := []string{
  79. "Path",
  80. }
  81. rows := [][]string{}
  82. for _, path := range caplets.LoadPaths {
  83. rows = append(rows, []string{path})
  84. }
  85. tui.Table(mod.Session.Events.Stdout, colNames, rows)
  86. mod.Printf("(paths can be customized by defining the %s environment variable)\n", tui.Bold(caplets.EnvVarName))
  87. return nil
  88. }
  89. func (mod *CapletsModule) Update() error {
  90. if !fs.Exists(caplets.InstallBase) {
  91. mod.Info("creating caplets install path %s ...", caplets.InstallBase)
  92. if err := os.MkdirAll(caplets.InstallBase, os.ModePerm); err != nil {
  93. return err
  94. }
  95. }
  96. out, err := os.Create(caplets.ArchivePath)
  97. if err != nil {
  98. return err
  99. }
  100. defer out.Close()
  101. mod.Info("downloading caplets from %s ...", caplets.InstallArchive)
  102. resp, err := http.Get(caplets.InstallArchive)
  103. if err != nil {
  104. return err
  105. }
  106. defer resp.Body.Close()
  107. if _, err := io.Copy(out, resp.Body); err != nil {
  108. return err
  109. }
  110. mod.Info("installing caplets to %s ...", caplets.InstallPath)
  111. if _, err = zip.Unzip(caplets.ArchivePath, caplets.InstallBase); err != nil {
  112. return err
  113. }
  114. os.RemoveAll(caplets.InstallPath)
  115. return os.Rename(caplets.InstallPathArchive, caplets.InstallPath)
  116. }