new: api.rest will return the stdout data after executing a session command

This commit is contained in:
evilsocket 2025-01-09 02:55:51 +01:00
parent c4e45b368d
commit 61a9069e50

View file

@ -221,6 +221,10 @@ func (mod *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", 400) http.Error(w, "Bad Request", 400)
} }
rescueStdout := os.Stdout
stdoutReader, stdoutWriter, _ := os.Pipe()
os.Stdout = stdoutWriter
for _, aCommand := range session.ParseCommands(cmd.Command) { for _, aCommand := range session.ParseCommands(cmd.Command) {
if err = mod.Session.Run(aCommand); err != nil { if err = mod.Session.Run(aCommand); err != nil {
http.Error(w, err.Error(), 400) http.Error(w, err.Error(), 400)
@ -228,7 +232,11 @@ func (mod *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) {
} }
} }
mod.toJSON(w, APIResponse{Success: true}) stdoutWriter.Close()
out, _ := io.ReadAll(stdoutReader)
os.Stdout = rescueStdout
mod.toJSON(w, APIResponse{Success: true, Message: string(out)})
} }
func (mod *RestAPI) getEvents(limit int) []session.Event { func (mod *RestAPI) getEvents(limit int) []session.Event {