mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -07:00
commit
84228f532f
11 changed files with 194 additions and 22 deletions
|
@ -1,6 +1,22 @@
|
|||
package core
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsDumbTerminal(t *testing.T) {
|
||||
term := os.Getenv("TERM")
|
||||
os.Setenv("TERM", "dumb")
|
||||
if !isDumbTerminal() {
|
||||
t.Fatal("Expected false when TERM==dumb")
|
||||
}
|
||||
os.Setenv("TERM", "")
|
||||
if !isDumbTerminal() {
|
||||
t.Fatal("Expected false when TERM empty")
|
||||
}
|
||||
os.Setenv("TERM", term)
|
||||
}
|
||||
|
||||
func TestW(t *testing.T) {
|
||||
exp := "<3\033[0m"
|
||||
|
@ -57,3 +73,23 @@ func TestYellow(t *testing.T) {
|
|||
t.Fatalf("expected path '%s', got '%s'", exp, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitSwag(t *testing.T) {
|
||||
// Run after other tests to avoid breaking globals
|
||||
// Test InitSwag unsets globals when set
|
||||
BOLD = "\033[1m"
|
||||
InitSwag(true)
|
||||
if BOLD != "" {
|
||||
t.Fatal("expected BOLD to be empty string")
|
||||
}
|
||||
term := os.Getenv("TERM")
|
||||
os.Setenv("TERM", "dumb")
|
||||
BOLD = "\033[1m"
|
||||
InitSwag(false)
|
||||
if BOLD != "" {
|
||||
t.Fatal("expected BOLD to be empty string")
|
||||
}
|
||||
os.Setenv("TERM", term)
|
||||
// Would be good to test BOLD *isn't* unset when we have a TTY
|
||||
// but less trivial to stub os.File.Fd() without complicating architecture
|
||||
}
|
||||
|
|
|
@ -50,13 +50,13 @@ func (f PfFirewall) sysCtlRead(param string) (string, error) {
|
|||
|
||||
func (f PfFirewall) sysCtlWrite(param string, value string) (string, error) {
|
||||
args := []string{"-w", fmt.Sprintf("%s=%s", param, value)}
|
||||
out, err := core.ExecSilent("sysctl", args)
|
||||
_, err := core.ExecSilent("sysctl", args)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// make sure we actually wrote the value
|
||||
if out, err = f.sysCtlRead(param); err != nil {
|
||||
if out, err := f.sysCtlRead(param); err != nil {
|
||||
return "", err
|
||||
} else if out != value {
|
||||
return "", fmt.Errorf("Expected value for '%s' is %s, found %s", param, value, out)
|
||||
|
|
|
@ -124,7 +124,7 @@ func (p *HTTPProxy) Configure(address string, proxyPort int, httpPort int, scrip
|
|||
}
|
||||
|
||||
if p.jsHook == "" && jsToInject != "" {
|
||||
if strings.HasPrefix(jsToInject, "<script ") == false {
|
||||
if !strings.HasPrefix(jsToInject, "<script ") {
|
||||
jsToInject = fmt.Sprintf("<script type=\"text/javascript\">%s</script>", jsToInject)
|
||||
}
|
||||
p.jsHook = fmt.Sprintf("%s</head>", jsToInject)
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type JSRequest struct {
|
||||
|
|
|
@ -146,9 +146,7 @@ func (d *Discovery) Show(by string) error {
|
|||
|
||||
rows := make([][]string, 0)
|
||||
for i, t := range targets {
|
||||
for _, r := range d.getRow(t, hasMeta) {
|
||||
rows = append(rows, r)
|
||||
}
|
||||
rows = append(rows, d.getRow(t, hasMeta)...)
|
||||
if i == pad {
|
||||
rows = append(rows, padCols)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func mdnsParser(ip *layers.IPv4, pkt gopacket.Packet, udp *layers.UDP) bool {
|
|||
for _, answer := range answers {
|
||||
if answer.Type == layers.DNSTypeA || answer.Type == layers.DNSTypeAAAA {
|
||||
hostname := string(answer.Name)
|
||||
if _, found := m[hostname]; found == false {
|
||||
if _, found := m[hostname]; !found {
|
||||
m[hostname] = make([]string, 0)
|
||||
}
|
||||
m[hostname] = append(m[hostname], answer.IP.String())
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
|
||||
const airPortPath = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
|
||||
|
||||
var IPv4RouteParser = regexp.MustCompile("^([a-z]+)+\\s+(\\d+\\.+\\d+.\\d.+\\d)+\\s+([a-zA-z]+)+\\s+(\\d+)+\\s+(\\d+)+\\s+([a-zA-Z]+\\d+)$")
|
||||
var IPv4RouteParser = regexp.MustCompile(`^([a-z]+)+\s+(\d+\.+\d+.\d.+\d)+\s+([a-zA-z]+)+\s+(\d+)+\s+(\d+)+\s+([a-zA-Z]+\d+)$`)
|
||||
var IPv4RouteTokens = 7
|
||||
var IPv4RouteCmd = "netstat"
|
||||
var IPv4RouteCmdOpts = []string{"-n", "-r"}
|
||||
|
|
|
@ -59,13 +59,12 @@ func SetInterfaceChannel(iface string, channel int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetSupportedFrequencies(iface string) ([]int, error) {
|
||||
func processSupportedFrequencies(output string, err error) ([]int, error) {
|
||||
freqs := make([]int, 0)
|
||||
out, err := core.Exec("iwlist", []string{iface, "freq"})
|
||||
if err != nil {
|
||||
return freqs, err
|
||||
} else if out != "" {
|
||||
scanner := bufio.NewScanner(strings.NewReader(out))
|
||||
} else if output != "" {
|
||||
scanner := bufio.NewScanner(strings.NewReader(output))
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
matches := WiFiFreqParser.FindStringSubmatch(line)
|
||||
|
@ -78,3 +77,8 @@ func GetSupportedFrequencies(iface string) ([]int, error) {
|
|||
}
|
||||
return freqs, nil
|
||||
}
|
||||
|
||||
func GetSupportedFrequencies(iface string) ([]int, error) {
|
||||
out, err := core.Exec("iwlist", []string{iface, "freq"})
|
||||
return processSupportedFrequencies(out, err)
|
||||
}
|
||||
|
|
59
network/net_linux_test.go
Normal file
59
network/net_linux_test.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestProcessSupportedFrequencies(t *testing.T) {
|
||||
// Actually test processSupportedFrequencies; IO is lifted out.
|
||||
cases := []struct {
|
||||
Name string
|
||||
InputString string
|
||||
InputError error
|
||||
ExpectedFreqs []int
|
||||
ExpectedError bool
|
||||
}{
|
||||
{
|
||||
"Returns appropriately formatted frequencies on valid input",
|
||||
`wlan1 11 channels in total; available frequencies :
|
||||
Channel 01 : 2.412 GHz
|
||||
Channel 02 : 2.417 GHz
|
||||
Channel 03 : 2.422 GHz
|
||||
Channel 04 : 2.427 GHz
|
||||
Channel 05 : 2.432 GHz
|
||||
Channel 06 : 2.437 GHz
|
||||
Channel 07 : 2.442 GHz
|
||||
Channel 08 : 2.447 GHz
|
||||
Channel 09 : 2.452 GHz
|
||||
Channel 10 : 2.457 GHz
|
||||
Channel 11 : 2.462 GHz
|
||||
Current Frequency:2.437 GHz (Channel 6)`,
|
||||
nil,
|
||||
[]int{2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Returns empty with an error",
|
||||
"Doesn't matter",
|
||||
errors.New("iwlist must have failed"),
|
||||
[]int{},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, test := range cases {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
freqs, err := processSupportedFrequencies(test.InputString, test.InputError)
|
||||
if err != nil && !test.ExpectedError {
|
||||
t.Errorf("unexpected error: %s", err)
|
||||
}
|
||||
if err == nil && test.ExpectedError {
|
||||
t.Error("expected error, but got none")
|
||||
}
|
||||
if !test.ExpectedError && !reflect.DeepEqual(freqs, test.ExpectedFreqs) {
|
||||
t.Errorf("got %v, want %v", freqs, test.ExpectedFreqs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -35,15 +35,52 @@ func TestNormalizeMac(t *testing.T) {
|
|||
|
||||
// TODO: refactor to parse targets with an actual alias map
|
||||
func TestParseTargets(t *testing.T) {
|
||||
ips, macs, err := ParseTargets("192.168.1.2, 192.168.1.3", &Aliases{})
|
||||
if err != nil {
|
||||
t.Error("ips:", ips, "macs:", macs, "err:", err)
|
||||
cases := []struct {
|
||||
Name string
|
||||
InputTargets string
|
||||
InputAliases *Aliases
|
||||
ExpectedIPCount int
|
||||
ExpectedMACCount int
|
||||
ExpectedError bool
|
||||
}{
|
||||
// Not sure how to trigger sad path where macParser.FindAllString()
|
||||
// finds a MAC but net.ParseMac() fails on the result.
|
||||
{
|
||||
"empty target string causes empty return",
|
||||
"",
|
||||
&Aliases{},
|
||||
0,
|
||||
0,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"MACs are parsed",
|
||||
"192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0",
|
||||
&Aliases{},
|
||||
2,
|
||||
2,
|
||||
false,
|
||||
},
|
||||
}
|
||||
if len(ips) != 2 {
|
||||
t.Fatalf("expected '%d', got '%d'", 2, len(ips))
|
||||
}
|
||||
if len(macs) != 0 {
|
||||
t.Fatalf("expected '%d', got '%d'", 0, len(macs))
|
||||
for _, test := range cases {
|
||||
t.Run(test.Name, func(t *testing.T) {
|
||||
ips, macs, err := ParseTargets(test.InputTargets, test.InputAliases)
|
||||
if err != nil && !test.ExpectedError {
|
||||
t.Errorf("unexpected error: %s", err)
|
||||
}
|
||||
if err == nil && test.ExpectedError {
|
||||
t.Error("Expected error, but got none")
|
||||
}
|
||||
if test.ExpectedError {
|
||||
return
|
||||
}
|
||||
if len(ips) != test.ExpectedIPCount {
|
||||
t.Errorf("Wrong number of IPs. Got %v for targets %s", ips, test.InputTargets)
|
||||
}
|
||||
if len(macs) != test.ExpectedMACCount {
|
||||
t.Errorf("Wrong number of MACs. Got %v for targets %s", macs, test.InputTargets)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
38
session/session_test.go
Normal file
38
session/session_test.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseCommands(t *testing.T) {
|
||||
//commands := ParseCommands("wifi.recon on; asdf; \"asdf;\" asdf")
|
||||
t.Run("handles a semicolon as a delimiter", func(t *testing.T) {
|
||||
first := "wifi.recon on"
|
||||
second := "wifi.ap"
|
||||
cmd := fmt.Sprintf("%s; %s", first, second)
|
||||
commands := ParseCommands(cmd)
|
||||
if l := len(commands); l != 2 {
|
||||
t.Fatalf("Expected 2 commands, got %d", l)
|
||||
}
|
||||
if got := commands[0]; got != first {
|
||||
t.Fatalf("expected %s got %s", first, got)
|
||||
}
|
||||
if got := commands[1]; got != second {
|
||||
t.Fatalf("expected %s got %s", second, got)
|
||||
}
|
||||
})
|
||||
t.Run("handles semicolon inside quotes", func(t *testing.T) {
|
||||
cmd := "set ticker.commands \"clear; net.show\""
|
||||
commands := ParseCommands(cmd)
|
||||
if l := len(commands); l != 1 {
|
||||
t.Fatalf("expected 1 command, got %d", l)
|
||||
}
|
||||
// Expect double-quotes stripped
|
||||
expected := "set ticker.commands clear; net.show"
|
||||
if got := commands[0]; got != expected {
|
||||
fmt.Println(got)
|
||||
t.Fatalf("expected %s got %s", cmd, got)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue