| 1 | package net_probe |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/bettercap/bettercap/network" |
| 8 | "github.com/bettercap/bettercap/session" |
| 9 | |
| 10 | "github.com/malfunkt/iprange" |
| 11 | ) |
| 12 | |
| 13 | type Probes struct { |
| 14 | NBNS bool |
| 15 | MDNS bool |
| 16 | UPNP bool |
| 17 | WSD bool |
| 18 | } |
| 19 | |
| 20 | type Prober struct { |
| 21 | session.SessionModule |
| 22 | throttle int |
| 23 | probes Probes |
| 24 | waitGroup *sync.WaitGroup |
| 25 | } |
| 26 | |
| 27 | func NewProber(s *session.Session) *Prober { |
| 28 | mod := &Prober{ |
| 29 | SessionModule: session.NewSessionModule("net.probe", s), |
| 30 | waitGroup: &sync.WaitGroup{}, |
| 31 | } |
| 32 | |
| 33 | mod.SessionModule.Requires("net.recon") |
| 34 | |
| 35 | mod.AddParam(session.NewBoolParameter("net.probe.nbns", |
| 36 | "true", |
| 37 | "Enable NetBIOS name service discovery probes.")) |
| 38 | |
| 39 | mod.AddParam(session.NewBoolParameter("net.probe.mdns", |
| 40 | "true", |
| 41 | "Enable mDNS discovery probes.")) |
| 42 | |
| 43 | mod.AddParam(session.NewBoolParameter("net.probe.upnp", |
| 44 | "true", |
| 45 | "Enable UPNP discovery probes.")) |
| 46 | |
| 47 | mod.AddParam(session.NewBoolParameter("net.probe.wsd", |
| 48 | "true", |
| 49 | "Enable WSD discovery probes.")) |
| 50 | |
| 51 | mod.AddParam(session.NewIntParameter("net.probe.throttle", |
| 52 | "10", |
| 53 | "If greater than 0, probe packets will be throttled by this value in milliseconds.")) |
| 54 | |
| 55 | mod.AddHandler(session.NewModuleHandler("net.probe on", "", |
| 56 | "Start network hosts probing in background.", |
| 57 | func(args []string) error { |
| 58 | return mod.Start() |
| 59 | })) |
| 60 | |
| 61 | mod.AddHandler(session.NewModuleHandler("net.probe off", "", |
| 62 | "Stop network hosts probing in background.", |
| 63 | func(args []string) error { |
| 64 | return mod.Stop() |
| 65 | })) |
| 66 | |
| 67 | return mod |
| 68 | } |
| 69 | |
| 70 | func (mod Prober) Name() string { |
| 71 | return "net.probe" |
| 72 | } |
| 73 | |
| 74 | func (mod Prober) Description() string { |
| 75 | return "Keep probing for new hosts on the network by sending dummy UDP packets to every possible IP on the subnet." |
| 76 | } |
| 77 | |
| 78 | func (mod Prober) Author() string { |
| 79 | return "Simone Margaritelli <[email protected]>" |
| 80 | } |
| 81 | |
| 82 | func (mod *Prober) Configure() error { |
| 83 | var err error |
| 84 | if err, mod.throttle = mod.IntParam("net.probe.throttle"); err != nil { |
| 85 | return err |
| 86 | } else if err, mod.probes.NBNS = mod.BoolParam("net.probe.nbns"); err != nil { |
| 87 | return err |
| 88 | } else if err, mod.probes.MDNS = mod.BoolParam("net.probe.mdns"); err != nil { |
| 89 | return err |
| 90 | } else if err, mod.probes.UPNP = mod.BoolParam("net.probe.upnp"); err != nil { |
| 91 | return err |
| 92 | } else if err, mod.probes.WSD = mod.BoolParam("net.probe.wsd"); err != nil { |
| 93 | return err |
| 94 | } else { |
| 95 | mod.Debug("Throttling packets of %d ms.", mod.throttle) |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func (mod *Prober) Start() error { |
| 101 | if err := mod.Configure(); err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | return mod.SetRunning(true, func() { |
| 106 | mod.waitGroup.Add(1) |
| 107 | defer mod.waitGroup.Done() |
| 108 | |
| 109 | if mod.Session.Interface.IpAddress == network.MonitorModeAddress { |
| 110 | mod.Info("Interface is in monitor mode, skipping net.probe") |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | cidr := mod.Session.Interface.CIDR() |
| 115 | list, err := iprange.Parse(cidr) |
| 116 | if err != nil { |
| 117 | mod.Fatal("%s", err) |
| 118 | } |
| 119 | |
| 120 | if mod.probes.MDNS { |
| 121 | go mod.mdnsProber() |
| 122 | } |
| 123 | |
| 124 | fromIP := mod.Session.Interface.IP |
| 125 | fromHW := mod.Session.Interface.HW |
| 126 | addresses := list.Expand() |
| 127 | throttle := time.Duration(mod.throttle) * time.Millisecond |
| 128 | |
| 129 | mod.Info("probing %d addresses on %s", len(addresses), cidr) |
| 130 | |
| 131 | for mod.Running() { |
| 132 | if mod.probes.MDNS { |
| 133 | mod.sendProbeMDNS(fromIP, fromHW) |
| 134 | } |
| 135 | |
| 136 | if mod.probes.UPNP { |
| 137 | mod.sendProbeUPNP(fromIP, fromHW) |
| 138 | } |
| 139 | |
| 140 | if mod.probes.WSD { |
| 141 | mod.sendProbeWSD(fromIP, fromHW) |
| 142 | } |
| 143 | |
| 144 | for _, ip := range addresses { |
| 145 | if !mod.Running() { |
| 146 | return |
| 147 | } else if mod.Session.Skip(ip) { |
| 148 | mod.Debug("skipping address %s from probing.", ip) |
| 149 | continue |
| 150 | } else if mod.probes.NBNS { |
| 151 | mod.sendProbeNBNS(fromIP, fromHW, ip) |
| 152 | } |
| 153 | time.Sleep(throttle) |
| 154 | } |
| 155 | |
| 156 | time.Sleep(5 * time.Second) |
| 157 | } |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | func (mod *Prober) Stop() error { |
| 162 | return mod.SetRunning(false, func() { |
| 163 | mod.waitGroup.Wait() |
| 164 | }) |
| 165 | } |