-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsshmux.go
459 lines (441 loc) · 11.6 KB
/
sshmux.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"log/slog"
"net"
"net/netip"
"net/url"
"os"
"strconv"
"sync"
"time"
reuse "github.com/libp2p/go-reuseport"
"github.com/pires/go-proxyproto"
"golang.org/x/crypto/ssh"
)
type Server struct {
listener net.Listener
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
Address string
Banner string
SSHConfig *ssh.ServerConfig
Authenticator Authenticator
LogWriter io.Writer
ProxyPolicy ProxyPolicyConfig
}
type upstreamInformation struct {
Address string
Signer ssh.Signer
Password *string
ProxyProtocol *byte
ProxyDestination string
}
func validateKey(config SSHKeyConfig) (ssh.Signer, error) {
if config.Path == "" && config.Base64 == "" && config.Content == "" {
return nil, errors.New("one of path, base64 or content of the SSH key must be set")
}
if (config.Path != "" && config.Base64 != "") || (config.Path != "" && config.Content != "") || (config.Base64 != "" && config.Content != "") {
return nil, errors.New("only one of path, base64 or content of the SSH key can be set")
}
var pemFile []byte
if config.Path != "" {
bytes, err := os.ReadFile(config.Path)
if err != nil {
return nil, err
}
pemFile = bytes
}
if config.Base64 != "" {
bytes, err := base64.StdEncoding.DecodeString(config.Base64)
if err != nil {
return nil, err
}
pemFile = bytes
}
if config.Content != "" {
pemFile = []byte(config.Content)
}
return ssh.ParsePrivateKey(pemFile)
}
func makeServer(config Config) (*Server, error) {
sshConfig := &ssh.ServerConfig{
ServerVersion: "SSH-2.0-taokystrong",
PublicKeyAuthAlgorithms: ssh.DefaultPubKeyAuthAlgos(),
}
for _, keyConf := range config.SSH.HostKeys {
key, err := validateKey(keyConf)
if err != nil {
return nil, err
}
sshConfig.AddHostKey(key)
}
proxyPolicyConfig, err := convertProxyPolicyConfig(config.ProxyProtocol)
if err != nil {
return nil, err
}
var logWriter io.Writer
if config.Logger.Enabled {
loggerURL, err := url.Parse(config.Logger.Endpoint)
if err != nil {
return nil, err
}
if loggerURL.Scheme == "udp" {
conn, err := net.Dial("udp", loggerURL.Host)
if err != nil {
return nil, fmt.Errorf("logger dial failed: %w", err)
}
logWriter = conn
} else {
return nil, fmt.Errorf("unsupported logger endpoint: %s", config.Logger.Endpoint)
}
} else {
logWriter = io.Discard
}
var authenticator Authenticator
if config.Auth.Version == "" || config.Auth.Version == "legacy" {
legacyAuthenticator := makeLegacyAuthenticator(config.Auth, config.Recovery)
authenticator = &legacyAuthenticator
} else {
var err error
authenticator, err = makeAuthenticator(config.Auth)
if err != nil {
return nil, err
}
}
sshmux := &Server{
Address: config.Address,
Banner: config.SSH.Banner,
SSHConfig: sshConfig,
Authenticator: authenticator,
LogWriter: logWriter,
ProxyPolicy: proxyPolicyConfig,
}
return sshmux, nil
}
func (s *Server) serve() {
defer s.wg.Done()
for {
select {
case <-s.ctx.Done():
return
default:
conn, err := s.listener.Accept()
if err != nil {
if s.ctx.Err() != nil {
// Context cancelled, stop accepting connections
return
}
log.Printf("Error on Accept: %s\n", err)
continue
}
s.wg.Add(1)
go s.handler(conn)
}
}
}
func (s *Server) handler(conn net.Conn) {
defer s.wg.Done()
defer conn.Close()
session, err := ssh.NewPipeSession(conn, s.SSHConfig)
if err != nil {
return
}
defer session.Close()
logger := slog.New(slog.NewJSONHandler(s.LogWriter, nil))
logger = logger.With(
slog.Int64("connect_time", time.Now().Unix()),
slog.String("remote_ip", conn.RemoteAddr().String()),
slog.String("client_type", "SSH"),
)
defer func() {
logger.Info("SSH proxy session", slog.Int64("disconnect_time", time.Now().Unix()))
}()
select {
case <-s.ctx.Done():
return
default:
attrs, err := s.RunPipeSession(session)
if err != nil {
log.Println("runPipeSession:", err)
}
for _, attr := range attrs {
logger = logger.With(attr)
}
}
}
func (s *Server) Handshake(session *ssh.PipeSession) error {
hasSetUser := false
var user string
var upstream *upstreamInformation
if s.Banner != "" {
err := session.Downstream.SendBanner(s.Banner)
if err != nil {
return err
}
}
// Stage 1: Authenticate the user with API
auth_requests:
for {
authReq, err := session.Downstream.ReadAuthRequest(true)
if err != nil {
return err
}
if !hasSetUser {
user = authReq.User
session.Downstream.SetUser(user)
hasSetUser = true
}
req := AuthRequest{Method: authReq.Method}
if authReq.Method == "publickey" && !authReq.IsPublicKeyQuery {
req.PublicKey = string(ssh.MarshalAuthorizedKey(*authReq.PublicKey))
}
for {
status, resp, err := s.Authenticator.Auth(req, user)
if err != nil {
return err
}
switch status {
case 200:
upstreamResp := *resp.Upstream
if upstreamResp.Port == 0 {
upstreamResp.Port = 22
}
upstream = &upstreamInformation{
Signer: parsePrivateKey(upstreamResp.PrivateKey, upstreamResp.Certificate),
Password: upstreamResp.Password,
}
upstream.Address = net.JoinHostPort(upstreamResp.Host, strconv.Itoa(int(upstreamResp.Port)))
if resp.Proxy != nil {
proxyConfig := *resp.Proxy
// parse protocol version
var protocolVersion byte
if proxyConfig.Protocol != nil {
switch *proxyConfig.Protocol {
case "v1":
protocolVersion = 1
case "v2":
protocolVersion = 2
default:
return fmt.Errorf("unknown PROXY protocol version: %s", *proxyConfig.Protocol)
}
}
upstream.ProxyProtocol = &protocolVersion
// parse protocol destination
upstream.ProxyDestination = upstream.Address
if proxyConfig.Host == "" {
proxyConfig.Host = upstreamResp.Host
}
if proxyConfig.Port == 0 {
proxyConfig.Port = upstreamResp.Port
}
upstream.Address = net.JoinHostPort(proxyConfig.Host, strconv.Itoa(int(proxyConfig.Port)))
}
break auth_requests
case 401:
if len(resp.Challenges) == 0 {
// The API server is requesting no challenges, which is abnormal and will
// likely lead to an infinite loop
session.Downstream.WriteAuthFailure([]string{"publickey", "keyboard-interactive"}, false)
continue auth_requests
}
for _, challenge := range resp.Challenges {
questions := make([]string, 0, len(challenge.Fields))
withEcho := make([]bool, 0, len(challenge.Fields))
for _, field := range challenge.Fields {
questions = append(questions, field.Prompt)
withEcho = append(withEcho, !field.Secret)
}
answers, err := session.Downstream.InteractiveChallenge("", challenge.Instruction, questions, withEcho)
if err != nil {
return err
}
if len(answers) != len(questions) {
return errors.New("ssh: numbers of answers and questions do not match")
}
if req.Payload == nil {
req.Payload = make(map[string]string, len(challenge.Fields))
}
for i, answer := range answers {
req.Payload[challenge.Fields[i].Key] = answer
}
}
continue
case 403:
if resp.Failure != nil {
failure := *resp.Failure
if failure.Disconnect {
if failure.Reason == 0 {
// 11: SSH_DISCONNECT_BY_APPLICATION
failure.Reason = 11
}
session.Downstream.WriteDisconnectMsg(failure.Reason, failure.Message)
return fmt.Errorf("ssh(%d): %s", failure.Reason, failure.Message)
}
}
fallthrough
default:
session.Downstream.WriteAuthFailure([]string{"publickey", "keyboard-interactive"}, false)
continue auth_requests
}
}
}
// Stage 2: connect to upstream
conn, err := net.Dial("tcp", upstream.Address)
if err != nil {
return err
}
if upstream.ProxyProtocol != nil {
dest := conn.RemoteAddr()
if upstream.ProxyDestination != upstream.Address {
if addr, err := net.ResolveTCPAddr("tcp", upstream.ProxyDestination); err == nil {
dest = addr
}
}
header := proxyproto.HeaderProxyFromAddrs(*upstream.ProxyProtocol, session.Downstream.RemoteAddr(), dest)
_, err := header.WriteTo(conn)
if err != nil {
return err
}
}
sshConfig := &ssh.ClientConfig{
User: user,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
err = session.InitUpstream(conn, upstream.Address, sshConfig)
if err != nil {
return err
}
// Firstly try publickey or password
if upstream.Signer != nil {
err = session.Upstream.WriteAuthRequestPublicKey(user, upstream.Signer)
} else if upstream.Password != nil {
err = session.Upstream.WriteAuthRequestPassword(user, *upstream.Password)
} else {
// Send a none auth request
err = session.Upstream.WriteAuthNone(user)
}
if err != nil {
return err
}
res, err := session.Upstream.ReadAuthResult()
if err != nil {
return err
}
// For the first auth fail, we mark it as partial success
if !res.Success {
err = session.Downstream.WriteAuthFailure(removePublicKeyMethod(res.Methods), true)
} else {
err = session.Downstream.WriteAuthResult(res)
}
if err != nil {
return err
}
if res.Success {
return nil
}
// Finally, pipe downstream and upstream's auth requests and results
// Note that publickey auth cannot be used anymore after this point
for {
req, err := session.Downstream.ReadAuthRequest(true)
if err != nil {
return err
}
err = session.Upstream.WriteAuthRequest(req)
if err != nil {
return err
}
res, err := session.Upstream.ReadAuthResult()
if err != nil {
return err
}
if !res.Success {
err = session.Downstream.WriteAuthFailure(removePublicKeyMethod(res.Methods), res.PartialSuccess)
} else {
err = session.Downstream.WriteAuthResult(res)
}
if err != nil {
return err
}
if res.Success {
return nil
}
}
}
func (s *Server) RunPipeSession(session *ssh.PipeSession) ([]slog.Attr, error) {
err := s.Handshake(session)
if err != nil {
return nil, err
}
attrs := []slog.Attr{
slog.String("username", session.Downstream.User()),
slog.String("host_ip", session.Upstream.RemoteAddr().String()),
slog.Bool("authenticated", true),
}
return attrs, session.RunPipe()
}
func (s *Server) Start() error {
// set up TCP listener
listener, err := reuse.Listen("tcp", s.Address)
if err != nil {
return err
}
if len(s.ProxyPolicy.AllowedCIDRs) > 0 || len(s.ProxyPolicy.AllowedHosts) > 0 {
listener = &proxyproto.Listener{
Listener: listener,
Policy: func(upstream net.Addr) (proxyproto.Policy, error) {
// parse upstream address
upstreamAddrPort, err := netip.ParseAddrPort(upstream.String())
if err != nil {
return proxyproto.SKIP, nil
}
upstreamAddr := upstreamAddrPort.Addr()
// only read PROXY header from allowed CIDRs or hosts
for _, network := range s.ProxyPolicy.AllowedCIDRs {
if network.Contains(upstreamAddr) {
return proxyproto.USE, nil
}
}
for _, host := range s.ProxyPolicy.AllowedHosts {
ips, err := net.LookupIP(host)
if err != nil {
continue
}
for _, ip := range ips {
ipAddr, ok := netip.AddrFromSlice(ip)
if ok && ipAddr.Unmap() == upstreamAddr {
return proxyproto.USE, nil
}
}
}
// do nothing if upstream not in the allow list
return proxyproto.SKIP, nil
},
}
}
// set up server context
s.ctx, s.cancel = context.WithCancel(context.Background())
s.listener = listener
s.wg.Add(1)
// main handler loop
go s.serve()
return nil
}
func (s *Server) Wait() {
s.wg.Wait()
}
func (s *Server) Shutdown() {
if s.cancel != nil {
s.cancel()
}
if s.listener != nil {
s.listener.Close()
}
s.wg.Wait()
}