-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
remote_shell_service.go
433 lines (366 loc) · 10 KB
/
remote_shell_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"bufio"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/subtle"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
"github.com/gliderlabs/ssh"
"github.com/google/uuid"
"go.uber.org/zap"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/term"
)
type ConsoleTarget int32
const (
RSAKeyType string = "RSA PRIVATE KEY"
ECKeyType = "EC PRIVATE KEY"
)
const (
stdOutTarget ConsoleTarget = 0
stdErrTarget ConsoleTarget = 1
)
type Console struct {
stdInLock sync.Mutex
stdInPipe io.Writer
stdOutPipe io.Reader
stdErrPipe io.Reader
sessionLock sync.Mutex
remoteSessions map[uuid.UUID]ssh.Session
}
func makeConsole(stdin io.Writer, stdout io.Reader, stderr io.Reader) *Console {
return &Console{
stdInPipe: stdin,
stdOutPipe: stdout,
stdErrPipe: stderr,
remoteSessions: map[uuid.UUID]ssh.Session{},
}
}
func (c *Console) OutputPipe(target ConsoleTarget) io.Reader {
switch target {
case stdOutTarget:
return c.stdOutPipe
case stdErrTarget:
return c.stdErrPipe
default:
return c.stdOutPipe
}
}
// Safely write to server's stdin
func (c *Console) WriteToStdIn(p []byte) (n int, err error) {
c.stdInLock.Lock()
n, err = c.stdInPipe.Write(p)
c.stdInLock.Unlock()
return n, err
}
// Register a remote console session for output
func (c *Console) RegisterSession(id uuid.UUID, session ssh.Session) {
c.sessionLock.Lock()
c.remoteSessions[id] = session
c.sessionLock.Unlock()
}
// Deregister a remote console session
func (c *Console) UnregisterSession(id uuid.UUID) {
c.sessionLock.Lock()
delete(c.remoteSessions, id)
c.sessionLock.Unlock()
}
// Fetch current sessions in a thread-safe way
func (c *Console) CurrentSessions() []ssh.Session {
c.sessionLock.Lock()
values := []ssh.Session{}
for _, value := range c.remoteSessions {
values = append(values, value)
}
c.sessionLock.Unlock()
return values
}
func passwordHandler(ctx ssh.Context, password string, logger *zap.Logger) bool {
expectedPassword := os.Getenv("RCON_PASSWORD")
if expectedPassword == "" {
expectedPassword = "minecraft"
}
lengthComp := subtle.ConstantTimeEq(int32(len(password)), int32(len(expectedPassword)))
contentComp := subtle.ConstantTimeCompare([]byte(password), []byte(expectedPassword))
isValid := lengthComp == 1 && contentComp == 1
if !isValid {
logger.Warn(fmt.Sprintf("Remote console session rejected (%s/%s)", ctx.User(), ctx.RemoteAddr().String()))
}
return isValid
}
func handleSession(session ssh.Session, console *Console, logger *zap.Logger) {
// Setup state for the console session
sessionId := uuid.New()
_, _, isTty := session.Pty()
logger.Info(fmt.Sprintf("Remote console session accepted (%s/%s) isTTY: %t", session.User(), session.RemoteAddr().String(), isTty))
console.RegisterSession(sessionId, session)
// Wrap the session in a terminal so we can read lines.
// Individual lines will be sent to the input channel to be processed as commands for the server.
// If the user sends Ctrl-C/D, this shows up as an EOF and will close the channel.
input := make(chan string)
go func() {
terminal := term.NewTerminal(session, "")
for {
line, err := terminal.ReadLine()
if err != nil {
// Check for client-triggered (expected) exit before logging as an error.
if err != io.EOF {
logger.Error(fmt.Sprintf("Unable to read line from session (%s/%s)", session.User(), session.RemoteAddr().String()), zap.Error(err))
}
close(input)
return
}
input <- line
}
}()
InputLoop:
for {
select {
case line, ok := <-input:
if !ok {
break InputLoop
}
lineBytes := []byte(fmt.Sprintf("%s\n", line))
_, err := console.WriteToStdIn(lineBytes)
if err != nil {
logger.Error(fmt.Sprintf("Session failed to write to stdin (%s/%s)", session.User(), session.RemoteAddr().String()), zap.Error(err))
}
case <-session.Context().Done():
break InputLoop
}
}
// Tear down the session
console.UnregisterSession(sessionId)
logger.Info(fmt.Sprintf("Remote console session disconnected (%s/%s)", session.User(), session.RemoteAddr().String()))
}
// Use stdOut or stdErr for output.
// There should only ever be one at a time per pipe
func consoleOutRoutine(output io.Writer, console *Console, target ConsoleTarget, logger *zap.Logger) {
scanner := bufio.NewScanner(console.OutputPipe(target))
for scanner.Scan() {
outBytes := []byte(fmt.Sprintf("%s\n", scanner.Text()))
_, err := output.Write(outBytes)
if err != nil {
logger.Error("Failed to write to stdout")
}
remoteSessions := console.CurrentSessions()
for _, session := range remoteSessions {
switch target {
case stdOutTarget:
session.Write(outBytes)
case stdErrTarget:
session.Stderr().Write(outBytes)
}
}
}
}
// Use os.Stdin for console.
func consoleInRoutine(stdIn io.Reader, console *Console, logger *zap.Logger) {
scanner := bufio.NewScanner(stdIn)
for scanner.Scan() {
text := scanner.Text()
outBytes := []byte(fmt.Sprintf("%s\n", text))
_, err := console.WriteToStdIn(outBytes)
if err != nil {
logger.Error("Failed to write to stdin")
}
}
}
const (
// Current filename, hides on Linux systems.
HostKeyFilename string = ".hostKey.pem"
// Old filename, not hidden.
OldHostKeyFilename = "hostKey.pem"
)
// Use the hidden form first, but fallback to the non-hidden one if it already exists.
func pickHostKeyPath(homeDir string) string {
defaultKeyfilePath := filepath.Join(homeDir, HostKeyFilename)
_, err := os.Stat(defaultKeyfilePath)
if !os.IsNotExist(err) {
return defaultKeyfilePath
}
fallbackKeyfilePath := filepath.Join(homeDir, OldHostKeyFilename)
_, err = os.Stat(fallbackKeyfilePath)
if !os.IsNotExist(err) {
return fallbackKeyfilePath
}
return defaultKeyfilePath
}
// Exists to clean up the non-hidden key file if it still exists
func cleanupOldHostKey() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
keyfilePath := filepath.Join(homeDir, OldHostKeyFilename)
_, err = os.Stat(keyfilePath)
if os.IsNotExist(err) {
return nil
}
err = os.Remove(keyfilePath)
if err != nil {
return err
}
_, err = os.Stat(keyfilePath)
if !os.IsNotExist(err) {
return err
}
return nil
}
type hostKeys struct {
rsaKey *rsa.PrivateKey
ecKey *ecdsa.PrivateKey
}
func populateKeys(keys *hostKeys, logger *zap.Logger) (bool, error) {
didAdd := false
if keys.ecKey == nil {
logger.Info("Generating ECDSA SSH Host Key")
ellipticKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
return didAdd, err
}
keys.ecKey = ellipticKey
didAdd = true
}
if keys.rsaKey == nil {
logger.Info("Generating RSA SSH Host Key")
rsaKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return didAdd, err
}
keys.rsaKey = rsaKey
didAdd = true
}
return didAdd, nil
}
func writeKeys(hostKeyPath string, keys *hostKeys, logger *zap.Logger) error {
keysFile, err := os.OpenFile(hostKeyPath, os.O_CREATE+os.O_WRONLY+os.O_TRUNC, 0600)
if err != nil {
return err
}
defer keysFile.Close()
logger.Info(fmt.Sprintf("Writing Host Keys to %s.", hostKeyPath))
if keys.ecKey != nil {
ecDER, err := x509.MarshalECPrivateKey(keys.ecKey)
if err != nil {
return err
}
ecBlock := pem.Block{
Type: ECKeyType,
Bytes: ecDER,
}
pem.Encode(keysFile, &ecBlock)
}
if keys.rsaKey != nil {
rsaDER := x509.MarshalPKCS1PrivateKey(keys.rsaKey)
rsaBlock := pem.Block{
Type: RSAKeyType,
Bytes: rsaDER,
}
pem.Encode(keysFile, &rsaBlock)
}
return nil
}
func readKeys(hostKeyPath string) (*hostKeys, error) {
bytes, err := os.ReadFile(hostKeyPath)
if err != nil {
return nil, err
}
var keys hostKeys
for len(bytes) > 0 {
pemBlock, next := pem.Decode(bytes)
if pemBlock == nil {
break
}
switch pemBlock.Type {
case RSAKeyType:
rsaKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
if err != nil {
return &keys, err
}
keys.rsaKey = rsaKey
case ECKeyType:
ecKey, err := x509.ParseECPrivateKey(pemBlock.Bytes)
if err != nil {
return &keys, err
}
keys.ecKey = ecKey
}
bytes = next
}
return &keys, nil
}
func ensureHostKeys(logger *zap.Logger) (*hostKeys, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
keyfilePath := pickHostKeyPath(homeDir)
defaultKeyfilePath := filepath.Join(homeDir, HostKeyFilename)
fileChanged := keyfilePath != defaultKeyfilePath
_, err = os.Stat(keyfilePath)
if os.IsNotExist(err) {
logger.Info("Generating host keys for remote shell server.")
var hostKeys hostKeys
addedKeys, err := populateKeys(&hostKeys, logger)
if (fileChanged || addedKeys) && err == nil {
writeKeys(defaultKeyfilePath, &hostKeys, logger)
}
return &hostKeys, err
} else {
logger.Info(fmt.Sprintf("Reading host keys for remote shell from %s.", keyfilePath))
hostKeys, err := readKeys(keyfilePath)
if err != nil {
return nil, err
}
// Populate missing keys (older files only have RSA)
addedKeys, err := populateKeys(hostKeys, logger)
if (fileChanged || addedKeys) && err == nil {
writeKeys(defaultKeyfilePath, hostKeys, logger)
}
return hostKeys, err
}
}
func twinKeys(keys *hostKeys) ssh.Option {
return func(srv *ssh.Server) error {
rsaSigner, err := gossh.NewSignerFromKey(keys.rsaKey)
if err != nil {
return err
}
srv.AddHostKey(rsaSigner)
ecSigner, err := gossh.NewSignerFromKey(keys.ecKey)
if err != nil {
return err
}
srv.AddHostKey(ecSigner)
return nil
}
}
func runRemoteShellServer(console *Console, logger *zap.Logger) {
logger.Info("Starting remote shell server on 2222...")
ssh.Handle(func(s ssh.Session) { handleSession(s, console, logger) })
hostKeys, err := ensureHostKeys(logger)
if err != nil {
logger.Error("Unable to ensure host keys exist", zap.Error(err))
return
}
err = cleanupOldHostKey()
if err != nil {
logger.Warn("Unable to remote old host key file", zap.Error(err))
}
log.Fatal(ssh.ListenAndServe(
":2222",
nil,
twinKeys(hostKeys),
ssh.PasswordAuth(func(ctx ssh.Context, password string) bool { return passwordHandler(ctx, password, logger) }),
))
}