diff --git a/pkg/shexec/parser.go b/pkg/shexec/parser.go index aafb39c3..ecee19d7 100644 --- a/pkg/shexec/parser.go +++ b/pkg/shexec/parser.go @@ -76,6 +76,7 @@ var NoStoreVarNames = map[string]bool{ "BASH_CMDS": true, "BASH_COMMAND": true, "BASH_EXECUTION_STRING": true, + "LINENO": true, "BASH_LINENO": true, "BASH_REMATCH": true, "BASH_SOURCE": true, @@ -330,13 +331,15 @@ func ParseShellStateOutput(outputBytes []byte) (*packet.ShellState, error) { return nil, fmt.Errorf("invalid shell state output, wrong number of fields, fields=%d", len(fields)) } rtn := &packet.ShellState{} - rtn.Version = string(fields[0]) + rtn.Version = strings.TrimSpace(string(fields[0])) if strings.Index(rtn.Version, "bash") == -1 { return nil, fmt.Errorf("invalid shell state output, only bash is supported") } cwdStr := string(fields[1]) if strings.HasSuffix(cwdStr, "\r\n") { cwdStr = cwdStr[0 : len(cwdStr)-2] + } else if strings.HasSuffix(cwdStr, "\n") { + cwdStr = cwdStr[0 : len(cwdStr)-1] } rtn.Cwd = string(cwdStr) err := parseDeclareOutput(rtn, fields[2]) diff --git a/pkg/shexec/shexec.go b/pkg/shexec/shexec.go index 7d29ccdc..30b0f537 100644 --- a/pkg/shexec/shexec.go +++ b/pkg/shexec/shexec.go @@ -47,6 +47,7 @@ const MaxMaxPtySize = 100 * 1024 * 1024 const GetStateTimeout = 5 * time.Second +const BaseBashOpts = `set +m; set +H; shopt -s extglob` const GetShellStateCmd = `echo bash v${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}.${BASH_VERSINFO[2]}; printf "\x00\x00"; pwd; printf "\x00\x00"; declare -p $(compgen -A variable); printf "\x00\x00"; alias -p; printf "\x00\x00"; declare -f;` const ClientCommandFmt = ` @@ -965,12 +966,7 @@ func getTermType(pk *packet.RunPacketType) string { func makeRcFileStr(pk *packet.RunPacketType) string { var rcBuf bytes.Buffer - rcBuf.WriteString(` -set +m -set +H -shopt -s extglob -`) - + rcBuf.WriteString(BaseBashOpts + "\n") varDecls := VarDeclsFromState(pk.State) for _, varDecl := range varDecls { if varDecl.IsExport() || varDecl.IsReadOnly() { @@ -1452,7 +1448,8 @@ func GetShellStateRedirectCommandStr(outputFdNum int) string { func GetShellState() (*packet.ShellState, error) { ctx, _ := context.WithTimeout(context.Background(), GetStateTimeout) - ecmd := exec.CommandContext(ctx, "bash", "-l", "-i", "-c", GetShellStateCmd) + cmdStr := BaseBashOpts + "; " + GetShellStateCmd + ecmd := exec.CommandContext(ctx, "bash", "-l", "-i", "-c", cmdStr) outputBytes, err := runSimpleCmdInPty(ecmd) if err != nil { return nil, err