Import upstream version 2.32.0+git20211129.1.ed4239f
Kali Janitor
2 years ago
2 | 2 | |
3 | 3 | ENV SRC_DIR $GOPATH/src/github.com/bettercap/bettercap |
4 | 4 | |
5 | RUN apk add --update ca-certificates | |
6 | RUN apk add --no-cache --update bash iptables wireless-tools build-base libpcap-dev libusb-dev linux-headers libnetfilter_queue-dev git | |
5 | RUN apk add --no-cache ca-certificates | |
6 | RUN apk add --no-cache bash iptables wireless-tools build-base libpcap-dev libusb-dev linux-headers libnetfilter_queue-dev git | |
7 | 7 | |
8 | 8 | WORKDIR $SRC_DIR |
9 | 9 | ADD . $SRC_DIR |
15 | 15 | |
16 | 16 | # final stage |
17 | 17 | FROM alpine |
18 | RUN apk add --update ca-certificates | |
19 | RUN apk add --no-cache --update bash iproute2 libpcap libusb-dev libnetfilter_queue wireless-tools | |
18 | RUN apk add --no-cache ca-certificates | |
19 | RUN apk add --no-cache bash iproute2 libpcap libusb-dev libnetfilter_queue wireless-tools | |
20 | 20 | COPY --from=build-env /go/src/github.com/bettercap/bettercap/bettercap /app/ |
21 | 21 | COPY --from=build-env /usr/local/share/bettercap/caplets /app/ |
22 | 22 | WORKDIR /app |
28 | 28 | |
29 | 29 | var ( |
30 | 30 | BroadcastHw = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff} |
31 | IPv4Validator = regexp.MustCompile(`^[0-9\.]+/?\d*$`) | |
32 | IPv4RangeValidator = regexp.MustCompile(`^[0-9\.\-]+/?\d*$`) | |
33 | MACValidator = regexp.MustCompile(`(?i)^[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}$`) | |
31 | IPv4BlockValidator = regexp.MustCompile(`^` + | |
32 | `(?:(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])\.){3}` + | |
33 | `(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])` + | |
34 | `/(?:3[0-2]|2[0-9]|[1]?[0-9])` + `$`) | |
35 | IPv4RangeValidator = regexp.MustCompile(`^` + | |
36 | `(?:(?:(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])-)?(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])\.){3}` + | |
37 | `(?:(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])-)?(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])` + `$`) | |
38 | IPv4Validator = regexp.MustCompile(`^` + | |
39 | `(?:(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])\.){3}` + | |
40 | `(?:25[0-5]|2[0-4][0-9]|[1][0-9]{2}|[1-9]?[0-9])` + `$`) | |
41 | MACValidator = regexp.MustCompile(`(?i)^(?:[a-f0-9]{2}:){5}[a-f0-9]{2}$`) | |
34 | 42 | // lulz this sounds like a hamburger |
35 | macParser = regexp.MustCompile(`(?i)([a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2}:[a-f0-9]{1,2})`) | |
43 | macParser = regexp.MustCompile(`(?i)((?:[a-f0-9]{2}:){5}[a-f0-9]{2})`) | |
36 | 44 | aliasParser = regexp.MustCompile(`(?i)([a-z_][a-z_0-9]+)`) |
37 | 45 | ) |
38 | 46 | |
187 | 195 | |
188 | 196 | for _, a := range addrs { |
189 | 197 | address := a.String() |
190 | if IPv4Validator.MatchString(address) { | |
191 | if !strings.ContainsRune(address, '/') { | |
192 | // plain ip | |
193 | e.SetIP(address) | |
194 | } else { | |
195 | // ip/bits | |
196 | e.SetNetwork(address) | |
197 | } | |
198 | } else { | |
199 | // ipv6/xxx | |
198 | switch true { | |
199 | case IPv4Validator.MatchString(address): | |
200 | e.SetIP(address) | |
201 | break | |
202 | case IPv4BlockValidator.MatchString(address): | |
203 | e.SetNetwork(address) | |
204 | break | |
205 | default: | |
200 | 206 | e.SetIPv6(address) |
207 | break | |
201 | 208 | } |
202 | 209 | } |
203 | 210 | |
246 | 253 | |
247 | 254 | // user did not provide an interface name, |
248 | 255 | // return the first one with a valid ipv4 |
249 | // address | |
256 | // address that does not loop back | |
250 | 257 | for _, iface := range ifaces { |
251 | 258 | addrs, err := iface.Addrs() |
252 | 259 | if err != nil { |
256 | 263 | |
257 | 264 | for _, address := range addrs { |
258 | 265 | ip := address.String() |
259 | if !strings.Contains(ip, "127.0.0.1") && IPv4Validator.MatchString(ip) { | |
266 | if !strings.HasPrefix(ip, "127.0.0.1") && IPv4BlockValidator.MatchString(ip) { | |
260 | 267 | return buildEndpointFromInterface(iface) |
261 | 268 | } |
262 | 269 | } |
72 | 72 | } |
73 | 73 | |
74 | 74 | var iwPhyParser = regexp.MustCompile(`^\s*wiphy\s+(\d+)$`) |
75 | var iwFreqParser = regexp.MustCompile(`^\s+\*\s+(\d+)\s+MHz.+$`) | |
75 | var iwFreqParser = regexp.MustCompile(`^\s+\*\s+(\d+)\s+MHz.+dBm.+$`) | |
76 | 76 | |
77 | 77 | func iwSupportedFrequencies(iface string) ([]int, error) { |
78 | 78 | // first determine phy index |
0 | 0 | package routing |
1 | 1 | |
2 | 2 | import ( |
3 | "regexp" | |
4 | "strings" | |
5 | ||
3 | 6 | "github.com/bettercap/bettercap/core" |
4 | 7 | "github.com/evilsocket/islazy/str" |
5 | "regexp" | |
6 | "strings" | |
7 | 8 | ) |
8 | 9 | |
9 | var parser = regexp.MustCompile(`^(.+)\sdev\s([^\s]+)\s(.+)$`) | |
10 | var ( | |
11 | routeHeadings []string | |
12 | whitespaceParser = regexp.MustCompile(`\s+`) | |
13 | ) | |
10 | 14 | |
11 | 15 | func update() ([]Route, error) { |
12 | 16 | table = make([]Route, 0) |
13 | 17 | |
14 | for ip, inet := range map[RouteType]string{IPv4: "inet", IPv6: "inet6"} { | |
15 | output, err := core.Exec("ip", []string{"-f", inet, "route"}) | |
16 | if err != nil { | |
17 | return nil, err | |
18 | } | |
18 | output, err := core.Exec("netstat", []string{"-r", "-n", "-4", "-6"}) | |
19 | if err != nil { | |
20 | return nil, err | |
21 | } | |
22 | // because entries are separated by whitespace | |
23 | output = strings.ReplaceAll(output, "Next Hop", "Gateway") | |
19 | 24 | |
20 | for _, line := range strings.Split(output, "\n") { | |
21 | if line = str.Trim(line); len(line) > 0 { | |
22 | matches := parser.FindStringSubmatch(line) | |
23 | if num := len(matches); num == 4 { | |
24 | route := Route{ | |
25 | Type: ip, | |
26 | Destination: matches[1], | |
27 | Device: matches[2], | |
28 | Flags: matches[3], | |
29 | Default: strings.Index(matches[1], "default ") == 0, | |
30 | } | |
25 | for _, line := range strings.Split(output, "\n") { | |
26 | if line = str.Trim(line); len(line) != 0 { | |
27 | parts := whitespaceParser.Split(line, -1) | |
28 | if parts[0] == "Kernel" { | |
29 | continue | |
30 | } | |
31 | 31 | |
32 | if idx := strings.Index(route.Destination, " via "); idx >= 0 { | |
33 | route.Gateway = route.Destination[idx + len(" via "):] | |
34 | route.Destination = route.Destination[:idx] | |
35 | } | |
32 | if parts[0] == "Destination" { | |
33 | routeHeadings = parts | |
34 | continue | |
35 | } | |
36 | 36 | |
37 | table = append(table, route) | |
37 | route := Route{} | |
38 | for i, s := range parts { | |
39 | switch routeHeadings[i] { | |
40 | case "Destination": | |
41 | route.Destination = s | |
42 | break | |
43 | case "Flag": | |
44 | route.Flags = s | |
45 | break | |
46 | case "Flags": | |
47 | route.Flags = s | |
48 | break | |
49 | case "Gateway": | |
50 | route.Gateway = s | |
51 | break | |
52 | case "If": | |
53 | route.Device = s | |
54 | break | |
55 | case "Iface": | |
56 | route.Device = s | |
57 | break | |
58 | case "Netif": | |
59 | route.Device = s | |
60 | break | |
38 | 61 | } |
39 | 62 | } |
63 | ||
64 | route.Default = strings.Contains(route.Flags, "G") | |
65 | ||
66 | if strings.ContainsRune(route.Destination, '.') || strings.ContainsRune(route.Gateway, '.') { | |
67 | route.Type = IPv4 | |
68 | } else { | |
69 | route.Type = IPv6 | |
70 | } | |
71 | ||
72 | table = append(table, route) | |
40 | 73 | } |
41 | 74 | } |
42 | 75 |