| 1 | package net_recon |
| 2 | |
| 3 | import ( |
| 4 | "github.com/bettercap/bettercap/modules/utils" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/bettercap/bettercap/network" |
| 8 | "github.com/bettercap/bettercap/session" |
| 9 | ) |
| 10 | |
| 11 | type Discovery struct { |
| 12 | session.SessionModule |
| 13 | selector *utils.ViewSelector |
| 14 | } |
| 15 | |
| 16 | func NewDiscovery(s *session.Session) *Discovery { |
| 17 | mod := &Discovery{ |
| 18 | SessionModule: session.NewSessionModule("net.recon", s), |
| 19 | } |
| 20 | |
| 21 | mod.AddHandler(session.NewModuleHandler("net.recon on", "", |
| 22 | "Start network hosts discovery.", |
| 23 | func(args []string) error { |
| 24 | return mod.Start() |
| 25 | })) |
| 26 | |
| 27 | mod.AddHandler(session.NewModuleHandler("net.recon off", "", |
| 28 | "Stop network hosts discovery.", |
| 29 | func(args []string) error { |
| 30 | return mod.Stop() |
| 31 | })) |
| 32 | |
| 33 | mod.AddHandler(session.NewModuleHandler("net.clear", "", |
| 34 | "Clear all endpoints collected by the hosts discovery module.", |
| 35 | func(args []string) error { |
| 36 | mod.Session.Lan.Clear() |
| 37 | return nil |
| 38 | })) |
| 39 | |
| 40 | mod.AddParam(session.NewBoolParameter("net.show.meta", |
| 41 | "false", |
| 42 | "If true, the net.show command will show all metadata collected about each endpoint.")) |
| 43 | |
| 44 | mod.AddHandler(session.NewModuleHandler("net.show", "", |
| 45 | "Show cache hosts list (default sorting by ip).", |
| 46 | func(args []string) error { |
| 47 | return mod.Show("") |
| 48 | })) |
| 49 | |
| 50 | mod.AddHandler(session.NewModuleHandler("net.show ADDRESS1, ADDRESS2", `net.show (.+)`, |
| 51 | "Show information about a specific comma separated list of addresses (by IP or MAC).", |
| 52 | func(args []string) error { |
| 53 | return mod.Show(args[0]) |
| 54 | })) |
| 55 | |
| 56 | mod.AddHandler(session.NewModuleHandler("net.show.meta ADDRESS1, ADDRESS2", `net\.show\.meta (.+)`, |
| 57 | "Show meta information about a specific comma separated list of addresses (by IP or MAC).", |
| 58 | func(args []string) error { |
| 59 | return mod.showMeta(args[0]) |
| 60 | })) |
| 61 | |
| 62 | mod.selector = utils.ViewSelectorFor(&mod.SessionModule, "net.show", []string{"ip", "mac", "seen", "sent", "rcvd"}, |
| 63 | "ip asc") |
| 64 | |
| 65 | return mod |
| 66 | } |
| 67 | |
| 68 | func (mod Discovery) Name() string { |
| 69 | return "net.recon" |
| 70 | } |
| 71 | |
| 72 | func (mod Discovery) Description() string { |
| 73 | return "Read periodically the ARP cache in order to monitor for new hosts on the network." |
| 74 | } |
| 75 | |
| 76 | func (mod Discovery) Author() string { |
| 77 | return "Simone Margaritelli <[email protected]>" |
| 78 | } |
| 79 | |
| 80 | func (mod *Discovery) runDiff(cache network.ArpTable) { |
| 81 | // check for endpoints who disappeared |
| 82 | var rem network.ArpTable = make(network.ArpTable) |
| 83 | |
| 84 | mod.Session.Lan.EachHost(func(mac string, e *network.Endpoint) { |
| 85 | if _, found := cache[mac]; !found { |
| 86 | rem[mac] = e.IpAddress |
| 87 | } |
| 88 | }) |
| 89 | |
| 90 | for mac, ip := range rem { |
| 91 | mod.Session.Lan.Remove(ip, mac) |
| 92 | } |
| 93 | |
| 94 | // now check for new friends ^_^ |
| 95 | for ip, mac := range cache { |
| 96 | mod.Session.Lan.AddIfNew(ip, mac) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func (mod *Discovery) Configure() error { |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | func (mod *Discovery) Start() error { |
| 105 | if err := mod.Configure(); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | return mod.SetRunning(true, func() { |
| 110 | every := time.Duration(1) * time.Second |
| 111 | iface := mod.Session.Interface.Name() |
| 112 | for mod.Running() { |
| 113 | if table, err := network.ArpUpdate(iface); err != nil { |
| 114 | mod.Error("%s", err) |
| 115 | } else { |
| 116 | mod.runDiff(table) |
| 117 | } |
| 118 | time.Sleep(every) |
| 119 | } |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | func (mod *Discovery) Stop() error { |
| 124 | return mod.SetRunning(false, nil) |
| 125 | } |