Skip to content

Commit

Permalink
new: added saveJSON function to the session scripting runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Jun 10, 2022
1 parent eff8135 commit 2837108
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
30 changes: 28 additions & 2 deletions session/script_builtin_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package session
import (
"encoding/json"
"io/ioutil"
"os"

"github.com/bettercap/bettercap/js"
"github.com/evilsocket/islazy/log"
Expand Down Expand Up @@ -84,13 +85,38 @@ func jsOnEventFunc(call otto.FunctionCall) otto.Value {
return js.NullValue
}

func jsSaveJSONFunc(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 2 {
return js.ReportError("saveJSON accepts one object and one string arguments")
} else if argv[0].IsObject() == false {
return js.ReportError("saveJSON accepts one object and one string arguments")
} else if argv[1].IsString() == false {
return js.ReportError("saveJSON accepts one object and one string arguments")
}

obj := argv[0]
fileName := argv[1].String()

if exp, err := obj.Export(); err != nil {
return js.ReportError("error exporting object: %v", err)
} else if raw, err := json.Marshal(exp); err != nil {
return js.ReportError("error serializing object: %v", err)
} else if err = ioutil.WriteFile(fileName, raw, os.ModePerm); err != nil {
return js.ReportError("error writing to '%s': %v", fileName, err)
}

return js.NullValue
}

func jsLoadJSONFunc(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 1 {
return js.ReportError("LoadJSON accepts one string argument")
return js.ReportError("loadJSON accepts one string argument")
} else if argv[0].IsString() == false {
return js.ReportError("LoadJSON accepts one string argument")
return js.ReportError("loadJSON accepts one string argument")
}

fileName := argv[0].String()
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func (s *Session) Start() error {
plugin.Defines["env"] = jsEnvFunc
plugin.Defines["run"] = jsRunFunc
plugin.Defines["loadJSON"] = jsLoadJSONFunc
plugin.Defines["saveJSON"] = jsSaveJSONFunc
plugin.Defines["onEvent"] = jsOnEventFunc
plugin.Defines["session"] = s

Expand Down

0 comments on commit 2837108

Please sign in to comment.