| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "regexp" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/bettercap/bettercap/core" |
| 12 | ) |
| 13 | |
| 14 | // see Windows version to understand why .... |
| 15 | func getInterfaceName(iface net.Interface) string { |
| 16 | return iface.Name |
| 17 | } |
| 18 | |
| 19 | // See https://github.com/bettercap/bettercap/issues/819 |
| 20 | func ForceMonitorMode(iface string) error { |
| 21 | _, _ = core.Exec("ip", []string{"link", "set", iface, "down"}) |
| 22 | |
| 23 | out, err := core.Exec("iw", []string{"dev", iface, "set", "type", "monitor"}) |
| 24 | if err != nil { |
| 25 | return fmt.Errorf("iw: out=%s err=%s", out, err) |
| 26 | } else if out != "" { |
| 27 | return fmt.Errorf("Unexpected output while setting interface %s into monitor mode: %s", iface, out) |
| 28 | } |
| 29 | |
| 30 | _, _ = core.Exec("ip", []string{"link", "set", iface, "up"}) |
| 31 | |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | func SetInterfaceChannel(iface string, channel int) error { |
| 36 | curr := GetInterfaceChannel(iface) |
| 37 | // the interface is already on this channel |
| 38 | if curr == channel { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | if core.HasBinary("iw") { |
| 43 | // Debug("SetInterfaceChannel(%s, %d) iw based", iface, channel) |
| 44 | out, err := core.Exec("iw", []string{"dev", iface, "set", "channel", fmt.Sprintf("%d", channel)}) |
| 45 | if err != nil { |
| 46 | return fmt.Errorf("iw: out=%s err=%s", out, err) |
| 47 | } else if out != "" { |
| 48 | return fmt.Errorf("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out) |
| 49 | } |
| 50 | } else if core.HasBinary("iwconfig") { |
| 51 | // Debug("SetInterfaceChannel(%s, %d) iwconfig based") |
| 52 | out, err := core.Exec("iwconfig", []string{iface, "channel", fmt.Sprintf("%d", channel)}) |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("iwconfig: out=%s err=%s", out, err) |
| 55 | } else if out != "" { |
| 56 | return fmt.Errorf("Unexpected output while setting interface %s to channel %d: %s", iface, channel, out) |
| 57 | } |
| 58 | } else { |
| 59 | return fmt.Errorf("no iw or iwconfig binaries found in $PATH") |
| 60 | } |
| 61 | |
| 62 | SetInterfaceCurrentChannel(iface, channel) |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | var iwlistFreqParser = regexp.MustCompile(`^\s+Channel.([0-9]+)\s+:\s+([0-9\.]+)\s+GHz.*$`) |
| 67 | |
| 68 | func iwlistSupportedFrequencies(iface string) ([]int, error) { |
| 69 | out, err := 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 := bufio.NewScanner(strings.NewReader(out)) |
| 77 | for scanner.Scan() { |
| 78 | line := scanner.Text() |
| 79 | matches := iwlistFreqParser.FindStringSubmatch(line) |
| 80 | if len(matches) == 3 { |
| 81 | if freq, err := 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 iwPhyParser = regexp.MustCompile(`^\s*wiphy\s+(\d+)$`) |
| 92 | var iwFreqParser = regexp.MustCompile(`^\s+\*\s+(\d+)\s+MHz.+dBm.+$`) |
| 93 | |
| 94 | func iwSupportedFrequencies(iface string) ([]int, error) { |
| 95 | // first determine phy index |
| 96 | out, err := core.Exec("iw", []string{iface, "info"}) |
| 97 | if err != nil { |
| 98 | return nil, fmt.Errorf("error getting %s phy index: %v", iface, err) |
| 99 | } |
| 100 | |
| 101 | phy := int64(-1) |
| 102 | scanner := bufio.NewScanner(strings.NewReader(out)) |
| 103 | for scanner.Scan() { |
| 104 | line := scanner.Text() |
| 105 | matches := iwPhyParser.FindStringSubmatch(line) |
| 106 | if len(matches) == 2 { |
| 107 | if phy, err = strconv.ParseInt(matches[1], 10, 32); err != nil { |
| 108 | return nil, fmt.Errorf("error parsing %s phy index: %v (line: %s)", iface, err, line) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if phy == -1 { |
| 114 | return nil, fmt.Errorf("could not find %s phy index", iface) |
| 115 | } |
| 116 | |
| 117 | // then get phyN info |
| 118 | phyName := fmt.Sprintf("phy%d", phy) |
| 119 | out, err = core.Exec("iw", []string{phyName, "info"}) |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("error getting %s (%s) info: %v", phyName, iface, err) |
| 122 | } |
| 123 | |
| 124 | freqs := []int{} |
| 125 | scanner = bufio.NewScanner(strings.NewReader(out)) |
| 126 | for scanner.Scan() { |
| 127 | line := scanner.Text() |
| 128 | matches := iwFreqParser.FindStringSubmatch(line) |
| 129 | if len(matches) == 2 { |
| 130 | if freq, err := strconv.ParseInt(matches[1], 10, 64); err != nil { |
| 131 | return nil, 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 GetSupportedFrequencies(iface string) ([]int, error) { |
| 142 | // give priority to iwlist because of https://github.com/bettercap/bettercap/issues/881 |
| 143 | if core.HasBinary("iwlist") { |
| 144 | return iwlistSupportedFrequencies(iface) |
| 145 | } else if core.HasBinary("iw") { |
| 146 | return iwSupportedFrequencies(iface) |
| 147 | } |
| 148 | |
| 149 | return nil, fmt.Errorf("no iw or iwlist binaries found in $PATH") |
| 150 | } |