Skip to content

Commit

Permalink
Handle stderr for shell command
Browse files Browse the repository at this point in the history
  • Loading branch information
rkervella committed Jul 10, 2022
1 parent 0b9cf0f commit e7284b5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
26 changes: 26 additions & 0 deletions implant/sliver/handlers/tunnel_handlers/shell_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ func ShellReqHandler(envelope *sliverpb.Envelope, connection *transports.Connect
}
}

// Handle stderr
// Ideally we'd want the tunnel interface to use a slice of io.Readers and iterate over them.
// Not sure how that would work with the sequencing stuff in data_handler.go
go func() {
tWriter := tunnelWriter{
conn: connection,
tun: tunnel,
}
_, err := io.Copy(tWriter, systemShell.Stderr)

if err != nil {
cleanup("io error", err)
return
}
if systemShell.Command.ProcessState != nil {
if systemShell.Command.ProcessState.Exited() {
cleanup("process terminated", nil)
return
}
}
if err == io.EOF {
cleanup("EOF", err)
return
}
}()

go func() {
tWriter := tunnelWriter{
tun: tunnel,
Expand Down
10 changes: 10 additions & 0 deletions implant/sliver/shell/shell-pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ func pipedShell(tunnelID uint64, command []string) (*Shell, error) {
return nil, err
}

stderr, err := cmd.StderrPipe()
if err != nil {
// {{if .Config.Debug}}
log.Printf("[shell] stderr pipe failed\n")
// {{end}}
cancel()
return nil, err
}

err = cmd.Start()

return &Shell{
ID: tunnelID,
Command: cmd,
Stdout: stdout,
Stdin: stdin,
Stderr: stderr,
Cancel: cancel,
}, err
}
Expand Down
1 change: 1 addition & 0 deletions implant/sliver/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Shell struct {
Command *exec.Cmd
Stdout io.ReadCloser
Stdin io.WriteCloser
Stderr io.ReadCloser
Cancel context.CancelFunc
}

Expand Down
10 changes: 10 additions & 0 deletions implant/sliver/shell/shell_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,21 @@ func pipedShell(tunnelID uint64, command []string) (*Shell, error) {
return nil, err
}

stderr, err := cmd.StderrPipe()
if err != nil {
// {{if .Config.Debug}}
log.Printf("[shell] stderr pipe failed\n")
// {{end}}
cancel()
return nil, err
}

return &Shell{
ID: tunnelID,
Command: cmd,
Stdout: stdout,
Stdin: stdin,
Stderr: stderr,
Cancel: cancel,
}, nil
}
Expand Down
10 changes: 10 additions & 0 deletions implant/sliver/shell/shell_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ func pipedShell(tunnelID uint64, command []string) (*Shell, error) {
return nil, err
}

stderr, err := cmd.StderrPipe()
if err != nil {
// {{if .Config.Debug}}
log.Printf("[shell] stderr pipe failed\n")
// {{end}}
cancel()
return nil, err
}

err = cmd.Start()

return &Shell{
ID: tunnelID,
Command: cmd,
Stdout: stdout,
Stdin: stdin,
Stderr: stderr,
Cancel: cancel,
}, err
}

0 comments on commit e7284b5

Please sign in to comment.