arp-spoofer: Use netip package
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2023-02-19 23:33:26 +01:00
parent edb39c7b33
commit 4cf006c4f8
2 changed files with 13 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"math/rand" "math/rand"
"net" "net"
"net/netip"
"strings" "strings"
"time" "time"
@ -15,7 +16,7 @@ import (
var ARPTable string = "/proc/net/arp" var ARPTable string = "/proc/net/arp"
type ARPEntry struct { type ARPEntry struct {
IP net.IP IP netip.Addr
HWType int HWType int
Flags int Flags int
HWAddress net.HardwareAddr HWAddress net.HardwareAddr
@ -41,7 +42,9 @@ func ARPAnalyze() (ents []ARPEntry, err error) {
continue continue
} }
e.IP = net.ParseIP(f[0]) if e.IP, err = netip.ParseAddr(f[0]); err != nil {
continue
}
if e.HWAddress, err = net.ParseMAC(f[3]); err != nil { if e.HWAddress, err = net.ParseMAC(f[3]); err != nil {
continue continue
} }
@ -55,9 +58,9 @@ func ARPAnalyze() (ents []ARPEntry, err error) {
return return
} }
func ARPContainsIP(ents []ARPEntry, ip net.IP) *ARPEntry { func ARPContainsIP(ents []ARPEntry, ip netip.Addr) *ARPEntry {
for i, e := range ents { for i, e := range ents {
if e.IP.Equal(ip) && (e.Flags == 2 || e.Flags == 6) { if e.IP.Compare(ip) == 0 {
return &ents[i] return &ents[i]
} }
} }
@ -65,7 +68,7 @@ func ARPContainsIP(ents []ARPEntry, ip net.IP) *ARPEntry {
return nil return nil
} }
func ARPSpoof(iface *net.Interface, ipS net.IP) { func ARPSpoof(iface *net.Interface, ipS netip.Addr) {
clt, err := arp.Dial(iface) clt, err := arp.Dial(iface)
if err != nil { if err != nil {
log.Println("Unable to initiate ARP spoofing:", err) log.Println("Unable to initiate ARP spoofing:", err)
@ -83,7 +86,7 @@ func ARPSpoof(iface *net.Interface, ipS net.IP) {
randHW := net.HardwareAddr{0, byte(rand.Intn(255)), byte(rand.Intn(255)), byte(rand.Intn(255)), byte(rand.Intn(255)), byte(rand.Intn(255))} 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 { for _, e := range ents {
if e.IP[0] == ipS[0] && e.IP[1] == ipS[1] { if e.IP.As4()[0] == ipS.As4()[0] && e.IP.As4()[1] == ipS.As4()[1] {
req := &arp.Packet{ req := &arp.Packet{
SenderHardwareAddr: e.HWAddress, SenderHardwareAddr: e.HWAddress,
SenderIP: e.IP, SenderIP: e.IP,

View File

@ -4,6 +4,7 @@ import (
"flag" "flag"
"log" "log"
"net" "net"
"net/netip"
) )
func main() { func main() {
@ -16,9 +17,9 @@ func main() {
log.Fatal("Unable to find interface to do ARP spoof:", err) log.Fatal("Unable to find interface to do ARP spoof:", err)
} }
ipS := net.ParseIP(*ipspoof) ipS, err := netip.ParseAddr(*ipspoof)
if ipS == nil { if err != nil {
log.Fatal("No IP given to ARP spoof. Skipping it") log.Fatalf("No IP given to ARP spoof (%s). Skipping it", err.Error())
} }
// Start ARP spoofing // Start ARP spoofing