new: aliases are now persistant

This commit is contained in:
evilsocket 2018-01-28 21:07:21 +01:00
commit e216cdde48
3 changed files with 74 additions and 4 deletions

View file

@ -20,6 +20,8 @@ import (
"github.com/evilsocket/bettercap-ng/packets" "github.com/evilsocket/bettercap-ng/packets"
) )
const HistoryFile = "~/bettercap.history"
var ( var (
I = (*Session)(nil) I = (*Session)(nil)
@ -144,7 +146,7 @@ func (s *Session) setupInput() error {
history := "" history := ""
if *s.Options.NoHistory == false { if *s.Options.NoHistory == false {
history = "bettercap.history" history, _ = core.ExpandPath(HistoryFile)
} }
cfg := readline.Config{ cfg := readline.Config{

View file

@ -183,8 +183,7 @@ func (s *Session) aliasHandler(args []string, sess *Session) error {
mac := args[0] mac := args[0]
alias := args[1] alias := args[1]
if t, found := s.Targets.Targets[mac]; found == true { if s.Targets.SetAliasFor(mac, alias) == true {
t.Alias = alias
return nil return nil
} else { } else {
return fmt.Errorf("Could not find endpoint %s", mac) return fmt.Errorf("Could not find endpoint %s", mac)

View file

@ -1,11 +1,19 @@
package session package session
import ( import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"sync" "sync"
"github.com/evilsocket/bettercap-ng/core"
"github.com/evilsocket/bettercap-ng/net" "github.com/evilsocket/bettercap-ng/net"
) )
const TargetsAliasesFile = "~/bettercap.aliases"
type Targets struct { type Targets struct {
sync.Mutex sync.Mutex
@ -14,16 +22,72 @@ type Targets struct {
Gateway *net.Endpoint Gateway *net.Endpoint
Targets map[string]*net.Endpoint Targets map[string]*net.Endpoint
TTL map[string]uint TTL map[string]uint
Aliases map[string]string
aliasesFileName string
} }
func NewTargets(s *Session, iface, gateway *net.Endpoint) *Targets { func NewTargets(s *Session, iface, gateway *net.Endpoint) *Targets {
return &Targets{ t := &Targets{
Session: s, Session: s,
Interface: iface, Interface: iface,
Gateway: gateway, Gateway: gateway,
Targets: make(map[string]*net.Endpoint), Targets: make(map[string]*net.Endpoint),
TTL: make(map[string]uint), 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) 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 := strings.Trim(parts[0], "\r\n\t ")
alias := strings.Trim(parts[1], "\r\n\t ")
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 {
tp.Aliases[mac] = alias
t.Alias = alias
tp.saveAliases()
return true
}
return false
} }
func (tp *Targets) Remove(ip, mac string) { func (tp *Targets) Remove(ip, mac string) {
@ -66,6 +130,7 @@ func (tp *Targets) AddIfNotExist(ip, mac string) *net.Endpoint {
return nil return nil
} }
mac = net.NormalizeMac(mac)
if t, found := tp.Targets[mac]; found { if t, found := tp.Targets[mac]; found {
return t return t
} }
@ -75,6 +140,10 @@ func (tp *Targets) AddIfNotExist(ip, mac string) *net.Endpoint {
tp.Session.Events.Add("target.resolved", e) tp.Session.Events.Add("target.resolved", e)
} }
if alias, found := tp.Aliases[mac]; found {
e.Alias = alias
}
tp.Targets[mac] = e tp.Targets[mac] = e
tp.TTL[mac] = 2 tp.TTL[mac] = 2