Skip to content

Commit

Permalink
new: aliases are now centralized and can be used for any type of devi…
Browse files Browse the repository at this point in the history
…ce (closes bettercap#504)
  • Loading branch information
evilsocket committed Mar 21, 2019
1 parent 96853e6 commit 7e7f2ef
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 51 deletions.
2 changes: 1 addition & 1 deletion modules/wifi/wifi_recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (mod *WiFiModule) discoverClients(radiotap *layers.RadioTap, dot11 *layers.
freq := int(radiotap.ChannelFrequency)
rssi := radiotap.DBMAntennaSignal

if station, isNew := ap.AddClientIfNew(bssid, freq, rssi, mod.Session.Lan.Aliases()); isNew {
if station, isNew := ap.AddClientIfNew(bssid, freq, rssi); isNew {
mod.Session.Events.Add("wifi.client.new", ClientEvent{
AP: ap,
Client: station,
Expand Down
2 changes: 1 addition & 1 deletion modules/wifi/wifi_recon_handshakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (mod *WiFiModule) discoverHandshakes(radiotap *layers.RadioTap, dot11 *laye
staIsUs := bytes.Equal(staMac, mod.iface.HW)
station, found := ap.Get(staMac.String())
if !found {
station, _ = ap.AddClientIfNew(staMac.String(), ap.Frequency, ap.RSSI, mod.Session.Lan.Aliases())
station, _ = ap.AddClientIfNew(staMac.String(), ap.Frequency, ap.RSSI)
}

rawPMKID := []byte(nil)
Expand Down
11 changes: 10 additions & 1 deletion network/ble.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"

"github.com/bettercap/gatt"

"github.com/evilsocket/islazy/data"
)

const BLEMacValidator = "([a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2})"
Expand All @@ -18,6 +20,7 @@ type BLEDevLostCallback func(dev *BLEDevice)

type BLE struct {
sync.RWMutex
aliases *data.UnsortedKV
devices map[string]*BLEDevice
newCb BLEDevNewCallback
lostCb BLEDevLostCallback
Expand All @@ -27,9 +30,10 @@ type bleJSON struct {
Devices []*BLEDevice `json:"devices"`
}

func NewBLE(newcb BLEDevNewCallback, lostcb BLEDevLostCallback) *BLE {
func NewBLE(aliases *data.UnsortedKV, newcb BLEDevNewCallback, lostcb BLEDevLostCallback) *BLE {
return &BLE{
devices: make(map[string]*BLEDevice),
aliases: aliases,
newCb: newcb,
lostCb: lostcb,
}
Expand All @@ -55,14 +59,19 @@ func (b *BLE) AddIfNew(id string, p gatt.Peripheral, a *gatt.Advertisement, rssi
defer b.Unlock()

id = NormalizeMac(id)
alias := b.aliases.GetOr(id, "")
if dev, found := b.devices[id]; found {
dev.LastSeen = time.Now()
dev.RSSI = rssi
dev.Advertisement = a
if alias != "" {
dev.Alias = alias
}
return dev
}

newDev := NewBLEDevice(p, a, rssi)
newDev.Alias = alias
b.devices[id] = newDev

if b.newCb != nil {
Expand Down
3 changes: 3 additions & 0 deletions network/ble_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BLEService struct {
}

type BLEDevice struct {
Alias string
LastSeen time.Time
DeviceName string
Vendor string
Expand All @@ -40,6 +41,7 @@ type bleDeviceJSON struct {
LastSeen time.Time `json:"last_seen"`
Name string `json:"name"`
MAC string `json:"mac"`
Alias string `json:"alias"`
Vendor string `json:"vendor"`
RSSI int `json:"rssi"`
Connectable bool `json:"connectable"`
Expand Down Expand Up @@ -81,6 +83,7 @@ func (d *BLEDevice) MarshalJSON() ([]byte, error) {
LastSeen: d.LastSeen,
Name: d.Name(),
MAC: d.Device.ID(),
Alias: d.Alias,
Vendor: d.Vendor,
RSSI: d.RSSI,
Connectable: d.Advertisement.Connectable,
Expand Down
12 changes: 11 additions & 1 deletion network/hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"encoding/json"
"sync"
"time"

"github.com/evilsocket/islazy/data"
)

type HIDDevNewCallback func(dev *HIDDevice)
type HIDDevLostCallback func(dev *HIDDevice)

type HID struct {
sync.RWMutex
aliases *data.UnsortedKV
devices map[string]*HIDDevice
newCb HIDDevNewCallback
lostCb HIDDevLostCallback
Expand All @@ -20,9 +23,10 @@ type hidJSON struct {
Devices []*HIDDevice `json:"devices"`
}

func NewHID(newcb HIDDevNewCallback, lostcb HIDDevLostCallback) *HID {
func NewHID(aliases *data.UnsortedKV, newcb HIDDevNewCallback, lostcb HIDDevLostCallback) *HID {
return &HID{
devices: make(map[string]*HIDDevice),
aliases: aliases,
newCb: newcb,
lostCb: lostcb,
}
Expand Down Expand Up @@ -52,14 +56,20 @@ func (b *HID) AddIfNew(address []byte, channel int, payload []byte) (bool, *HIDD
defer b.Unlock()

id := HIDAddress(address)
alias := b.aliases.GetOr(id, "")

if dev, found := b.devices[id]; found {
dev.LastSeen = time.Now()
dev.AddChannel(channel)
dev.AddPayload(payload)
if alias != "" {
dev.Alias = alias
}
return false, dev
}

newDev := NewHIDDevice(address, channel, payload)
newDev.Alias = alias
b.devices[id] = newDev

if b.newCb != nil {
Expand Down
3 changes: 3 additions & 0 deletions network/hid_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type HIDDevice struct {
sync.Mutex
LastSeen time.Time
Type HIDType
Alias string
Address string
RawAddress []byte
channels map[int]bool
Expand All @@ -53,6 +54,7 @@ type hidDeviceJSON struct {
LastSeen time.Time `json:"last_seen"`
Type string `json:"type"`
Address string `json:"address"`
Alias string `json:"alias"`
Channels []string `json:"channels"`
Payloads []string `json:"payloads"`
PayloadsSize uint64 `json:"payloads_size"`
Expand Down Expand Up @@ -102,6 +104,7 @@ func (dev *HIDDevice) MarshalJSON() ([]byte, error) {
LastSeen: dev.LastSeen,
Type: dev.Type.String(),
Address: dev.Address,
Alias: dev.Alias,
Channels: dev.channelsListUnlocked(),
Payloads: make([]string, 0),
PayloadsSize: dev.payloadsSz,
Expand Down
25 changes: 1 addition & 24 deletions network/lan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package network

import (
"encoding/json"
"fmt"
"net"
"strings"
"sync"

"github.com/evilsocket/islazy/data"
"github.com/evilsocket/islazy/fs"
)

const LANDefaultttl = 10
const LANAliasesFile = "~/bettercap.aliases"

type EndpointNewCallback func(e *Endpoint)
type EndpointLostCallback func(e *Endpoint)

var aliasesFileName, _ = fs.Expand(LANAliasesFile)

type LAN struct {
sync.Mutex
hosts map[string]*Endpoint
Expand All @@ -34,12 +29,7 @@ type lanJSON struct {
Hosts []*Endpoint `json:"hosts"`
}

func NewLAN(iface, gateway *Endpoint, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
aliases, err := data.NewUnsortedKV(aliasesFileName, data.FlushOnEdit)
if err != nil {
fmt.Printf("error loading %s: %s", aliasesFileName, err)
}

func NewLAN(iface, gateway *Endpoint, aliases *data.UnsortedKV, newcb EndpointNewCallback, lostcb EndpointLostCallback) *LAN {
return &LAN{
iface: iface,
gateway: gateway,
Expand All @@ -63,19 +53,6 @@ func (l *LAN) MarshalJSON() ([]byte, error) {
return json.Marshal(doc)
}

func (lan *LAN) SetAliasFor(mac, alias string) bool {
lan.Lock()
defer lan.Unlock()

mac = NormalizeMac(mac)
lan.aliases.Set(mac, alias)
if e, found := lan.hosts[mac]; found {
e.Alias = alias
return true
}
return false
}

func (lan *LAN) Get(mac string) (*Endpoint, bool) {
lan.Lock()
defer lan.Unlock()
Expand Down
29 changes: 19 additions & 10 deletions network/wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"

"github.com/evilsocket/islazy/data"
"github.com/evilsocket/islazy/fs"
)

Expand Down Expand Up @@ -43,22 +44,24 @@ type APLostCallback func(ap *AccessPoint)
type WiFi struct {
sync.Mutex

aps map[string]*AccessPoint
iface *Endpoint
newCb APNewCallback
lostCb APLostCallback
aliases *data.UnsortedKV
aps map[string]*AccessPoint
iface *Endpoint
newCb APNewCallback
lostCb APLostCallback
}

type wifiJSON struct {
AccessPoints []*AccessPoint `json:"aps"`
}

func NewWiFi(iface *Endpoint, newcb APNewCallback, lostcb APLostCallback) *WiFi {
func NewWiFi(iface *Endpoint, aliases *data.UnsortedKV, newcb APNewCallback, lostcb APLostCallback) *WiFi {
return &WiFi{
aps: make(map[string]*AccessPoint),
iface: iface,
newCb: newcb,
lostCb: lostcb,
aps: make(map[string]*AccessPoint),
aliases: aliases,
iface: iface,
newCb: newcb,
lostCb: lostcb,
}
}

Expand Down Expand Up @@ -134,6 +137,7 @@ func (w *WiFi) AddIfNew(ssid, mac string, frequency int, rssi int8) (*AccessPoin
defer w.Unlock()

mac = NormalizeMac(mac)
alias := w.aliases.GetOr(mac, "")
if ap, found := w.aps[mac]; found {
ap.LastSeen = time.Now()
if rssi != 0 {
Expand All @@ -143,10 +147,15 @@ func (w *WiFi) AddIfNew(ssid, mac string, frequency int, rssi int8) (*AccessPoin
if !isBogusMacESSID(ssid) {
ap.Hostname = ssid
}

if alias != "" {
ap.Alias = alias
}
return ap, false
}

newAp := NewAccessPoint(ssid, mac, frequency, rssi)
newAp := NewAccessPoint(ssid, mac, frequency, rssi, w.aliases)
newAp.Alias = alias
w.aps[mac] = newAp

if w.newCb != nil {
Expand Down
15 changes: 8 additions & 7 deletions network/wifi_ap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type AccessPoint struct {
*Station
sync.Mutex

aliases *data.UnsortedKV
clients map[string]*Station
withKeyMaterial bool
}
Expand All @@ -22,9 +23,10 @@ type apJSON struct {
Handshake bool `json:"handshake"`
}

func NewAccessPoint(essid, bssid string, frequency int, rssi int8) *AccessPoint {
func NewAccessPoint(essid, bssid string, frequency int, rssi int8, aliases *data.UnsortedKV) *AccessPoint {
return &AccessPoint{
Station: NewStation(essid, bssid, frequency, rssi),
aliases: aliases,
clients: make(map[string]*Station),
}
}
Expand Down Expand Up @@ -67,29 +69,28 @@ func (ap *AccessPoint) RemoveClient(mac string) {
}
}

func (ap *AccessPoint) AddClientIfNew(bssid string, frequency int, rssi int8, aliases *data.UnsortedKV) (*Station, bool) {
func (ap *AccessPoint) AddClientIfNew(bssid string, frequency int, rssi int8) (*Station, bool) {
ap.Lock()
defer ap.Unlock()

bssid = NormalizeMac(bssid)
alias := ap.aliases.GetOr(bssid, "")

if s, found := ap.clients[bssid]; found {
// update
s.Frequency = frequency
s.RSSI = rssi
s.LastSeen = time.Now()

if aliases != nil {
s.Alias = aliases.GetOr(bssid, "")
if alias != "" {
s.Alias = alias
}

return s, false
}

s := NewStation("", bssid, frequency, rssi)
if aliases != nil {
s.Alias = aliases.GetOr(bssid, "")
}
s.Alias = alias
ap.clients[bssid] = s

return s, true
Expand Down
Loading

0 comments on commit 7e7f2ef

Please sign in to comment.