2019-07-18 21:29:16 +00:00
|
|
|
//+build !windows
|
|
|
|
|
|
|
|
package sshserver
|
|
|
|
|
|
|
|
import (
|
2019-09-06 21:37:58 +00:00
|
|
|
"encoding/json"
|
2019-08-22 16:36:21 +00:00
|
|
|
"errors"
|
2019-07-18 21:29:16 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
2019-09-04 15:37:53 +00:00
|
|
|
"runtime"
|
2019-07-18 21:29:16 +00:00
|
|
|
"strconv"
|
2019-09-06 21:37:58 +00:00
|
|
|
"strings"
|
2019-07-18 21:29:16 +00:00
|
|
|
"syscall"
|
2019-08-28 15:48:30 +00:00
|
|
|
"time"
|
2019-07-18 21:29:16 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2019-08-26 20:25:24 +00:00
|
|
|
"github.com/cloudflare/cloudflared/sshlog"
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
"github.com/creack/pty"
|
|
|
|
"github.com/gliderlabs/ssh"
|
2019-08-26 20:25:24 +00:00
|
|
|
"github.com/google/uuid"
|
2019-07-18 21:29:16 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
const (
|
|
|
|
auditEventAuth = "auth"
|
|
|
|
auditEventStart = "session_start"
|
|
|
|
auditEventStop = "session_stop"
|
|
|
|
auditEventExec = "exec"
|
|
|
|
auditEventScp = "scp"
|
|
|
|
auditEventResize = "resize"
|
|
|
|
auditEventTamper = "tamper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type auditEvent struct {
|
|
|
|
Event string `json:"event,omitempty"`
|
|
|
|
EventType string `json:"event_type,omitempty"`
|
|
|
|
SessionID string `json:"session_id,omitempty"`
|
|
|
|
User string `json:"user,omitempty"`
|
|
|
|
Login string `json:"login,omitempty"`
|
|
|
|
Datetime string `json:"datetime,omitempty"`
|
|
|
|
IPAddress string `json:"ip_address,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHServer adds on to the ssh.Server of the gliderlabs package
|
2019-07-18 21:29:16 +00:00
|
|
|
type SSHServer struct {
|
|
|
|
ssh.Server
|
2019-09-04 15:37:53 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
shutdownC chan struct{}
|
|
|
|
caCert ssh.PublicKey
|
|
|
|
logManager sshlog.Manager
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
// New creates a new SSHServer and configures its host keys and authenication by the data provided
|
2019-09-04 15:37:53 +00:00
|
|
|
func New(logManager sshlog.Manager, logger *logrus.Logger, version, address string, shutdownC chan struct{}, idleTimeout, maxTimeout time.Duration) (*SSHServer, error) {
|
2019-07-18 21:29:16 +00:00
|
|
|
currentUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if currentUser.Uid != "0" {
|
2019-08-19 18:51:59 +00:00
|
|
|
return nil, errors.New("cloudflared SSH server needs to run as root")
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 16:36:21 +00:00
|
|
|
sshServer := SSHServer{
|
2019-09-04 15:37:53 +00:00
|
|
|
Server: ssh.Server{
|
|
|
|
Addr: address,
|
|
|
|
MaxTimeout: maxTimeout,
|
|
|
|
IdleTimeout: idleTimeout,
|
|
|
|
Version: fmt.Sprintf("SSH-2.0-Cloudflare-Access_%s_%s", version, runtime.GOOS),
|
|
|
|
},
|
|
|
|
logger: logger,
|
|
|
|
shutdownC: shutdownC,
|
|
|
|
logManager: logManager,
|
2019-08-22 16:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 18:51:59 +00:00
|
|
|
if err := sshServer.configureHostKeys(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 20:36:45 +00:00
|
|
|
sshServer.configureAuthentication()
|
2019-07-18 21:29:16 +00:00
|
|
|
return &sshServer, nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
// Start the SSH server listener to start handling SSH connections from clients
|
2019-07-18 21:29:16 +00:00
|
|
|
func (s *SSHServer) Start() error {
|
|
|
|
s.logger.Infof("Starting SSH server at %s", s.Addr)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-s.shutdownC
|
|
|
|
if err := s.Close(); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Cannot close SSH server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.Handle(s.connectionHandler)
|
|
|
|
return s.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SSHServer) connectionHandler(session ssh.Session) {
|
2019-09-06 21:37:58 +00:00
|
|
|
sessionUUID, err := uuid.NewRandom()
|
|
|
|
|
2019-08-26 20:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, err := io.WriteString(session, "Failed to generate session ID\n"); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to generate session ID: Failed to write to SSH session")
|
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
s.errorAndExit(session, "", nil)
|
2019-07-18 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
sessionID := sessionUUID.String()
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
eventLogger, err := s.logManager.NewLogger(fmt.Sprintf("%s-event.log", sessionID), s.logger)
|
2019-08-22 16:36:21 +00:00
|
|
|
if err != nil {
|
2019-09-06 21:37:58 +00:00
|
|
|
if _, err := io.WriteString(session, "Failed to create event log\n"); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to create event log: Failed to write to create event logger")
|
|
|
|
}
|
|
|
|
s.errorAndExit(session, "", nil)
|
2019-07-18 21:29:16 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
|
|
|
|
// Get uid and gid of user attempting to login
|
|
|
|
sshUser, uidInt, gidInt, success := s.getSSHUser(session, sessionID, eventLogger)
|
|
|
|
if !success {
|
2019-08-22 16:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
// Spawn shell under user
|
2019-09-06 21:37:58 +00:00
|
|
|
cmd := s.spawnCmd(session, sshUser, uidInt, gidInt)
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
var shellInput io.WriteCloser
|
|
|
|
var shellOutput io.ReadCloser
|
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
ptyReq, winCh, isPty := session.Pty()
|
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
if isPty {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
|
2019-09-06 21:37:58 +00:00
|
|
|
shellInput, shellOutput, err = s.startPtySession(cmd, winCh, func() {
|
|
|
|
s.logAuditEvent(eventLogger, session, sessionID, auditEventResize)
|
|
|
|
})
|
2019-08-29 17:40:21 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to start pty session")
|
|
|
|
close(s.shutdownC)
|
|
|
|
return
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
s.logAuditEvent(eventLogger, session, sessionID, auditEventStart)
|
|
|
|
defer s.logAuditEvent(eventLogger, session, sessionID, auditEventStop)
|
2019-08-29 17:40:21 +00:00
|
|
|
} else {
|
|
|
|
shellInput, shellOutput, err = s.startNonPtySession(cmd)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to start non-pty session")
|
|
|
|
close(s.shutdownC)
|
|
|
|
return
|
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
event := auditEventExec
|
|
|
|
if strings.HasPrefix(session.RawCommand(), "scp") {
|
|
|
|
event = auditEventScp
|
|
|
|
}
|
|
|
|
s.logAuditEvent(eventLogger, session, sessionID, event)
|
2019-08-29 17:40:21 +00:00
|
|
|
}
|
2019-07-18 21:29:16 +00:00
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
// Write incoming commands to shell
|
2019-07-18 21:29:16 +00:00
|
|
|
go func() {
|
2019-08-29 17:40:21 +00:00
|
|
|
if _, err := io.Copy(shellInput, session); err != nil {
|
2019-07-18 21:29:16 +00:00
|
|
|
s.logger.WithError(err).Error("Failed to write incoming command to pty")
|
|
|
|
}
|
|
|
|
}()
|
2019-08-29 17:40:21 +00:00
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
pr, pw := io.Pipe()
|
2019-08-29 17:40:21 +00:00
|
|
|
defer pr.Close()
|
|
|
|
defer pw.Close()
|
|
|
|
|
2019-09-03 21:28:06 +00:00
|
|
|
sessionLogger, err := s.logManager.NewSessionLogger(fmt.Sprintf("%s-session.log", sessionID), s.logger)
|
2019-08-26 20:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
if _, err := io.WriteString(session, "Failed to create log\n"); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to create log: Failed to write to SSH session")
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
s.errorAndExit(session, "", nil)
|
|
|
|
return
|
2019-08-26 20:25:24 +00:00
|
|
|
}
|
2019-09-03 21:28:06 +00:00
|
|
|
defer sessionLogger.Close()
|
2019-08-26 20:25:24 +00:00
|
|
|
go func() {
|
2019-09-03 21:28:06 +00:00
|
|
|
io.Copy(sessionLogger, pr)
|
2019-07-18 21:29:16 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Write outgoing command output to both the command recorder, and remote user
|
|
|
|
mw := io.MultiWriter(pw, session)
|
2019-08-29 17:40:21 +00:00
|
|
|
if _, err := io.Copy(mw, shellOutput); err != nil {
|
2019-07-18 21:29:16 +00:00
|
|
|
s.logger.WithError(err).Error("Failed to write command output to user")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all resources associated with cmd to be released
|
|
|
|
// Returns error if shell exited with a non-zero status or received a signal
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
s.logger.WithError(err).Debug("Shell did not close correctly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
// spawnCmd spawns a shell under the user
|
|
|
|
func (s *SSHServer) spawnCmd(session ssh.Session, sshUser *User, uidInt, gidInt uint32) *exec.Cmd {
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
if session.RawCommand() != "" {
|
|
|
|
cmd = exec.Command(sshUser.Shell, "-c", session.RawCommand())
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command(sshUser.Shell)
|
|
|
|
}
|
|
|
|
// Supplementary groups are not explicitly specified. They seem to be inherited by default.
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: &syscall.Credential{Uid: uidInt, Gid: gidInt}, Setsid: true}
|
|
|
|
cmd.Env = append(cmd.Env, session.Environ()...)
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", sshUser.Username))
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", sshUser.HomeDir))
|
|
|
|
cmd.Dir = sshUser.HomeDir
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSSHUser gets the ssh user, uid, and gid of the user attempting to login
|
|
|
|
func (s *SSHServer) getSSHUser(session ssh.Session, sessionID string, eventLogger io.WriteCloser) (*User, uint32, uint32, bool) {
|
|
|
|
// Get uid and gid of user attempting to login
|
|
|
|
sshUser, ok := session.Context().Value("sshUser").(*User)
|
|
|
|
if !ok || sshUser == nil {
|
|
|
|
s.errorAndExit(session, "Error retrieving credentials from session", nil)
|
|
|
|
return nil, 0, 0, false
|
|
|
|
}
|
|
|
|
s.logAuditEvent(eventLogger, session, sessionID, auditEventAuth)
|
|
|
|
|
|
|
|
uidInt, err := stringToUint32(sshUser.Uid)
|
|
|
|
if err != nil {
|
|
|
|
s.errorAndExit(session, "Invalid user", err)
|
|
|
|
return sshUser, 0, 0, false
|
|
|
|
}
|
|
|
|
gidInt, err := stringToUint32(sshUser.Gid)
|
|
|
|
if err != nil {
|
|
|
|
s.errorAndExit(session, "Invalid user group", err)
|
|
|
|
return sshUser, 0, 0, false
|
|
|
|
}
|
|
|
|
return sshUser, uidInt, gidInt, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorAndExit reports an error with the session and exits
|
|
|
|
func (s *SSHServer) errorAndExit(session ssh.Session, errText string, err error) {
|
2019-08-20 17:48:47 +00:00
|
|
|
if err := session.Exit(1); err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to close SSH session")
|
2019-09-06 21:37:58 +00:00
|
|
|
} else if err != nil {
|
|
|
|
s.logger.WithError(err).Error(errText)
|
|
|
|
} else if errText != "" {
|
|
|
|
s.logger.Error(errText)
|
2019-08-20 17:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 17:40:21 +00:00
|
|
|
func (s *SSHServer) startNonPtySession(cmd *exec.Cmd) (io.WriteCloser, io.ReadCloser, error) {
|
|
|
|
in, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
out, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return in, out, nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:37:58 +00:00
|
|
|
func (s *SSHServer) startPtySession(cmd *exec.Cmd, winCh <-chan ssh.Window, logCallback func()) (io.WriteCloser, io.ReadCloser, error) {
|
2019-08-29 17:40:21 +00:00
|
|
|
tty, err := pty.Start(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle terminal window size changes
|
|
|
|
go func() {
|
|
|
|
for win := range winCh {
|
|
|
|
if errNo := setWinsize(tty, win.Width, win.Height); errNo != 0 {
|
|
|
|
s.logger.WithError(err).Error("Failed to set pty window size")
|
|
|
|
close(s.shutdownC)
|
|
|
|
return
|
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
logCallback()
|
2019-08-29 17:40:21 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return tty, tty, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
// Sets PTY window size for terminal
|
|
|
|
func setWinsize(f *os.File, w, h int) syscall.Errno {
|
|
|
|
_, _, errNo := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
|
|
|
|
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
|
|
|
|
return errNo
|
|
|
|
}
|
|
|
|
|
2019-08-20 17:48:47 +00:00
|
|
|
func stringToUint32(str string) (uint32, error) {
|
|
|
|
uid, err := strconv.ParseUint(str, 10, 32)
|
|
|
|
return uint32(uid), err
|
|
|
|
|
2019-07-18 21:29:16 +00:00
|
|
|
}
|
2019-09-06 21:37:58 +00:00
|
|
|
|
|
|
|
func (s *SSHServer) logAuditEvent(writer io.WriteCloser, session ssh.Session, sessionID string, eventType string) {
|
|
|
|
username := "unknown"
|
|
|
|
sshUser, ok := session.Context().Value("sshUser").(*User)
|
|
|
|
if ok && sshUser != nil {
|
|
|
|
username = sshUser.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
event := auditEvent{
|
|
|
|
Event: session.RawCommand(),
|
|
|
|
EventType: eventType,
|
|
|
|
SessionID: sessionID,
|
|
|
|
User: username,
|
|
|
|
Login: username,
|
|
|
|
Datetime: time.Now().UTC().Format(time.RFC3339),
|
|
|
|
IPAddress: session.RemoteAddr().String(),
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(&event)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.WithError(err).Error("Failed to log audit event. malformed audit object")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
line := string(data) + "\n"
|
|
|
|
writer.Write([]byte(line))
|
|
|
|
}
|