fix: removing bash escape sequences from stdout before sending it as api response

This commit is contained in:
evilsocket 2025-03-27 04:56:39 +01:00
parent 6ff2839e15
commit 2662831fab

View file

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -17,6 +18,10 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
var (
ansiEscapeRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`)
)
type CommandRequest struct { type CommandRequest struct {
Command string `json:"cmd"` Command string `json:"cmd"`
} }
@ -236,7 +241,8 @@ func (mod *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) {
out, _ := io.ReadAll(stdoutReader) out, _ := io.ReadAll(stdoutReader)
os.Stdout = rescueStdout os.Stdout = rescueStdout
mod.toJSON(w, APIResponse{Success: true, Message: string(out)}) // remove ANSI escape sequences (bash color codes) from output
mod.toJSON(w, APIResponse{Success: true, Message: ansiEscapeRegex.ReplaceAllString(string(out), "")})
} }
func (mod *RestAPI) getEvents(limit int) []session.Event { func (mod *RestAPI) getEvents(limit int) []session.Event {