-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathrop.js
187 lines (155 loc) · 3.91 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
/* 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 rop = function(p, addr) {
this.ropChain = undefined;
this.ropChainPtr = undefined;
if(addr == undefined)
{
this.ropChain = new Uint32Array(0x4000);
this.ropChainPtr = p.read8(p.leakval(this.ropChain).add32(0x28));
}
else
{
this.ropChainPtr = addr;
}
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;
};