-
Notifications
You must be signed in to change notification settings - Fork 25
/
method-arp.go
132 lines (108 loc) · 2.49 KB
/
method-arp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bytes"
"net"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
const (
arpPeriod = 50 * time.Millisecond
arpScanPeriod = 10 * time.Second
)
type methodArp struct {
p *program
listen chan []byte
}
func newMethodArp(p *program) error {
ma := &methodArp{
p: p,
listen: make(chan []byte),
}
p.ma = ma
return nil
}
func (ma *methodArp) run() {
go ma.runListener()
if !ma.p.passiveMode {
go ma.runPeriodicRequests()
}
}
func (ma *methodArp) runListener() {
var decodedLayers []gopacket.LayerType
var eth layers.Ethernet
var arp layers.ARP
var padding gopacket.Payload
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet,
ð,
&arp,
&padding)
parse := func(raw []byte) {
if err := parser.DecodeLayers(raw, &decodedLayers); err != nil {
return
}
if arp.Protocol != layers.EthernetTypeIPv4 ||
arp.HwAddressSize != 6 ||
arp.ProtAddressSize != 4 {
return
}
if bytes.Equal(arp.SourceProtAddress, []byte{0, 0, 0, 0}) {
return
}
srcMac := copyMac(arp.SourceHwAddress)
srcIP := copyIP(arp.SourceProtAddress)
// ethernet mac and arp mac must correspond
if !bytes.Equal(arp.SourceHwAddress, eth.SrcMAC) {
return
}
ma.p.arp <- arpReq{
srcMac: srcMac,
srcIP: srcIP,
}
}
for raw := range ma.listen {
parse(raw)
ma.p.ls.listenDone <- struct{}{}
}
}
func (ma *methodArp) runPeriodicRequests() {
eth := layers.Ethernet{
SrcMAC: ma.p.intf.HardwareAddr,
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
EthernetType: layers.EthernetTypeARP,
}
arp := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: layers.ARPRequest,
SourceHwAddress: ma.p.intf.HardwareAddr,
SourceProtAddress: ma.p.ownIP,
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
for {
ips, err := randAvailableIPs(ma.p.ownIP)
if err != nil {
panic(err)
}
for _, dstAddr := range ips {
arp.DstProtAddress = dstAddr
if err := gopacket.SerializeLayers(buf, opts, ð, &arp); err != nil {
panic(err)
}
err := ma.p.ls.socket.Write(buf.Bytes())
if err != nil {
panic(err)
}
// more results if there's a minimum delay between arps
time.Sleep(arpPeriod)
}
time.Sleep(arpScanPeriod)
}
}