-
Notifications
You must be signed in to change notification settings - Fork 32
/
rtmp.bpl
738 lines (642 loc) · 12.8 KB
/
rtmp.bpl
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
const (
VERBOSE = 0
RAWDATA = 0
)
init = {
global filterFlashVer = BPL_FILTER["flashVer"]
global filterReqMode = BPL_FILTER["reqMode"]
global filterPlay = (filterReqMode == "play")
global filterDir = BPL_FILTER["dir"]
if filterDir != undefined {
if filterDir != BPL_DIRECTION {
do exit(0)
}
}
global fmsKey = bytes.from([
0x47, 0x65, 0x6e, 0x75, 0x69, 0x6e, 0x65, 0x20,
0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x46, 0x6c,
0x61, 0x73, 0x68, 0x20, 0x4d, 0x65, 0x64, 0x69,
0x61, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
0x20, 0x30, 0x30, 0x31, // Genuine Adobe Flash Media Server 001
0xf0, 0xee, 0xc2, 0x4a, 0x80, 0x68, 0xbe, 0xe8,
0x2e, 0x00, 0xd0, 0xd1, 0x02, 0x9e, 0x7e, 0x57,
0x6e, 0xec, 0x5d, 0x2d, 0x29, 0x80, 0x6f, 0xab,
0x93, 0xb8, 0xe6, 0x36, 0xcf, 0xeb, 0x31, 0xae,
])
global fpKey = bytes.from([
0x47, 0x65, 0x6E, 0x75, 0x69, 0x6E, 0x65, 0x20,
0x41, 0x64, 0x6F, 0x62, 0x65, 0x20, 0x46, 0x6C,
0x61, 0x73, 0x68, 0x20, 0x50, 0x6C, 0x61, 0x79,
0x65, 0x72, 0x20, 0x30, 0x30, 0x31, // Genuine Adobe Flash Player 001
0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8,
0x2E, 0x00, 0xD0, 0xD1, 0x02, 0x9E, 0x7E, 0x57,
0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB,
0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE,
])
if BPL_DIRECTION == "REQ" {
global handshakeKey = fpKey[:30]
} else {
global handshakeKey = fmsKey[:36]
}
global lastMsgs = mkmap("int:var")
global chunksize = 128
global objectend = errors.new("object end")
global limitTypes = {
0: "Hard",
1: "Soft",
2: "Dynamic",
}
global audioFormats = {
0: "Linear PCM",
1: "ADPCM",
2: "MP3",
3: "Linear PCM le",
4: "Nellymoser 16kHz",
5: "Nellymoser 8kHz",
6: "Nellymoser",
7: "G711 A-law",
8: "G711 mu-law",
9: "Reserved",
10: "AAC",
11: "Speex",
12: "MP3 8kHz",
13: "Device Specific",
}
global audioRates = ["5.5 kHz", "11 kHz", "22 kHz", "44 kHz"]
global audioBits = ["8 bits", "16 bits"]
global audioChannels = ["Mono", "Stereo"]
global videoTypes = {
1: "AVC keyframe",
2: "AVC inter frame",
3: "H.263 disposable inter frame",
4: "generated keyframe",
5: "video info/command frame",
}
global videoCodecs = {
1: "JPEG",
2: "H.263",
3: "screen video",
4: "On2 VP6",
5: "On2 VP6 with alpha channel",
6: "screen video v2",
7: "AVC",
}
global avcTypes = {
0: "sequence header",
1: "NALU",
2: "end of sequence",
}
}
// --------------------------------------------------------------
AMF0_NUMBER = {
val float64be
if VERBOSE == 0 {
return val
}
}
AMF0_BOOLEAN = {
val byte
if VERBOSE == 0 {
return byte != 0
}
}
AMF0_STRING = {
len uint16be
val [len]char
if VERBOSE == 0 {
return val
}
}
AMF0_OBJECT_ITEMS = {
_key AMF0_STRING
_val AMF0_TYPE
let items = mkslice("var", 2)
do set(items, 0, _key, 1, _val)
if _val != objectend {
_next AMF0_OBJECT_ITEMS
let items = append(items, _next.items...)
}
}
AMF0_OBJECT_NORMAL = {
val AMF0_OBJECT_ITEMS
let n = len(val.items)
return mapFrom(val.items[:n-2]...) // 去掉了最后的 objectend
}
AMF0_OBJECT_VERBOSE = {
_key AMF0_STRING
_val AMF0_TYPE
let items = [{"key": _key, "val": _val}]
if _val.marker != 0x09 {
_next AMF0_OBJECT_VERBOSE
let items = append(items, _next.items...)
}
}
AMF0_OBJECT = if VERBOSE do AMF0_OBJECT_VERBOSE else AMF0_OBJECT_NORMAL
AMF0_STRICT_ARRAY = {
len uint32be
objs [len]AMF0_TYPE
if VERBOSE == 0 {
return objs
}
}
AMF0_MOVIECLIP = {
body *byte
fatal "todo - AMF0_MOVIECLIP"
}
AMF0_NULL = {
if VERBOSE {
let val = nil
} else {
return nil
}
}
AMF0_UNDEFINED = {
if VERBOSE {
let val = undefined
} else {
return undefined
}
}
AMF0_REFERENCE = {
reference uint16be
}
AMF0_ECMA_ARRAY = {
len uint32be
val AMF0_OBJECT
if VERBOSE == 0 {
return val
}
}
AMF0_OBJECT_END = if VERBOSE do nil else {
return objectend
}
AMF0_DATE = {
timestamp float64be
tz uint16be
}
AMF0_LONG_STRING = {
len uint32be
val [len]char
if VERBOSE == 0 {
return val
}
}
AMF0_UNSUPPORTED = {
body *byte
fatal "todo - AMF0_UNSUPPORTED"
}
AMF0_RECORDSET = {
body *byte
fatal "todo - AMF0_RECORDSET"
}
AMF0_XML_DOCUMENT = AMF0_LONG_STRING
AMF0_TYPED_OBJECT = {
type AMF0_STRING
val AMF0_OBJECT
}
AMF0_ACMPLUS_OBJECT = { // Switch to AMF3
body *byte
fatal "todo - AMF0_ACMPLUS_OBJECT"
}
AMF0_TYPE = {
marker byte
case marker {
0x00: AMF0_NUMBER
0x01: AMF0_BOOLEAN
0x02: AMF0_STRING
0x03: AMF0_OBJECT
0x04: AMF0_MOVIECLIP
0x05: AMF0_NULL
0x06: AMF0_UNDEFINED
0x07: AMF0_REFERENCE
0x08: AMF0_ECMA_ARRAY
0x09: AMF0_OBJECT_END
0x0a: AMF0_STRICT_ARRAY
0x0b: AMF0_DATE
0x0c: AMF0_LONG_STRING
0x0d: AMF0_UNSUPPORTED
0x0e: AMF0_RECORDSET
0x0f: AMF0_XML_DOCUMENT
0x10: AMF0_TYPED_OBJECT
0x11: AMF0_ACMPLUS_OBJECT
}
}
AMF0_CMDDATA = {
cmd AMF0_TYPE
transactionId AMF0_TYPE
value *AMF0_TYPE
if filterFlashVer != undefined {
if cmd == "connect" {
if value[0].flashVer != filterFlashVer {
do exit(0)
}
}
}
if filterPlay {
if cmd == "FCPublish" {
do exit(0)
}
}
}
AMF0 = {
msg *AMF0_TYPE
}
AMF0_CMD = {
msg AMF0_CMDDATA
}
// --------------------------------------------------------------
AMF3_UNDEFINED = AMF0_UNDEFINED
AMF3_NULL = AMF0_NULL
AMF3_FALSE = {
if VERBOSE {
let val = false
} else {
return false
}
}
AMF3_TRUE = {
if VERBOSE {
let val = true
} else {
return true
}
}
AMF3_INT = {
b1 byte
if b1 & 0x80 {
let b1 = b1 & 0x7f
b2 byte
if b2 & 0x80 {
let b2 = b2 & 0x7f
b3 byte
if b3 & 0x80 {
let b3 = b3 & 0x7f
b4 byte
return (b1 << 22) | (b2 << 15) | (b3 << 8) | b4
} else {
return (b1 << 14) | (b2 << 7) | b3
}
} else {
return (b1 << 7) | b2
}
} else {
return int(b1)
}
}
AMF3_INTEGER_VERBOSE = {
val AMF3_INT
}
AMF3_INTEGER = if VERBOSE do AMF3_INTEGER_VERBOSE else AMF3_INT
AMF3_DOUBLE = {
val float64be
if VERBOSE == 0 {
return val
}
}
AMF3_STRING = {
tag AMF3_INT
assert (tag & 1) != 0 // reference unsupported
if tag & 1 {
val [tag >> 1]char
}
if VERBOSE == 0 {
return val
}
}
AMF3_XMLDOC = {
body *byte
}
AMF3_DATE = {
tag AMF3_INT
assert (tag & 1) != 0 // reference unsupported
timestamp float64be
let tz = tag >> 1
if VERBOSE == 0 {
do unset("tag")
}
}
AMF3_ARRAY = {
tag AMF3_INT
assert (tag & 1) != 0 // reference unsupported
let len = tag >> 1
body *byte
fatal "todo - AMF3_ARRAY"
}
AMF3_OBJECT = {
body *byte
fatal "todo - AMF3_OBJECT"
}
AMF3_XML = {
body *byte
fatal "todo - AMF3_XML"
}
AMF3_BYTE_ARRAY = {
body *byte
do println(hex.dump(body))
fatal "todo - AMF3_BYTE_ARRAY"
}
AMF3_VECTOR_INT = {
body *byte
fatal "todo - AMF3_VECTOR_INT"
}
AMF3_VECTOR_UINT = {
body *byte
fatal "todo - AMF3_VECTOR_UINT"
}
AMF3_VECTOR_DOUBLE = {
body *byte
fatal "todo - AMF3_VECTOR_DOUBLE"
}
AMF3_VECTOR_OBJECT = {
body *byte
fatal "todo - AMF3_VECTOR_OBJECT"
}
AMF3_DICTIONARY = {
body *byte
fatal "todo - AMF3_DICTIONARY"
}
AMF3_TYPE = {
marker byte
case marker {
0x00: AMF3_UNDEFINED
0x01: AMF3_NULL
0x02: AMF3_FALSE
0x03: AMF3_TRUE
0x04: AMF3_INTEGER
0x05: AMF3_DOUBLE
0x06: AMF3_STRING
0x07: AMF3_XMLDOC
0x08: AMF3_DATE
0x09: AMF3_ARRAY
0x0a: AMF3_OBJECT
0x0b: AMF3_XML
0x0c: AMF3_BYTE_ARRAY
0x0d: AMF3_VECTOR_INT
0x0e: AMF3_VECTOR_UINT
0x0f: AMF3_VECTOR_DOUBLE
0x10: AMF3_VECTOR_OBJECT
0x11: AMF3_DICTIONARY
}
}
AMF3_CMDDATA = {
cmd AMF3_TYPE
transactionId AMF3_TYPE
value *AMF3_TYPE
}
AMF3 = {
msg *AMF3_TYPE
}
AMF3_CMD = {
msg AMF3_CMDDATA
}
// --------------------------------------------------------------
AudioData = {
tag byte
let format = tag >> 4
let formatKind = audioFormats[format]
let rate = (tag>>2) & 3
let rateKind = audioRates[rate]
let bits = (tag>>1) & 1
let bitsKind = audioBits[bits]
let channel = tag & 1
let channelKind = audioChannels[channel]
if formatKind == "AAC" {
aacPacketType byte
if aacPacketType == 0 {
let aacPacketTypeKind = "sequence header"
} else {
let aacPacketTypeKind = "raw data"
}
}
if RAWDATA != 0 {
raw *byte
}
}
Audio = {
audio AudioData
}
// --------------------------------------------------------------
VideoData = {
tag byte
let type = tag >> 4
let typeKind = videoTypes[type]
let codec = tag & 0xf
let codecKind = videoCodecs[codec]
if codecKind == "AVC" {
avctype byte
compositionTime uint24be
let avctypeKind = avcTypes[int(avctype)]
if avctype == 0 { // sequence header
configurationVersion byte
avcProfileIndication byte
profileCompatibility byte
avcLevelIndication byte
lengthSizeMinusOne byte
numOfSPS byte // SPS = SequenceParameterSets
bytesOfSPS uint16be
dataOfSPS [bytesOfSPS]byte // SPS包含视频长、宽的信息
numOfPPS byte // PPS = PictureParameterSets
bytesOfPPS uint16be
dataOfPPS [bytesOfPPS]byte
let lengthSizeMinusOne = (lengthSizeMinusOne & 3) + 1
let numOfSPS = numOfSPS & 0x1F
let numOfPPS = numOfPPS & 0x1F
unknown *byte
}
}
if RAWDATA != 0 {
raw *byte
}
}
Video = {
video VideoData
}
// --------------------------------------------------------------
SetChunkSize = {
size uint32be
let chunksize = size
}
Abort = {
csid uint32be
let _last = lastMsgs[csid]
do set(_last, "remain", 0)
}
UserControl = {
evType uint16be
evData *byte
}
AckWinsize = {
winsize uint32be
}
SetPeerBandwidth = {
winsize uint32be
limitType byte
let limitTypeKind = limitTypes[int(limitType)]
}
// --------------------------------------------------------------
Handshake0 = {
h0 byte
assert h0 == 3
}
Handshake1Verify = {
let i = _diggestOffset
let off = (_h[i] + _h[i+1] + _h[i+2] + _h[i+3]) % (764 - 32 - 4) + i + 4
let data1 = _h[:off]
let data2 = _h[off+32:]
let digest = _h[off:off+32]
let h = hmac.new(sha256.new, handshakeKey)
do h.write(data1)
do h.write(data2)
let ok = bytes.equal(h.sum(nil), digest)
}
Handshake1 = {
_h1 [1536]byte
eval _h1 do {
time uint32be
version uint32be
global _h = _h1
global _diggestOffset = 772
_verify1 Handshake1Verify
if _verify1.ok {
let digest = _verify1.digest
let msg = "Flash after 10.0.32.18"
} else {
global _diggestOffset = 8
_verify2 Handshake1Verify
if _verify2.ok {
let digest = _verify2.digest
let msg = "Flash before 10.0.32.18"
}
}
}
}
Handshake2 = {
_h2 [1536]byte
}
// --------------------------------------------------------------
AggregateItemHeader = {
typeid byte
length uint24be
ts uint24be
streamid uint32be
}
AggregateItem = {
header AggregateItemHeader
_body [header.length]byte
back uint32be
assert back == header.length + 11
eval _body do case header.typeid {
18: AMF0
20: AMF0_CMD
15: AMF3
17: AMF3_CMD
8: Audio
9: Video
default: let body = _body
}
}
AggregateMsg = {
msgs *AggregateItem
}
// --------------------------------------------------------------
ChunkHeader = {
_tag byte
let format = (_tag >> 6) & 3
assert format <= 3
let csid = _tag & 0x3f
if csid == 0 {
_v byte
let csid = _v + 0x40
} elif csid == 1 {
_v uint16le
let csid = _v + 0x40
}
let _last = lastMsgs[csid]
if format < 3 {
ts uint24be
if format < 2 {
length uint24be
typeid byte
if format < 1 {
streamid uint32le
} else {
let ts = ts + _last["ts"]
let streamid = _last["streamid"]
}
let remain = 0
} else {
let ts = ts + _last["ts"]
let length = _last["length"]
let typeid = _last["typeid"]
let streamid = _last["streamid"]
let remain = _last["remain"]
}
if ts == 0xffffff {
tsext uint32be
}
} else {
let ts = _last["ts"]
let length = _last["length"]
let typeid = _last["typeid"]
let streamid = _last["streamid"]
let remain = _last["remain"]
if ts == 0xffffff {
tsext uint32be
}
}
if remain == 0 {
let remain = length
let _body = bytes.buffer()
} else {
let _body = _last["body"]
}
}
Chunk = {
header ChunkHeader
let _length = chunksize
if header.remain < _length {
let _length = header.remain
}
let _header = {
"ts": header.ts,
"length": header.length,
"typeid": header.typeid,
"streamid": header.streamid,
"remain": header.remain - _length,
"body": header._body,
}
do set(lastMsgs, header.csid, _header)
data [_length]byte
do header._body.write(data)
if _length > 16 {
let data = data[:16]
}
if _header.remain == 0 {
let _body = header._body.bytes()
if header.csid == 2 && header.streamid == 0 {
eval _body do case header.typeid {
1: SetChunkSize
2: Abort
4: UserControl
5: AckWinsize
6: SetPeerBandwidth
default: let body = _body
}
} else {
eval _body do case header.typeid {
18: AMF0
20: AMF0_CMD
15: AMF3
17: AMF3_CMD
22: AggregateMsg
8: Audio
9: Video
default: let body = _body
}
}
} elif RAWDATA == 0 {
return undefined
}
}
doc = init Handshake0 Handshake1 Handshake2 dump *(Chunk dump)
// --------------------------------------------------------------