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