mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 14:03:17 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
e895dc6ab2
commit
eb5a72a44c
9 changed files with 234 additions and 220 deletions
|
@ -38,7 +38,7 @@ type Session struct {
|
|||
Gateway *network.Endpoint `json:"gateway"`
|
||||
Firewall firewall.FirewallManager `json:"-"`
|
||||
Env *Environment `json:"env"`
|
||||
Targets *Targets `json:"targets"`
|
||||
Lan *network.LAN `json:"lan"`
|
||||
WiFi *network.WiFi `json:"wifi"`
|
||||
Queue *packets.Queue `json:"packets"`
|
||||
Input *readline.Instance `json:"-"`
|
||||
|
@ -295,7 +295,7 @@ func (s *Session) startNetMon() {
|
|||
addr := event.IP.String()
|
||||
mac := event.MAC.String()
|
||||
|
||||
existing := s.Targets.AddIfNew(addr, mac)
|
||||
existing := s.Lan.AddIfNew(addr, mac)
|
||||
if existing != nil {
|
||||
existing.LastSeen = time.Now()
|
||||
}
|
||||
|
@ -376,9 +376,13 @@ func (s *Session) Start() error {
|
|||
s.Gateway = s.Interface
|
||||
}
|
||||
|
||||
s.WiFi = network.NewWiFi(s.Interface)
|
||||
s.Targets = NewTargets(s, s.Interface, s.Gateway)
|
||||
s.Firewall = firewall.Make(s.Interface)
|
||||
s.WiFi = network.NewWiFi(s.Interface)
|
||||
s.Lan = network.NewLAN(s.Interface, s.Gateway, func(e *network.Endpoint) {
|
||||
s.Events.Add("endpoint.new", e)
|
||||
}, func(e *network.Endpoint) {
|
||||
s.Events.Add("endpoint.lost", e)
|
||||
})
|
||||
|
||||
s.setupEnv()
|
||||
|
||||
|
|
|
@ -185,7 +185,7 @@ func (s *Session) aliasHandler(args []string, sess *Session) error {
|
|||
mac := args[0]
|
||||
alias := core.Trim(args[1])
|
||||
|
||||
if s.Targets.SetAliasFor(mac, alias) == true {
|
||||
if s.Lan.SetAliasFor(mac, alias) == true {
|
||||
return nil
|
||||
} else {
|
||||
return fmt.Errorf("Could not find endpoint %s", mac)
|
||||
|
@ -298,7 +298,7 @@ func (s *Session) registerCoreHandlers() {
|
|||
readline.PcItem("alias", readline.PcItemDynamic(func(prefix string) []string {
|
||||
prefix = core.Trim(prefix[5:])
|
||||
macs := []string{""}
|
||||
for mac := range s.Targets.Targets {
|
||||
for mac := range s.Lan.Hosts {
|
||||
if prefix == "" || strings.HasPrefix(mac, prefix) == true {
|
||||
macs = append(macs, mac)
|
||||
}
|
||||
|
|
|
@ -1,204 +0,0 @@
|
|||
package session
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/evilsocket/bettercap-ng/core"
|
||||
"github.com/evilsocket/bettercap-ng/network"
|
||||
)
|
||||
|
||||
const TargetsDefaultTTL = 10
|
||||
const TargetsAliasesFile = "~/bettercap.aliases"
|
||||
|
||||
type Targets struct {
|
||||
sync.Mutex
|
||||
|
||||
Session *Session `json:"-"`
|
||||
Interface *network.Endpoint
|
||||
Gateway *network.Endpoint
|
||||
Targets map[string]*network.Endpoint
|
||||
TTL map[string]uint
|
||||
Aliases map[string]string
|
||||
|
||||
aliasesFileName string
|
||||
}
|
||||
|
||||
func NewTargets(s *Session, iface, gateway *network.Endpoint) *Targets {
|
||||
t := &Targets{
|
||||
Session: s,
|
||||
Interface: iface,
|
||||
Gateway: gateway,
|
||||
Targets: make(map[string]*network.Endpoint),
|
||||
TTL: make(map[string]uint),
|
||||
Aliases: make(map[string]string),
|
||||
}
|
||||
|
||||
t.aliasesFileName, _ = core.ExpandPath(TargetsAliasesFile)
|
||||
if core.Exists(t.aliasesFileName) {
|
||||
if err := t.loadAliases(); err != nil {
|
||||
s.Events.Log(core.ERROR, "%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (tp *Targets) List() (list []*network.Endpoint) {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
list = make([]*network.Endpoint, 0)
|
||||
for _, t := range tp.Targets {
|
||||
list = append(list, t)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (tp *Targets) loadAliases() error {
|
||||
tp.Session.Events.Log(core.INFO, "Loading aliases from %s ...", tp.aliasesFileName)
|
||||
file, err := os.Open(tp.aliasesFileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
mac := core.Trim(parts[0])
|
||||
alias := core.Trim(parts[1])
|
||||
tp.Session.Events.Log(core.DEBUG, " aliases[%s] = '%s'", mac, alias)
|
||||
tp.Aliases[mac] = alias
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tp *Targets) saveAliases() {
|
||||
data := ""
|
||||
for mac, alias := range tp.Aliases {
|
||||
data += fmt.Sprintf("%s %s\n", mac, alias)
|
||||
}
|
||||
ioutil.WriteFile(tp.aliasesFileName, []byte(data), 0644)
|
||||
}
|
||||
|
||||
func (tp *Targets) SetAliasFor(mac, alias string) bool {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
if t, found := tp.Targets[mac]; found == true {
|
||||
if alias != "" {
|
||||
tp.Aliases[mac] = alias
|
||||
} else {
|
||||
delete(tp.Aliases, mac)
|
||||
}
|
||||
|
||||
t.Alias = alias
|
||||
tp.saveAliases()
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (tp *Targets) WasMissed(mac string) bool {
|
||||
if mac == tp.Session.Interface.HwAddress || mac == tp.Session.Gateway.HwAddress {
|
||||
return false
|
||||
}
|
||||
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
if ttl, found := tp.TTL[mac]; found == true {
|
||||
return ttl < TargetsDefaultTTL
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (tp *Targets) Remove(ip, mac string) {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
if e, found := tp.Targets[mac]; found {
|
||||
tp.TTL[mac]--
|
||||
if tp.TTL[mac] == 0 {
|
||||
tp.Session.Events.Add("endpoint.lost", e)
|
||||
delete(tp.Targets, mac)
|
||||
delete(tp.TTL, mac)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (tp *Targets) shouldIgnore(ip string) bool {
|
||||
// skip our own address
|
||||
if ip == tp.Interface.IpAddress {
|
||||
return true
|
||||
}
|
||||
// skip the gateway
|
||||
if ip == tp.Gateway.IpAddress {
|
||||
return true
|
||||
}
|
||||
// skip broadcast addresses
|
||||
if strings.HasSuffix(ip, ".255") {
|
||||
return true
|
||||
}
|
||||
// skip everything which is not in our subnet (multicast noise)
|
||||
addr := net.ParseIP(ip)
|
||||
return tp.Session.Interface.Net.Contains(addr) == false
|
||||
}
|
||||
|
||||
func (tp *Targets) Has(ip string) bool {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
for _, e := range tp.Targets {
|
||||
if e.IpAddress == ip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (tp *Targets) AddIfNew(ip, mac string) *network.Endpoint {
|
||||
tp.Lock()
|
||||
defer tp.Unlock()
|
||||
|
||||
if tp.shouldIgnore(ip) {
|
||||
return nil
|
||||
}
|
||||
|
||||
mac = network.NormalizeMac(mac)
|
||||
if t, found := tp.Targets[mac]; found {
|
||||
if tp.TTL[mac] < TargetsDefaultTTL {
|
||||
tp.TTL[mac]++
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
e := network.NewEndpoint(ip, mac)
|
||||
/*
|
||||
e.ResolvedCallback = func(e *network.Endpoint) {
|
||||
tp.Session.Events.Add("endpoint.resolved", e)
|
||||
}
|
||||
*/
|
||||
|
||||
if alias, found := tp.Aliases[mac]; found {
|
||||
e.Alias = alias
|
||||
}
|
||||
|
||||
tp.Targets[mac] = e
|
||||
tp.TTL[mac] = TargetsDefaultTTL
|
||||
|
||||
tp.Session.Events.Add("endpoint.new", e)
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue