forked from nellaisamurai/Exploit-jpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRAX.cpp
425 lines (337 loc) · 14.2 KB
/
CRAX.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
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
// 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/S2E.h>
#include <s2e/ConfigFile.h>
#include <filesystem>
#include "CRAX.h"
#define __CRAX_CONFIG_GET_T(Type, key, defaultValue) \
(g_s2e->getConfig()->get##Type(getConfigKey() + key, defaultValue))
#define CRAX_CONFIG_GET_BOOL(key, defaultValue) \
__CRAX_CONFIG_GET_T(Bool, key, defaultValue)
#define CRAX_CONFIG_GET_INT(key, defaultValue) \
__CRAX_CONFIG_GET_T(Int, key, defaultValue)
#define CRAX_CONFIG_GET_STRING(key, defaultValue) \
__CRAX_CONFIG_GET_T(String, key, defaultValue)
using namespace klee;
namespace s2e::plugins::crax {
CRAX *g_crax = nullptr;
S2E_DEFINE_PLUGIN(CRAX, "Modular Exploit Generation System", "",
"LinuxMonitor", "MemoryMap", "ModuleMap");
const std::string CRAX::s_symArg = "sym_arg";
const std::string CRAX::s_symEnv = "sym_env";
const std::string CRAX::s_symFile = "sym_file";
const std::string CRAX::s_symStdin = "sym_stdin";
pybind11::scoped_interpreter CRAX::s_pybind11;
pybind11::module CRAX::s_pwnlib(pybind11::module::import("pwnlib.elf"));
CRAX::CRAX(S2E *s2e)
: Plugin(s2e),
beforeInstruction(),
afterInstruction(),
beforeSyscall(),
afterSyscall(),
onStateForkModuleDecide(),
beforeExploitGeneration(),
m_currentState(),
m_linuxMonitor(),
m_showInstructions(CRAX_CONFIG_GET_BOOL(".showInstructions", false)),
m_showSyscalls(CRAX_CONFIG_GET_BOOL(".showSyscalls", true)),
m_concolicMode(CRAX_CONFIG_GET_BOOL(".concolicMode", false)),
m_exploitForm(CRAX::ExploitForm::SCRIPT),
m_proxy(CRAX::Proxy::NONE),
m_register(),
m_memory(),
m_disassembler(),
m_exploit(CRAX_CONFIG_GET_STRING(".elfFilename", DEFAULT_BINARY_FILENAME),
CRAX_CONFIG_GET_STRING(".libcFilename", DEFAULT_LIBC_FILENAME),
CRAX_CONFIG_GET_STRING(".ldFilename", DEFAULT_LD_FILENAME)),
m_exploitGenerator(),
m_modules(),
m_techniques(),
m_targetProcessPid(),
m_allowedForkingStates() {}
void CRAX::initialize() {
g_crax = this;
m_register.initialize();
m_memory.initialize();
m_linuxMonitor = s2e()->getPlugin<LinuxMonitor>();
m_linuxMonitor->onProcessLoad.connect(
sigc::mem_fun(*this, &CRAX::onProcessLoad));
// Install symbolic RIP handler.
s2e()->getCorePlugin()->onSymbolicAddress.connect(
sigc::mem_fun(*this, &CRAX::onSymbolicRip));
s2e()->getCorePlugin()->onStateForkDecide.connect(
sigc::mem_fun(*this, &CRAX::onStateForkDecide));
// Run `ROPgadget <elf>` on the following ELF files in a worker thread
// and cache their outputs.
std::vector<const ELF *> elfFiles = {
&m_exploit.getElf(),
&m_exploit.getLibc()
};
m_exploitGenerator.getRopGadgetResolver().buildRopGadgetOutputCacheAsync(elfFiles);
// Initialize modules.
ConfigFile *cfg = s2e()->getConfig();
ConfigFile::string_list moduleNames = cfg->getStringList(getConfigKey() + ".modules");
foreach2 (it, moduleNames.begin(), moduleNames.end()) {
log<INFO>() << "Creating module: " << *it << '\n';
m_modules.push_back(Module::create(*it));
}
// Initialize techniques.
ConfigFile::string_list techniqueNames = cfg->getStringList(getConfigKey() + ".techniques");
foreach2 (it, techniqueNames.begin(), techniqueNames.end()) {
log<INFO>() << "Creating technique: " << *it << '\n';
m_techniques.push_back(Technique::create(*it));
}
}
void CRAX::onSymbolicRip(S2EExecutionState *state,
ref<Expr> symbolicRip,
uint64_t concreteRip,
bool &concretize,
CorePlugin::symbolicAddressReason reason) {
if (reason != CorePlugin::symbolicAddressReason::PC) {
return;
}
// Set m_currentState to state.
// All subsequent calls to reg() and mem() will operate on m_currentState.
setCurrentState(state);
log<WARN>()
<< "Detected symbolic RIP: " << hexval(concreteRip)
<< ", original value was: " << hexval(reg().readConcrete(Register::X64::RIP))
<< '\n';
reg().setRipSymbolic(symbolicRip);
// Dump CPU registers and virtual memory mappings.
reg().showRegInfo();
mem().showMapInfo();
// Let the modules do whatever that needs to be done.
beforeExploitGeneration.emit(state);
// Generate the exploit.
m_exploitGenerator.run(state);
s2e()->getExecutor()->terminateState(*state, "End of exploit generation");
}
void CRAX::onProcessLoad(S2EExecutionState *state,
uint64_t cr3,
uint64_t pid,
const std::string &imageFileName) {
setCurrentState(state);
log<WARN>() << "onProcessLoad: " << imageFileName << '\n';
if (m_proxy == CRAX::Proxy::NONE) {
// For SYM_ARG and SYM_ENV, the stage1 payload is sent as
// command-line argument(s) and environment variable(s).
if (imageFileName == s_symArg) {
m_proxy = Proxy::SYM_ARG;
g_crax->getExploit().getProcess().getArgv().push_back("payload");
} else if (imageFileName == s_symEnv) {
m_proxy = Proxy::SYM_ENV;
g_crax->getExploit().getProcess().getEnv().insert({"'placeholder'", "payload"});
} else if (imageFileName == s_symFile) {
m_proxy = Proxy::SYM_FILE;
} else if (imageFileName == s_symStdin) {
m_proxy = Proxy::SYM_STDIN;
}
}
// If the user provides "./target" instead of "target" as the elf filename,
// then we use std::filesystem::path to discard the leading "./"
if (imageFileName == std::filesystem::path(m_exploit.getElf().getFilename()).filename()) {
m_targetProcessPid = pid;
m_linuxMonitor->onModuleLoad.connect(
sigc::mem_fun(*this, &CRAX::onModuleLoad));
s2e()->getCorePlugin()->onTranslateInstructionStart.connect(
sigc::mem_fun(*this, &CRAX::onTranslateInstructionStart));
s2e()->getCorePlugin()->onTranslateInstructionEnd.connect(
sigc::mem_fun(*this, &CRAX::onTranslateInstructionEnd));
}
}
void CRAX::onModuleLoad(S2EExecutionState *state,
const ModuleDescriptor &md) {
setCurrentState(state);
log<WARN>() << "onModuleLoad: " << md.Name << '\n';
// Resolve ELF base if the target binary has PIE.
if (md.Name == "target" && m_exploit.getElf().checksec.hasPIE) {
assert(md.Sections.size());
uint64_t elfBase = md.Sections.front().runtimeLoadBase;
elfBase = Memory::roundDownToPageBoundary(elfBase);
log<WARN>() << "ELF loaded at: " << hexval(elfBase) << '\n';
m_exploit.getElf().setBase(elfBase);
}
}
void CRAX::onTranslateInstructionStart(ExecutionSignal *onInstructionExecute,
S2EExecutionState *state,
TranslationBlock *tb,
uint64_t pc) {
if (m_linuxMonitor->isKernelAddress(pc)) {
return;
}
// Register the instruction hook which will be called
// before the instruction is executed.
onInstructionExecute->connect(
sigc::mem_fun(*this, &CRAX::onExecuteInstructionStart));
}
void CRAX::onTranslateInstructionEnd(ExecutionSignal *onInstructionExecute,
S2EExecutionState *state,
TranslationBlock *tb,
uint64_t pc) {
if (m_linuxMonitor->isKernelAddress(pc)) {
return;
}
// Register the instruction hook which will be called
// after the instruction is executed.
onInstructionExecute->connect(
sigc::mem_fun(*this, &CRAX::onExecuteInstructionEnd));
}
void CRAX::onExecuteInstructionStart(S2EExecutionState *state,
uint64_t pc) {
setCurrentState(state);
std::optional<Instruction> i = m_disassembler.disasm(pc);
if (!i) {
return;
}
auto craxState = getPluginState(state);
auto &pending = craxState->m_pendingOnExecuteSyscallEnd;
if (pending.size()) {
auto it = pending.find(pc);
if (it != pending.end()) {
onExecuteSyscallEnd(state, *i, it->second);
pending.erase(pc);
}
}
if (m_showInstructions && !m_linuxMonitor->isKernelAddress(pc)) {
log<INFO>()
<< hexval(i->address) << ": "
<< i->mnemonic << ' ' << i->opStr
<< '\n';
}
if (i->mnemonic == "syscall") {
onExecuteSyscallStart(state, *i);
}
// Execute instruction hooks installed by the user.
beforeInstruction.emit(state, *i);
}
void CRAX::onExecuteInstructionEnd(S2EExecutionState *state,
uint64_t pc) {
setCurrentState(state);
std::optional<Instruction> i = m_disassembler.disasm(pc);
if (!i) {
return;
}
// Execute instruction hooks installed by the user.
afterInstruction.emit(state, *i);
}
void CRAX::onExecuteSyscallStart(S2EExecutionState *state,
const Instruction &i) {
constexpr bool verbose = false;
SyscallCtx syscall;
syscall.ret = 0;
syscall.nr = reg().readConcrete(Register::X64::RAX, verbose);
syscall.arg1 = reg().readConcrete(Register::X64::RDI, verbose);
syscall.arg2 = reg().readConcrete(Register::X64::RSI, verbose);
syscall.arg3 = reg().readConcrete(Register::X64::RDX, verbose);
syscall.arg4 = reg().readConcrete(Register::X64::R10, verbose);
syscall.arg5 = reg().readConcrete(Register::X64::R8, verbose);
syscall.arg6 = reg().readConcrete(Register::X64::R9, verbose);
if (m_showSyscalls) {
log<INFO>() << "syscall: "
<< hexval(syscall.nr) << " ("
<< hexval(syscall.arg1) << ", "
<< hexval(syscall.arg2) << ", "
<< hexval(syscall.arg3) << ", "
<< hexval(syscall.arg4) << ", "
<< hexval(syscall.arg5) << ", "
<< hexval(syscall.arg6) << ")\n";
}
auto craxState = getPluginState(state);
auto &pending = craxState->m_pendingOnExecuteSyscallEnd;
// Schedule the syscall hook to be called before the next instruction is executed.
uint64_t nextInsnAddr = i.address + i.size;
pending[nextInsnAddr] = syscall;
// Execute syscall hooks installed by the user.
beforeSyscall.emit(state, pending[nextInsnAddr]);
}
void CRAX::onExecuteSyscallEnd(S2EExecutionState *state,
const Instruction &i,
SyscallCtx &syscall) {
constexpr bool verbose = false;
// The kernel has finished serving the system call,
// and the return value is now placed in RAX.
syscall.ret = reg().readConcrete(Register::X64::RAX, verbose);
// Execute syscall hooks installed by the user.
afterSyscall.emit(state, syscall);
}
void CRAX::onStateForkDecide(S2EExecutionState *state,
const ref<Expr> &condition,
bool &allowForking) {
if (!m_concolicMode) {
return;
}
setCurrentState(state);
// The user sets `m_concolicMode` to true,
// so we should disallow this fork by default.
allowForking = false;
// Let the modules of CRAX++ decide whether this fork should be done.
onStateForkModuleDecide.emit(state, condition, allowForking);
// We'll also check if current state forking was requested by CRAX.
// If yes, then `state` should be in `m_allowedForkingStates`.
allowForking |= m_allowedForkingStates.erase(state) == 1;
}
bool CRAX::isCallSiteOf(const Instruction &i, const std::string &symbol) const {
const ELF &elf = m_exploit.getElf();
if (i.mnemonic != "call" || !elf.hasSymbol(symbol)) {
return false;
}
const uint64_t symbolPlt = m_exploit.getElf().getRuntimeAddress(symbol);
uint64_t operand = 0;
try {
operand = std::stoull(i.opStr, nullptr, 16);
} catch (...) {
// This can happen when `i` is something like `call r13`,
// which is legit, so let's just silently swallow it...
}
return symbolPlt == operand;
}
std::string CRAX::getBelongingSymbol(uint64_t instructionAddr) const {
const ELF &elf = m_exploit.getElf();
instructionAddr -= elf.getBase();
std::vector<std::pair<std::string, uint64_t>> syms(
elf.symbols().begin(), elf.symbols().end());
std::sort(syms.begin(),
syms.end(),
[](const auto &p1, const auto &p2) { return p1.second < p2.second; });
if (instructionAddr < syms.front().second) {
log<WARN>()
<< "Unable to find which symbol " << hexval(instructionAddr)
<< " belongs to.\n";
return "";
}
// Use binary search to find out which symbol `instructionAddr` belongs to.
int l = 0;
int r = syms.size() - 1;
while (l < r) {
int m = l + (r - l) / 2;
uint64_t addr = syms[m].second;
if (addr < instructionAddr) {
l = m + 1;
} else {
r = m - 1;
}
}
if (instructionAddr < syms[l].second) {
l--;
}
return syms[l].first;
}
} // namespace s2e::plugins::crax