mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 05:53:20 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
bf3671465b
commit
4eead7eafa
58 changed files with 2052 additions and 2052 deletions
|
@ -26,7 +26,7 @@ type ArpSpoofer struct {
|
|||
}
|
||||
|
||||
func NewArpSpoofer(s *session.Session) *ArpSpoofer {
|
||||
p := &ArpSpoofer{
|
||||
mod := &ArpSpoofer{
|
||||
SessionModule: session.NewSessionModule("arp.spoof", s),
|
||||
addresses: make([]net.IP, 0),
|
||||
macs: make([]net.HardwareAddr, 0),
|
||||
|
@ -38,123 +38,123 @@ func NewArpSpoofer(s *session.Session) *ArpSpoofer {
|
|||
waitGroup: &sync.WaitGroup{},
|
||||
}
|
||||
|
||||
p.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."))
|
||||
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."))
|
||||
|
||||
p.AddParam(session.NewStringParameter("arp.spoof.whitelist", "", "", "Comma separated list of IP addresses, MAC addresses or aliases to skip while spoofing."))
|
||||
mod.AddParam(session.NewStringParameter("arp.spoof.whitelist", "", "", "Comma separated list of IP addresses, MAC addresses or aliases to skip while spoofing."))
|
||||
|
||||
p.AddParam(session.NewBoolParameter("arp.spoof.internal",
|
||||
mod.AddParam(session.NewBoolParameter("arp.spoof.internal",
|
||||
"false",
|
||||
"If true, local connections among computers of the network will be spoofed, otherwise only connections going to and coming from the external network."))
|
||||
|
||||
p.AddParam(session.NewBoolParameter("arp.spoof.fullduplex",
|
||||
mod.AddParam(session.NewBoolParameter("arp.spoof.fullduplex",
|
||||
"false",
|
||||
"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)."))
|
||||
|
||||
p.AddHandler(session.NewModuleHandler("arp.spoof on", "",
|
||||
mod.AddHandler(session.NewModuleHandler("arp.spoof on", "",
|
||||
"Start ARP spoofer.",
|
||||
func(args []string) error {
|
||||
return p.Start()
|
||||
return mod.Start()
|
||||
}))
|
||||
|
||||
p.AddHandler(session.NewModuleHandler("arp.ban on", "",
|
||||
mod.AddHandler(session.NewModuleHandler("arp.ban on", "",
|
||||
"Start ARP spoofer in ban mode, meaning the target(s) connectivity will not work.",
|
||||
func(args []string) error {
|
||||
p.ban = true
|
||||
return p.Start()
|
||||
mod.ban = true
|
||||
return mod.Start()
|
||||
}))
|
||||
|
||||
p.AddHandler(session.NewModuleHandler("arp.spoof off", "",
|
||||
mod.AddHandler(session.NewModuleHandler("arp.spoof off", "",
|
||||
"Stop ARP spoofer.",
|
||||
func(args []string) error {
|
||||
return p.Stop()
|
||||
return mod.Stop()
|
||||
}))
|
||||
|
||||
p.AddHandler(session.NewModuleHandler("arp.ban off", "",
|
||||
mod.AddHandler(session.NewModuleHandler("arp.ban off", "",
|
||||
"Stop ARP spoofer.",
|
||||
func(args []string) error {
|
||||
return p.Stop()
|
||||
return mod.Stop()
|
||||
}))
|
||||
|
||||
return p
|
||||
return mod
|
||||
}
|
||||
|
||||
func (p ArpSpoofer) Name() string {
|
||||
func (mod ArpSpoofer) Name() string {
|
||||
return "arp.spoof"
|
||||
}
|
||||
|
||||
func (p ArpSpoofer) Description() string {
|
||||
func (mod ArpSpoofer) Description() string {
|
||||
return "Keep spoofing selected hosts on the network."
|
||||
}
|
||||
|
||||
func (p ArpSpoofer) Author() string {
|
||||
func (mod ArpSpoofer) Author() string {
|
||||
return "Simone Margaritelli <evilsocket@gmail.com>"
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) Configure() error {
|
||||
func (mod *ArpSpoofer) Configure() error {
|
||||
var err error
|
||||
var targets string
|
||||
var whitelist string
|
||||
|
||||
if err, p.fullDuplex = p.BoolParam("arp.spoof.fullduplex"); err != nil {
|
||||
if err, mod.fullDuplex = mod.BoolParam("arp.spoof.fullduplex"); err != nil {
|
||||
return err
|
||||
} else if err, p.internal = p.BoolParam("arp.spoof.internal"); err != nil {
|
||||
} else if err, mod.internal = mod.BoolParam("arp.spoof.internal"); err != nil {
|
||||
return err
|
||||
} else if err, targets = p.StringParam("arp.spoof.targets"); err != nil {
|
||||
} else if err, targets = mod.StringParam("arp.spoof.targets"); err != nil {
|
||||
return err
|
||||
} else if err, whitelist = p.StringParam("arp.spoof.whitelist"); err != nil {
|
||||
} else if err, whitelist = mod.StringParam("arp.spoof.whitelist"); err != nil {
|
||||
return err
|
||||
} else if p.addresses, p.macs, err = network.ParseTargets(targets, p.Session.Lan.Aliases()); err != nil {
|
||||
} else if mod.addresses, mod.macs, err = network.ParseTargets(targets, mod.Session.Lan.Aliases()); err != nil {
|
||||
return err
|
||||
} else if p.wAddresses, p.wMacs, err = network.ParseTargets(whitelist, p.Session.Lan.Aliases()); err != nil {
|
||||
} else if mod.wAddresses, mod.wMacs, err = network.ParseTargets(whitelist, mod.Session.Lan.Aliases()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.Debug(" addresses=%v macs=%v whitelisted-addresses=%v whitelisted-macs=%v", p.addresses, p.macs, p.wAddresses, p.wMacs)
|
||||
mod.Debug(" addresses=%v macs=%v whitelisted-addresses=%v whitelisted-macs=%v", mod.addresses, mod.macs, mod.wAddresses, mod.wMacs)
|
||||
|
||||
if p.ban {
|
||||
p.Warning("running in ban mode, forwarding not enabled!")
|
||||
p.Session.Firewall.EnableForwarding(false)
|
||||
} else if !p.Session.Firewall.IsForwardingEnabled() {
|
||||
p.Info("enabling forwarding")
|
||||
p.Session.Firewall.EnableForwarding(true)
|
||||
if mod.ban {
|
||||
mod.Warning("running in ban mode, forwarding not enabled!")
|
||||
mod.Session.Firewall.EnableForwarding(false)
|
||||
} else if !mod.Session.Firewall.IsForwardingEnabled() {
|
||||
mod.Info("enabling forwarding")
|
||||
mod.Session.Firewall.EnableForwarding(true)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) Start() error {
|
||||
if err := p.Configure(); err != nil {
|
||||
func (mod *ArpSpoofer) Start() error {
|
||||
if err := mod.Configure(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return p.SetRunning(true, func() {
|
||||
return mod.SetRunning(true, func() {
|
||||
neighbours := []net.IP{}
|
||||
nTargets := len(p.addresses) + len(p.macs)
|
||||
nTargets := len(mod.addresses) + len(mod.macs)
|
||||
|
||||
if p.internal {
|
||||
list, _ := iprange.ParseList(p.Session.Interface.CIDR())
|
||||
if mod.internal {
|
||||
list, _ := iprange.ParseList(mod.Session.Interface.CIDR())
|
||||
neighbours = list.Expand()
|
||||
nNeigh := len(neighbours) - 2
|
||||
|
||||
p.Warning("arp spoofer started targeting %d possible network neighbours of %d targets.", nNeigh, nTargets)
|
||||
mod.Warning("arp spoofer started targeting %d possible network neighbours of %d targets.", nNeigh, nTargets)
|
||||
} else {
|
||||
p.Info("arp spoofer started, probing %d targets.", nTargets)
|
||||
mod.Info("arp spoofer started, probing %d targets.", nTargets)
|
||||
}
|
||||
|
||||
if p.fullDuplex {
|
||||
p.Warning("full duplex spoofing enabled, if the router has ARP spoofing mechanisms, the attack will fail.")
|
||||
if mod.fullDuplex {
|
||||
mod.Warning("full duplex spoofing enabled, if the router has ARP spoofing mechanisms, the attack will fail.")
|
||||
}
|
||||
|
||||
p.waitGroup.Add(1)
|
||||
defer p.waitGroup.Done()
|
||||
mod.waitGroup.Add(1)
|
||||
defer mod.waitGroup.Done()
|
||||
|
||||
gwIP := p.Session.Gateway.IP
|
||||
myMAC := p.Session.Interface.HW
|
||||
for p.Running() {
|
||||
p.arpSpoofTargets(gwIP, myMAC, true, false)
|
||||
gwIP := mod.Session.Gateway.IP
|
||||
myMAC := mod.Session.Interface.HW
|
||||
for mod.Running() {
|
||||
mod.arpSpoofTargets(gwIP, myMAC, true, false)
|
||||
for _, address := range neighbours {
|
||||
if !p.Session.Skip(address) {
|
||||
p.arpSpoofTargets(address, myMAC, true, false)
|
||||
if !mod.Session.Skip(address) {
|
||||
mod.arpSpoofTargets(address, myMAC, true, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -163,18 +163,18 @@ func (p *ArpSpoofer) Start() error {
|
|||
})
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) unSpoof() error {
|
||||
nTargets := len(p.addresses) + len(p.macs)
|
||||
p.Info("restoring ARP cache of %d targets.", nTargets)
|
||||
p.arpSpoofTargets(p.Session.Gateway.IP, p.Session.Gateway.HW, false, false)
|
||||
func (mod *ArpSpoofer) unSpoof() error {
|
||||
nTargets := len(mod.addresses) + len(mod.macs)
|
||||
mod.Info("restoring ARP cache of %d targets.", nTargets)
|
||||
mod.arpSpoofTargets(mod.Session.Gateway.IP, mod.Session.Gateway.HW, false, false)
|
||||
|
||||
if p.internal {
|
||||
list, _ := iprange.ParseList(p.Session.Interface.CIDR())
|
||||
if mod.internal {
|
||||
list, _ := iprange.ParseList(mod.Session.Interface.CIDR())
|
||||
neighbours := list.Expand()
|
||||
for _, address := range neighbours {
|
||||
if !p.Session.Skip(address) {
|
||||
if realMAC, err := p.Session.FindMAC(address, false); err == nil {
|
||||
p.arpSpoofTargets(address, realMAC, false, false)
|
||||
if !mod.Session.Skip(address) {
|
||||
if realMAC, err := mod.Session.FindMAC(address, false); err == nil {
|
||||
mod.arpSpoofTargets(address, realMAC, false, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -183,23 +183,23 @@ func (p *ArpSpoofer) unSpoof() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) Stop() error {
|
||||
return p.SetRunning(false, func() {
|
||||
p.Info("waiting for ARP spoofer to stop ...")
|
||||
p.unSpoof()
|
||||
p.ban = false
|
||||
p.waitGroup.Wait()
|
||||
func (mod *ArpSpoofer) Stop() error {
|
||||
return mod.SetRunning(false, func() {
|
||||
mod.Info("waiting for ARP spoofer to stop ...")
|
||||
mod.unSpoof()
|
||||
mod.ban = false
|
||||
mod.waitGroup.Wait()
|
||||
})
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) isWhitelisted(ip string, mac net.HardwareAddr) bool {
|
||||
for _, addr := range p.wAddresses {
|
||||
func (mod *ArpSpoofer) isWhitelisted(ip string, mac net.HardwareAddr) bool {
|
||||
for _, addr := range mod.wAddresses {
|
||||
if ip == addr.String() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
for _, hw := range p.wMacs {
|
||||
for _, hw := range mod.wMacs {
|
||||
if bytes.Equal(hw, mac) {
|
||||
return true
|
||||
}
|
||||
|
@ -208,28 +208,28 @@ func (p *ArpSpoofer) isWhitelisted(ip string, mac net.HardwareAddr) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) getTargets(probe bool) map[string]net.HardwareAddr {
|
||||
func (mod *ArpSpoofer) getTargets(probe bool) map[string]net.HardwareAddr {
|
||||
targets := make(map[string]net.HardwareAddr)
|
||||
|
||||
// add targets specified by IP address
|
||||
for _, ip := range p.addresses {
|
||||
if p.Session.Skip(ip) {
|
||||
p.Debug("skipping IP %s from arp spoofing.", ip)
|
||||
for _, ip := range mod.addresses {
|
||||
if mod.Session.Skip(ip) {
|
||||
mod.Debug("skipping IP %s from arp spoofing.", ip)
|
||||
continue
|
||||
}
|
||||
// do we have this ip mac address?
|
||||
if hw, err := p.Session.FindMAC(ip, probe); err != nil {
|
||||
p.Debug("could not find hardware address for %s", ip.String())
|
||||
if hw, err := mod.Session.FindMAC(ip, probe); err != nil {
|
||||
mod.Debug("could not find hardware address for %s", ip.String())
|
||||
} else {
|
||||
targets[ip.String()] = hw
|
||||
}
|
||||
}
|
||||
// add targets specified by MAC address
|
||||
for _, hw := range p.macs {
|
||||
if ip, err := network.ArpInverseLookup(p.Session.Interface.Name(), hw.String(), false); err != nil {
|
||||
p.Warning("could not find IP address for %s", hw.String())
|
||||
} else if p.Session.Skip(net.ParseIP(ip)) {
|
||||
p.Debug("skipping address %s from arp spoofing.", ip)
|
||||
for _, hw := range mod.macs {
|
||||
if ip, err := network.ArpInverseLookup(mod.Session.Interface.Name(), hw.String(), false); err != nil {
|
||||
mod.Warning("could not find IP address for %s", hw.String())
|
||||
} else if mod.Session.Skip(net.ParseIP(ip)) {
|
||||
mod.Debug("skipping address %s from arp spoofing.", ip)
|
||||
} else {
|
||||
targets[ip] = hw
|
||||
}
|
||||
|
@ -238,13 +238,13 @@ func (p *ArpSpoofer) getTargets(probe bool) map[string]net.HardwareAddr {
|
|||
return targets
|
||||
}
|
||||
|
||||
func (p *ArpSpoofer) arpSpoofTargets(saddr net.IP, smac net.HardwareAddr, check_running bool, probe bool) {
|
||||
p.waitGroup.Add(1)
|
||||
defer p.waitGroup.Done()
|
||||
func (mod *ArpSpoofer) arpSpoofTargets(saddr net.IP, smac net.HardwareAddr, check_running bool, probe bool) {
|
||||
mod.waitGroup.Add(1)
|
||||
defer mod.waitGroup.Done()
|
||||
|
||||
gwIP := p.Session.Gateway.IP
|
||||
gwHW := p.Session.Gateway.HW
|
||||
ourHW := p.Session.Interface.HW
|
||||
gwIP := mod.Session.Gateway.IP
|
||||
gwHW := mod.Session.Gateway.HW
|
||||
ourHW := mod.Session.Interface.HW
|
||||
isGW := false
|
||||
isSpoofing := false
|
||||
|
||||
|
@ -257,11 +257,11 @@ func (p *ArpSpoofer) arpSpoofTargets(saddr net.IP, smac net.HardwareAddr, check_
|
|||
}
|
||||
}
|
||||
|
||||
for ip, mac := range p.getTargets(probe) {
|
||||
if check_running && !p.Running() {
|
||||
for ip, mac := range mod.getTargets(probe) {
|
||||
if check_running && !mod.Running() {
|
||||
return
|
||||
} else if p.isWhitelisted(ip, mac) {
|
||||
p.Debug("%s (%s) is whitelisted, skipping from spoofing loop.", ip, mac)
|
||||
} else if mod.isWhitelisted(ip, mac) {
|
||||
mod.Debug("%s (%s) is whitelisted, skipping from spoofing loop.", ip, mac)
|
||||
continue
|
||||
} else if saddr.String() == ip {
|
||||
continue
|
||||
|
@ -269,34 +269,34 @@ func (p *ArpSpoofer) arpSpoofTargets(saddr net.IP, smac net.HardwareAddr, check_
|
|||
|
||||
rawIP := net.ParseIP(ip)
|
||||
if err, pkt := packets.NewARPReply(saddr, smac, rawIP, mac); err != nil {
|
||||
p.Error("error while creating ARP spoof packet for %s: %s", ip, err)
|
||||
mod.Error("error while creating ARP spoof packet for %s: %s", ip, err)
|
||||
} else {
|
||||
p.Debug("sending %d bytes of ARP packet to %s:%s.", len(pkt), ip, mac.String())
|
||||
p.Session.Queue.Send(pkt)
|
||||
mod.Debug("sending %d bytes of ARP packet to %s:%s.", len(pkt), ip, mac.String())
|
||||
mod.Session.Queue.Send(pkt)
|
||||
}
|
||||
|
||||
if p.fullDuplex && isGW {
|
||||
if mod.fullDuplex && isGW {
|
||||
err := error(nil)
|
||||
gwPacket := []byte(nil)
|
||||
|
||||
if isSpoofing {
|
||||
p.Debug("telling the gw we are %s", ip)
|
||||
mod.Debug("telling the gw we are %s", ip)
|
||||
// we told the target we're te gateway, not let's tell the
|
||||
// gateway that we are the target
|
||||
if err, gwPacket = packets.NewARPReply(rawIP, ourHW, gwIP, gwHW); err != nil {
|
||||
p.Error("error while creating ARP spoof packet: %s", err)
|
||||
mod.Error("error while creating ARP spoof packet: %s", err)
|
||||
}
|
||||
} else {
|
||||
p.Debug("telling the gw %s is %s", ip, mac)
|
||||
mod.Debug("telling the gw %s is %s", ip, mac)
|
||||
// send the gateway the original MAC of the target
|
||||
if err, gwPacket = packets.NewARPReply(rawIP, mac, gwIP, gwHW); err != nil {
|
||||
p.Error("error while creating ARP spoof packet: %s", err)
|
||||
mod.Error("error while creating ARP spoof packet: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if gwPacket != nil {
|
||||
if err = p.Session.Queue.Send(gwPacket); err != nil {
|
||||
p.Error("error while sending packet: %v", err)
|
||||
if err = mod.Session.Queue.Send(gwPacket); err != nil {
|
||||
mod.Error("error while sending packet: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue