forked from bettercap/bettercap
/
queue.go
228 lines (185 loc) · 4.36 KB
/
queue.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
package packets
import (
"bytes"
"fmt"
"net"
"sync"
"github.com/bettercap/bettercap/network"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
type Activity struct {
IP net.IP
MAC net.HardwareAddr
Source bool
}
type Traffic struct {
Sent uint64
Received uint64
}
type Stats struct {
sync.RWMutex
Sent uint64
Received uint64
PktReceived uint64
Errors uint64
}
type PacketCallback func(pkt gopacket.Packet)
type Queue struct {
sync.RWMutex
Activities chan Activity `json:"-"`
Stats Stats
Protos map[string]uint64
Traffic map[string]*Traffic
iface *network.Endpoint
handle *pcap.Handle
source *gopacket.PacketSource
srcChannel chan gopacket.Packet
writes *sync.WaitGroup
pktCb PacketCallback
active bool
}
func NewQueue(iface *network.Endpoint) (q *Queue, err error) {
q = &Queue{
Protos: make(map[string]uint64),
Traffic: make(map[string]*Traffic),
Activities: make(chan Activity),
writes: &sync.WaitGroup{},
iface: iface,
active: !iface.IsMonitor(),
pktCb: nil,
}
if q.active == true {
if q.handle, err = pcap.OpenLive(iface.Name(), 1024, true, pcap.BlockForever); err != nil {
return
}
q.source = gopacket.NewPacketSource(q.handle, q.handle.LinkType())
q.srcChannel = q.source.Packets()
go q.worker()
}
return
}
func (q *Queue) OnPacket(cb PacketCallback) {
q.Lock()
defer q.Unlock()
q.pktCb = cb
}
func (q *Queue) onPacketCallback(pkt gopacket.Packet) {
q.RLock()
defer q.RUnlock()
if q.pktCb != nil {
q.pktCb(pkt)
}
}
func (q *Queue) trackProtocols(pkt gopacket.Packet) {
// gather protocols stats
pktLayers := pkt.Layers()
for _, layer := range pktLayers {
proto := layer.LayerType()
if proto == gopacket.LayerTypeDecodeFailure || proto == gopacket.LayerTypePayload {
continue
}
q.Lock()
name := proto.String()
if _, found := q.Protos[name]; found == false {
q.Protos[name] = 1
} else {
q.Protos[name] += 1
}
q.Unlock()
}
}
func (q *Queue) trackActivity(eth *layers.Ethernet, ip4 *layers.IPv4, address net.IP, pktSize uint64, isSent bool) {
// push to activity channel
q.Activities <- Activity{
IP: address,
MAC: eth.SrcMAC,
Source: isSent,
}
q.Lock()
defer q.Unlock()
// initialize or update stats
addr := address.String()
if _, found := q.Traffic[addr]; found == false {
if isSent {
q.Traffic[addr] = &Traffic{Sent: pktSize}
} else {
q.Traffic[addr] = &Traffic{Received: pktSize}
}
} else {
if isSent {
q.Traffic[addr].Sent += pktSize
} else {
q.Traffic[addr].Received += pktSize
}
}
}
func (q *Queue) worker() {
for pkt := range q.srcChannel {
if q.active == false {
return
}
q.trackProtocols(pkt)
pktSize := uint64(len(pkt.Data()))
q.Stats.Lock()
q.Stats.PktReceived++
q.Stats.Received += pktSize
q.Stats.Unlock()
q.onPacketCallback(pkt)
// decode eth and ipv4 layers
leth := pkt.Layer(layers.LayerTypeEthernet)
lip4 := pkt.Layer(layers.LayerTypeIPv4)
if leth != nil && lip4 != nil {
eth := leth.(*layers.Ethernet)
ip4 := lip4.(*layers.IPv4)
// here we try to discover new hosts
// on this lan by inspecting packets
// we manage to sniff
// something coming from someone on the LAN
isFromMe := bytes.Compare(q.iface.IP, ip4.SrcIP) == 0
isFromLAN := q.iface.Net.Contains(ip4.SrcIP)
if isFromMe == false && isFromLAN {
q.trackActivity(eth, ip4, ip4.SrcIP, pktSize, true)
}
// something going to someone on the LAN
isToMe := bytes.Compare(q.iface.IP, ip4.DstIP) == 0
isToLAN := q.iface.Net.Contains(ip4.DstIP)
if isToMe == false && isToLAN {
q.trackActivity(eth, ip4, ip4.DstIP, pktSize, false)
}
}
}
}
func (q *Queue) Send(raw []byte) error {
q.Lock()
defer q.Unlock()
if q.active == false {
return fmt.Errorf("Packet queue is not active.")
}
q.writes.Add(1)
defer q.writes.Done()
if err := q.handle.WritePacketData(raw); err != nil {
q.Stats.Lock()
q.Stats.Errors++
q.Stats.Unlock()
return err
} else {
q.Stats.Lock()
q.Stats.Sent += uint64(len(raw))
q.Stats.Unlock()
}
return nil
}
func (q *Queue) Stop() {
q.Lock()
defer q.Unlock()
if q.active == true {
// wait for write operations to be completed
q.writes.Wait()
// signal the main loop to exit and close the handle
q.active = false
q.srcChannel <- nil
q.handle.Close()
}
}