| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "sort" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/bettercap/bettercap/core" |
| 16 | "github.com/bettercap/bettercap/network" |
| 17 | |
| 18 | "github.com/bettercap/readline" |
| 19 | "github.com/evilsocket/islazy/str" |
| 20 | "github.com/evilsocket/islazy/tui" |
| 21 | ) |
| 22 | |
| 23 | func (s *Session) generalHelp() { |
| 24 | fmt.Println() |
| 25 | |
| 26 | maxLen := 0 |
| 27 | for _, h := range s.CoreHandlers { |
| 28 | len := len(h.Name) |
| 29 | if len > maxLen { |
| 30 | maxLen = len |
| 31 | } |
| 32 | } |
| 33 | pad := "%" + strconv.Itoa(maxLen) + "s" |
| 34 | |
| 35 | for _, h := range s.CoreHandlers { |
| 36 | s.Events.Printf(" "+tui.Yellow(pad)+" : %s\n", h.Name, h.Description) |
| 37 | } |
| 38 | |
| 39 | s.Events.Printf("%s\n", tui.Bold("\nModules\n")) |
| 40 | |
| 41 | maxLen = 0 |
| 42 | for _, m := range s.Modules { |
| 43 | len := len(m.Name()) |
| 44 | if len > maxLen { |
| 45 | maxLen = len |
| 46 | } |
| 47 | } |
| 48 | pad = "%" + strconv.Itoa(maxLen) + "s" |
| 49 | |
| 50 | for _, m := range s.Modules { |
| 51 | status := "" |
| 52 | if m.Running() { |
| 53 | status = tui.Green("running") |
| 54 | } else { |
| 55 | status = tui.Red("not running") |
| 56 | } |
| 57 | s.Events.Printf(" "+tui.Yellow(pad)+" > %s\n", m.Name(), status) |
| 58 | } |
| 59 | |
| 60 | fmt.Println() |
| 61 | } |
| 62 | |
| 63 | func (s *Session) moduleHelp(filter string) error { |
| 64 | err, m := s.Module(filter) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | fmt.Println() |
| 70 | status := "" |
| 71 | if m.Running() { |
| 72 | status = tui.Green("running") |
| 73 | } else { |
| 74 | status = tui.Red("not running") |
| 75 | } |
| 76 | s.Events.Printf("%s (%s): %s\n\n", tui.Yellow(m.Name()), status, tui.Dim(m.Description())) |
| 77 | |
| 78 | maxLen := 0 |
| 79 | handlers := m.Handlers() |
| 80 | for _, h := range handlers { |
| 81 | len := len(h.Name) |
| 82 | if len > maxLen { |
| 83 | maxLen = len |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | for _, h := range handlers { |
| 88 | s.Events.Printf("%s", h.Help(maxLen)) |
| 89 | } |
| 90 | fmt.Println() |
| 91 | |
| 92 | params := m.Parameters() |
| 93 | if len(params) > 0 { |
| 94 | v := make([]*ModuleParam, 0) |
| 95 | maxLen := 0 |
| 96 | for _, h := range params { |
| 97 | len := len(h.Name) |
| 98 | if len > maxLen { |
| 99 | maxLen = len |
| 100 | } |
| 101 | v = append(v, h) |
| 102 | } |
| 103 | |
| 104 | sort.Slice(v, func(i, j int) bool { |
| 105 | return v[i].Name < v[j].Name |
| 106 | }) |
| 107 | |
| 108 | s.Events.Printf(" Parameters\n\n") |
| 109 | for _, p := range v { |
| 110 | s.Events.Printf("%s", p.Help(maxLen)) |
| 111 | } |
| 112 | fmt.Println() |
| 113 | } |
| 114 | |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | func (s *Session) helpHandler(args []string, sess *Session) error { |
| 119 | filter := "" |
| 120 | if len(args) == 2 { |
| 121 | filter = str.Trim(args[1]) |
| 122 | } |
| 123 | |
| 124 | if filter == "" { |
| 125 | s.generalHelp() |
| 126 | } else { |
| 127 | if err := s.moduleHelp(filter); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return nil |
| 133 | } |
| 134 | |
| 135 | func (s *Session) activeHandler(args []string, sess *Session) error { |
| 136 | for _, m := range s.Modules { |
| 137 | if !m.Running() { |
| 138 | continue |
| 139 | } |
| 140 | |
| 141 | s.Events.Printf("%s (%s)\n", tui.Bold(m.Name()), tui.Dim(m.Description())) |
| 142 | params := m.Parameters() |
| 143 | if len(params) > 0 { |
| 144 | fmt.Println() |
| 145 | for _, p := range params { |
| 146 | _, val := s.Env.Get(p.Name) |
| 147 | s.Events.Printf(" %s : %s\n", tui.Yellow(p.Name), val) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | fmt.Println() |
| 152 | } |
| 153 | |
| 154 | return nil |
| 155 | } |
| 156 | |
| 157 | func (s *Session) exitHandler(args []string, sess *Session) error { |
| 158 | // notify any listener that the session is about to end |
| 159 | s.Events.Add("session.stopped", nil) |
| 160 | |
| 161 | for _, mod := range s.Modules { |
| 162 | if mod.Running() { |
| 163 | mod.Stop() |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | s.Active = false |
| 168 | s.Input.Close() |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | func (s *Session) sleepHandler(args []string, sess *Session) error { |
| 173 | if secs, err := strconv.Atoi(args[0]); err == nil { |
| 174 | time.Sleep(time.Duration(secs) * time.Second) |
| 175 | return nil |
| 176 | } else { |
| 177 | return err |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func (s *Session) getHandler(args []string, sess *Session) error { |
| 182 | key := args[0] |
| 183 | if strings.Contains(key, "*") { |
| 184 | prev_ns := "" |
| 185 | |
| 186 | fmt.Println() |
| 187 | last := len(key) - 1 |
| 188 | prefix := key[:last] |
| 189 | sortedKeys := s.Env.Sorted() |
| 190 | padding := 0 |
| 191 | |
| 192 | for _, k := range sortedKeys { |
| 193 | l := len(k) |
| 194 | if l > padding { |
| 195 | padding = l |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | for _, k := range sortedKeys { |
| 200 | if strings.HasPrefix(k, prefix) { |
| 201 | ns := "" |
| 202 | toks := strings.Split(k, ".") |
| 203 | if len(toks) > 0 { |
| 204 | ns = toks[0] |
| 205 | } |
| 206 | |
| 207 | if ns != prev_ns { |
| 208 | fmt.Println() |
| 209 | prev_ns = ns |
| 210 | } |
| 211 | |
| 212 | s.Events.Printf(" %"+strconv.Itoa(padding)+"s: '%s'\n", k, s.Env.Data[k]) |
| 213 | } |
| 214 | } |
| 215 | fmt.Println() |
| 216 | } else if found, value := s.Env.Get(key); found { |
| 217 | fmt.Println() |
| 218 | s.Events.Printf(" %s: '%s'\n", key, value) |
| 219 | fmt.Println() |
| 220 | } else { |
| 221 | return fmt.Errorf("%s not found", key) |
| 222 | } |
| 223 | |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | func (s *Session) setHandler(args []string, sess *Session) error { |
| 228 | key := args[0] |
| 229 | value := args[1] |
| 230 | |
| 231 | if value == "\"\"" || value == "''" { |
| 232 | value = "" |
| 233 | } |
| 234 | |
| 235 | s.Env.Set(key, value) |
| 236 | return nil |
| 237 | } |
| 238 | |
| 239 | func (s *Session) readHandler(args []string, sess *Session) error { |
| 240 | key := args[0] |
| 241 | prompt := args[1] |
| 242 | |
| 243 | reader := bufio.NewReader(os.Stdin) |
| 244 | fmt.Printf("%s ", prompt) |
| 245 | |
| 246 | value, _ := reader.ReadString('\n') |
| 247 | value = str.Trim(value) |
| 248 | if value == "\"\"" || value == "''" { |
| 249 | value = "" |
| 250 | } |
| 251 | |
| 252 | s.Env.Set(key, value) |
| 253 | return nil |
| 254 | } |
| 255 | |
| 256 | func (s *Session) clsHandler(args []string, sess *Session) error { |
| 257 | cmd := "clear" |
| 258 | if runtime.GOOS == "windows" { |
| 259 | cmd = "cls" |
| 260 | } |
| 261 | |
| 262 | c := exec.Command(cmd) |
| 263 | c.Stdout = os.Stdout |
| 264 | c.Run() |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | func (s *Session) includeHandler(args []string, sess *Session) error { |
| 269 | return s.RunCaplet(args[0]) |
| 270 | } |
| 271 | |
| 272 | func (s *Session) shHandler(args []string, sess *Session) error { |
| 273 | out, err := core.Shell(args[0]) |
| 274 | if err == nil { |
| 275 | s.Events.Printf("%s\n", out) |
| 276 | } |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | func normalizeMac(mac string) string { |
| 281 | var parts []string |
| 282 | if strings.ContainsRune(mac, '-') { |
| 283 | parts = strings.Split(mac, "-") |
| 284 | } else { |
| 285 | parts = strings.Split(mac, ":") |
| 286 | } |
| 287 | |
| 288 | for i, p := range parts { |
| 289 | if len(p) < 2 { |
| 290 | parts[i] = "0" + p |
| 291 | } |
| 292 | } |
| 293 | return strings.ToLower(strings.Join(parts, ":")) |
| 294 | } |
| 295 | |
| 296 | func (s *Session) propagateAlias(mac, alias string) { |
| 297 | mac = normalizeMac(mac) |
| 298 | |
| 299 | s.Aliases.Set(mac, alias) |
| 300 | |
| 301 | if dev, found := s.BLE.Get(mac); found { |
| 302 | dev.Alias = alias |
| 303 | } |
| 304 | |
| 305 | if dev, found := s.HID.Get(mac); found { |
| 306 | dev.Alias = alias |
| 307 | } |
| 308 | |
| 309 | if ap, found := s.WiFi.Get(mac); found { |
| 310 | ap.Alias = alias |
| 311 | } |
| 312 | |
| 313 | if sta, found := s.WiFi.GetClient(mac); found { |
| 314 | sta.Alias = alias |
| 315 | } |
| 316 | |
| 317 | if host, found := s.Lan.Get(mac); found { |
| 318 | host.Alias = alias |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | func (s *Session) aliasHandler(args []string, sess *Session) error { |
| 323 | mac := args[0] |
| 324 | alias := str.Trim(args[1]) |
| 325 | if alias == "\"\"" || alias == "''" { |
| 326 | alias = "" |
| 327 | } |
| 328 | s.propagateAlias(mac, alias) |
| 329 | return nil |
| 330 | } |
| 331 | |
| 332 | func (s *Session) addHandler(h CommandHandler, c *readline.PrefixCompleter) { |
| 333 | h.Completer = c |
| 334 | s.CoreHandlers = append(s.CoreHandlers, h) |
| 335 | } |
| 336 | |
| 337 | func (s *Session) registerCoreHandlers() { |
| 338 | s.addHandler(NewCommandHandler("help MODULE", |
| 339 | "^(help|\\?)(.*)$", |
| 340 | "List available commands or show module specific help if no module name is provided.", |
| 341 | s.helpHandler), |
| 342 | readline.PcItem("help", readline.PcItemDynamic(func(prefix string) []string { |
| 343 | prefix = str.Trim(prefix[4:]) |
| 344 | modNames := []string{""} |
| 345 | for _, m := range s.Modules { |
| 346 | if prefix == "" || strings.HasPrefix(m.Name(), prefix) { |
| 347 | modNames = append(modNames, m.Name()) |
| 348 | } |
| 349 | } |
| 350 | return modNames |
| 351 | }))) |
| 352 | |
| 353 | s.addHandler(NewCommandHandler("active", |
| 354 | "^active$", |
| 355 | "Show information about active modules.", |
| 356 | s.activeHandler), |
| 357 | readline.PcItem("active")) |
| 358 | |
| 359 | s.addHandler(NewCommandHandler("quit", |
| 360 | "^(q|quit|e|exit)$", |
| 361 | "Close the session and exit.", |
| 362 | s.exitHandler), |
| 363 | readline.PcItem("quit")) |
| 364 | |
| 365 | s.addHandler(NewCommandHandler("sleep SECONDS", |
| 366 | "^sleep\\s+(\\d+)$", |
| 367 | "Sleep for the given amount of seconds.", |
| 368 | s.sleepHandler), |
| 369 | readline.PcItem("sleep")) |
| 370 | |
| 371 | s.addHandler(NewCommandHandler("get NAME", |
| 372 | "^get\\s+(.+)", |
| 373 | "Get the value of variable NAME, use * alone for all, or NAME* as a wildcard.", |
| 374 | s.getHandler), |
| 375 | readline.PcItem("get", readline.PcItemDynamic(func(prefix string) []string { |
| 376 | prefix = str.Trim(prefix[3:]) |
| 377 | varNames := []string{""} |
| 378 | for key := range s.Env.Data { |
| 379 | if prefix == "" || strings.HasPrefix(key, prefix) { |
| 380 | varNames = append(varNames, key) |
| 381 | } |
| 382 | } |
| 383 | return varNames |
| 384 | }))) |
| 385 | |
| 386 | s.addHandler(NewCommandHandler("set NAME VALUE", |
| 387 | "^set\\s+([^\\s]+)\\s+(.+)", |
| 388 | "Set the VALUE of variable NAME.", |
| 389 | s.setHandler), |
| 390 | readline.PcItem("set", readline.PcItemDynamic(func(prefix string) []string { |
| 391 | prefix = str.Trim(prefix[3:]) |
| 392 | varNames := []string{""} |
| 393 | for key := range s.Env.Data { |
| 394 | if prefix == "" || strings.HasPrefix(key, prefix) { |
| 395 | varNames = append(varNames, key) |
| 396 | } |
| 397 | } |
| 398 | return varNames |
| 399 | }))) |
| 400 | |
| 401 | s.addHandler(NewCommandHandler("read VARIABLE PROMPT", |
| 402 | `^read\s+([^\s]+)\s+(.+)$`, |
| 403 | "Show a PROMPT to ask the user for input that will be saved inside VARIABLE.", |
| 404 | s.readHandler), |
| 405 | readline.PcItem("read")) |
| 406 | |
| 407 | s.addHandler(NewCommandHandler("clear", |
| 408 | "^(clear|cls)$", |
| 409 | "Clear the screen.", |
| 410 | s.clsHandler), |
| 411 | readline.PcItem("clear")) |
| 412 | |
| 413 | s.addHandler(NewCommandHandler("include CAPLET", |
| 414 | "^include\\s+(.+)", |
| 415 | "Load and run this caplet in the current session.", |
| 416 | s.includeHandler), |
| 417 | readline.PcItem("include", readline.PcItemDynamic(func(prefix string) []string { |
| 418 | prefix = str.Trim(prefix[8:]) |
| 419 | if prefix == "" { |
| 420 | prefix = "." |
| 421 | } |
| 422 | |
| 423 | files, _ := filepath.Glob(prefix + "*") |
| 424 | return files |
| 425 | }))) |
| 426 | |
| 427 | s.addHandler(NewCommandHandler("! COMMAND", |
| 428 | "^!\\s*(.+)$", |
| 429 | "Execute a shell command and print its output.", |
| 430 | s.shHandler), |
| 431 | readline.PcItem("!")) |
| 432 | |
| 433 | s.addHandler(NewCommandHandler("alias MAC NAME", |
| 434 | "^alias\\s+([a-fA-F0-9:]{14,17})\\s*(.*)", |
| 435 | "Assign an alias to a given endpoint given its MAC address.", |
| 436 | s.aliasHandler), |
| 437 | readline.PcItem("alias", readline.PcItemDynamic(func(prefix string) []string { |
| 438 | prefix = str.Trim(prefix[5:]) |
| 439 | macs := []string{""} |
| 440 | s.Lan.EachHost(func(mac string, e *network.Endpoint) { |
| 441 | if prefix == "" || strings.HasPrefix(mac, prefix) { |
| 442 | macs = append(macs, mac) |
| 443 | } |
| 444 | }) |
| 445 | return macs |
| 446 | }))) |
| 447 | |
| 448 | } |