diff --git a/pclone/main.cpp b/pclone/main.cpp index 98dc291..b782e2d 100644 --- a/pclone/main.cpp +++ b/pclone/main.cpp @@ -1,5 +1,5 @@ #include "vdm_ctx/vdm_ctx.hpp" -#include "mem_ctx/mem_ctx.hpp" +#include "ptm_ctx/ptm_ctx.hpp" #include "pclone_ctx/pclone_ctx.hpp" #include "set_mgr/set_mgr.hpp" #include "vad/vad.hpp" @@ -21,33 +21,38 @@ int __cdecl main(int argc, char** argv) // read physical memory using the driver... vdm::read_phys_t _read_phys = - [&](void* addr, void* buffer, std::size_t size) -> bool + [&](void* addr, void* buffer, std::size_t size) -> bool { return vdm::read_phys(addr, buffer, size); }; // write physical memory using the driver... vdm::write_phys_t _write_phys = - [&](void* addr, void* buffer, std::size_t size) -> bool + [&](void* addr, void* buffer, std::size_t size) -> bool { return vdm::write_phys(addr, buffer, size); }; vdm::vdm_ctx vdm(_read_phys, _write_phys); - nasa::mem_ctx my_proc(vdm); + ptm::ptm_ctx my_proc(&vdm); // shoot the tires off the working set manager thread... - const auto set_mgr_pethread = set_mgr::get_setmgr_pethread(vdm); - const auto result = set_mgr::stop_setmgr(vdm, set_mgr_pethread); + const auto set_mgr_pethread = + set_mgr::get_setmgr_pethread(vdm); + + const auto result = + set_mgr::stop_setmgr(vdm, set_mgr_pethread); // read physical memory via paging tables and not with the driver... - _read_phys = [&my_proc](void* addr, void* buffer, std::size_t size) -> bool + _read_phys = + [&my_proc](void* addr, void* buffer, std::size_t size) -> bool { return my_proc.read_phys(buffer, addr, size); }; // write physical memory via paging tables and not with the driver... - _write_phys = [&my_proc](void* addr, void* buffer, std::size_t size) -> bool + _write_phys = + [&my_proc](void* addr, void* buffer, std::size_t size) -> bool { return my_proc.write_phys(buffer, addr, size); }; @@ -61,7 +66,7 @@ int __cdecl main(int argc, char** argv) vdm.set_read(_read_phys); vdm.set_write(_write_phys); - nasa::mem_ctx target_proc(vdm, std::atoi(argv[2])); + ptm::ptm_ctx target_proc(&vdm, std::atoi(argv[2])); nasa::pclone_ctx clone_ctx(&target_proc); const auto [clone_pid, clone_handle] = clone_ctx.clone(); @@ -69,13 +74,16 @@ int __cdecl main(int argc, char** argv) vdm.get_peprocess(clone_pid); const auto clone_vad = - vad::get_vad_root(vdm, vdm.get_peprocess(std::atoi(argv[2]))); + vad::get_vad_root(vdm, + vdm.get_peprocess(std::atoi(argv[2]))); vad::set_vad_root(vdm, clone_peproc, clone_vad); unsigned short mz = 0u; std::size_t bytes_read; - ReadProcessMemory(clone_handle, GetModuleHandleA("ntdll.dll"), &mz, sizeof mz, &bytes_read); + + ReadProcessMemory(clone_handle, + GetModuleHandleA("ntdll.dll"), &mz, sizeof mz, &bytes_read); std::printf("[+] handle -> 0x%x, clone pid -> 0x%x\n", clone_handle, clone_pid); std::printf("[+] ntdll mz in clone -> 0x%x\n", mz); diff --git a/pclone/mem_ctx/mem_ctx.hpp b/pclone/mem_ctx/mem_ctx.hpp deleted file mode 100644 index 22a7ca2..0000000 --- a/pclone/mem_ctx/mem_ctx.hpp +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once -#include "../util/nt.hpp" -#include "../vdm_ctx/vdm_ctx.hpp" - -namespace nasa -{ - class mem_ctx - { - friend class pclone_ctx; - public: - mem_ctx(vdm::vdm_ctx& v_ctx, std::uint32_t pid = GetCurrentProcessId()); - std::pair get_pte(void* addr, bool use_hyperspace = false); - void set_pte(void* addr, const ::pte& pte, bool use_hyperspace = false); - - std::pair get_pde(void* addr, bool use_hyperspace = false); - void set_pde(void* addr, const ::pde& pde, bool use_hyperspace = false); - - std::pair get_pdpte(void* addr, bool use_hyperspace = false); - void set_pdpte(void* addr, const ::pdpte& pdpte, bool use_hyperspace = false); - - std::pair get_pml4e(void* addr, bool use_hyperspace = false); - void set_pml4e(void* addr, const ::pml4e& pml4e, bool use_hyperspace = false); - - void* get_dirbase() const; - static void* get_dirbase(vdm::vdm_ctx& v_ctx, std::uint32_t pid); - - bool read_phys(void* buffer, void* addr, std::size_t size); - bool write_phys(void* buffer, void* addr, std::size_t size); - - template - T read_phys(void* addr) - { - T buffer; - read_phys((void*)&buffer, addr, sizeof(T)); - return buffer; - } - - template - void write_phys(void* addr, const T& data) - { - write_phys((void*)&data, addr, sizeof(T)); - } - - std::pair read_virtual(void* buffer, void* addr, std::size_t size); - std::pair write_virtual(void* buffer, void* addr, std::size_t size); - - template - __forceinline T read_virtual(void* addr) - { - T buffer; - read_virtual((void*)&buffer, addr, sizeof(T)); - return buffer; - } - - template - __forceinline void write_virtual(void* addr, const T& data) - { - write_virtual((void*)&data, addr, sizeof(T)); - } - - void* virt_to_phys(pt_entries& entries, void* addr); - void* set_page(void* addr); - void* get_page() const; - unsigned get_pid() const; - - pml4e operator[](std::uint16_t pml4_idx); - pdpte operator[](const std::pair& entry_idx); - pde operator[](const std::tuple& entry_idx); - pte operator[](const std::tuple& entry_idx); - private: - - bool hyperspace_entries(pt_entries& entries, void* addr); - void* dirbase; - vdm::vdm_ctx* v_ctx; - std::uint16_t pml4e_index, - pdpte_index, - pde_index, - pte_index, - page_offset; - - std::pair new_pdpt; - std::pair new_pd; - std::pair new_pt; - unsigned pid; - }; -} \ No newline at end of file diff --git a/pclone/pclone.vcxproj b/pclone/pclone.vcxproj index e107aa3..a78a148 100644 --- a/pclone/pclone.vcxproj +++ b/pclone/pclone.vcxproj @@ -84,15 +84,15 @@ - + - + diff --git a/pclone/pclone.vcxproj.filters b/pclone/pclone.vcxproj.filters index 2c11f10..3d02fa0 100644 --- a/pclone/pclone.vcxproj.filters +++ b/pclone/pclone.vcxproj.filters @@ -23,9 +23,6 @@ Source Files - - Source Files - Source Files @@ -35,6 +32,9 @@ Source Files + + Source Files + @@ -55,9 +55,6 @@ Header Files\util - - Header Files - Header Files @@ -67,6 +64,9 @@ Header Files + + Header Files + diff --git a/pclone/pclone_ctx/pclone_ctx.cpp b/pclone/pclone_ctx/pclone_ctx.cpp index d4fe9c0..42777de 100644 --- a/pclone/pclone_ctx/pclone_ctx.cpp +++ b/pclone/pclone_ctx/pclone_ctx.cpp @@ -2,7 +2,7 @@ namespace nasa { - pclone_ctx::pclone_ctx(mem_ctx* clone_ctx) + pclone_ctx::pclone_ctx(ptm::ptm_ctx* clone_ctx) : clone_target_ctx(clone_ctx) {} @@ -43,7 +43,7 @@ namespace nasa // cloned... const auto clone_target_peproc = reinterpret_cast( - v_ctx->get_peprocess(clone_target_ctx->get_pid())); + v_ctx->get_peprocess(clone_target_ctx->pid)); const auto clone_target_dirbase = v_ctx->rkm(clone_target_peproc + 0x28); diff --git a/pclone/pclone_ctx/pclone_ctx.hpp b/pclone/pclone_ctx/pclone_ctx.hpp index d1da464..a0440a7 100644 --- a/pclone/pclone_ctx/pclone_ctx.hpp +++ b/pclone/pclone_ctx/pclone_ctx.hpp @@ -1,4 +1,4 @@ -#include "../mem_ctx/mem_ctx.hpp" +#include "../ptm_ctx/ptm_ctx.hpp" namespace nasa { @@ -6,9 +6,9 @@ namespace nasa { friend class mem_ctx; public: - explicit pclone_ctx(mem_ctx* clone_ctx); + explicit pclone_ctx(ptm::ptm_ctx* clone_ctx); auto clone() -> std::pair; private: - mem_ctx* clone_target_ctx; + ptm::ptm_ctx* clone_target_ctx; }; } \ No newline at end of file diff --git a/pclone/mem_ctx/mem_ctx.cpp b/pclone/ptm_ctx/ptm_ctx.cpp similarity index 66% rename from pclone/mem_ctx/mem_ctx.cpp rename to pclone/ptm_ctx/ptm_ctx.cpp index 27abee6..bb87228 100644 --- a/pclone/mem_ctx/mem_ctx.cpp +++ b/pclone/ptm_ctx/ptm_ctx.cpp @@ -1,21 +1,26 @@ -#include "mem_ctx.hpp" +#include "ptm_ctx.hpp" -namespace nasa +namespace ptm { - mem_ctx::mem_ctx(vdm::vdm_ctx& v_ctx, std::uint32_t pid) + ptm_ctx::ptm_ctx(vdm::vdm_ctx* v_ctx, std::uint32_t pid) : - v_ctx(&v_ctx), - pid(pid), - dirbase(get_dirbase(v_ctx, pid)) + v_ctx(v_ctx), + dirbase(get_dirbase(*v_ctx, pid)), + pid(pid) { // find an empty pml4e inside of current processes pml4... const auto current_pml4 = - v_ctx.get_virtual(reinterpret_cast( - get_dirbase(v_ctx, GetCurrentProcessId()))); + v_ctx->get_virtual(reinterpret_cast( + get_dirbase(*v_ctx, GetCurrentProcessId()))); for (auto idx = 100u; idx > 0u; --idx) - if (!v_ctx.rkm(current_pml4 + (idx * sizeof pml4e)).value) + { + if (!v_ctx->rkm(current_pml4 + (idx * sizeof pml4e)).present) + { this->pml4e_index = idx; + break; + } + } // allocate a pdpt this->new_pdpt.second = @@ -31,13 +36,17 @@ namespace nasa // get page table entries for new pdpt pt_entries new_pdpt_entries; hyperspace_entries(new_pdpt_entries, new_pdpt.second); - this->new_pdpt.first = reinterpret_cast(new_pdpt_entries.pt.second.pfn << 12); + + this->new_pdpt.first = + reinterpret_cast( + new_pdpt_entries.pt.second.pfn << 12); // make a new pml4e that points to our new pdpt. new_pdpt_entries.pml4.second.pfn = new_pdpt_entries.pt.second.pfn; // set the pml4e to point to the new pdpt - set_pml4e(reinterpret_cast<::ppml4e>(get_dirbase()) + this->pml4e_index, new_pdpt_entries.pml4.second, true); + set_pml4e(reinterpret_cast<::ppml4e>(this->dirbase) + + this->pml4e_index, new_pdpt_entries.pml4.second, true); // make a new pd this->new_pd.second = @@ -54,7 +63,10 @@ namespace nasa // get paging table entries for pd pt_entries new_pd_entries; hyperspace_entries(new_pd_entries, this->new_pd.second); - this->new_pd.first = reinterpret_cast(new_pd_entries.pt.second.pfn << 12); + + this->new_pd.first = + reinterpret_cast( + new_pd_entries.pt.second.pfn << 12); // make a new pt this->new_pt.second = @@ -67,29 +79,39 @@ namespace nasa )); PAGE_IN(this->new_pt.second, PAGE_4KB); + // get paging table entries for pt pt_entries new_pt_entries; hyperspace_entries(new_pt_entries, this->new_pt.second); - this->new_pt.first = reinterpret_cast(new_pt_entries.pt.second.pfn << 12); + + this->new_pt.first = + reinterpret_cast( + new_pt_entries.pt.second.pfn << 12); + } + + ptm_ctx::~ptm_ctx() + { + const auto pml4 = + reinterpret_cast( + set_page(dirbase))[pml4e_index] = pml4e{ NULL }; } - void* mem_ctx::set_page(void* addr) + void* ptm_ctx::set_page(void* addr) { - // table entry change. ++pte_index; - if (pte_index >= 511) + if (pte_index > 511) { ++pde_index; pte_index = 0; } - if (pde_index >= 511) + if (pde_index > 511) { ++pdpte_index; pde_index = 0; } - if (pdpte_index >= 511) + if (pdpte_index > 511) pdpte_index = 0; pdpte new_pdpte = { NULL }; @@ -97,7 +119,6 @@ namespace nasa new_pdpte.rw = true; new_pdpte.pfn = reinterpret_cast(new_pd.first) >> 12; new_pdpte.user_supervisor = true; - new_pdpte.accessed = true; // set pdpte entry *reinterpret_cast(new_pdpt.second + pdpte_index) = new_pdpte; @@ -107,7 +128,6 @@ namespace nasa new_pde.rw = true; new_pde.pfn = reinterpret_cast(new_pt.first) >> 12; new_pde.user_supervisor = true; - new_pde.accessed = true; // set pde entry *reinterpret_cast(new_pd.second + pde_index) = new_pde; @@ -117,7 +137,6 @@ namespace nasa new_pte.rw = true; new_pte.pfn = reinterpret_cast(addr) >> 12; new_pte.user_supervisor = true; - new_pte.accessed = true; // set pte entry *reinterpret_cast(new_pt.second + pte_index) = new_pte; @@ -127,7 +146,7 @@ namespace nasa return get_page(); } - void* mem_ctx::get_page() const + void* ptm_ctx::get_page() const { // builds a new address given the state of all table indexes virt_addr_t new_addr; @@ -136,21 +155,42 @@ namespace nasa new_addr.pd_index = this->pde_index; new_addr.pt_index = this->pte_index; new_addr.offset = this->page_offset; + + // handle TLB issues, the TLB might need to be flushed for this entry... + __try + { + *(std::uint8_t*)new_addr.value = *(std::uint8_t*)new_addr.value; + return new_addr.value; + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + // try again to access the page... + __try + { + *(std::uint8_t*)new_addr.value = *(std::uint8_t*)new_addr.value; + return new_addr.value; + } + // try one last time by yeilding execution... + __except (EXCEPTION_EXECUTE_HANDLER) + { + while (!SwitchToThread()) + continue; + } + } return new_addr.value; } - void* mem_ctx::get_dirbase(vdm::vdm_ctx& v_ctx, std::uint32_t pid) + void* ptm_ctx::get_dirbase(vdm::vdm_ctx& v_ctx, DWORD pid) { const auto peproc = - reinterpret_cast(v_ctx.get_peprocess(pid)); - - const auto dirbase = - v_ctx.rkm(peproc + 0x28); + reinterpret_cast( + v_ctx.get_peprocess(pid)); - return reinterpret_cast(dirbase.pfn << 12); + return reinterpret_cast( + v_ctx.rkm(peproc + 0x28).pfn << 12); } - bool mem_ctx::hyperspace_entries(pt_entries& entries, void* addr) + bool ptm_ctx::hyperspace_entries(pt_entries& entries, void* addr) { if (!addr || !dirbase) return false; @@ -192,95 +232,106 @@ namespace nasa return true; } - std::pair mem_ctx::get_pte(void* addr, bool use_hyperspace) + auto ptm_ctx::get_pte(void* addr, bool use_hyperspace) -> std::pair { if (!dirbase || !addr) - return {}; + return { {}, {} }; pt_entries entries; if ((use_hyperspace ? hyperspace_entries(entries, addr) : (bool)virt_to_phys(entries, addr))) return { entries.pt.first, entries.pt.second }; - return {}; + + return { {}, {} }; } - void mem_ctx::set_pte(void* addr, const ::pte& pte, bool use_hyperspace) + bool ptm_ctx::set_pte(void* addr, const ::pte& pte, bool use_hyperspace) { if (!dirbase || !addr) - return; + return false; if (use_hyperspace) - v_ctx->wkm(v_ctx->get_virtual(reinterpret_cast(addr)), pte); - else - write_phys(addr, pte); + return v_ctx->wkm( + v_ctx->get_virtual( + reinterpret_cast(addr)), pte); + + return write_phys(addr, pte); } - std::pair mem_ctx::get_pde(void* addr, bool use_hyperspace) + auto ptm_ctx::get_pde(void* addr, bool use_hyperspace) -> std::pair { if (!dirbase || !addr) - return {}; + return { {}, {} }; pt_entries entries; if ((use_hyperspace ? hyperspace_entries(entries, addr) : (bool)virt_to_phys(entries, addr))) return { entries.pd.first, entries.pd.second }; - return {}; + return { {}, {} }; } - void mem_ctx::set_pde(void* addr, const ::pde& pde, bool use_hyperspace) + bool ptm_ctx::set_pde(void* addr, const ::pde& pde, bool use_hyperspace) { - if (!this->dirbase || !addr) - return; + if (!dirbase || !addr) + return false; if (use_hyperspace) - v_ctx->wkm(v_ctx->get_virtual(reinterpret_cast(addr)), pde); - else - write_phys(addr, pde); + return v_ctx->wkm( + v_ctx->get_virtual( + reinterpret_cast(addr)), pde); + + return write_phys(addr, pde); } - std::pair mem_ctx::get_pdpte(void* addr, bool use_hyperspace) + auto ptm_ctx::get_pdpte(void* addr, bool use_hyperspace) -> std::pair { if (!dirbase || !addr) - return {}; + return { {}, {} }; pt_entries entries; if ((use_hyperspace ? hyperspace_entries(entries, addr) : (bool)virt_to_phys(entries, addr))) return { entries.pdpt.first, entries.pdpt.second }; - return {}; + + return { {}, {} }; } - void mem_ctx::set_pdpte(void* addr, const ::pdpte& pdpte, bool use_hyperspace) + bool ptm_ctx::set_pdpte(void* addr, const ::pdpte& pdpte, bool use_hyperspace) { - if (!this->dirbase || !addr) - return; + if (!dirbase || !addr) + return false; if (use_hyperspace) - v_ctx->wkm(v_ctx->get_virtual(reinterpret_cast(addr)), pdpte); - else - write_phys(addr, pdpte); + return v_ctx->wkm( + v_ctx->get_virtual( + reinterpret_cast(addr)), pdpte); + + return write_phys(addr, pdpte); } - std::pair mem_ctx::get_pml4e(void* addr, bool use_hyperspace) + auto ptm_ctx::get_pml4e(void* addr, bool use_hyperspace) -> std::pair { - if (!this->dirbase || !addr) - return {}; + if (!dirbase || !addr) + return { {}, {} }; pt_entries entries; if ((use_hyperspace ? hyperspace_entries(entries, addr) : (bool)virt_to_phys(entries, addr))) return { entries.pml4.first, entries.pml4.second }; - return {}; + + return { {}, {} }; } - void mem_ctx::set_pml4e(void* addr, const ::pml4e& pml4e, bool use_hyperspace) + bool ptm_ctx::set_pml4e(void* addr, const ::pml4e& pml4e, bool use_hyperspace) { - if (!this->dirbase || !addr) - return; + if (!dirbase || !addr) + return false; if (use_hyperspace) - v_ctx->wkm(v_ctx->get_virtual(reinterpret_cast(addr)), pml4e); - else - write_phys(addr, pml4e); + return v_ctx->wkm( + v_ctx->get_virtual( + reinterpret_cast(addr)), pml4e); + + return write_phys(addr, pml4e); } - std::pair mem_ctx::read_virtual(void* buffer, void* addr, std::size_t size) + auto ptm_ctx::read_virtual(void* buffer, void* addr, std::size_t size) -> std::pair { if (!buffer || !addr || !size || !dirbase) return {}; @@ -322,7 +373,7 @@ namespace nasa } } - std::pair mem_ctx::write_virtual(void* buffer, void* addr, std::size_t size) + auto ptm_ctx::write_virtual(void* buffer, void* addr, std::size_t size) -> std::pair { if (!buffer || !addr || !size || !dirbase) return {}; @@ -364,7 +415,7 @@ namespace nasa } } - bool mem_ctx::read_phys(void* buffer, void* addr, std::size_t size) + bool ptm_ctx::read_phys(void* buffer, void* addr, std::size_t size) { if (!buffer || !addr || !size) return false; @@ -381,7 +432,7 @@ namespace nasa return true; } - bool mem_ctx::write_phys(void* buffer, void* addr, std::size_t size) + bool ptm_ctx::write_phys(void* buffer, void* addr, std::size_t size) { if (!buffer || !addr || !size) return false; @@ -398,9 +449,9 @@ namespace nasa return true; } - void* mem_ctx::virt_to_phys(pt_entries& entries, void* addr) + void* ptm_ctx::virt_to_phys(pt_entries& entries, void* addr) { - if (!addr || !this->dirbase) + if (!addr || !dirbase) return {}; const virt_addr_t virt_addr{ addr }; @@ -444,37 +495,4 @@ namespace nasa return reinterpret_cast((pte.pfn << 12) + virt_addr.offset); } - - unsigned mem_ctx::get_pid() const - { - return this->pid; - } - - void* mem_ctx::get_dirbase() const - { - return this->dirbase; - } - - pml4e mem_ctx::operator[](std::uint16_t pml4_idx) - { - return read_phys<::pml4e>(reinterpret_cast(this->dirbase) + pml4_idx); - } - - pdpte mem_ctx::operator[](const std::pair& entry_idx) - { - const auto pml4_entry = this->operator[](entry_idx.first); - return read_phys<::pdpte>(reinterpret_cast(pml4_entry.pfn << 12) + entry_idx.second); - } - - pde mem_ctx::operator[](const std::tuple& entry_idx) - { - const auto pdpt_entry = this->operator[]({ std::get<0>(entry_idx), std::get<1>(entry_idx) }); - return read_phys<::pde>(reinterpret_cast(pdpt_entry.pfn << 12) + std::get<2>(entry_idx)); - } - - pte mem_ctx::operator[](const std::tuple& entry_idx) - { - const auto pd_entry = this->operator[]({ std::get<0>(entry_idx), std::get<1>(entry_idx), std::get<2>(entry_idx) }); - return read_phys<::pte>(reinterpret_cast(pd_entry.pfn << 12) + std::get<3>(entry_idx)); - } } \ No newline at end of file diff --git a/pclone/ptm_ctx/ptm_ctx.hpp b/pclone/ptm_ctx/ptm_ctx.hpp new file mode 100644 index 0000000..2ac89e8 --- /dev/null +++ b/pclone/ptm_ctx/ptm_ctx.hpp @@ -0,0 +1,80 @@ +#pragma once +#include "../util/nt.hpp" +#include "../vdm_ctx/vdm_ctx.hpp" + +namespace ptm +{ + class ptm_ctx + { + public: + explicit ptm_ctx(vdm::vdm_ctx* v_ctx, std::uint32_t pid = GetCurrentProcessId()); + ~ptm_ctx(); + + auto get_pte(void* addr, bool use_hyperspace = false) -> std::pair; + bool set_pte(void* addr, const ::pte& pte, bool use_hyperspace = false); + + auto get_pde(void* addr, bool use_hyperspace = false) -> std::pair; + bool set_pde(void* addr, const ::pde& pde, bool use_hyperspace = false); + + auto get_pdpte(void* addr, bool use_hyperspace = false) -> std::pair; + bool set_pdpte(void* addr, const ::pdpte& pdpte, bool use_hyperspace = false); + + auto get_pml4e(void* addr, bool use_hyperspace = false) -> std::pair; + bool set_pml4e(void* addr, const ::pml4e& pml4e, bool use_hyperspace = false); + static void* get_dirbase(vdm::vdm_ctx& v_ctx, DWORD pid); + + bool read_phys(void* buffer, void* addr, std::size_t size); + bool write_phys(void* buffer, void* addr, std::size_t size); + + template + __forceinline T read_phys(void* addr) + { + T buffer; + read_phys((void*)&buffer, addr, sizeof(T)); + return buffer; + } + + template + __forceinline bool write_phys(void* addr, const T& data) + { + return write_phys((void*)&data, addr, sizeof(T)); + } + + auto read_virtual(void* buffer, void* addr, std::size_t size) -> std::pair; + auto write_virtual(void* buffer, void* addr, std::size_t size) -> std::pair; + + template + __forceinline T read_virtual(void* addr) + { + T buffer; + read_virtual((void*)&buffer, addr, sizeof(T)); + return buffer; + } + + template + __forceinline void write_virtual(void* addr, const T& data) + { + write_virtual((void*)&data, addr, sizeof(T)); + } + + void* virt_to_phys(pt_entries& entries, void* addr); + bool hyperspace_entries(pt_entries& entries, void* addr); + + void* set_page(void* addr); + void* get_page() const; + + unsigned pid; + void* dirbase; + vdm::vdm_ctx* v_ctx; + private: + std::uint16_t pml4e_index, + pdpte_index, + pde_index, + pte_index, + page_offset; + + std::pair new_pdpt; + std::pair new_pd; + std::pair new_pt; + }; +} \ No newline at end of file diff --git a/pclone/vdm_ctx/vdm_ctx.cpp b/pclone/vdm_ctx/vdm_ctx.cpp index 1454478..d699f74 100644 --- a/pclone/vdm_ctx/vdm_ctx.cpp +++ b/pclone/vdm_ctx/vdm_ctx.cpp @@ -2,11 +2,7 @@ namespace vdm { - vdm_ctx::vdm_ctx - ( - std::function& read_func, - std::function& write_func - ) + vdm_ctx::vdm_ctx(read_phys_t& read_func, write_phys_t& write_func) : read_phys(read_func), write_phys(write_func) @@ -41,12 +37,12 @@ namespace vdm search_thread.join(); } - void vdm_ctx::set_read(std::function& read_func) + void vdm_ctx::set_read(read_phys_t& read_func) { this->read_phys = read_func; } - void vdm_ctx::set_write(std::function& write_func) + void vdm_ctx::set_write(write_phys_t& write_func) { this->write_phys = write_func; } diff --git a/pclone/vdm_ctx/vdm_ctx.hpp b/pclone/vdm_ctx/vdm_ctx.hpp index 37bc3c4..3d862d5 100644 --- a/pclone/vdm_ctx/vdm_ctx.hpp +++ b/pclone/vdm_ctx/vdm_ctx.hpp @@ -26,14 +26,9 @@ namespace vdm class vdm_ctx { public: - vdm_ctx - ( - std::function& read_func, - std::function& write_func - ); - - void set_read(std::function& read_func); - void set_write(std::function& write_func); + explicit vdm_ctx(read_phys_t& read_func, write_phys_t& write_func); + void set_read(read_phys_t& read_func); + void set_write(write_phys_t& write_func); template __forceinline std::invoke_result_t syscall(void* addr, Ts ... args) const @@ -83,12 +78,12 @@ namespace vdm } template - __forceinline void wkm(std::uintptr_t addr, const T& value) + __forceinline auto wkm(std::uintptr_t addr, const T& value) -> bool { static const auto ntoskrnl_memcpy = util::get_kmodule_export("ntoskrnl.exe", "memcpy"); - this->syscall( + return this->syscall( ntoskrnl_memcpy, (void*)addr, &value, sizeof T); }