-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgadgets.js
771 lines (697 loc) · 18.4 KB
/
gadgets.js
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
const BASE = 0x3ff7ea1000n;
const bfInstructions = {};
class Gadget {
frameLocation = null;
nextRa = null;
synthesize() {
throw "not defined";
}
getSize() { //size of gadget stack frame
throw "not defined";
}
setFrameLocation(location) {
this.frameLocation = location;
}
getFrameLocation() {
if (!this.frameLocation) {
throw "location not defined";
}
return this.frameLocation;
}
setNextRa(ra) {
this.nextRa = ra;
}
getEntryPoint() {
throw "not defined";
}
}
class Sequence extends Gadget {
constructor(seq){ //array of gadgets
super();
this.seq = seq;
}
getSize() { //sum of gadget sizes
return this.seq
.map(gadget => gadget.getSize())
.reduce((p, n) => p+n, 0);
}
synthesize() {
let result = [];
for (let i = 0; i < this.seq.length; i++) {
let gadget = this.seq[i];
if(i !== this.seq.length - 1){
gadget.setNextRa(this.seq[i+1].getEntryPoint());
}else{
gadget.setNextRa(this.nextRa);
}
result = result.concat(gadget.synthesize());
}
return result;
}
setFrameLocation(location) {
super.setFrameLocation(location);
let nextLocation = location;
for(let gadget of this.seq){
gadget.setFrameLocation(nextLocation);
nextLocation += gadget.getSize();
}
}
getEntryPoint(){
return this.seq[0].getEntryPoint();
}
}
class NOP extends Gadget{
getSize() {
return 0x10;
}
synthesize() {
return [0, this.nextRa];
}
getEntryPoint() {
return 0x0000000000097a68n + BASE;
}
}
class PopA0 extends Gadget {
constructor(a0) {
super();
this.a0 = a0;
}
getSize() {
return 0x20;
}
synthesize() {
return [
0n, this.a0, 0n, this.nextRa
];
}
getEntryPoint() {
return 0x0000000000058d9en + BASE;
}
getPoppedA0Location(){
return this.getFrameLocation() + 8;
}
}
class PopS0 extends Gadget {
constructor(s0) {
super();
this.s0 = s0;
}
getSize() {
return 0x10;
}
synthesize() {
return [this.s0, this.nextRa];
}
getEntryPoint() {
return 0x000000000005c172n + BASE;
}
getPoppedS0Location() {
return this.getFrameLocation();
}
}
class Add1A0 extends Gadget {
//SIDE EFFECT: also pops s0
constructor(s0) {
super();
this.s0 = s0;
}
getSize(){
return 0x10;
}
synthesize() {
return [
this.s0,
this.nextRa
]
}
getEntryPoint(){
return 0x000000000006dc7en + BASE;
}
}
class Dec2A0 extends Gadget {
getSize(){
return 0x10;
}
synthesize() {
return [ 0, this.nextRa ];
}
getEntryPoint(){
return 0x000000000006437en + BASE;
}
}
class _LdA5_S0 extends Gadget {
/*
0x00000000000a4ac8 :
c.ldsp a4, 0x48(sp)
c.ld a5, 0(s0)
bne a4, a5, 0x10
c.ldsp ra, 0x58(sp)
c.ldsp s0, 0x50(sp)
c.addi16sp sp, 0x60
c.jr ra
*/
constructor(a4, s0){
//warning: popped a4 must equal [prev_s0]
super();
this.a4 = a4;
this.s0 = s0;
}
getSize() {
return 0x60;
}
synthesize() {
return [0, 0, 0, 0, 0, 0, 0, 0, 0, this.a4, this.s0, this.nextRa ];
}
getEntryPoint() {
return 0x00000000000a4ac8n + BASE;
}
}
class PrepareLdA0_A0 extends Sequence {
constructor() {
super([
new PopS0(0x30000000), //scratch space
new _LdA5_S0(0, 0x30000000),
])
}
}
class _LdA0_8A0 extends Gadget {
/*
0x00000000000d3230 :
c.ld a0, 8(a0)
c.add a0, a5
c.ldsp a4, 0x28(sp)
c.ld a5, 0(s0)
bne a4, a5, 0x1e
c.ldsp ra, 0x38(sp)
c.ldsp s0, 0x30(sp)
c.addi16sp sp, 0x40
c.jr ra
*/
constructor(a4, s0){
super();
this.a4 = a4;
this.s0 = s0;
}
getSize() {
return 0x40;
}
synthesize() {
return [0, 0, 0, 0, 0, this.a4, this.s0, this.nextRa ]
}
getEntryPoint() {
return 0x00000000000d3230n + BASE;
}
}
class LdA0_8A0 extends Sequence {
//reads the value of 8(a0) into a0
//SIDE EFFECTS: will cobble a4, a5, and s0
constructor() {
super([
new PrepareLdA0_A0(),
new _LdA0_8A0(0, 0x30000000)
])
}
}
class SdA0_0x10S0 extends Gadget {
/*
0x00000000000d30de :
c.ldsp ra, 8(sp)
c.sd a0, 0x10(s0)
c.ldsp s0, 0(sp)
c.addi sp, 0x10
c.jr ra
*/
constructor(nextS0){
super();
this.nextS0 = nextS0;
}
getSize(){
return 16;
}
synthesize() {
return [this.nextS0, this.nextRa];
}
getEntryPoint() {
return 0x00000000000d30den + BASE;
}
}
class WriteA0 extends Sequence {
constructor(dest, nextS0) {
super([
new PopS0(dest-0x10),
new SdA0_0x10S0(nextS0)
])
}
}
class Spacer extends Gadget {
constructor(size){
super();
this.size = size;
}
getSize() {
return this.size * 8;
}
synthesize(){
return new Array(this.size).fill(0);
}
getEntryPoint(){
return 0;
}
}
class _PopA5 extends Gadget {
//SIDE EFFECT: cobbles a0
/*
0x000000000002d9d6 :
c.ldsp a5, 8(sp)
c.ldsp ra, 0x18(sp)
c.mv a0, a5
c.addi16sp sp, 0x20
c.jr ra
*/
constructor(a5) {
super();
this.a5 = a5;
}
getSize(){
return 0x20;
}
synthesize() {
return [0, this.a5, 0, this.nextRa]
}
getEntryPoint() {
return 0x000000000002d9d6n + BASE;
}
}
class _CallA5 extends Gadget {
//PRECONDITION: s0+50 is valid scratch space
//SIDE EFFECT: pops s0, a0 is set to the retval
/*
0x00000000000b95d4 :
c.jalr a5
c.ldsp ra, 8(sp)
sd zero, 0x50(s0)
c.ldsp s0, 0(sp)
c.addi sp, 0x10
c.jr ra
*/
constructor(s0) {
super();
this.s0 = s0;
}
getSize(){
return 0x10;
}
synthesize() {
return [this.s0, this.nextRa];
}
getEntryPoint(){
return 0x00000000000b95d4n + BASE;
}
}
class _Longjmp extends Gadget {
getSize() {
return 0;
}
synthesize() {
return [];
}
getEntryPoint() {
return 0x00000000000325b4n + BASE;
}
}
class StackPivot extends Sequence {
constructor(destRa, destSp) {
super([
new PopA0(null),
new _Longjmp()
])
if(destRa && destSp){
this.setDest(destRa, destSp);
}
}
setDest(ra, sp) {
this.seq[0] = new PopA0(jmpBuf.makeTarget(ra, sp))
}
}
class SeqzA0 extends Gadget {
getSize() {
return 0x10;
}
synthesize() {
return [0, this.nextRa];
}
getEntryPoint(){
return 0x00000000000d1ad6n + BASE
}
}
class PopS0S1S2 extends Gadget {
constructor(s0, s1, s2){
super();
this.s0 = s0;
this.s1 = s1;
this.s2 = s2;
}
getSize() {
return 0x20;
}
synthesize() {
return [
this.s2, this.s1, this.s0, this.nextRa
]
}
getEntryPoint() {
return 0xa3b34n + BASE
}
}
class _AddA5A0 extends Gadget {
constructor(s0, s1, s2, s3){
super();
this.s0 = s0;
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
getSize(){
return 0x30;
}
synthesize() {
return [0, this.s3, this.s2, this.s1, this.s0, this.nextRa];
}
getEntryPoint(){
return 0x0000000000060f40n + BASE
}
}
class _AddA0A5 extends Gadget {
getSize(){
return 0x50;
}
synthesize() {
return [0, 0, 0, 0, 0, 0, 0, 0, 0, this.nextRa];
}
getEntryPoint(){
return 0x00000000000a91c0n + BASE;
}
}
//based on the truthiness of a0
class ConditionalStackPivot extends Sequence {
//cobbles a0
constructor(){
super([
new WriteA0(null, 0), //make a copy of a0 before the next gadget cobbles it
new _PopA5(0), //want a5 = 8*Seqz(A0), cobbles a0
new PopA0(0), //restored value from before
new SeqzA0(),
new PopS0S1S2(0, 0, 0x30000000),
new _AddA5A0(0, 0, 0x30000000, 0), //add a5 to a0 8 times
new _AddA5A0(0, 0, 0x30000000, 0),
new _AddA5A0(0, 0, 0x30000000, 0),
new _AddA5A0(0, 0, 0x30000000, 0),
new _AddA5A0(0, 0, 0x30000000, 0),
new _AddA5A0(0, 0, 0x30000000, 0),
new _AddA5A0(0, 0, 0x30000000, 0),
new _AddA5A0(0, 0, 0x30000000, 0),
new PopA0(null), //the jump buffer
new _AddA0A5(),
new _Longjmp()
])
}
setFrameLocation(location){
super.setFrameLocation(location);
this.seq[0] = new WriteA0(this.seq[2].getPoppedA0Location(), 0);
super.setFrameLocation(location);
}
setDests(trueRa, trueSp, falseRa, falseSp){
if(jmpBuf.nextSpot % 13 === 12) { //we cannot allocate two more jump buffers or else they will be split
jmpBuf.makeTarget(0, 0); //allocate a dummy so we move on to the next set
}
const trueJmp = jmpBuf.makeTarget(trueRa, trueSp);
const falseJmp = jmpBuf.makeTarget(falseRa, falseSp);
if(falseJmp !== trueJmp + 8) {
throw "jmpbuf didn't allocate contiguously";
}
if(!this.seq[13].getPoppedA0Location){
throw "expected pop a0 gadget";
}
this.seq[13] = new PopA0(trueJmp);
}
}
class WriteVal extends Sequence {
//preserves A0
constructor(val, dest) {
super([
new WriteA0(null, 0),
new PopA0(val),
new WriteA0(dest, 0),
new PopA0(0)
])
}
setFrameLocation(location) {
super.setFrameLocation(location);
this.seq[0] = new WriteA0(this.seq[3].getPoppedA0Location(), 0);
super.setFrameLocation(location);
}
}
class WriteVals extends Sequence {
constructor(vals, dest) {
super(vals.map((val, i) => new WriteVal(val, dest + 8*i)));
}
}
class CallFunc extends Sequence {
//may cobble all coller-saved regs
//retval stored in a0
constructor(func) {
super([
new WriteVals([0, 0, 0, 0, 0, 0], 0),
new WriteA0(null, 0),
new _PopA5(func), //putchar
new StackPivot(null, null),
new Spacer(512),
new PopA0(0), //this will get overwritten
new PopS0(0x30000000), //this too
new _CallA5(0),
]);
}
setFrameLocation(location) {
super.setFrameLocation(location);
this.seq[0] = new WriteVals([0, 0, 0, this.seq[6].getEntryPoint(), 0x30000000, this.seq[7].getEntryPoint()], this.seq[5].getFrameLocation());
this.seq[1] = new WriteA0(this.seq[5].getPoppedA0Location(), 0);
this.seq[3] = new StackPivot(this.seq[5].getEntryPoint(), this.seq[5].getFrameLocation()); //TODO UPDATE
super.setFrameLocation(location);
}
}
class OutputCharAtA0 extends Sequence {
constructor() {
super([
new WriteA0(null, 0),
new Sub8FromA0(),
new LdA0_8A0(),
new CallFunc(0x000000000005b70an + BASE), //putchar
new PopA0(0)
])
}
setFrameLocation(location){
super.setFrameLocation(location);
this.seq[0] = new WriteA0(this.seq[4].getPoppedA0Location(), 0);
super.setFrameLocation(location);
}
}
class InputCharAtA0 extends Sequence {
constructor() {
super([
new WriteA0(null, 0), //write to the pop A0
new Sub8FromA0(),
new Sub8FromA0(),
new WriteA0(null, 0), //write to the pop S0
new CallFunc(0x000000000005eaa6n + BASE), //getchar
new PopS0(0),
new SdA0_0x10S0(0),
new PopA0(0)
])
}
setFrameLocation(location){
super.setFrameLocation(location);
this.seq[0] = new WriteA0(this.seq[7].getPoppedA0Location(), 0);
this.seq[3] = new WriteA0(this.seq[5].getPoppedS0Location(), 0);
super.setFrameLocation(location);
}
}
class BeginLoop extends Sequence {
constructor() {
super([
new WriteA0(null, 0), //should write to the below gadget
new PopA0(0), //this is where EndLoop will jump back
new WriteA0(null, 0), //write to our conditional branch target
new WriteA0(null, 0), //write to their conditional branch target
new Sub8FromA0(),
new LdA0_8A0(), //read the value into a0
new ConditionalStackPivot(),
new PopA0(0),
])
}
setFrameLocation(location){
super.setFrameLocation(location);
this.seq[0] = new WriteA0(this.seq[1].getPoppedA0Location(), 0);
this.seq[2] = new WriteA0(this.seq[7].getPoppedA0Location(), 0);
super.setFrameLocation(location);
}
setEnd(endLoop){ //called after setFrameLocation
const endPop = endLoop.seq[2];
const beginPop = this.seq[7];
this.seq[3] = new WriteA0(endPop.getPoppedA0Location(), 0);
this.seq[6].setDests(beginPop.getEntryPoint(), beginPop.getFrameLocation(), endPop.getEntryPoint(), endPop.getFrameLocation());
}
}
class EndLoop extends Sequence {
constructor() {
super([
new WriteA0(null, 0),
new StackPivot(null, null),
new PopA0(0)
])
}
setBeginning(beginLoop) { //should be called after setFrameLocation
const popA0 = beginLoop.seq[1]
this.seq[0] = new WriteA0(popA0.getPoppedA0Location(), 0);
this.seq[1] = new StackPivot(popA0.getEntryPoint(), popA0.getFrameLocation());
}
}
class Add8ToA0 extends Sequence {
//SIDE EFFECT: sets s0 to 0
constructor() {
super([
new Add1A0(0),
new Add1A0(0),
new Add1A0(0),
new Add1A0(0),
new Add1A0(0),
new Add1A0(0),
new Add1A0(0),
new Add1A0(0), //s0 = 0
])
}
}
class Sub8FromA0 extends Sequence {
constructor() {
super([
new Dec2A0(),
new Dec2A0(),
new Dec2A0(),
new Dec2A0(),
])
}
}
class IncrementAtA0 extends Sequence {
constructor() {
super([
new WriteA0(null, 0),
new Sub8FromA0(),
new Sub8FromA0(),
new WriteA0(null, 0),
new Add8ToA0(),
new LdA0_8A0(),
new Add1A0(0),
new PopS0(0),
new SdA0_0x10S0(0),
new PopA0(0),
])
}
setFrameLocation(location){
super.setFrameLocation(location);
//fill in the self-modifying ROP chain destinations
this.seq[0] = new WriteA0(this.seq[9].getPoppedA0Location(), 0);
this.seq[3] = new WriteA0(this.seq[7].getPoppedS0Location(), 0);
super.setFrameLocation(location);
}
}
class DecrementAtA0 extends Sequence {
constructor() {
super([
new WriteA0(null, 0),
new Sub8FromA0(),
new Sub8FromA0(),
new WriteA0(null, 0),
new Add8ToA0(),
new LdA0_8A0(),
new Add1A0(0),
new Dec2A0(),
new PopS0(0),
new SdA0_0x10S0(0),
new PopA0(0),
])
}
setFrameLocation(location){
super.setFrameLocation(location);
//fill in the self-modifying ROP chain destinations
this.seq[0] = new WriteA0(this.seq[10].getPoppedA0Location(), 0);
this.seq[3] = new WriteA0(this.seq[8].getPoppedS0Location(), 0);
super.setFrameLocation(location);
}
}
bfInstructions['>'] = Add8ToA0;
bfInstructions['<'] = Sub8FromA0;
bfInstructions['+'] = IncrementAtA0;
bfInstructions['-'] = DecrementAtA0;
bfInstructions['.'] = OutputCharAtA0;
bfInstructions[','] = InputCharAtA0;
bfInstructions['['] = BeginLoop;
bfInstructions[']'] = EndLoop;
function createProgram(gadgets) {
const result = new Sequence([new NOP(), ...gadgets]);
result.setNextRa(new NOP().getEntryPoint());
result.setFrameLocation(0x10000000);
return result;
}
//beginningIndex -- index of a [
//return value -- index of the corresponding ]
function getLoopEnd(bf, beginningIndex) {
if(bf.charAt(beginningIndex) !== '['){
throw "beginningIndex is not a beginning";
}
let levels = 0;
for(let i = beginningIndex; i < bf.length; i++){
if(bf.charAt(i) === '['){
levels++;
}else if(bf.charAt(i) === ']'){
levels--;
}
if(levels === 0) return i;
}
throw "no end found";
}
function sanitizeBrainfuck(bf) {
return bf.split('').filter(ch => bfInstructions[ch]).join('');
}
function brainfuckToRop(bf) {
let initSeq = [new NOP(), new PopA0(0x38000000)];
let endSeq = [new PopA0(0), new CallFunc(0x00000000000342e4n + BASE)] //exit(0)
let seq = initSeq.concat(
bf.split('')
.map(instr =>
bfInstructions[instr]
? new bfInstructions[instr]()
: new NOP())
).concat(endSeq);
const result = new Sequence(seq);
result.setNextRa(new NOP().getEntryPoint());
result.setFrameLocation(0x10000000);
//initialize the loops
for(let i = 0; i < bf.length; i++){
if(bf.charAt(i) == '['){
const beginIndex = i;
const endIndex = getLoopEnd(bf, i);
const beginLoop = result.seq[initSeq.length + beginIndex];
const endLoop = result.seq[initSeq.length + endIndex];
beginLoop.setEnd(endLoop);
endLoop.setBeginning(beginLoop);
}
}
return result;
}
function showRopChain(gadget){
document.getElementById('stackbuf').innerHTML = (gadget.synthesize().map(num => num.toString(16)).join("\n"));
document.getElementById('jmpbuf').innerHTML = (jmpBuf.synthesize().map(num => num.toString(16)).join("\n"));
}
function generateCode() {
showRopChain(brainfuckToRop(sanitizeBrainfuck(document.getElementById("source").value)));
return false;
}