| 1 | package networkscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/ |
| 2 | |
| 3 | import ( |
| 4 | "bufioscip-go gomod github.com/golang/go/src go1.16 bufio/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 6 | "netscip-go gomod github.com/golang/go/src go1.16 net/" |
| 7 | "regexpscip-go gomod github.com/golang/go/src go1.16 regexp/" |
| 8 | "strconvscip-go gomod github.com/golang/go/src go1.16 strconv/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 10 | |
| 11 | "github.com/bettercap/bettercap/corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/" |
| 12 | ) |
| 13 | |
| 14 | // see Windows version to understand why .... |
| 15 | func getInterfaceNamescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/getInterfaceName().(iface netscip-go gomod github.com/golang/go/src go1.16 net/.Interfacescip-go gomod github.com/golang/go/src go1.16 net/Interface#) string { |
| 16 | return iface.Namescip-go gomod github.com/golang/go/src go1.16 net/Interface#Name. |
| 17 | } |
| 18 | |
| 19 | // See https://github.com/bettercap/bettercap/issues/819 |
| 20 | func ForceMonitorModescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/ForceMonitorMode().(iface string) error { |
| 21 | _, _ = corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("ip", []string{"link", "set", iface, "down"}) |
| 22 | |
| 23 | out, err := corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("iw", []string{"dev", iface, "set", "type", "monitor"}) |
| 24 | if err != nil { |
| 25 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("iw: out=%s err=%s", out, err) |
| 26 | } else if out != "" { |
| 27 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("Unexpected output while setting interface %s into monitor mode: %s", iface, out) |
| 28 | } |
| 29 | |
| 30 | _, _ = corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("ip", []string{"link", "set", iface, "up"}) |
| 31 | |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | func SetInterfaceChannelscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/SetInterfaceChannel().(iface string, channel int) error { |
| 36 | curr := GetInterfaceChannelscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/GetInterfaceChannel().(iface) |
| 37 | // the interface is already on this channel |
| 38 | if curr == channel { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | if corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.HasBinaryscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/HasBinary().("iw") { |
| 43 | // Debug("SetInterfaceChannel(%s, %d) iw based", iface, channel) |
| 44 | out, err := corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("iw", []string{"dev", iface, "set", "channel", fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%d", channel)}) |
| 45 | if err != nil { |
| 46 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("iw: out=%s err=%s", out, err) |
| 47 | } else if out != "" { |
| 48 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out) |
| 49 | } |
| 50 | } else if corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.HasBinaryscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/HasBinary().("iwconfig") { |
| 51 | // Debug("SetInterfaceChannel(%s, %d) iwconfig based") |
| 52 | out, err := corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("iwconfig", []string{iface, "channel", fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%d", channel)}) |
| 53 | if err != nil { |
| 54 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("iwconfig: out=%s err=%s", out, err) |
| 55 | } else if out != "" { |
| 56 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out) |
| 57 | } |
| 58 | } else { |
| 59 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("no iw or iwconfig binaries found in $PATH") |
| 60 | } |
| 61 | |
| 62 | SetInterfaceCurrentChannelscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/SetInterfaceCurrentChannel().(iface, channel) |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | var iwlistFreqParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwlistFreqParser. = regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`^\s+Channel.([0-9]+)\s+:\s+([0-9\.]+)\s+GHz.*$`) |
| 67 | |
| 68 | func iwlistSupportedFrequenciesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwlistSupportedFrequencies().(iface string) ([]int, error) { |
| 69 | out, err := corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("iwlist", []string{iface, "freq"}) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | |
| 74 | freqs := make([]int, 0) |
| 75 | if out != "" { |
| 76 | scanner := bufioscip-go gomod github.com/golang/go/src go1.16 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.16 bufio/NewScanner().(stringsscip-go gomod github.com/golang/go/src go1.16 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.16 strings/NewReader().(out)) |
| 77 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.16 bufio/Scanner#Scan().() { |
| 78 | line := scanner.Textscip-go gomod github.com/golang/go/src go1.16 bufio/Scanner#Text().() |
| 79 | matches := iwlistFreqParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwlistFreqParser..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindStringSubmatch().(line) |
| 80 | if len(matches) == 3 { |
| 81 | if freq, err := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.ParseFloatscip-go gomod github.com/golang/go/src go1.16 strconv/ParseFloat().(matches[2], 64); err == nil { |
| 82 | freqs = append(freqs, int(freq*1000)) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return freqs, nil |
| 89 | } |
| 90 | |
| 91 | var iwPhyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwPhyParser. = regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`^\s*wiphy\s+(\d+)$`) |
| 92 | var iwFreqParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwFreqParser. = regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`^\s+\*\s+(\d+)\s+MHz.+dBm.+$`) |
| 93 | |
| 94 | func iwSupportedFrequenciesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwSupportedFrequencies().(iface string) ([]int, error) { |
| 95 | // first determine phy index |
| 96 | out, err := corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("iw", []string{iface, "info"}) |
| 97 | if err != nil { |
| 98 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error getting %s phy index: %v", iface, err) |
| 99 | } |
| 100 | |
| 101 | phy := int64(-1) |
| 102 | scanner := bufioscip-go gomod github.com/golang/go/src go1.16 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.16 bufio/NewScanner().(stringsscip-go gomod github.com/golang/go/src go1.16 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.16 strings/NewReader().(out)) |
| 103 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.16 bufio/Scanner#Scan().() { |
| 104 | line := scanner.Textscip-go gomod github.com/golang/go/src go1.16 bufio/Scanner#Text().() |
| 105 | matches := iwPhyParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwPhyParser..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindStringSubmatch().(line) |
| 106 | if len(matches) == 2 { |
| 107 | if phy, err = strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.16 strconv/ParseInt().(matches[1], 10, 32); err != nil { |
| 108 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error parsing %s phy index: %v (line: %s)", iface, err, line) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if phy == -1 { |
| 114 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("could not find %s phy index", iface) |
| 115 | } |
| 116 | |
| 117 | // then get phyN info |
| 118 | phyName := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("phy%d", phy) |
| 119 | out, err = corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.Execscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/Exec().("iw", []string{phyName, "info"}) |
| 120 | if err != nil { |
| 121 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error getting %s (%s) info: %v", phyName, iface, err) |
| 122 | } |
| 123 | |
| 124 | freqs := []int{} |
| 125 | scanner = bufioscip-go gomod github.com/golang/go/src go1.16 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.16 bufio/NewScanner().(stringsscip-go gomod github.com/golang/go/src go1.16 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.16 strings/NewReader().(out)) |
| 126 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.16 bufio/Scanner#Scan().() { |
| 127 | line := scanner.Textscip-go gomod github.com/golang/go/src go1.16 bufio/Scanner#Text().() |
| 128 | matches := iwFreqParserscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwFreqParser..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindStringSubmatch().(line) |
| 129 | if len(matches) == 2 { |
| 130 | if freq, err := strconvscip-go gomod github.com/golang/go/src go1.16 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.16 strconv/ParseInt().(matches[1], 10, 64); err != nil { |
| 131 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error parsing %s freq: %v (line: %s)", iface, err, line) |
| 132 | } else { |
| 133 | freqs = append(freqs, int(freq)) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return freqs, nil |
| 139 | } |
| 140 | |
| 141 | func GetSupportedFrequenciesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/GetSupportedFrequencies().(iface string) ([]int, error) { |
| 142 | // give priority to iwlist because of https://github.com/bettercap/bettercap/issues/881 |
| 143 | if corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.HasBinaryscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/HasBinary().("iwlist") { |
| 144 | return iwlistSupportedFrequenciesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwlistSupportedFrequencies().(iface) |
| 145 | } else if corescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/.HasBinaryscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/core`/HasBinary().("iw") { |
| 146 | return iwSupportedFrequenciesscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/iwSupportedFrequencies().(iface) |
| 147 | } |
| 148 | |
| 149 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("no iw or iwlist binaries found in $PATH") |
| 150 | } |