-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
409 lines (371 loc) · 10.7 KB
/
messages.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
package ssh
import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"regexp"
)
type ProtocolVersion string
// ProtocolAgreement represents the client and server protocol banners
//
// RFC specifies the format for the banner as specified in RFC 4523 Section 4.2
// SSH-protoversion-softwareversion SP comments CR LF
//
// The server MAY send other lines of data before sending the version.
// The lines are terminated by CR LF, and SHOULD be encoded as UTF-8
//
// See http://tools.ietf.org/html/rfc4253 for details
type ProtocolAgreement struct {
RawBanner string `json:"raw_banner,omitempty"`
ProtocolVersion string `json:"protocol_version,omitempty"`
SoftwareVersion string `json:"software_version,omitempty"`
Comments string `json:"comments,omitempty"`
}
// MakeZGrabProtocolAgreement returns the default client
// ProtocolAgreement message for ZGrab.
//
// This sets protocol version to "2.0", software version to "ZGrab",
// and comments to "ZGrab SSH Survey"
func MakeZGrabProtocolAgreement() *ProtocolAgreement {
h := ProtocolAgreement{
ProtocolVersion: "2.0",
SoftwareVersion: "ZGrab",
Comments: "ZGrab SSH Survey",
}
return &h
}
// ParseRawBanner populates a ProtocolAgreement struct based on the
// contents of the RawBanner field.
func (h *ProtocolAgreement) ParseRawBanner() {
matches := serverBannerRegex.FindStringSubmatch(h.RawBanner)
if len(matches) != 6 {
return
}
if matches[1] != "" {
h.ProtocolVersion = matches[1]
} else if matches[4] != "" {
h.ProtocolVersion = matches[4]
}
if matches[2] != "" {
h.SoftwareVersion = matches[2]
} else if matches[5] != "" {
h.SoftwareVersion = matches[5]
}
h.Comments = matches[3]
}
// Marshal returns a byte array suitable for a call to write
func (h *ProtocolAgreement) Marshal() []byte {
if h.RawBanner != "" {
return []byte(h.RawBanner)
}
if h.Comments != "" {
h.RawBanner = fmt.Sprintf("SSH-%s-%s %s\r\n", h.ProtocolVersion, h.SoftwareVersion, h.Comments)
} else {
h.RawBanner = fmt.Sprintf("SSH-%s-%s\r\n", h.ProtocolVersion, h.SoftwareVersion)
}
return []byte(h.RawBanner)
}
var serverBannerRegex = regexp.MustCompile(`(?:SSH-([!-,.-~]*)-([!-,.-~]*) ([^\r\n]*)\r\n)|(?:SSH-([!-,.-~]*)-([!-,.-~]*)\r\n)`)
// The Cookie type represents the random cookie sent during the server
// key exchange.
type Cookie [16]byte
// MarshalJSON encodes a Cookie to JSON as a base64-encoded byte array.
func (c *Cookie) MarshalJSON() ([]byte, error) {
return json.Marshal(c[:])
}
// UnmarshalJSON unmarshal a byte-array encoded in base64 to a cookie.
// The byte array must be either null, empty array, or exactly 16 bytes
// long.
func (c *Cookie) UnmarshalJSON(b []byte) error {
raw := c[:]
if err := json.Unmarshal(b, raw); err != nil {
return err
}
length := len(raw)
if length != 0 && length != 16 {
return fmt.Errorf("Cookies must be 16 bytes long, given %d", length)
}
return nil
}
type KeyExchangeInit struct {
raw []byte
Cookie Cookie `json:"cookie"`
KexAlgorithms NameList `json:"key_exchange_algorithms"`
HostKeyAlgorithms NameList `json:"host_key_algorithms"`
EncryptionClientToServer NameList `json:"encryption_client_to_server"`
EncryptionServerToClient NameList `json:"encryption_server_to_client"`
MACClientToServer NameList `json:"mac_client_to_server"`
MACServerToClient NameList `json:"mac_server_to_client"`
CompressionClientToServer NameList `json:"compression_client_to_server"`
CompressionServerToClient NameList `json:"compression_server_to_client"`
LanguageClientToServer NameList `json:"language_client_to_server"`
LanguageServerToClient NameList `json:"language_server_to_client"`
FirstKexPacketFollows bool `json:"first_kex_packet_follows"`
Zero uint32 `json:"zero"`
}
func (kxi *KeyExchangeInit) MsgType() byte {
return SSH_MSG_KEXINIT
}
func (kxi *KeyExchangeInit) Marshal() ([]byte, error) {
if kxi.raw != nil {
return kxi.raw, nil
}
payloadLength := 0
payloadLength += 16 // Cookie
payloadLength += kxi.KexAlgorithms.MarshaledLength()
payloadLength += kxi.HostKeyAlgorithms.MarshaledLength()
payloadLength += kxi.EncryptionClientToServer.MarshaledLength()
payloadLength += kxi.EncryptionServerToClient.MarshaledLength()
payloadLength += kxi.MACClientToServer.MarshaledLength()
payloadLength += kxi.MACServerToClient.MarshaledLength()
payloadLength += kxi.CompressionClientToServer.MarshaledLength()
payloadLength += kxi.CompressionServerToClient.MarshaledLength()
payloadLength += kxi.LanguageClientToServer.MarshaledLength()
payloadLength += kxi.LanguageServerToClient.MarshaledLength()
payloadLength += 1 + 4 // Bool + Reserved
out := make([]byte, payloadLength)
b := out
copy(b[0:16], kxi.Cookie[:])
b = b[16:]
var err error
if b, err = kxi.KexAlgorithms.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.HostKeyAlgorithms.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.EncryptionClientToServer.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.EncryptionServerToClient.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.MACClientToServer.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.MACServerToClient.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.CompressionClientToServer.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.CompressionServerToClient.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.LanguageClientToServer.MarshalInto(b); err != nil {
return nil, err
}
if b, err = kxi.LanguageServerToClient.MarshalInto(b); err != nil {
return nil, err
}
return out, nil
}
// Unmarshal a byte array into a KeyExchangeInit struct. The byte array
// should be the entire KeyExchangeInit message payload starting after
// the SSH_MSG_KEXINIT byte.
func (kxi *KeyExchangeInit) Unmarshal(raw []byte) bool {
kxi.raw = raw
b := raw
if len(b) < 16 {
return false
}
copy(kxi.Cookie[:], b[0:16])
b = b[16:]
var ok bool
if b, ok = kxi.KexAlgorithms.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.HostKeyAlgorithms.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.EncryptionClientToServer.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.EncryptionServerToClient.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.MACClientToServer.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.MACServerToClient.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.CompressionClientToServer.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.CompressionServerToClient.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.LanguageClientToServer.Unmarshal(b); !ok {
return false
}
if b, ok = kxi.LanguageServerToClient.Unmarshal(b); !ok {
return false
}
if len(b) < 1 {
return false
}
kxi.FirstKexPacketFollows = (b[0] != 0)
b = b[1:]
if len(b) < 4 {
return false
}
// Read the zero out from the end
kxi.Zero = binary.BigEndian.Uint32(b)
b = b[4:]
// We should be done
if len(b) != 0 {
return false
}
return true
}
// KeyExchangeDHInit represent the SSH_MSG_KEXDH_INIT message for transferring
// Diffie-Hellman public values. E = G^X mod P, where G is the generator and P
// is the prime. E is the public DHE value. See RFC 4253 Section 8.
type KeyExchangeDHInit struct {
raw []byte
E mpint
}
// MsgType returns the SSH_MSG_KEXDH_INIT message type
func (dhi *KeyExchangeDHInit) MsgType() byte {
return SSH_MSG_KEXDH_INIT
}
// Marshal encodes a KeyExchangeDHInit message payload
func (dhi *KeyExchangeDHInit) Marshal() ([]byte, error) {
if dhi.raw != nil {
return dhi.raw, nil
}
e, _ := dhi.E.Marshal()
out := make([]byte, 4+len(e))
binary.BigEndian.PutUint32(out, uint32(len(e)))
copy(out[4:], e)
return out, nil
}
func (dhi *KeyExchangeDHInit) Unmarshal(raw []byte) bool {
b := raw
if len(b) < 4 {
return false
}
length := binary.BigEndian.Uint32(b)
b = b[4:]
if uint32(len(b)) != length {
return false
}
dhi.E.SetBytes(b[0:length])
dhi.raw = raw
return true
}
type KeyExchangeDHInitReply struct {
raw []byte
K_S []byte `json:"k_s"`
F mpint `json:"f"`
Signature []byte `json:"signature"`
}
func (dhr *KeyExchangeDHInitReply) MsgType() byte {
return SSH_MSG_KEXDH_REPLY
}
func (dhr *KeyExchangeDHInitReply) Marshal() ([]byte, error) {
return nil, errors.New("unimplemented")
}
func (dhr *KeyExchangeDHInitReply) Unmarshal(raw []byte) bool {
b := raw
if len(b) < 4 {
return false
}
ksLength := binary.BigEndian.Uint32(b)
b = b[4:]
if ksLength > uint32(len(b)) {
return false
}
dhr.K_S = make([]byte, ksLength)
copy(dhr.K_S, b[0:ksLength])
b = b[ksLength:]
if len(b) < 4 {
return false
}
fLength := binary.BigEndian.Uint32(b)
b = b[4:]
if fLength > uint32(len(b)) {
return false
}
dhr.F.SetBytes(b[0:fLength])
b = b[fLength:]
if len(b) < 4 {
return false
}
sigLength := binary.BigEndian.Uint32(b)
b = b[4:]
if sigLength > uint32(len(b)) {
return false
}
dhr.Signature = make([]byte, sigLength)
copy(dhr.Signature, b[0:sigLength])
b = b[sigLength:]
if len(b) > 0 {
return false
}
return true
}
/*
byte SSH_MSG_KEY_DH_GEX_REQUEST
uint32 min, minimal size in bits of an acceptable group
uint32 n, preferred size in bits of the group the server will send
uint32 max, maximal size in bits of an acceptable group
*/
type KeyExchangeDHGroupRequest struct {
Min uint32 `json:"min"`
Preferred uint32 `json:"preferred"`
Max uint32 `json:"max"`
}
func (gex *KeyExchangeDHGroupRequest) MsgType() byte {
return SSH_MSG_KEY_DH_GEX_REQUEST
}
func (gex *KeyExchangeDHGroupRequest) Marshal() ([]byte, error) {
out := make([]byte, 12)
b := out
binary.BigEndian.PutUint32(b, gex.Min)
b = b[4:]
binary.BigEndian.PutUint32(b, gex.Preferred)
b = b[4:]
binary.BigEndian.PutUint32(b, gex.Max)
return out, nil
}
func (gex *KeyExchangeDHGroupRequest) Unmarshal([]byte) bool {
panic("unimplemented")
}
type KeyExchangeDHGroupParameters struct {
Prime mpint `json:"prime"`
Generator mpint `json:"generator"`
}
func (gex *KeyExchangeDHGroupParameters) MsgType() byte {
return SSH_MSG_KEX_DH_GEX_GROUP
}
func (gex *KeyExchangeDHGroupParameters) Marshal() ([]byte, error) {
return nil, errors.New("unimplemented")
}
func (gex *KeyExchangeDHGroupParameters) Unmarshal(raw []byte) (ok bool) {
b := raw
if b, ok = gex.Prime.Unmarshal(b); !ok {
return
}
if b, ok = gex.Generator.Unmarshal(b); !ok {
return
}
if len(b) > 0 {
return
}
return true
}
type KeyExchangeDHGroupInit struct {
KeyExchangeDHInit
}
func (gex *KeyExchangeDHGroupInit) MsgType() byte {
return SSH_MSG_KEX_DH_GEX_INIT
}
type KeyExchangeDHGroupReply struct {
KeyExchangeDHInitReply
}
func (gex *KeyExchangeDHGroupReply) MsgType() byte {
return SSH_MSG_KEX_DH_GEX_REPLY
}