| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "sync" |
| 8 | "sync/atomic" |
| 9 | |
| 10 | "github.com/bettercap/bettercap/network" |
| 11 | |
| 12 | "github.com/google/gopacket" |
| 13 | "github.com/google/gopacket/layers" |
| 14 | "github.com/google/gopacket/pcap" |
| 15 | ) |
| 16 | |
| 17 | type Activity struct { |
| 18 | IP net.IP |
| 19 | MAC net.HardwareAddr |
| 20 | Meta map[string]string |
| 21 | Source bool |
| 22 | } |
| 23 | |
| 24 | type Traffic struct { |
| 25 | Sent uint64 `json:"sent"` |
| 26 | Received uint64 `json:"received"` |
| 27 | } |
| 28 | |
| 29 | type Stats struct { |
| 30 | Sent uint64 `json:"sent"` |
| 31 | Received uint64 `json:"received"` |
| 32 | PktReceived uint64 `json:"pkts_received"` |
| 33 | Errors uint64 `json:"errors"` |
| 34 | } |
| 35 | |
| 36 | type Queue struct { |
| 37 | sync.RWMutex |
| 38 | |
| 39 | // keep on top because of https://github.com/bettercap/bettercap/issues/500 |
| 40 | Stats Stats |
| 41 | Protos sync.Map |
| 42 | Traffic sync.Map |
| 43 | Activities chan Activity |
| 44 | |
| 45 | iface *network.Endpoint |
| 46 | handle *pcap.Handle |
| 47 | source *gopacket.PacketSource |
| 48 | srcChannel chan gopacket.Packet |
| 49 | writes *sync.WaitGroup |
| 50 | active bool |
| 51 | } |
| 52 | |
| 53 | type queueJSON struct { |
| 54 | Stats Stats `json:"stats"` |
| 55 | Protos map[string]int `json:"protos"` |
| 56 | Traffic map[string]*Traffic `json:"traffic"` |
| 57 | } |
| 58 | |
| 59 | func NewQueue(iface *network.Endpoint) (q *Queue, err error) { |
| 60 | q = &Queue{ |
| 61 | Protos: sync.Map{}, |
| 62 | Traffic: sync.Map{}, |
| 63 | Stats: Stats{}, |
| 64 | Activities: make(chan Activity), |
| 65 | |
| 66 | writes: &sync.WaitGroup{}, |
| 67 | iface: iface, |
| 68 | active: !iface.IsMonitor(), |
| 69 | } |
| 70 | |
| 71 | if q.active { |
| 72 | if q.handle, err = network.Capture(iface.Name()); err != nil { |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | q.source = gopacket.NewPacketSource(q.handle, q.handle.LinkType()) |
| 77 | q.srcChannel = q.source.Packets() |
| 78 | go q.worker() |
| 79 | } |
| 80 | |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | func (q *Queue) MarshalJSON() ([]byte, error) { |
| 85 | q.Lock() |
| 86 | defer q.Unlock() |
| 87 | doc := queueJSON{ |
| 88 | Stats: q.Stats, |
| 89 | Protos: make(map[string]int), |
| 90 | Traffic: make(map[string]*Traffic), |
| 91 | } |
| 92 | |
| 93 | q.Protos.Range(func(k, v interface{}) bool { |
| 94 | doc.Protos[k.(string)] = v.(int) |
| 95 | return true |
| 96 | }) |
| 97 | |
| 98 | q.Traffic.Range(func(k, v interface{}) bool { |
| 99 | doc.Traffic[k.(string)] = v.(*Traffic) |
| 100 | return true |
| 101 | }) |
| 102 | |
| 103 | return json.Marshal(doc) |
| 104 | } |
| 105 | |
| 106 | func (q *Queue) trackProtocols(pkt gopacket.Packet) { |
| 107 | // gather protocols stats |
| 108 | pktLayers := pkt.Layers() |
| 109 | for _, layer := range pktLayers { |
| 110 | proto := layer.LayerType() |
| 111 | if proto == gopacket.LayerTypeDecodeFailure || proto == gopacket.LayerTypePayload { |
| 112 | continue |
| 113 | } |
| 114 | |
| 115 | name := proto.String() |
| 116 | if v, found := q.Protos.Load(name); !found { |
| 117 | q.Protos.Store(name, 1) |
| 118 | } else { |
| 119 | q.Protos.Store(name, v.(int)+1) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | func (q *Queue) trackActivity(eth *layers.Ethernet, address net.IP, meta map[string]string, pktSize uint64, isSent bool) { |
| 125 | // push to activity channel |
| 126 | q.Activities <- Activity{ |
| 127 | IP: address, |
| 128 | MAC: eth.SrcMAC, |
| 129 | Meta: meta, |
| 130 | Source: isSent, |
| 131 | } |
| 132 | |
| 133 | // initialize or update stats |
| 134 | addr := address.String() |
| 135 | if v, found := q.Traffic.Load(addr); !found { |
| 136 | if isSent { |
| 137 | q.Traffic.Store(addr, &Traffic{Sent: pktSize}) |
| 138 | } else { |
| 139 | q.Traffic.Store(addr, &Traffic{Received: pktSize}) |
| 140 | } |
| 141 | } else { |
| 142 | if isSent { |
| 143 | v.(*Traffic).Sent += pktSize |
| 144 | } else { |
| 145 | v.(*Traffic).Received += pktSize |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func (q *Queue) TrackPacket(size uint64) { |
| 151 | // https://github.com/bettercap/bettercap/issues/500 |
| 152 | if q == nil { |
| 153 | panic("track packet on nil queue!") |
| 154 | } |
| 155 | atomic.AddUint64(&q.Stats.PktReceived, 1) |
| 156 | atomic.AddUint64(&q.Stats.Received, size) |
| 157 | } |
| 158 | |
| 159 | func (q *Queue) TrackSent(size uint64) { |
| 160 | atomic.AddUint64(&q.Stats.Sent, size) |
| 161 | } |
| 162 | |
| 163 | func (q *Queue) TrackError() { |
| 164 | atomic.AddUint64(&q.Stats.Errors, 1) |
| 165 | } |
| 166 | |
| 167 | func (q *Queue) getPacketMeta(pkt gopacket.Packet) map[string]string { |
| 168 | meta := make(map[string]string) |
| 169 | if mdns := MDNSGetMeta(pkt); mdns != nil { |
| 170 | meta = mdns |
| 171 | } else if nbns := NBNSGetMeta(pkt); nbns != nil { |
| 172 | meta = nbns |
| 173 | } else if upnp := UPNPGetMeta(pkt); upnp != nil { |
| 174 | meta = upnp |
| 175 | } |
| 176 | return meta |
| 177 | } |
| 178 | |
| 179 | func (q *Queue) worker() { |
| 180 | for pkt := range q.srcChannel { |
| 181 | if !q.active { |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | q.trackProtocols(pkt) |
| 186 | |
| 187 | pktSize := uint64(len(pkt.Data())) |
| 188 | |
| 189 | q.TrackPacket(pktSize) |
| 190 | |
| 191 | // decode eth and ipv4/6 layers |
| 192 | leth := pkt.Layer(layers.LayerTypeEthernet) |
| 193 | lip4 := pkt.Layer(layers.LayerTypeIPv4) |
| 194 | lip6 := pkt.Layer(layers.LayerTypeIPv6) |
| 195 | if leth != nil && (lip4 != nil || lip6 != nil) { |
| 196 | var srcIP, dstIP net.IP |
| 197 | if lip4 != nil { |
| 198 | ip4 := lip4.(*layers.IPv4) |
| 199 | srcIP = ip4.SrcIP |
| 200 | dstIP = ip4.DstIP |
| 201 | } else { |
| 202 | ip6 := lip6.(*layers.IPv6) |
| 203 | srcIP = ip6.SrcIP |
| 204 | dstIP = ip6.DstIP |
| 205 | } |
| 206 | |
| 207 | // here we try to discover new hosts |
| 208 | // on this lan by inspecting packets |
| 209 | // we manage to sniff |
| 210 | eth := leth.(*layers.Ethernet) |
| 211 | |
| 212 | // something coming from someone on the LAN |
| 213 | isFromMe := q.iface.IP.Equal(srcIP) || q.iface.IPv6.Equal(srcIP) |
| 214 | isFromLAN := q.iface.Net.Contains(srcIP) |
| 215 | if !isFromMe && isFromLAN { |
| 216 | meta := q.getPacketMeta(pkt) |
| 217 | q.trackActivity(eth, srcIP, meta, pktSize, true) |
| 218 | } |
| 219 | |
| 220 | // something going to someone on the LAN |
| 221 | isToMe := q.iface.IP.Equal(dstIP) || q.iface.IPv6.Equal(dstIP) |
| 222 | isToLAN := q.iface.Net.Contains(dstIP) |
| 223 | if !isToMe && isToLAN { |
| 224 | q.trackActivity(eth, dstIP, nil, pktSize, false) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func (q *Queue) Send(raw []byte) error { |
| 231 | q.Lock() |
| 232 | defer q.Unlock() |
| 233 | |
| 234 | if !q.active { |
| 235 | return fmt.Errorf("Packet queue is not active.") |
| 236 | } |
| 237 | |
| 238 | q.writes.Add(1) |
| 239 | defer q.writes.Done() |
| 240 | |
| 241 | if err := q.handle.WritePacketData(raw); err != nil { |
| 242 | q.TrackError() |
| 243 | return err |
| 244 | } else { |
| 245 | q.TrackSent(uint64(len(raw))) |
| 246 | } |
| 247 | |
| 248 | return nil |
| 249 | } |
| 250 | |
| 251 | func (q *Queue) Stop() { |
| 252 | q.Lock() |
| 253 | defer q.Unlock() |
| 254 | |
| 255 | if q.active { |
| 256 | // wait for write operations to be completed |
| 257 | q.writes.Wait() |
| 258 | // signal the main loop to exit and close the handle |
| 259 | q.active = false |
| 260 | q.srcChannel <- nil |
| 261 | q.handle.Close() |
| 262 | } |
| 263 | } |