forked from nellaisamurai/Exploit-jpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExploitGenerator.cpp
209 lines (171 loc) · 6.64 KB
/
ExploitGenerator.cpp
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
// Copyright 2021-2022 Software Quality Laboratory, NYCU.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <s2e/Plugins/CRAX/CRAX.h>
#include <s2e/Plugins/CRAX/Expr/BinaryExprEval.h>
#include <s2e/Plugins/CRAX/Utils/StringUtil.h>
#include <cassert>
#include <fstream>
#include "ExploitGenerator.h"
namespace s2e::plugins::crax {
ExploitGenerator::ExploitGenerator()
: m_state(),
m_ropGadgetResolver(),
m_ropPayloadBuilder(),
m_coreGenerator() {}
void ExploitGenerator::run(S2EExecutionState *state) {
std::vector<RopPayload> ropPayload;
m_state = state;
if (!checkRequirements()) {
return;
}
initialize();
if (g_crax->getExploitForm() == CRAX::ExploitForm::SCRIPT) {
ropPayload = buildFullRopPayload();
generateExploitScript(ropPayload);
} else {
ropPayload = buildStage1RopPayload();
generateExploit(RopPayloadBuilder::getStage1Payload(ropPayload));
}
}
bool ExploitGenerator::checkRequirements() const {
for (auto m : g_crax->getModules()) {
if (!m->checkRequirements()) {
log<WARN>() << "Requirements unmet (Module: " << m->toString() << ")\n";
return false;
}
}
for (auto t : g_crax->getTechniques()) {
if (!t->checkRequirements()) {
log<WARN>() << "Requirements unmet (Technique: " << t->toString() << ")\n";
return false;
}
}
return true;
}
void ExploitGenerator::initialize() {
// Maybe initialize the core generator.
// XXX: What if more than one module has a core generator?
if (!m_coreGenerator) {
for (auto m : g_crax->getModules()) {
if (auto coreGenerator = m->makeCoreGenerator()) {
log<WARN>() << "Using core generator from module: " << m->toString() << '\n';
m_coreGenerator = std::move(coreGenerator);
break;
}
}
// No module has a core generator, so we'll use the default one.
if (!m_coreGenerator) {
m_coreGenerator = std::make_unique<CoreGenerator>();
}
}
g_crax->getExploit().reset();
m_ropPayloadBuilder.reset();
for (auto t : g_crax->getTechniques()) {
log<INFO>() << "Initializing technique: " << t->toString() << '\n';
t->initialize();
}
}
std::vector<RopPayload> ExploitGenerator::buildFullRopPayload() {
for (auto t : g_crax->getTechniques()) {
if (!m_ropPayloadBuilder.chain(*t)) {
return {};
}
}
return m_ropPayloadBuilder.build();
}
std::vector<RopPayload> ExploitGenerator::buildStage1RopPayload() {
for (auto t : g_crax->getTechniques()) {
if (!m_ropPayloadBuilder.chain(*t)) {
return {};
}
if (!m_ropPayloadBuilder.isSymbolicMode()) {
break;
}
}
return m_ropPayloadBuilder.build();
}
bool ExploitGenerator::generateExploitScript(const std::vector<RopPayload> &ropPayload) const {
if (ropPayload.empty()) {
return false;
}
Exploit &exploit = g_crax->getExploit();
const ELF &elf = exploit.getElf();
const ELF &libc = exploit.getLibc();
const std::string &elfPrefix = elf.getVarPrefix();
const std::string &libcPrefix = libc.getVarPrefix();
// Write exploit shebang.
exploit.writeline(Exploit::s_shebang);
// Pwntools stuff.
exploit.writelines({
"from pwn import *",
"context.update(arch = 'amd64', os = 'linux', log_level = 'info')",
"",
format("%s = ELF('%s', checksec=False)", elfPrefix.c_str(), elf.getFilename().c_str()),
format("%s = ELF('%s', checksec=False)", libcPrefix.c_str(), libc.getFilename().c_str()),
""
});
exploit.registerSymbol("canary", 0);
// XXX: add support for shared libraries other than libc.so.6
exploit.registerSymbol(elfPrefix + "_base", 0);
exploit.registerSymbol(libcPrefix + "_base", 0);
// Declare symbols and values.
for (const auto &[name, value] : exploit.getSymtab()) {
exploit.writeline(format("%s = 0x%llx", name.c_str(), value));
}
exploit.writeline();
// Define solve_stage1() function.
if (elf.checksec.hasCanary || elf.checksec.hasPIE) {
exploit.writeline("def solve_stage1(canary, elf_base, iostates) -> bytes:");
exploit.setIndentLevel(4);
exploit.writelines({
"os.system('./launch-crax.sh -c {} -e {} -s {}'"
".format(hex(canary), hex(elf_base), iostates))",
"with open('stage1.bin', 'rb') as f:",
" return f.read()",
});
exploit.setIndentLevel(0);
exploit.writeline();
}
// Generate the main function.
exploit.writeline("if __name__ == '__main__':");
exploit.setIndentLevel(4);
// Generate the main logic of the exploit script.
m_coreGenerator->generateMainFunction(m_state, ropPayload);
// Write exploit trailer.
exploit.setIndentLevel(4);
exploit.writeline("proc.interactive()");
// Write the buffered content to the file.
std::string filename = exploit.getFilename(m_state->getID());
std::ofstream ofs(filename);
ofs << exploit.getContent();
log<WARN>() << "Generated exploit script: " << filename << '\n';
return true;
}
bool ExploitGenerator::generateExploit(const std::vector<uint8_t> &stage1,
std::string filename) const {
if (stage1.empty()) {
return false;
}
std::ofstream ofs(filename, std::ios::binary);
ofs.write(reinterpret_cast<const char *>(stage1.data()), stage1.size());
log<WARN>() << "Generated exploit: " << filename << '\n';
return true;
}
} // namespace s2e::plugins::crax