main.go 2.7 KB
Newer Older
Sophie Brun's avatar
Sophie Brun committed
1 2 3 4 5 6
package main

import (
	"fmt"
	"io"
	"os"
Sophie Brun's avatar
Sophie Brun committed
7
	"strings"
Sophie Brun's avatar
Sophie Brun committed
8

Sophie Brun's avatar
Sophie Brun committed
9 10
	"runtime"

Sophie Brun's avatar
Sophie Brun committed
11 12 13 14
	"github.com/bettercap/bettercap/core"
	"github.com/bettercap/bettercap/log"
	"github.com/bettercap/bettercap/modules"
	"github.com/bettercap/bettercap/session"
Sophie Brun's avatar
Sophie Brun committed
15 16 17

	"github.com/evilsocket/islazy/str"
	"github.com/evilsocket/islazy/tui"
Sophie Brun's avatar
Sophie Brun committed
18 19 20 21 22 23 24 25 26 27
)

func main() {
	sess, err := session.New()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer sess.Close()

Sophie Brun's avatar
Sophie Brun committed
28
	if !tui.Effects() {
Sophie Brun's avatar
Sophie Brun committed
29
		if *sess.Options.NoColors {
Sophie Brun's avatar
Sophie Brun committed
30 31 32 33 34 35
			fmt.Printf("\n\nWARNING: Terminal colors have been disabled, view will be very limited.\n\n")
		} else {
			fmt.Printf("\n\nWARNING: This terminal does not support colors, view will be very limited.\n\n")
		}
	}

Sophie Brun's avatar
Sophie Brun committed
36 37 38 39 40
	if *sess.Options.PrintVersion {
		fmt.Printf("%s v%s (built for %s %s with %s)\n", core.Name, core.Version, runtime.GOOS, runtime.GOARCH, runtime.Version())
		return
	}

Sophie Brun's avatar
Sophie Brun committed
41
	appName := fmt.Sprintf("%s v%s", core.Name, core.Version)
Sophie Brun's avatar
Sophie Brun committed
42
	appBuild := fmt.Sprintf("(built for %s %s with %s)", runtime.GOOS, runtime.GOARCH, runtime.Version())
Sophie Brun's avatar
Sophie Brun committed
43

Sophie Brun's avatar
Sophie Brun committed
44
	fmt.Printf("%s %s [type '%s' for a list of commands]\n\n", tui.Bold(appName), tui.Dim(appBuild), tui.Bold("help"))
Sophie Brun's avatar
Sophie Brun committed
45

Sophie Brun's avatar
Sophie Brun committed
46 47
	// Load all modules
	modules.LoadModules(sess)
Sophie Brun's avatar
Sophie Brun committed
48 49 50 51 52

	if err = sess.Start(); err != nil {
		log.Fatal("%s", err)
	}

Sophie Brun's avatar
Sophie Brun committed
53 54 55 56 57 58 59 60
	// Some modules are enabled by default in order
	// to make the interactive session useful.
	for _, modName := range str.Comma(*sess.Options.AutoStart) {
		if err = sess.Run(modName + " on"); err != nil {
			log.Fatal("error while starting module %s: %s", modName, err)
		}
	}

Sophie Brun's avatar
Sophie Brun committed
61 62 63 64 65 66
	// Commands sent with -eval are used to set specific
	// caplet parameters (i.e. arp.spoof.targets) via command
	// line, therefore they need to be executed first otherwise
	// modules might already be started.
	for _, cmd := range session.ParseCommands(*sess.Options.Commands) {
		if err = sess.Run(cmd); err != nil {
Sophie Brun's avatar
Sophie Brun committed
67
			log.Error("error while running '%s': %s", tui.Bold(cmd), tui.Red(err.Error()))
Sophie Brun's avatar
Sophie Brun committed
68 69 70 71 72 73
		}
	}

	// Then run the caplet if specified.
	if *sess.Options.Caplet != "" {
		if err = sess.RunCaplet(*sess.Options.Caplet); err != nil {
Sophie Brun's avatar
Sophie Brun committed
74
			log.Error("error while running caplet %s: %s", tui.Bold(*sess.Options.Caplet), tui.Red(err.Error()))
Sophie Brun's avatar
Sophie Brun committed
75 76 77 78 79 80 81
		}
	}

	// Eventually start the interactive session.
	for sess.Active {
		line, err := sess.ReadLine()
		if err != nil {
Sophie Brun's avatar
Sophie Brun committed
82 83
			if err == io.EOF || err.Error() == "Interrupt" {
				if exitPrompt() {
Sophie Brun's avatar
Sophie Brun committed
84 85 86 87 88 89
					sess.Run("exit")
					os.Exit(0)
				}
				continue
			} else {
				log.Fatal("%s", err)
Sophie Brun's avatar
Sophie Brun committed
90 91 92 93 94 95 96 97 98 99
			}
		}

		for _, cmd := range session.ParseCommands(line) {
			if err = sess.Run(cmd); err != nil {
				log.Error("%s", err)
			}
		}
	}
}
Sophie Brun's avatar
Sophie Brun committed
100 101 102 103 104 105 106 107

func exitPrompt() bool {
	var ans string
	fmt.Printf("Are you sure you want to quit this session? y/n ")
	fmt.Scan(&ans)

	return strings.ToLower(ans) == "y"
}