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