new: implemented 'env' builtin function for proxy modules in order to get and set session variables

This commit is contained in:
evilsocket 2018-02-22 15:17:36 +01:00
parent 9a8afc312a
commit abca005651

View file

@ -11,6 +11,7 @@ import (
// define functions available to proxy scripts
func (s *ProxyScript) defineBuiltins() error {
// used to read a file ... doh
s.VM.Set("readFile", func(call otto.FunctionCall) otto.Value {
filename := call.Argument(0).String()
raw, err := ioutil.ReadFile(filename)
@ -27,6 +28,7 @@ func (s *ProxyScript) defineBuiltins() error {
return v
})
// log something
s.VM.Set("log", func(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
fmt.Printf("%s", v.String())
@ -37,5 +39,32 @@ func (s *ProxyScript) defineBuiltins() error {
return otto.Value{}
})
// read or write environment variable
s.VM.Set("env", func(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc == 1 {
// get
varName := call.Argument(0).String()
if found, varValue := s.sess.Env.Get(varName); found == true {
v, err := s.VM.ToValue(varValue)
if err != nil {
log.Error("Could not convert to string: %s", varValue)
return otto.Value{}
}
return v
}
} else if argc == 2 {
// set
varName := call.Argument(0).String()
varValue := call.Argument(1).String()
s.sess.Env.Set(varName, varValue)
}
return otto.Value{}
})
return nil
}