-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
backend.cc
379 lines (305 loc) · 11.9 KB
/
backend.cc
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
// Axel '0vercl0k' Souchet - May 25 2020
#include "backend.h"
#include "debugger.h"
#include "nt.h"
#include "utils.h"
#include <fmt/format.h>
#include <string_view>
Backend_t *g_Backend = nullptr;
bool Backend_t::SetTraceFile(const fs::path &, const TraceType_t) {
fmt::print("SetTraceFile not implemented.\n");
return true;
}
[[nodiscard]] bool Backend_t::EnableSingleStep(CpuState_t &) { return true; }
bool Backend_t::PhysWrite(const Gpa_t Gpa, const uint8_t *Buffer,
const uint64_t BufferSize, const bool Dirty) {
uint8_t *Dst = PhysTranslate(Gpa);
memcpy(Dst, Buffer, BufferSize);
if (Dirty) {
const Gpa_t End = Gpa + Gpa_t(BufferSize);
for (Gpa_t Cur = Gpa; Cur < End; Cur += Gpa_t(Page::Size)) {
DirtyGpa(Cur);
}
}
return true;
}
bool Backend_t::VirtRead(const Gva_t Gva, uint8_t *Buffer,
const uint64_t BufferSize) const {
uint64_t Size = BufferSize;
Gva_t CurrentGva = Gva;
while (Size > 0) {
Gpa_t Gpa;
const bool Translate =
VirtTranslate(CurrentGva, Gpa, MemoryValidate_t::ValidateRead);
if (!Translate) {
fmt::print("Translation of GVA {:#x} failed\n", CurrentGva);
__debugbreak();
return false;
}
const Gva_t GvaOffset = CurrentGva.Offset();
const uint64_t BytesReadable = Page::Size - GvaOffset.U64();
const uint64_t Size2Read = std::min(Size, BytesReadable);
const uint8_t *Hva = PhysTranslate(Gpa);
memcpy(Buffer, Hva, Size2Read);
Size -= Size2Read;
CurrentGva += Gva_t(Size2Read);
Buffer += Size2Read;
}
return true;
}
uint32_t Backend_t::VirtRead4(const Gva_t Gva) const {
uint32_t Ret = 0;
if (!VirtReadStruct(Gva, &Ret)) {
__debugbreak();
}
return Ret;
}
uint64_t Backend_t::VirtRead8(const Gva_t Gva) const {
uint64_t Ret = 0;
if (!VirtReadStruct(Gva, &Ret)) {
__debugbreak();
}
return Ret;
}
Gva_t Backend_t::VirtReadGva(const Gva_t Gva) const {
return Gva_t(VirtRead8(Gva));
}
Gpa_t Backend_t::VirtReadGpa(const Gva_t Gva) const {
return Gpa_t(VirtRead8(Gva));
}
std::string Backend_t::VirtReadString(const Gva_t Gva,
const uint64_t MaxLength) const {
return VirtReadBasicString<std::string::value_type>(Gva, MaxLength);
}
std::u16string Backend_t::VirtReadWideString(const Gva_t Gva,
const uint64_t MaxLength) const {
return VirtReadBasicString<std::u16string::value_type>(Gva, MaxLength);
}
bool Backend_t::VirtWrite(const Gva_t Gva, const uint8_t *Buffer,
const uint64_t BufferSize, const bool Dirty) {
uint64_t Size = BufferSize;
Gva_t CurrentGva = Gva;
while (Size > 0) {
Gpa_t Gpa;
// XXX: Reenable ValidateReadWrite when bug is figured out.
const bool Translate = VirtTranslate(
CurrentGva, Gpa, MemoryValidate_t::ValidateRead /*Write*/);
if (!Translate) {
fmt::print("Translation of GVA {:#x} failed\n", CurrentGva);
__debugbreak();
return false;
}
const Gva_t GvaOffset = CurrentGva.Offset();
const uint64_t BytesWriteable = Page::Size - GvaOffset.U64();
const uint64_t Size2Write = std::min(Size, BytesWriteable);
uint8_t *Hva = PhysTranslate(Gpa);
memcpy(Hva, Buffer, Size2Write);
Size -= Size2Write;
CurrentGva += Gva_t(Size2Write);
Buffer += Size2Write;
if (Dirty) {
DirtyGpa(Gpa);
}
}
return true;
}
bool Backend_t::VirtWriteDirty(const Gva_t Gva, const uint8_t *Buffer,
const uint64_t BufferSize) {
return VirtWrite(Gva, Buffer, BufferSize, true);
}
bool Backend_t::SimulateReturnFromFunction(const uint64_t Return) {
//
// Set return value.
//
Rax(Return);
const uint64_t Stack = Rsp();
const uint64_t SavedReturnAddress = VirtRead8(Gva_t(Stack));
//
// Eat up the saved return address.
//
Rsp(Stack + 8);
Rip(SavedReturnAddress);
return true;
}
bool Backend_t::SimulateReturnFrom32bitFunction(
const uint32_t Return, const uint32_t StdcallArgsCount) {
//
// Set return value.
//
Rax(Return);
const uint64_t Stack = Rsp();
const uint32_t SavedReturnAddress = VirtRead4(Gva_t(Stack));
//
// Eat up the saved return address.
//
Rsp(Stack + (4 + (4 * StdcallArgsCount)));
Rip(SavedReturnAddress);
return true;
}
Gva_t Backend_t::GetArgAddress(const uint64_t Idx) {
if (Idx <= 3) {
fmt::print("The first four arguments are stored in registers (@rcx, @rdx, "
"@r8, @r9) which means you cannot get their addresses.\n");
std::abort();
}
return Gva_t(Rsp() + (8 + (Idx * 8)));
}
uint64_t Backend_t::GetArg(const uint64_t Idx) {
switch (Idx) {
case 0:
return Rcx();
case 1:
return Rdx();
case 2:
return R8();
case 3:
return R9();
default: {
return VirtRead8(GetArgAddress(Idx));
}
}
}
uint64_t Backend_t::GetArg8(const uint64_t Idx) {
switch (Idx) {
case 0:
return Rcx();
case 1:
return Rdx();
case 2:
return R8();
case 3:
return R9();
default: {
return VirtRead8(GetArgAddress(Idx));
}
}
}
uint32_t Backend_t::GetArg4(const uint64_t Idx) {
return uint32_t(GetArg8(Idx));
}
Gva_t Backend_t::GetArgGva(const uint64_t Idx) { return Gva_t(GetArg8(Idx)); }
std::pair<Gva_t, Gva_t> Backend_t::GetArgGvaAndAddress(const uint64_t Idx) {
return {GetArgGva(Idx), GetArgAddress(Idx)};
}
std::pair<uint64_t, Gva_t> Backend_t::GetArg8AndAddress(const uint64_t Idx) {
return {GetArg8(Idx), GetArgAddress(Idx)};
}
std::pair<uint32_t, Gva_t> Backend_t::GetArg4AndAddress(const uint64_t Idx) {
return {GetArg4(Idx), GetArgAddress(Idx)};
}
bool Backend_t::SaveCrash(const Gva_t ExceptionAddress,
const uint32_t ExceptionCode) {
const auto ExceptionCodeStr = ExceptionCodeToStr(ExceptionCode);
const std::string Filename =
fmt::format("crash-{}-{:#x}", ExceptionCodeStr, ExceptionAddress);
Stop(Crash_t(Filename));
return true;
}
bool Backend_t::SetBreakpoint(const char *Symbol,
const BreakpointHandler_t Handler) {
const Gva_t Gva = Gva_t(g_Dbg->GetSymbol(Symbol));
if (Gva == Gva_t(0)) {
fmt::print("Could not set a breakpoint at {}.\n", Symbol);
return false;
}
return SetBreakpoint(Gva, Handler);
}
bool Backend_t::SetCrashBreakpoint(const Gva_t Gva) {
auto CrashBreakpointHandler = [](Backend_t *Backend) {
Backend->Stop(Crash_t());
};
return SetBreakpoint(Gva, CrashBreakpointHandler);
}
bool Backend_t::SetCrashBreakpoint(const char *Symbol) {
auto CrashBreakpointHandler = [](Backend_t *Backend) {
Backend->Stop(Crash_t());
};
return SetBreakpoint(Symbol, CrashBreakpointHandler);
}
void Backend_t::TrapFlag(const bool Arm) {
uint64_t NewValue = Rflags();
if (Arm) {
NewValue |= RFLAGS_TRAP_FLAG_FLAG;
} else {
NewValue &= ~RFLAGS_TRAP_FLAG_FLAG;
}
Rflags(NewValue);
}
uint64_t Backend_t::Rflags() const { return GetReg(Registers_t::Rflags); }
void Backend_t::Rflags(const uint64_t Value) {
SetReg(Registers_t::Rflags, Value);
}
void Backend_t::Rflags(const Gva_t Value) { Rflags(Value.U64()); }
uint64_t Backend_t::Rsp() const { return GetReg(Registers_t::Rsp); }
void Backend_t::Rsp(const uint64_t Value) { SetReg(Registers_t::Rsp, Value); }
void Backend_t::Rsp(const Gva_t Value) { Rsp(Value.U64()); }
uint64_t Backend_t::Rbp() const { return GetReg(Registers_t::Rbp); }
void Backend_t::Rbp(const uint64_t Value) { SetReg(Registers_t::Rbp, Value); }
void Backend_t::Rbp(const Gva_t Value) { Rbp(Value.U64()); }
uint64_t Backend_t::Rip() const { return GetReg(Registers_t::Rip); }
void Backend_t::Rip(const uint64_t Value) { SetReg(Registers_t::Rip, Value); }
void Backend_t::Rip(const Gva_t Value) { Rip(Value.U64()); }
uint64_t Backend_t::Rax() const { return GetReg(Registers_t::Rax); }
void Backend_t::Rax(const uint64_t Value) { SetReg(Registers_t::Rax, Value); }
void Backend_t::Rax(const Gva_t Value) { Rax(Value.U64()); }
uint64_t Backend_t::Rbx() const { return GetReg(Registers_t::Rbx); }
void Backend_t::Rbx(const uint64_t Value) { SetReg(Registers_t::Rbx, Value); }
void Backend_t::Rbx(const Gva_t Value) { Rbx(Value.U64()); }
uint64_t Backend_t::Rcx() const { return GetReg(Registers_t::Rcx); }
void Backend_t::Rcx(const uint64_t Value) { SetReg(Registers_t::Rcx, Value); }
void Backend_t::Rcx(const Gva_t Value) { Rcx(Value.U64()); }
uint64_t Backend_t::Rdx() const { return GetReg(Registers_t::Rdx); }
void Backend_t::Rdx(const uint64_t Value) { SetReg(Registers_t::Rdx, Value); }
void Backend_t::Rdx(const Gva_t Value) { Rdx(Value.U64()); }
uint64_t Backend_t::Rsi() const { return GetReg(Registers_t::Rsi); }
void Backend_t::Rsi(const uint64_t Value) { SetReg(Registers_t::Rsi, Value); }
void Backend_t::Rsi(const Gva_t Value) { Rsi(Value.U64()); }
uint64_t Backend_t::Rdi() const { return GetReg(Registers_t::Rdi); }
void Backend_t::Rdi(const uint64_t Value) { SetReg(Registers_t::Rdi, Value); }
void Backend_t::Rdi(const Gva_t Value) { Rdi(Value.U64()); }
uint64_t Backend_t::R8() const { return GetReg(Registers_t::R8); }
void Backend_t::R8(const uint64_t Value) { SetReg(Registers_t::R8, Value); }
void Backend_t::R8(const Gva_t Value) { R8(Value.U64()); }
uint64_t Backend_t::R9() const { return GetReg(Registers_t::R9); }
void Backend_t::R9(const uint64_t Value) { SetReg(Registers_t::R9, Value); }
void Backend_t::R9(const Gva_t Value) { R9(Value.U64()); }
uint64_t Backend_t::R10() const { return GetReg(Registers_t::R10); }
void Backend_t::R10(const uint64_t Value) { SetReg(Registers_t::R10, Value); }
void Backend_t::R10(const Gva_t Value) { R10(Value.U64()); }
uint64_t Backend_t::R11() const { return GetReg(Registers_t::R11); }
void Backend_t::R11(const uint64_t Value) { SetReg(Registers_t::R11, Value); }
void Backend_t::R11(const Gva_t Value) { R11(Value.U64()); }
uint64_t Backend_t::R12() const { return GetReg(Registers_t::R12); }
void Backend_t::R12(const uint64_t Value) { SetReg(Registers_t::R12, Value); }
void Backend_t::R12(const Gva_t Value) { R12(Value.U64()); }
uint64_t Backend_t::R13() const { return GetReg(Registers_t::R13); }
void Backend_t::R13(const uint64_t Value) { SetReg(Registers_t::R13, Value); }
void Backend_t::R13(const Gva_t Value) { R13(Value.U64()); }
uint64_t Backend_t::R14() const { return GetReg(Registers_t::R14); }
void Backend_t::R14(const uint64_t Value) { SetReg(Registers_t::R14, Value); }
void Backend_t::R14(const Gva_t Value) { R14(Value.U64()); }
uint64_t Backend_t::R15() const { return GetReg(Registers_t::R15); }
void Backend_t::R15(const uint64_t Value) { SetReg(Registers_t::R15, Value); }
void Backend_t::R15(const Gva_t Value) { R15(Value.U64()); }
uint64_t Backend_t::Cr2() const { return GetReg(Registers_t::Cr2); }
void Backend_t::Cr2(const uint64_t Value) { SetReg(Registers_t::Cr2, Value); }
void Backend_t::Cr2(const Gva_t Value) { Cr2(Value.U64()); }
void Backend_t::PrintRegisters() {
const uint64_t Rax = GetReg(Registers_t::Rax), Rbx = GetReg(Registers_t::Rbx),
Rcx = GetReg(Registers_t::Rcx);
fmt::print("rax={:016x} rbx={:016x} rcx={:016x}\n", Rax, Rbx, Rcx);
const uint64_t Rdx = GetReg(Registers_t::Rdx), Rsi = GetReg(Registers_t::Rsi),
Rdi = GetReg(Registers_t::Rdi);
fmt::print("rdx={:016x} rsi={:016x} rdi={:016x}\n", Rdx, Rsi, Rdi);
const uint64_t Rip = GetReg(Registers_t::Rip), Rsp = GetReg(Registers_t::Rsp),
Rbp = GetReg(Registers_t::Rbp);
fmt::print("rip={:016x} rsp={:016x} rbp={:016x}\n", Rip, Rsp, Rbp);
const uint64_t R8 = GetReg(Registers_t::R8), R9 = GetReg(Registers_t::R9),
R10 = GetReg(Registers_t::R10);
fmt::print(" r8={:016x} r9={:016x} r10={:016x}\n", R8, R9, R10);
const uint64_t R11 = GetReg(Registers_t::R11), R12 = GetReg(Registers_t::R12),
R13 = GetReg(Registers_t::R13);
fmt::print("r11={:016x} r12={:016x} r13={:016x}\n", R11, R12, R13);
const uint64_t R14 = GetReg(Registers_t::R14), R15 = GetReg(Registers_t::R15);
fmt::print("r14={:016x} r15={:016x}\n", R14, R15);
}