mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
new: exposing session environment to proxy scripts
This commit is contained in:
parent
1b3cc9fbca
commit
5b969ffa9e
4 changed files with 20 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
function onLoad() {
|
function onLoad() {
|
||||||
console.log( "BeefInject loaded." );
|
console.log( "BeefInject loaded." );
|
||||||
|
console.log("targets: " + env['arp.spoof.targets']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onResponse(req, res) {
|
function onResponse(req, res) {
|
||||||
|
|
|
@ -23,6 +23,10 @@ func NewEnvironment() *Environment {
|
||||||
return env
|
return env
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *Environment) Storage() *map[string]string {
|
||||||
|
return &env.storage
|
||||||
|
}
|
||||||
|
|
||||||
func (env *Environment) Has(name string) bool {
|
func (env *Environment) Has(name string) bool {
|
||||||
env.lock.Lock()
|
env.lock.Lock()
|
||||||
defer env.lock.Unlock()
|
defer env.lock.Unlock()
|
||||||
|
|
|
@ -166,7 +166,7 @@ func (p *HttpProxy) Start() error {
|
||||||
} else {
|
} else {
|
||||||
scriptPath := v.(string)
|
scriptPath := v.(string)
|
||||||
if scriptPath != "" {
|
if scriptPath != "" {
|
||||||
if err, p.script = LoadProxyScript(scriptPath); err != nil {
|
if err, p.script = LoadProxyScript(scriptPath, p.Session); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Proxy script %s loaded.", scriptPath)
|
log.Debugf("Proxy script %s loaded.", scriptPath)
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/evilsocket/bettercap-ng/session"
|
||||||
|
|
||||||
"github.com/robertkrimen/otto"
|
"github.com/robertkrimen/otto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,7 +22,7 @@ type ProxyScript struct {
|
||||||
cbCache map[string]bool
|
cbCache map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
func LoadProxyScript(path string, sess *session.Session) (err error, s *ProxyScript) {
|
||||||
log.Infof("Loading proxy script %s ...", path)
|
log.Infof("Loading proxy script %s ...", path)
|
||||||
|
|
||||||
raw, err := ioutil.ReadFile(path)
|
raw, err := ioutil.ReadFile(path)
|
||||||
|
@ -32,6 +34,7 @@ func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
||||||
Path: path,
|
Path: path,
|
||||||
Source: string(raw),
|
Source: string(raw),
|
||||||
VM: otto.New(),
|
VM: otto.New(),
|
||||||
|
|
||||||
gil: &sync.Mutex{},
|
gil: &sync.Mutex{},
|
||||||
onRequestScript: nil,
|
onRequestScript: nil,
|
||||||
onResponseScript: nil,
|
onResponseScript: nil,
|
||||||
|
@ -45,6 +48,13 @@ func LoadProxyScript(path string) (err error, s *ProxyScript) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// define session pointer
|
||||||
|
err = s.VM.Set("env", *sess.Env.Storage())
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error while defining environment: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// run onLoad if defined
|
// run onLoad if defined
|
||||||
if s.hasCallback("onLoad") {
|
if s.hasCallback("onLoad") {
|
||||||
_, err = s.VM.Run("onLoad()")
|
_, err = s.VM.Run("onLoad()")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue