|
|
@@ -6,14 +6,11 @@ import (
|
|
6
|
6
|
"crypto/sha256"
|
|
7
|
7
|
"crypto/subtle"
|
|
8
|
8
|
"encoding/binary"
|
|
9
|
|
- "errors"
|
|
10
|
9
|
"fmt"
|
|
11
|
10
|
"io"
|
|
12
|
11
|
"net"
|
|
13
|
12
|
"slices"
|
|
14
|
13
|
"time"
|
|
15
|
|
-
|
|
16
|
|
- "github.com/dolonet/mtg-multi/mtglib/internal/tls"
|
|
17
|
14
|
)
|
|
18
|
15
|
|
|
19
|
16
|
const (
|
|
|
@@ -24,11 +21,6 @@ const (
|
|
24
|
21
|
RandomOffset = 1 + 2 + 2 + 1 + 3 + 2
|
|
25
|
22
|
|
|
26
|
23
|
sniDNSNamesListType = 0
|
|
27
|
|
-
|
|
28
|
|
- // maxContinuationRecords limits the number of continuation TLS records
|
|
29
|
|
- // that reassembleTLSHandshake will read. This prevents resource exhaustion
|
|
30
|
|
- // from adversarial fragmentation.
|
|
31
|
|
- maxContinuationRecords = 10
|
|
32
|
24
|
)
|
|
33
|
25
|
|
|
34
|
26
|
var (
|
|
|
@@ -77,31 +69,17 @@ func ReadClientHelloMulti(
|
|
77
|
69
|
}
|
|
78
|
70
|
defer conn.SetReadDeadline(resetDeadline) //nolint: errcheck
|
|
79
|
71
|
|
|
80
|
|
- reassembled, err := reassembleTLSHandshake(conn)
|
|
|
72
|
+ clientHelloCopy, handshakeReader, err := parseClientHello(conn)
|
|
81
|
73
|
if err != nil {
|
|
82
|
|
- return nil, fmt.Errorf("cannot reassemble TLS records: %w", err)
|
|
83
|
|
- }
|
|
84
|
|
-
|
|
85
|
|
- handshakeCopyBuf := &bytes.Buffer{}
|
|
86
|
|
- reader := io.TeeReader(reassembled, handshakeCopyBuf)
|
|
87
|
|
-
|
|
88
|
|
- // Skip the TLS record header (validated during reassembly).
|
|
89
|
|
- // The header still flows through TeeReader into handshakeCopyBuf for HMAC.
|
|
90
|
|
- if _, err = io.CopyN(io.Discard, reader, tls.SizeHeader); err != nil {
|
|
91
|
|
- return nil, fmt.Errorf("cannot skip tls header: %w", err)
|
|
|
74
|
+ return nil, fmt.Errorf("cannot read client hello: %w", err)
|
|
92
|
75
|
}
|
|
93
|
76
|
|
|
94
|
|
- reader, err = parseHandshakeHeader(reader)
|
|
95
|
|
- if err != nil {
|
|
96
|
|
- return nil, fmt.Errorf("cannot parse handshake header: %w", err)
|
|
97
|
|
- }
|
|
98
|
|
-
|
|
99
|
|
- hello, err := parseHandshake(reader)
|
|
|
77
|
+ hello, err := parseHandshake(handshakeReader)
|
|
100
|
78
|
if err != nil {
|
|
101
|
79
|
return nil, fmt.Errorf("cannot parse handshake: %w", err)
|
|
102
|
80
|
}
|
|
103
|
81
|
|
|
104
|
|
- sniHostnames, err := parseSNI(reader)
|
|
|
82
|
+ sniHostnames, err := parseSNI(handshakeReader)
|
|
105
|
83
|
if err != nil {
|
|
106
|
84
|
return nil, fmt.Errorf("cannot parse SNI: %w", err)
|
|
107
|
85
|
}
|
|
|
@@ -110,8 +88,8 @@ func ReadClientHelloMulti(
|
|
110
|
88
|
return nil, fmt.Errorf("cannot find %s in %v", hostname, sniHostnames)
|
|
111
|
89
|
}
|
|
112
|
90
|
|
|
113
|
|
- // Save the handshake bytes so we can reuse them for each secret attempt.
|
|
114
|
|
- handshakeBytes := handshakeCopyBuf.Bytes()
|
|
|
91
|
+ // Save the full client hello bytes so we can replay them for each secret.
|
|
|
92
|
+ handshakeBytes := clientHelloCopy.Bytes()
|
|
115
|
93
|
|
|
116
|
94
|
for idx, secret := range secrets {
|
|
117
|
95
|
digest := hmac.New(sha256.New, secret)
|
|
|
@@ -147,152 +125,6 @@ func ReadClientHelloMulti(
|
|
147
|
125
|
return nil, ErrBadDigest
|
|
148
|
126
|
}
|
|
149
|
127
|
|
|
150
|
|
-// reassembleTLSHandshake reads one or more TLS records from conn,
|
|
151
|
|
-// validates the record type and version, and reassembles fragmented
|
|
152
|
|
-// handshake payloads into a single TLS record.
|
|
153
|
|
-//
|
|
154
|
|
-// Per RFC 5246 Section 6.2.1, handshake messages may be fragmented
|
|
155
|
|
-// across multiple TLS records. DPI bypass tools like ByeDPI use this
|
|
156
|
|
-// to evade censorship.
|
|
157
|
|
-//
|
|
158
|
|
-// The returned buffer contains the full TLS record (header + payload)
|
|
159
|
|
-// so that callers can include the header in HMAC computation.
|
|
160
|
|
-func reassembleTLSHandshake(conn io.Reader) (*bytes.Buffer, error) {
|
|
161
|
|
- header := [tls.SizeHeader]byte{}
|
|
162
|
|
-
|
|
163
|
|
- if _, err := io.ReadFull(conn, header[:]); err != nil {
|
|
164
|
|
- return nil, fmt.Errorf("cannot read record header: %w", err)
|
|
165
|
|
- }
|
|
166
|
|
-
|
|
167
|
|
- length := int64(binary.BigEndian.Uint16(header[3:]))
|
|
168
|
|
- payload := &bytes.Buffer{}
|
|
169
|
|
-
|
|
170
|
|
- if _, err := io.CopyN(payload, conn, length); err != nil {
|
|
171
|
|
- return nil, fmt.Errorf("cannot read record payload: %w", err)
|
|
172
|
|
- }
|
|
173
|
|
-
|
|
174
|
|
- if header[0] != tls.TypeHandshake {
|
|
175
|
|
- return nil, fmt.Errorf("unexpected record type %#x", header[0])
|
|
176
|
|
- }
|
|
177
|
|
-
|
|
178
|
|
- if header[1] != 3 || header[2] != 1 {
|
|
179
|
|
- return nil, fmt.Errorf("unexpected protocol version %#x %#x", header[1], header[2])
|
|
180
|
|
- }
|
|
181
|
|
-
|
|
182
|
|
- // Reassemble fragmented payload. continuationCount caps the total
|
|
183
|
|
- // number of continuation records across both phases below.
|
|
184
|
|
- continuationCount := 0
|
|
185
|
|
-
|
|
186
|
|
- // Phase 1: read continuation records until we have at least the
|
|
187
|
|
- // 4-byte handshake header (type + uint24 length) to determine the
|
|
188
|
|
- // expected total size.
|
|
189
|
|
- for ; payload.Len() < 4 && continuationCount < maxContinuationRecords; continuationCount++ {
|
|
190
|
|
- prevLen := payload.Len()
|
|
191
|
|
-
|
|
192
|
|
- if err := readContinuationRecord(conn, payload); err != nil {
|
|
193
|
|
- payload.Truncate(prevLen) // discard partial data on error
|
|
194
|
|
-
|
|
195
|
|
- if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
|
|
196
|
|
- break // no more records — let downstream parsing handle what we have
|
|
197
|
|
- }
|
|
198
|
|
-
|
|
199
|
|
- return nil, err
|
|
200
|
|
- }
|
|
201
|
|
- }
|
|
202
|
|
-
|
|
203
|
|
- // Phase 2: we know the expected handshake size — read remaining
|
|
204
|
|
- // continuation records until the payload is complete.
|
|
205
|
|
- if payload.Len() >= 4 {
|
|
206
|
|
- p := payload.Bytes()
|
|
207
|
|
- expectedTotal := 4 + (int(p[1])<<16 | int(p[2])<<8 | int(p[3]))
|
|
208
|
|
-
|
|
209
|
|
- if expectedTotal > 0xFFFF {
|
|
210
|
|
- return nil, fmt.Errorf("handshake message too large: %d bytes", expectedTotal)
|
|
211
|
|
- }
|
|
212
|
|
-
|
|
213
|
|
- for ; payload.Len() < expectedTotal && continuationCount < maxContinuationRecords; continuationCount++ {
|
|
214
|
|
- if err := readContinuationRecord(conn, payload); err != nil {
|
|
215
|
|
- return nil, err
|
|
216
|
|
- }
|
|
217
|
|
- }
|
|
218
|
|
-
|
|
219
|
|
- if payload.Len() < expectedTotal {
|
|
220
|
|
- return nil, fmt.Errorf("cannot reassemble handshake: too many continuation records")
|
|
221
|
|
- }
|
|
222
|
|
-
|
|
223
|
|
- payload.Truncate(expectedTotal)
|
|
224
|
|
- }
|
|
225
|
|
-
|
|
226
|
|
- if payload.Len() > 0xFFFF {
|
|
227
|
|
- return nil, fmt.Errorf("reassembled payload too large: %d bytes", payload.Len())
|
|
228
|
|
- }
|
|
229
|
|
-
|
|
230
|
|
- // Reconstruct a single TLS record with the reassembled payload.
|
|
231
|
|
- result := &bytes.Buffer{}
|
|
232
|
|
- result.Grow(tls.SizeHeader + payload.Len())
|
|
233
|
|
- result.Write(header[:3])
|
|
234
|
|
- binary.Write(result, binary.BigEndian, uint16(payload.Len())) //nolint:errcheck // bytes.Buffer.Write never fails
|
|
235
|
|
- result.Write(payload.Bytes())
|
|
236
|
|
-
|
|
237
|
|
- return result, nil
|
|
238
|
|
-}
|
|
239
|
|
-
|
|
240
|
|
-// readContinuationRecord reads the next TLS record header and appends its
|
|
241
|
|
-// full payload to dst. It returns an error if the record is not a handshake
|
|
242
|
|
-// record.
|
|
243
|
|
-func readContinuationRecord(conn io.Reader, dst *bytes.Buffer) error {
|
|
244
|
|
- nextHeader := [tls.SizeHeader]byte{}
|
|
245
|
|
-
|
|
246
|
|
- if _, err := io.ReadFull(conn, nextHeader[:]); err != nil {
|
|
247
|
|
- return fmt.Errorf("cannot read continuation record header: %w", err)
|
|
248
|
|
- }
|
|
249
|
|
-
|
|
250
|
|
- if nextHeader[0] != tls.TypeHandshake {
|
|
251
|
|
- return fmt.Errorf("unexpected continuation record type %#x", nextHeader[0])
|
|
252
|
|
- }
|
|
253
|
|
-
|
|
254
|
|
- if nextHeader[1] != 3 || nextHeader[2] != 1 {
|
|
255
|
|
- return fmt.Errorf("unexpected continuation record version %#x %#x", nextHeader[1], nextHeader[2])
|
|
256
|
|
- }
|
|
257
|
|
-
|
|
258
|
|
- nextLength := int64(binary.BigEndian.Uint16(nextHeader[3:]))
|
|
259
|
|
-
|
|
260
|
|
- if nextLength == 0 {
|
|
261
|
|
- return fmt.Errorf("zero-length continuation record")
|
|
262
|
|
- }
|
|
263
|
|
-
|
|
264
|
|
- if _, err := io.CopyN(dst, conn, nextLength); err != nil {
|
|
265
|
|
- return fmt.Errorf("cannot read continuation record payload: %w", err)
|
|
266
|
|
- }
|
|
267
|
|
-
|
|
268
|
|
- return nil
|
|
269
|
|
-}
|
|
270
|
|
-
|
|
271
|
|
-func parseHandshakeHeader(r io.Reader) (io.Reader, error) {
|
|
272
|
|
- // type(1) + size(3 / uint24)
|
|
273
|
|
- // 01 - handshake message type 0x01 (client hello)
|
|
274
|
|
- // 00 00 f4 - 0xF4 (244) bytes of client hello data follows
|
|
275
|
|
- header := [1 + 3]byte{}
|
|
276
|
|
-
|
|
277
|
|
- if _, err := io.ReadFull(r, header[:]); err != nil {
|
|
278
|
|
- return nil, fmt.Errorf("cannot read handshake header: %w", err)
|
|
279
|
|
- }
|
|
280
|
|
-
|
|
281
|
|
- if header[0] != TypeHandshakeClient {
|
|
282
|
|
- return nil, fmt.Errorf("incorrect handshake type: %#x", header[0])
|
|
283
|
|
- }
|
|
284
|
|
-
|
|
285
|
|
- // unfortunately there is not uint24 in golang, so we just reust header
|
|
286
|
|
- header[0] = 0
|
|
287
|
|
-
|
|
288
|
|
- length := int64(binary.BigEndian.Uint32(header[:]))
|
|
289
|
|
- buf := &bytes.Buffer{}
|
|
290
|
|
-
|
|
291
|
|
- _, err := io.CopyN(buf, r, length)
|
|
292
|
|
-
|
|
293
|
|
- return buf, err
|
|
294
|
|
-}
|
|
295
|
|
-
|
|
296
|
128
|
func parseHandshake(r io.Reader) (*ClientHello, error) {
|
|
297
|
129
|
// A protocol version of "3,3" (meaning TLS 1.2) is given.
|
|
298
|
130
|
header := [2]byte{}
|