-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.go
554 lines (431 loc) · 15.7 KB
/
decoder.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package binarycookies
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"time"
)
// Decode reads the entire file, validates and returns all cookies.
func (b *BinaryCookies) Decode() ([]Page, error) {
if err := b.readSignature(); err != nil {
return nil, err
}
if err := b.readPageSize(); err != nil {
return nil, err
}
if err := b.readAllPages(); err != nil {
return nil, err
}
for i := 0; i < int(b.size); i++ {
if err := b.readOnePage(); err != nil {
return nil, err
}
}
if err := b.readChecksum(); err != nil {
return nil, err
}
// NOTES(cixtor): optional extra bytes may exist after this point, these
// bytes usually represent a Binary Property List (bplist00) and contain
// a dictionary with additional information, for example, the cookie accept
// policy for all tasks within sessions based on the website configuration.
//
// Example:
//
// $ hexdump -v -C Cookies.binarycookies
// [...]
// 00000000 62 70 6c 69 73 74 30 30 d1 01 02 5f 10 18 4e 53 |bplist00..._..NS|
// 00000010 48 54 54 50 43 6f 6f 6b 69 65 41 63 63 65 70 74 |HTTPCookieAccept|
// 00000020 50 6f 6c 69 63 79 10 02 08 0b 26 00 00 00 00 00 |Policy....&.....|
// 00000030 00 01 01 00 00 00 00 00 00 00 03 00 00 00 00 00 |................|
// 00000040 00 00 00 00 00 00 00 00 00 00 28 |..........(|
// 0000004b
//
// $ plutil -p Tail.plist
// {
// "NSHTTPCookieAcceptPolicy" => 2
// }
return b.pages, nil
}
// readSignature reads a number of bytes that are supposed to represent the
// magic number of valid binary cookies. If the file format is different then
// the function returns an error with some information.
func (b *BinaryCookies) readSignature() error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readSignature %q; %w", data[:n], err)
}
if !bytes.Equal(data, magic) {
return fmt.Errorf("readSignature invalid signature %q", data)
}
return nil
}
// readPageSize reads an integer representing the number of pages in the file.
func (b *BinaryCookies) readPageSize() error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageSize from %q; %w", data[:n], err)
}
b.size = binary.BigEndian.Uint32(data)
return nil
}
// readAllPages reads the size for all pages in the file.
func (b *BinaryCookies) readAllPages() error {
size := int(b.size)
data := make([]byte, 4)
for i := 0; i < size; i++ {
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readAllPages %q; %w", data[:n], err)
}
b.page = append(b.page, binary.BigEndian.Uint32(data))
}
return nil
}
// readOnePage reads one single page in the file.
func (b *BinaryCookies) readOnePage() error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readOnePage page tag %q; %w", data[:n], err)
}
if !bytes.Equal(data, []byte{0x0, 0x0, 0x1, 0x0}) {
return fmt.Errorf("readOnePage invalid page tag %q", data)
}
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readOnePage number of cookies %q; %w", data[:n], err)
}
length := binary.LittleEndian.Uint32(data)
offsets := make([]uint32, int(length))
for i := 0; i < int(length); i++ {
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readOnePage cookie offset %q; %w", data[:n], err)
}
offsets[i] = binary.LittleEndian.Uint32(data)
}
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readOnePage page end %q; %w", data[:n], err)
}
if !bytes.Equal(data, []byte{0x0, 0x0, 0x0, 0x0}) {
return fmt.Errorf("readOnePage invalid page end %q", data)
}
cookies, err := b.readPageCookies(length)
if err != nil {
return err
}
b.pages = append(b.pages, Page{
Length: length,
Offsets: offsets,
Cookies: cookies,
})
return nil
}
// readPageCookies reads and returns all cookies associated to a single page.
func (b *BinaryCookies) readPageCookies(length uint32) ([]Cookie, error) {
var err error
var cookie Cookie
cookies := make([]Cookie, length)
for i := uint32(0); i < length; i++ {
if cookie, err = b.readPageCookie(); err != nil {
return []Cookie{}, err
}
cookies[i] = cookie
}
return cookies, nil
}
// readPageCookie reads and returns one cookie associated to a single page.
func (b *BinaryCookies) readPageCookie() (Cookie, error) {
var err error
var cookie Cookie
functions := []cookieHelperFunction{
b.readPageCookieSize,
b.readPageCookieUnknownOne,
b.readPageCookieFlags,
b.readPageCookieUnknownTwo,
b.readPageCookieDomainOffset,
b.readPageCookieNameOffset,
b.readPageCookiePathOffset,
b.readPageCookieValueOffset,
b.readPageCookieCommentOffset,
b.readPageCookieEndHeader,
b.readPageCookieExpires,
b.readPageCookieCreation,
b.checkCookieOverallSize,
b.readPageCookieComment,
b.readPageCookieDomain,
b.readPageCookieName,
b.readPageCookiePath,
b.readPageCookieValue,
}
for _, fun := range functions {
if err = fun(&cookie); err != nil {
return Cookie{}, err
}
}
return cookie, nil
}
// RFC 2965
// HTTP State Management Mechanism
// https://www.ietf.org/rfc/rfc2965.txt
//
// # Section 5.3 Implementation Limits
//
// Practical user agent implementations have limits on the number and
// size of cookies that they can store. In general, user agents' cookie
// support should have no fixed limits. They should strive to store as
// many frequently-used cookies as possible. Furthermore, general-use
// user agents SHOULD provide each of the following minimum capabilities
// individually, although not necessarily simultaneously:
//
// - at least 300 cookies
//
// - at least 4096 bytes per cookie (as measured by the characters
// that comprise the cookie non-terminal in the syntax description
// of the Set-Cookie2 header, and as received in the Set-Cookie2
// header)
//
// - at least 20 cookies per unique host or domain name
//
// User agents created for specific purposes or for limited-capacity
// devices SHOULD provide at least 20 cookies of 4096 bytes, to ensure
// that the user can interact with a session-based origin server.
const maxCookieSize = 4096
func (b *BinaryCookies) checkCookieOverallSize(cookie *Cookie) error {
var total uint32
sizes := []uint32{
/* Cookie Domain */ (cookie.domainOffset - cookie.commentOffset),
/* Cookie Name */ (cookie.nameOffset - cookie.domainOffset),
/* Cookie Path */ (cookie.pathOffset - cookie.nameOffset),
/* Cookie Value */ (cookie.valueOffset - cookie.pathOffset),
/* Cookie Comment */ (cookie.Size - cookie.valueOffset),
}
// Check if cookie component in case of uint overflow.
for i, n := range sizes {
if n > maxCookieSize {
return fmt.Errorf("maximum cookie size exceeded in component[%d] (%d > 4096)", i, n)
}
total += n
}
if total > maxCookieSize {
// Cookie Limitations
//
// Most browsers support cookies of up to 4096 bytes. Because of this
// small limit, cookies are best used to store small amounts of data,
// or better yet, an identifier such as a user ID. The user ID can
// then be used to identify the user and read user information from a
// database or other data store.
//
// http://msdn.microsoft.com/en-us/library/ms178194.aspx
return fmt.Errorf("maximum overall cookie size exceeded %d > 4096", total)
}
return nil
}
// readPageCookieSize reads and stores the cookie size.
func (b *BinaryCookies) readPageCookieSize(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie size %q; %w", data[:n], err)
}
cookie.Size = binary.LittleEndian.Uint32(data)
return nil
}
// readPageCookieUnknownOne reads and stores one of the unknown cookie fields.
func (b *BinaryCookies) readPageCookieUnknownOne(cookie *Cookie) error {
data := make([]byte, 4)
// NOTES(cixtor): unknown field that some people believe is related to the
// cookie flags but so far no relevant articles online have been able to
// confirm this claim. It may be possible to discover the purpose of these
// bytes with some reverse engineering work on modern browsers.
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie unknown one %q; %w", data[:n], err)
}
cookie.unknownOne = data
return nil
}
// readPageCookieFlags reads and stores the cookie flags.
func (b *BinaryCookies) readPageCookieFlags(cookie *Cookie) error {
data := make([]byte, 4)
// NOTES(cixtor): cookie flags: secure, httpOnly, sameSite.
// - 0x0 = None
// - 0x1 = Secure
// - 0x4 = HttpOnly
// - 0x5 = Secure+HttpOnly
// Ref: https://en.wikipedia.org/wiki/HTTP_cookie#Terminology
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie flags %q; %w", data[:n], err)
}
cookie.Flags = binary.LittleEndian.Uint32(data)
if cookie.Flags == 0x0 {
cookie.Secure = false
cookie.HttpOnly = false
} else if cookie.Flags == 0x1 {
cookie.Secure = true
} else if cookie.Flags == 0x4 {
cookie.HttpOnly = true
} else if cookie.Flags == 0x5 {
cookie.Secure = true
cookie.HttpOnly = true
}
return nil
}
// readPageCookieUnknownTwo reads and stores one of the unknown cookie fields.
func (b *BinaryCookies) readPageCookieUnknownTwo(cookie *Cookie) error {
data := make([]byte, 4)
// NOTES(cixtor): unknown field that some people believe is related to the
// cookie flags but so far no relevant articles online have been able to
// confirm this claim. It may be possible to discover the purpose of these
// bytes with some reverse engineering work on modern browsers.
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie unknown two %q; %w", data[:n], err)
}
cookie.unknownTwo = data
return nil
}
// readPageCookieDomainOffset reads and stores the cookie domain offset.
func (b *BinaryCookies) readPageCookieDomainOffset(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie domain offset %q; %w", data[:n], err)
}
cookie.domainOffset = binary.LittleEndian.Uint32(data)
return nil
}
// readPageCookieNameOffset reads and stores the cookie name offset.
func (b *BinaryCookies) readPageCookieNameOffset(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie name offset %q; %w", data[:n], err)
}
cookie.nameOffset = binary.LittleEndian.Uint32(data)
return nil
}
// readPageCookiePathOffset reads and stores the cookie path offset.
func (b *BinaryCookies) readPageCookiePathOffset(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie path offset %q; %w", data[:n], err)
}
cookie.pathOffset = binary.LittleEndian.Uint32(data)
return nil
}
// readPageCookieValueOffset reads and stores the cookie value offset.
func (b *BinaryCookies) readPageCookieValueOffset(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie value offset %q; %w", data[:n], err)
}
cookie.valueOffset = binary.LittleEndian.Uint32(data)
return nil
}
// readPageCookieCommentOffset reads and stores the cookie comment offset.
func (b *BinaryCookies) readPageCookieCommentOffset(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie comment offset %q; %w", data[:n], err)
}
cookie.commentOffset = binary.LittleEndian.Uint32(data)
return nil
}
// readPageCookieEndHeader reads and validates the cookie end header.
func (b *BinaryCookies) readPageCookieEndHeader(cookie *Cookie) error {
data := make([]byte, 4)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie end header %q; %w", data[:n], err)
}
if !bytes.Equal(data, []byte{0x0, 0x0, 0x0, 0x0}) {
return fmt.Errorf("readPageCookie invalid end header %q", data)
}
return nil
}
// readPageCookieExpires reads and decodes the cookie expiration time.
func (b *BinaryCookies) readPageCookieExpires(cookie *Cookie) error {
data := make([]byte, 8)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie expiration time %q; %w", data[:n], err)
}
// NOTES(cixtor): convert Mac epoch time into a Unix timestamp.
number := math.Float64frombits(binary.LittleEndian.Uint64(data))
cookie.Expires = time.Unix(int64(number+timePadding), 0)
return nil
}
// readPageCookieCreation reads and decodes the cookie creation time.
func (b *BinaryCookies) readPageCookieCreation(cookie *Cookie) error {
data := make([]byte, 8)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie creation time %q; %w", data[:n], err)
}
// NOTES(cixtor): convert Mac epoch time into a Unix timestamp.
number := math.Float64frombits(binary.LittleEndian.Uint64(data))
cookie.Creation = time.Unix(int64(number+timePadding), 0)
return nil
}
// readPageCookieComment reads and stores the cookie comment field.
//
// Because cookies can contain private information about a user, the Cookie
// attribute allows an origin server to document its intended use of a cookie.
// The user can inspect the information to decide whether to initiate or
// continue a session with this cookie.
//
// Cookie comments are optional. If the comment offset is zero it means there
// are no comments in the file associated to the cookie we are decoding. This
// information is usually confused with the end cookie header which is always
// 0x00000000 but it is important to distinguish between these values to read
// the entire cookie data.
func (b *BinaryCookies) readPageCookieComment(cookie *Cookie) error {
if cookie.commentOffset == 0 {
return nil
}
data := make([]byte, cookie.domainOffset-cookie.commentOffset)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie comment text %q; %w", data[:n], err)
}
cookie.Comment = data
return nil
}
// readPageCookieDomain reads and stores the cookie domain field.
func (b *BinaryCookies) readPageCookieDomain(cookie *Cookie) error {
data := make([]byte, cookie.nameOffset-cookie.domainOffset)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie domain text %q; %w", data[:n], err)
}
// NOTES(cixtor): fix null-terminated string.
cookie.Domain = data[0 : len(data)-1]
return nil
}
// readPageCookieName reads and stores the cookie name field.
func (b *BinaryCookies) readPageCookieName(cookie *Cookie) error {
data := make([]byte, cookie.pathOffset-cookie.nameOffset)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie name text %q; %w", data[:n], err)
}
// NOTES(cixtor): fix null-terminated string.
cookie.Name = data[0 : len(data)-1]
return nil
}
// readPageCookiePath reads and stores the cookie path field.
func (b *BinaryCookies) readPageCookiePath(cookie *Cookie) error {
data := make([]byte, cookie.valueOffset-cookie.pathOffset)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie path text %q; %w", data[:n], err)
}
// NOTES(cixtor): fix null-terminated string.
cookie.Path = data[0 : len(data)-1]
return nil
}
// readPageCookieValue reads and stores the cookie value field.
func (b *BinaryCookies) readPageCookieValue(cookie *Cookie) error {
data := make([]byte, cookie.Size-cookie.valueOffset)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readPageCookie value text %q; %w", data[:n], err)
}
// NOTES(cixtor): fix null-terminated string.
cookie.Value = data[0 : len(data)-1]
return nil
}
// readChecksum reads and stores the checksum of the entire file.
func (b *BinaryCookies) readChecksum() error {
data := make([]byte, 8)
if n, err := b.file.Read(data); err != nil {
return fmt.Errorf("readChecksum from %q; %w", data[:n], err)
}
b.checksum = data
return nil
}