-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathrequest.go
202 lines (185 loc) · 5.42 KB
/
request.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
package main
import (
"bytes"
cryptoRand "crypto/rand"
"encoding/binary"
"errors"
mathRand "math/rand"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"golang.org/x/crypto/ssh"
)
type globalRequestPayload interface {
reply(context *connContext) []byte
logEntry(context *connContext) logEntry
}
type globalRequestPayloadParser func(data []byte, context *connContext) (globalRequestPayload, error)
type tcpipRequest struct {
Address string
Port uint32
}
func (request tcpipRequest) reply(context *connContext) []byte {
if request.Port != 0 {
return nil
}
return ssh.Marshal(struct{ port uint32 }{uint32(mathRand.Intn(65536-1024) + 1024)})
}
func (request tcpipRequest) logEntry(context *connContext) logEntry {
return tcpipForwardLog{
Address: getAddressLog(request.Address, int(request.Port), context.cfg),
}
}
type cancelTCPIPRequest struct {
Address string
Port uint32
}
func (request cancelTCPIPRequest) reply(context *connContext) []byte {
return nil
}
func (request cancelTCPIPRequest) logEntry(context *connContext) logEntry {
return cancelTCPIPForwardLog{
Address: getAddressLog(request.Address, int(request.Port), context.cfg),
}
}
type noMoreSessionsRequest struct {
}
func (request noMoreSessionsRequest) reply(context *connContext) []byte {
return nil
}
func (request noMoreSessionsRequest) logEntry(context *connContext) logEntry {
return noMoreSessionsLog{}
}
type hostKeysProveRequest struct {
hostKeyIndices []int
}
func (request hostKeysProveRequest) reply(context *connContext) []byte {
signatures := make([][]byte, len(request.hostKeyIndices))
for i, index := range request.hostKeyIndices {
signature, err := context.cfg.parsedHostKeys[index].Sign(cryptoRand.Reader, ssh.Marshal(struct {
requestType, sessionID, hostKey string
}{
string(context.SessionID()),
string(context.cfg.parsedHostKeys[index].PublicKey().Marshal()),
}))
if err != nil {
warningLogger.Printf("Failed to sign host key: %v", err)
return nil
}
signatures[i] = ssh.Marshal(signature)
}
return marshalBytes(signatures)
}
func (request hostKeysProveRequest) logEntry(context *connContext) logEntry {
hostKeyFiles := make([]string, len(request.hostKeyIndices))
for i, index := range request.hostKeyIndices {
hostKeyFiles[i] = context.cfg.Server.HostKeys[index]
}
return hostKeysProveLog{
HostKeyFiles: hostKeyFiles,
}
}
var globalRequestPayloads = map[string]globalRequestPayloadParser{
"tcpip-forward": func(data []byte, context *connContext) (globalRequestPayload, error) {
payload := &tcpipRequest{}
if err := ssh.Unmarshal(data, payload); err != nil {
return nil, err
}
return payload, nil
},
"cancel-tcpip-forward": func(data []byte, context *connContext) (globalRequestPayload, error) {
payload := &cancelTCPIPRequest{}
if err := ssh.Unmarshal(data, payload); err != nil {
return nil, err
}
return payload, nil
},
"[email protected]": func(data []byte, context *connContext) (globalRequestPayload, error) {
if len(data) != 0 {
return nil, errors.New("invalid request payload")
}
return &noMoreSessionsRequest{}, nil
},
"[email protected]": func(data []byte, context *connContext) (globalRequestPayload, error) {
payloadBytes, err := unmarshalBytes(data)
if err != nil {
return nil, err
}
hostKeyIndices := make([]int, len(payloadBytes))
for i, publicKeyBytes := range payloadBytes {
found := false
for j, hostKey := range context.cfg.parsedHostKeys {
if bytes.Equal(publicKeyBytes, hostKey.PublicKey().Marshal()) {
hostKeyIndices[i] = j
found = true
break
}
}
if !found {
return nil, errors.New("host key not found")
}
}
return &hostKeysProveRequest{hostKeyIndices}, nil
},
}
var (
globalRequestsMetric = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "sshesame_global_requests_total",
Help: "Total number of global requests",
}, []string{"type"})
)
func handleGlobalRequest(request *ssh.Request, context *connContext) error {
parser := globalRequestPayloads[request.Type]
if parser == nil {
globalRequestsMetric.WithLabelValues("unknown").Inc()
warningLogger.Printf("Unsupported global request type %v", request.Type)
if request.WantReply {
if err := request.Reply(false, nil); err != nil {
return err
}
}
return nil
}
globalRequestsMetric.WithLabelValues(request.Type).Inc()
payload, err := parser(request.Payload, context)
if err != nil {
return err
}
switch payload.(type) {
case *noMoreSessionsRequest:
context.noMoreSessions = true
}
if request.WantReply {
if err := request.Reply(true, payload.reply(context)); err != nil {
return err
}
}
context.logEvent(payload.logEntry(context))
return nil
}
func marshalBytes(data [][]byte) []byte {
var result []byte
for _, b := range data {
result = append(result, ssh.Marshal(struct{ string }{string(b)})...)
}
return result
}
func unmarshalBytes(data []byte) ([][]byte, error) {
var result [][]byte
for len(data) > 0 {
if len(data) < 4 {
return nil, errors.New("invalid request payload")
}
length := binary.BigEndian.Uint32(data[:4])
if len(data) < 4+int(length) {
return nil, errors.New("invalid request payload")
}
var s struct{ S string }
if err := ssh.Unmarshal(data[:4+length], &s); err != nil {
return nil, err
}
result = append(result, []byte(s.S))
data = data[4+length:]
}
return result, nil
}