-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
386 lines (360 loc) · 9.76 KB
/
scanner.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/rand"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/mailgun/holster/v3/collections"
log "github.com/sirupsen/logrus"
)
const maxTimeout = 10 * time.Second
const tick = 10 * time.Millisecond
type Scanner struct {
iface *net.Interface
handle *pcap.Handle
ip6net *net.IPNet
ip4net *net.IPNet
arpTargets ip4targets
ndpTargets *ip6targets
cacheTTL time.Duration
timeout time.Duration
domainFormat string
revAliases map[string][]string
cache *collections.LRUCache
}
var ErrNoNetworksFound = errors.New("no networks found")
func NewScanner(ifaceName string, cacheTTL time.Duration, domain string, aliases map[string][]string, cache *collections.LRUCache) (Scanner, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return Scanner{}, err
}
addrs, err := iface.Addrs()
if err != nil {
return Scanner{}, err
}
var ip4net, ip6net *net.IPNet
for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ip4 := ipnet.IP.To4(); ip4 != nil {
ip4net = &net.IPNet{
IP: ip4,
Mask: ipnet.Mask[len(ipnet.Mask)-4:],
}
} else if ip6 := ipnet.IP.To16(); ip6 != nil {
ip6net = ipnet
}
}
if ip4net == nil && ip6net == nil {
return Scanner{}, ErrNoNetworksFound
}
handle, err := pcap.OpenLive(iface.Name, 65536, true, pcap.BlockForever)
if err != nil {
return Scanner{}, err
}
if linkType := handle.LinkType(); linkType != layers.LinkTypeEthernet {
return Scanner{}, fmt.Errorf("network interface %q has unsupported link type %q", iface.Name, linkType)
}
revAliases := make(map[string][]string, len(aliases))
for host, hwAddrs := range aliases {
for _, hwAddr := range hwAddrs {
revAliases[hwAddr] = append(revAliases[hwAddr], host)
}
}
timeout := 1000 * time.Millisecond
var (
arpTargets ip4targets
ndpTargets *ip6targets
)
if ip4net != nil {
arpTargets = makeip4targets(ip4net, timeout)
}
if ip6net != nil {
ndpTargets = makeip6targets(timeout)
}
return Scanner{
iface: iface,
handle: handle,
ip4net: ip4net,
ip6net: ip6net,
arpTargets: arpTargets,
ndpTargets: ndpTargets,
cacheTTL: cacheTTL,
timeout: timeout,
domainFormat: "%s." + domain + ".",
revAliases: revAliases,
cache: cache,
}, nil
}
func (scanner Scanner) Scan(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
errors := make(chan error, 4)
defer close(errors)
go func() {
defer wg.Done()
if err := scanner.reader(ctx); err != nil && err != context.DeadlineExceeded {
cancel()
errors <- err
}
}()
if scanner.ip4net != nil {
wg.Add(1)
go func() {
defer wg.Done()
if err := scanner.writerARP(ctx); err != nil && err != context.DeadlineExceeded {
cancel()
errors <- err
}
}()
}
if scanner.ip6net != nil {
wg.Add(2)
go func() {
defer wg.Done()
if err := scanner.writerNDP(ctx); err != nil && err != context.DeadlineExceeded {
cancel()
errors <- err
}
}()
go func() {
defer wg.Done()
if err := scanner.writerICMPPing6Multicast(ctx); err != nil && err != context.DeadlineExceeded {
cancel()
errors <- err
}
}()
}
wg.Wait()
select {
case err := <-errors:
return err
default:
return nil
}
}
func (scanner Scanner) reader(ctx context.Context) error {
var eth layers.Ethernet
var arp layers.ARP
var ipv6 layers.IPv6
var icmpv6 layers.ICMPv6
var icmpv6echo layers.ICMPv6Echo
var icmpv6ns layers.ICMPv6NeighborSolicitation
var icmpv6na layers.ICMPv6NeighborAdvertisement
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, ð, &arp, &ipv6, &icmpv6, &icmpv6echo, &icmpv6na, &icmpv6ns)
var decoded []gopacket.LayerType
for {
if err := ctx.Err(); err != nil {
return err
}
data, _, err := scanner.handle.ReadPacketData()
if err == io.EOF {
return nil
} else if err != nil {
log.Error("Error reading packet:", err)
continue
}
if err = parser.DecodeLayers(data, &decoded); err != nil {
continue
}
var (
targetIP net.IP
targetHwAddr net.HardwareAddr
)
for _, layerType := range decoded {
switch layerType {
case layers.LayerTypeARP:
if arp.Operation != layers.ARPReply {
continue
}
if bytes.Equal(scanner.iface.HardwareAddr, arp.SourceHwAddress) {
continue
}
targetIP = net.IP(arp.SourceProtAddress).To4()
targetHwAddr = net.HardwareAddr(arp.SourceHwAddress)
scanner.arpTargets.reset(ip4(targetIP))
case layers.LayerTypeICMPv6:
switch icmpv6.TypeCode.Type() {
case layers.ICMPv6TypeEchoReply:
if ipv6.SrcIP.Equal(scanner.ip6net.IP) {
continue
}
scanner.ndpTargets.add(ip6(ipv6.SrcIP.To16()))
case layers.ICMPv6TypeNeighborAdvertisement:
targetIP = icmpv6na.TargetAddress
for _, opt := range icmpv6na.Options {
if opt.Type == layers.ICMPv6OptTargetAddress {
targetHwAddr = net.HardwareAddr(opt.Data)
}
}
scanner.ndpTargets.reset(ip6(targetIP))
}
}
}
if targetIP == nil || targetHwAddr == nil {
continue
}
targetHwAddrStr := targetHwAddr.String()
var targetHwAddrKey string
if targetIP4 := targetIP.To4(); targetIP4 != nil {
targetHwAddrKey = "ipv4-" + targetHwAddrStr
} else if targetIP16 := targetIP.To16(); targetIP16 != nil {
targetHwAddrKey = "ipv6-" + targetHwAddrStr
} else {
panic(fmt.Sprintf("invalid ip address %v", targetIP))
}
if !scanner.cache.AddWithTTL(targetHwAddrKey, targetIP, scanner.cacheTTL) {
domain := fmt.Sprintf(scanner.domainFormat, strings.ReplaceAll(targetHwAddrStr, ":", "-"))
log := log.WithFields(log.Fields{
"ip": targetIP,
"domain": strings.TrimSuffix(domain, "."),
"key": targetHwAddrKey,
})
if aliases, ok := scanner.revAliases[domain]; ok {
for i, alias := range aliases {
key := "domalias"
if i > 0 {
key = key + strconv.Itoa(i)
}
log = log.WithField(key, strings.TrimSuffix(alias, "."))
}
}
log.Infof("Added to cache")
}
}
}
func (scanner Scanner) writerARP(ctx context.Context) error {
eth := layers.Ethernet{
SrcMAC: scanner.iface.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: []byte(scanner.iface.HardwareAddr),
SourceProtAddress: []byte(scanner.ip4net.IP),
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
return scanner.arpTargets.loop(ctx, func(ip4 ip4) error {
arp.DstProtAddress = ip4[:]
if err := gopacket.SerializeLayers(buf, opts, ð, &arp); err != nil {
return err
}
if err := scanner.handle.WritePacketData(buf.Bytes()); err != nil {
return err
}
return nil
})
}
func (scanner Scanner) writerNDP(ctx context.Context) error {
eth := layers.Ethernet{
SrcMAC: scanner.iface.HardwareAddr,
DstMAC: net.HardwareAddr{0x33, 0x33, 0x00, 0x00, 0x00, 0x00},
EthernetType: layers.EthernetTypeIPv6,
}
ipv6 := layers.IPv6{
Version: 6,
NextHeader: layers.IPProtocolICMPv6,
HopLimit: 255,
SrcIP: scanner.ip6net.IP,
DstIP: net.ParseIP("ff02:0000:0000:0000:0000:0001:ff00:0000"),
}
icmpv6 := layers.ICMPv6{
TypeCode: layers.CreateICMPv6TypeCode(layers.ICMPv6TypeNeighborSolicitation, 0),
}
icmpv6ns := layers.ICMPv6NeighborSolicitation{
Options: layers.ICMPv6Options{
layers.ICMPv6Option{Type: layers.ICMPv6OptSourceAddress, Data: []byte(scanner.iface.HardwareAddr)},
},
}
if err := icmpv6.SetNetworkLayerForChecksum(&ipv6); err != nil {
return err
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
return scanner.ndpTargets.loop(ctx, func(ip6 ip6) error {
targetIP := net.IP(ip6[:])
copy(ipv6.DstIP[13:], targetIP[len(targetIP)-3:])
copy(eth.DstMAC[2:], ipv6.DstIP[len(ipv6.DstIP)-4:])
icmpv6ns.TargetAddress = targetIP
if err := gopacket.SerializeLayers(buf, opts, ð, &ipv6, &icmpv6, &icmpv6ns); err != nil {
return err
}
if err := scanner.handle.WritePacketData(buf.Bytes()); err != nil {
return err
}
return nil
})
}
func (scanner Scanner) writerICMPPing6Multicast(ctx context.Context) error {
eth := layers.Ethernet{
SrcMAC: scanner.iface.HardwareAddr,
DstMAC: net.HardwareAddr{0x33, 0x33, 0x00, 0x00, 0x00, 0x01},
EthernetType: layers.EthernetTypeIPv6,
}
ipv6 := layers.IPv6{
Version: 6,
NextHeader: layers.IPProtocolICMPv6,
HopLimit: 255,
SrcIP: scanner.ip6net.IP,
DstIP: net.ParseIP("ff02::1"),
}
icmpv6 := layers.ICMPv6{
TypeCode: layers.CreateICMPv6TypeCode(layers.ICMPv6TypeEchoRequest, 0),
}
icmpv6echo := layers.ICMPv6Echo{
Identifier: uint16(rand.Int31()),
}
if err := icmpv6.SetNetworkLayerForChecksum(&ipv6); err != nil {
return err
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
seqNumber := uint16(0)
for {
select {
case <-ticker.C:
case <-ctx.Done():
return ctx.Err()
}
icmpv6echo.SeqNumber = seqNumber
seqNumber++
if err := gopacket.SerializeLayers(buf, opts, ð, &ipv6, &icmpv6, &icmpv6echo); err != nil {
return err
}
if err := scanner.handle.WritePacketData(buf.Bytes()); err != nil {
return err
}
}
}