I started a bit researching around fuzzers, fuzzing techniques and practices. As i study materials about fuzzing, code (node / edge) coverage approach quickly impressed me. But for this method is essential to have a good dbi. Pin or valgrind are good solutions, but will i try to make it in lightweight way – specificated for further fuzzing needs.
Already implemented features :
- BTF – Hypervisor based
- PageTable walker
- VAD walker
- full Process control [images, threads, memory]
- Syscall monitoring – implemented process virtual memory monitor
FEATURES
- BTF – Hypervisor based
Branch tracing is known method already implemented in some of tracers, but known (*known for me) methods implemented it just under debugger. When it comes to play with binary code (tracing, unpacking, monitoring) – i dont like simulated enviroment like debugger, because it is too slow… it can be little crappy to set up BTF in msr, in ring0, wait for exception processing and for finaly executing your exception handler to handle branch tracing, and for keeping track to set up BTF in msr == switch to ring0 again! this seems like a solid perfomance overkill.
But in previous post i mentioned possibility how to use intel vtx technology to gain some advantages for reasonable performance penalty. After a bit playing with documentation and some debuging, i come to easy way how to extend hypervisor, that was introduced, with handling TRAPS and keep eye on BTF!
In other words when Trap flag is set then each trap exception will cause VM_EXIT. So tracing on branches will be handled not by system exception handling but by our monitor! and with perfomance penalty == our processing BTF and VM_EXIT cost!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//turn on vm_exit on traps ! if (m_exceptionhandling) { //activate exception handling ULONG_PTR int_state; vmread(VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE, &int_state); if((int_state & 3)) { int_state &= ~(3); vmwrite(VMX_VMCS32_GUEST_INTERRUPTIBILITY_STATE, int_state); } ULONG_PTR intercepts; vmread(VMX_VMCS_CTRL_EXCEPTION_BITMAP, &intercepts); unsigned long mask = BTS(TRAP_debug);// | BTS(TRAP_page_fault); intercepts |= mask; vmwrite(VMX_VMCS_CTRL_EXCEPTION_BITMAP, intercepts); } |
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 |
... m_traps[VMX_EXIT_EXCEPTION] = (ULONG_PTR)TrapHandler; ... void CDbiMonitor::TrapHandler( __inout ULONG_PTR reg[REG_COUNT] ) { size_t ins_len; if (!vmread(VMX_VMCS32_RO_EXIT_INSTR_LENGTH, &ins_len)) { ULONG_PTR ins_addr; //original 'ret'-addr if (!vmread(VMX_VMCS64_GUEST_RIP, &ins_addr)) { ins_addr -= ins_len; CDbiMonitor::GetInstance().GetPrintfStack().Push(ins_addr); //print some info => i < 4 == old cpu :P ULONG_PTR src = 0; for (BYTE i = 0; i < 4; i++) { if (rdmsr(MSR_LASTBRANCH_0_TO_IP + i) == ins_addr) { src = rdmsr(MSR_LASTBRANCH_0_FROM_IP + i); CDbiMonitor::GetInstance().Printf().Push(i); CDbiMonitor::GetInstance().Printf().Push(src); break; } } //printf marker CDbiMonitor::GetInstance().GetPrintfStack().Push(0xBADF00D0); //set-up next BTF hook ULONG_PTR rflags = 0; if (!vmread(VMX_VMCS_GUEST_RFLAGS, &rflags)) vmwrite(VMX_VMCS_GUEST_RFLAGS, (rflags | TRAP)); vmwrite(VMX_VMCS64_GUEST_RIP, ins_addr); } } } |
- PageTable walker
For effective fuzzing it is necessary to fuzz application from particular state (or you can just kill perfomance for re-running application per fuzz test case), and with this is related saving context -> memory address space, thread context (registers / stack). It is no such big deal just enumerate memory address space, save context and you have it .. but it need a lot of memory resources and it is time consuming as well …
For handling this step, i propose protecting all (not write-copy, and exluding stacks) memory as non-writeable, monitoring acces to write and saving affected memory (by custom PAGE_SIZE granularity – not saving just affected bytes alone => lookup & copy performance). But doing it by additional VirtualProtect dont bring time effective results…
So after some educating how exactly PageTable looks like, and some googling how to handle it i create PoC of pykd – script :
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 |
#.load pykd.pyd; !py mmu import sys from pykd import * nt = module("nt") ctx = {} def IsInRange(module, addr): return (addr >= module.begin() and addr <= module.end()) import ctypes c_uint64 = ctypes.c_uint64 class CVAFlags(ctypes.LittleEndianStructure): _pack_ = 1 _fields_ = [ ("ByteOffset", c_uint64, 12), ("PTESelector", c_uint64, 9), ("PTSelector", c_uint64, 9), ("PDPSelector", c_uint64, 9), ("PML4Selector", c_uint64, 9), ] class CVirtualAddress(ctypes.Union): _fields_ = [("VA", CVAFlags), ("Value", c_uint64)] class CMMPTEFlags(ctypes.LittleEndianStructure): _pack_ = 1 _fields_ = [ ("Valid", c_uint64, 1), ("Write", c_uint64, 1), ("Owner", c_uint64, 1), ("WriteTrough", c_uint64, 1), ("CacheDisabled", c_uint64, 1), ("Accessed", c_uint64, 1), ("Dirty", c_uint64, 1), ("LargePage", c_uint64, 1), ("Global", c_uint64, 1), ("SFCopyOnWrite", c_uint64, 1), ("SFPrototypePTE", c_uint64, 1), ("SFWrite", c_uint64, 1), ("PageFrameNumber", c_uint64, 28), ("Reserved", c_uint64, 12), ("SFWorkingSetIndex", c_uint64, 11), ("NoExecute", c_uint64, 1), ] class CMMPTE(ctypes.Union): _fields_ = [("HWPTE", CMMPTEFlags), ("Value", c_uint64)] X64SIZE = 8 def GetNextTable(table, selector): offset = (table.HWPTE.PageFrameNumber << 12) + selector * X64SIZE print [table.HWPTE.LargePage, hex(table.Value), hex(table.HWPTE.PageFrameNumber), hex(offset)] #print dbgCommand("!dq %s"%(hex(offset).replace("L", ""))).split(" ") #split("")[INDEX] index should be propebly choosen,look at print above table.Value = int(dbgCommand("!dq %s"%(hex(offset).replace("L", ""))).split(" ")[1].replace("`", ""), 16) return table addr = CVirtualAddress() addr.Value = 0x2340000 #HERE IS ADDRESS WHICH WE WOULD LIKE TO LOOKUP print dbgCommand("!pte %s"%hex(addr.Value).replace("L", "")) print "ByteOffset: ", hex(addr.VA.ByteOffset) print "PTESelector: ", hex(addr.VA.PTESelector) print "PTSelector: ", hex(addr.VA.PTSelector) print "PDPSelector: ", hex(addr.VA.PDPSelector) print "PML4Selector: ", hex(addr.VA.PML4Selector) print "------" offset = reg("CR3") + addr.VA.PML4Selector * X64SIZE print dbgCommand("!dq %s"%(hex(offset).replace("L", ""))).split(" ") #split("")[INDEX] index should be propebly choosen,look at print above pml4 = CMMPTE() pml4.Value = int(dbgCommand("!dq %s"%(hex(offset).replace("L", ""))).split(" ")[1].replace("`", ""), 16) print hex(pml4.HWPTE.PageFrameNumber) print hex(pml4.HWPTE.LargePage) print hex(pml4.HWPTE.Valid) print "get next table", hex(offset) pdp = GetNextTable(pml4, addr.VA.PDPSelector) pt = GetNextTable(pdp, addr.VA.PTSelector) pte = GetNextTable(pt, addr.VA.PTESelector) print [pte.HWPTE.Write, pte.HWPTE.NoExecute, hex(pte.HWPTE.PageFrameNumber), pte.HWPTE.LargePage] |
And wondering how it is easy, implemented c++ equivalent :
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 |
class CMMU { public: CMMU( __in const void* address ) : m_va(*reinterpret_cast<const VIRTUAL_ADDRESS*>(&address)), m_pml4(readcr3() + m_va.Selector.PML4Selector * sizeof(void*), sizeof(PAGE_TABLE_ENTRY)), m_pdp(GetNextTable(PML4(), m_va.Selector.PDPSelector), sizeof(PAGE_TABLE_ENTRY)), m_pt(GetNextTable(PDP(), m_va.Selector.PTSelector), sizeof(PAGE_TABLE_ENTRY)), m_pte(GetNextTable(PT(), m_va.Selector.PTESelector), sizeof(PAGE_TABLE_ENTRY)) { } ... protected: __forceinline __checkReturn const void* GetNextTable( __in const PAGE_TABLE_ENTRY* table, __in size_t selector ) { if (!table) return NULL; return reinterpret_cast<const void*>( (table->PageFrameNumber << PAGE_SHIFT) + selector * sizeof(void*)); } ... protected: CDispatchLvl m_irql; VIRTUAL_ADDRESS m_va; CMmMap m_pml4; CMmMap m_pdp; CMmMap m_pt; CMmMap m_pte; }; |
which save my day against perfomance kill by virtual protect API
Handling memory write attempts from app to protected memory is done via hook on PageFault, in which is memory temporary updated with original protection mask.
- VAD walker
But it have some issues! .. first of all, i will try to disable write by unset this flag in PTE by address when memory is allocated, buut … in this moment is PTE(addr).Valid == 0 … magic is, that for performance reason m$ will not create PTE per allocation request, but instead of this by first access (== pagefault) to this memory.
It can be overcomed to handling it after PTE will be craeted for given memory range, but more simplier option comes here. How m$ code know flags of memory, and even so, is that memory even allocated and so for sure access should be granted ? answer is VAD ! Some interesting reading can be found at Windows Internals, 6th edition [Chapter 10 Memory Management] .
So lets go update VAD instead of PTE per alloc . PTE should in exchange unlock (write enable) memory in PageFault caused by application attempt to writting to its own memory – but also get callback to us that particular bytes are likely to change.
VAD can be found at EPROCESS structure, and i am not proud of it, but it needs some system dependent constants (to avoid rebuild whole project, when it should be shipped on another version of windows, will be mentioned some TODO at the end of blog). And also great source of internal knowledge of m$ code (excluding ntoskrnl binary itself ) is reactos project.
From now it is easy to handle it :
- VAD is AVL-tree structured
- ptr to VAD is stored in EPROCESS
- lock address space is necessary
* With VAD walker is also easy to enumerate whole process address space *
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 |
//---------------------------------------------------------------- // ***************** VAD_ROOT ADDRESS SPACE LOCK ***************** //---------------------------------------------------------------- class CVADScanLock { public: CVADScanLock( __in PEPROCESS process ); ~CVADScanLock(); __checkReturn bool IsLocked(); protected: bool m_locked; CAutoProcessAttach m_attach; //CDisableKernelApc m_kernelapcDisabled; CAutoLock<CExclusiveLock> m_addressSpaceLock; CExclusiveLock m_workingSetLock; }; //----------------------------------------------------- // ****************** VAD AVL WALKER ****************** //----------------------------------------------------- class CVadWalker : public CBinTreeWalker<VAD_SHORT> { public: CVadWalker( __in PEPROCESS process ); __forceinline size_t GetSize() { return m_avlInfo->NumberGenericTableElements; } private: const AVL_INFO* m_avlInfo; }; //------------------------------------------------------ // ****************** VAD AVL SCANNER ****************** //------------------------------------------------------ __checkReturn bool CVadScanner::ScanAddressSpace() { CApcLvl irql; CVADScanLock vad_lock(m_process); if (vad_lock.IsLocked()) { CVadWalker vad(m_process); bool found = false; const VAD_SHORT* mem_descryptor = vad.GetLowerBound(); if (mem_descryptor) { do { CVadNodeMemRange mem(mem_descryptor); DbgPrint("\n>>> !Memory by VAD : %p %p [%p] %s", mem.Begin(), mem.End(), mem.GetFlags(), (mem.IsWriteable() ? "is writeable!" : "non writeable!") ); } while(vad.GetNext(&mem_descryptor)); return true; } } DbgPrint("\nerror not locked!!!"); return false; } |
- full Process control [images, threads, memory]
Under debugger you get events about everything, but if you set up correctly in your on-the-fly (>debuger free) monitor you can get same results as a callbacks – which ensure speed up of whole processing
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 |
struct FUZZ_THREAD_INFO; class CProcess2Fuzz : public CProcessContext, public CSyscallCallbacks { public: explicit CProcess2Fuzz( __inout PEPROCESS process, __in HANDLE processId, __inout_opt PS_CREATE_NOTIFY_INFO* createInfo ); ~CProcess2Fuzz(); static __checkReturn bool WatchProcess( __inout PEPROCESS eprocess, __in HANDLE processId, __inout_opt PS_CREATE_NOTIFY_INFO* createInfo ); void ProcessNotifyRoutineEx( __inout PEPROCESS eprocess, __in HANDLE processId, __inout_opt PS_CREATE_NOTIFY_INFO* createInfo ); void ChildProcessNotifyRoutineEx( __inout PEPROCESS eprocess, __in HANDLE processId, __inout_opt PS_CREATE_NOTIFY_INFO* createInfo ); void ImageNotifyRoutine( __in_opt UNICODE_STRING* fullImageName, __in HANDLE processId, __in IMAGE_INFO* imageInfo ); void ThreadNotifyRoutine( __in HANDLE processId, __in HANDLE threadId, __in BOOLEAN create ); void RemoteThreadNotifyRoutine( __in HANDLE processId, __in HANDLE threadId, __in BOOLEAN create ); __checkReturn virtual bool Syscall( __inout ULONG_PTR reg[REG_COUNT] ); __checkReturn bool PageFault( __inout ULONG_PTR reg[REG_COUNT] ); protected: __checkReturn bool VirtualMemoryCallback( __in void* memory, __in size_t size, __in bool write, __inout ULONG_PTR reg[REG_COUNT], __inout_opt BYTE* buffer = NULL ) override; void SetUnwriteable( __in const void* addr, __in size_t size ); protected: CLockedAVL<FUZZ_THREAD_INFO> m_threads; CLockedAVL<CHILD_PROCESS> m_childs; CLockedAVL<LOADED_IMAGE> m_loadedImgs; CLockedAVL<CMemoryRange> m_nonWritePages; CLockedAVL< CRange<ULONG_PTR> > m_stacks; }; //install CProcessMonitor() { m_processWorker = new CProcessCtxWorker<TYPE>; if (!m_processWorker) return; //registry callback UNICODE_STRING altitude; RtlInitUnicodeString(&altitude, L"360055");//FSFilter Activity Monitor { CPassiveLvl irql; NTSTATUS status; status = PsSetCreateProcessNotifyRoutineEx(ProcessNotifyRoutineEx, FALSE); ASSERT(STATUS_SUCCESS == status); status = PsSetLoadImageNotifyRoutine(ImageNotifyRoutine); ASSERT(STATUS_SUCCESS == status); status = PsSetCreateThreadNotifyRoutine(ThreadNotifyRoutine); ASSERT(STATUS_SUCCESS == status); status = CmRegisterCallbackEx(RegisterCallback, &altitude, gDriverObject, NULL, &m_cookie, NULL); ASSERT(STATUS_SUCCESS == status); } } |
- Syscall monitoring – implemented process virtual memory monitor
“System calls provide an essential interface between a process and the operating system.” – and so it is nice point to get hook, and monitor process. Now it is just implemented virtual memory monitor to keep eye on memory address space – protection of memory pages
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 |
class CSYSCALL { public: __checkReturn virtual bool Syscall( __inout ULONG_PTR reg[REG_COUNT] ) { ULONG_PTR ring0rsp = reg[RSP]; //-2 == simulating push ebp, pushfq to copy state as in reg[REG_COUNT] reg[RSP] = (ULONG_PTR)(get_ring3_rsp() - 2); bool status = false; switch ((ULONG)reg[RAX]) { case ntdll_NtAllocateVirtualMemory: status = NtAllocateVirtualMemory(reg); break; case ntdll_ZwFreeVirtualMemory: status = ZwFreeVirtualMemory(reg); break; case ntdll_ZwQueryVirtualMemory: status = ZwQueryVirtualMemory(reg); break; case ntdll_NtWriteVirtualMemory: status = NtWriteVirtualMemory(reg); break; case ntdll_NtReadVirtualMemory: status = NtReadVirtualMemory(reg); break; case ntdll_NtProtectVirtualMemory: status = NtProtectVirtualMemory(reg); break; case ntdll_NtFlushVirtualMemory: status = NtFlushVirtualMemory(reg); break; case ntdll_NtLockVirtualMemory: status = NtLockVirtualMemory(reg); break; case ntdll_ZwSetInformationVirtualMemory: status = ZwSetInformationVirtualMemory(reg); break; case ntdll_ZwUnlockVirtualMemory: status = ZwUnlockVirtualMemory(reg); break; default: break; } reg[RSP] = ring0rsp; return status; } ... } |
PROBLEMS :
- SysCall hook => PatchGuard
- PageFaul hook => PatchGuard
- VAD walker => windows version dependent!
- ring3 – ring0, ring3 – vmm communication => performance
SOLUTIONS [implemented just partlialy] :
- PatchGuard => VMM
- SysCall protect MSR via VMX_EXIT_RDMSR [implemented]
- PageFault protection via DRx – VMX_EXIT_DRX_MOVE
- ?? ->
- we can easly protect hook via DRx at IDT[page_fault] pointer
- to avoid this PatchGuard needs to clear dr7
- in VMM we trap dr acces and fool / terminate PatchGuard thread
- windows version dependent constants => constants should be provided be user app using this framework. This constants can be obtained manualy from windbg, ida, by windbg + script – pykd, or by playing with SymLoadModuleEx
- communication with ring0 and vmm parts of dbi => implement own fast calls [not properly implemented yet]
- ring3-ring0 => SYSENTER (mov eax, VMM_FASTCALL_R0)
- ring3-vmm => CPUID (mov eax, VMM_FASTCALL_VMM)
Idea is implement this dbi tool for fuzzing as a module, which can be fully used from python (or other -c++, ruby …), and has fastcall access to dbi modules.
FEATURES – present + TODO :
- implement all needed callbakcs :
- branch tracing -or single step
- memory access
- exception occured
- process / thread termination
- syscalls
- …
- accessible all needed info :
- loaded images
- enumerate whole process address space
- per thread information – stack, context …
- child processes
- …
- full process control
- save + restart state [ memory + context ]
- stop / pause / resume threads
- deny / allow process creation
- alter process context / memory
- alter process control flow
- …
So idea looks like :
For now i have just implemented PoC on crappy ring3 app. Windows 8, dbi driver x64, app x86.
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 |
#include "stdafx.h" #include "Windows.h" #include <intrin.h> #include <excpt.h> int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep) { printf("in filter."); OutputDebugString(L"\n********** >> in filter."); if (code == EXCEPTION_ACCESS_VIOLATION) { printf("caught AV as expected."); OutputDebugString(L"\n********** >> ********** caught AV as expected."); return EXCEPTION_EXECUTE_HANDLER; } else { printf("didn't catch AV, unexpected."); OutputDebugString(L"\n********** >> ********** didn't catch AV, unexpected."); return EXCEPTION_CONTINUE_SEARCH; }; } int _tmain(int argc, _TCHAR* argv[]) { #define DEF_SIZE 0x100 char* mem = (char*)VirtualAlloc((LPVOID)0x2340000, DEF_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (mem && mem == (char*)0x2340000) { int CPUInfo[4] = {0}; int InfoType = 0xBADF00D0; __cpuid(CPUInfo, InfoType); int z = 0; for (int i = 0; i < 0x10; i++) z += i; __try { accesv: *(mem) = 2; if (false) goto accesv; } __except (filter(GetExceptionCode(), GetExceptionInformation())) { } *(mem + 2) = 2; DebugBreak(); //*(mem) = 2; printf("koncek"); OutputDebugString(L"\n********** << finish"); } else { printf("mem : %p", mem); VirtualFree(mem, DEF_SIZE, MEM_FREE); } DebugBreak(); return 0; } |
.. as demo was written this concept, which at alloc set unwritable allocated memory, and at first access it ignore it – exception handling in try blog is invoked, but at second acces is access granted by setting PTE(address).Write = 1 in PageFault
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 |
__checkReturn bool CProcess2Fuzz::VirtualMemoryCallback( __in void* memory, __in size_t size, __in bool write, __inout ULONG_PTR reg[REG_COUNT], __inout_opt BYTE* buffer /*= NULL*/ ) { DbgPrint("\n@VirtualMemoryCallback %p %p [thread : %p]\n", PsGetThreadProcessId(PsGetCurrentThread()), m_processId, PsGetCurrentThread()); FUZZ_THREAD_INFO* fuzz_thread; if (m_threads.Find(FUZZ_THREAD_INFO(), &fuzz_thread)) { ULONG_PTR* r3stack = get_ring3_rsp(); DbgPrint("\n > I. @Prologue %p %p [%p]\n", r3stack, *r3stack, reg[RCX]); fuzz_thread->SetCallbackEpilogue(reg, memory, size, write); } return false; } __checkReturn bool CProcess2Fuzz::Syscall( __inout ULONG_PTR reg[REG_COUNT] ) { //implement ref counting ? auto_ptr... //but assumption, if thread is in syscall then it can not exit for now good enough... FUZZ_THREAD_INFO* fuzz_thread; if (m_threads.Find(FUZZ_THREAD_INFO(), &fuzz_thread)) { if (fuzz_thread->WaitForSyscallEpilogue()) { if (fuzz_thread->MemoryInfo.Write && //CMemoryRange((BYTE*)fuzz_thread->MemoryInfo.Memory, fuzz_thread->MemoryInfo.Size, 0).IsInRange((BYTE*)0x2340000)) !m_stacks.Find(CRange<ULONG_PTR>(reinterpret_cast<ULONG_PTR*>(fuzz_thread->MemoryInfo.Memory)))) { SetUnwriteable(fuzz_thread->MemoryInfo.Memory, fuzz_thread->MemoryInfo.Size); } DbgPrint("\n > @Epilogue %p %x %s\n", fuzz_thread->MemoryInfo.Memory, fuzz_thread->MemoryInfo.Size, fuzz_thread->MemoryInfo.Write ? "attempt to write" : "easy RE+ attempt"); fuzz_thread->EpilogueProceeded(); return true; } } return CSYSCALL::Syscall(reg); } __checkReturn bool CProcess2Fuzz::PageFault( __inout ULONG_PTR reg[REG_COUNT] ) { BYTE* fault_addr = reinterpret_cast<BYTE*>(readcr2()); //temporary for demo if (0x2340000 == (ULONG_PTR)fault_addr) return false; if (0x2340002 == (ULONG_PTR)fault_addr) KeBreak(); if (m_nonWritePages.Find(CMemoryRange(fault_addr, sizeof(BYTE)))) { m_nonWritePages.Pop(CMemoryRange(fault_addr, sizeof(BYTE))); if (!CMMU::IsWriteable(fault_addr)) { CMMU::SetWriteable(fault_addr, sizeof(ULONG_PTR)); //+set trap after instruction, to set unwriteable! //sync problem, not locked and acces via ref, via ref counting ... return true; } } return false; } |
and some DbgPrint by monitoring it follows :
00000000BADF00D0 is marker of BTF, before is printed source instruction (which changed control flow), and after follow destination address (current rip). @VirtualMemoryCallback + @Prologue + @Epilogue is implementation of current state of SYSCALL + PageFault cooperating to handle memory writes – used PTE and VAD.
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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
CCRonos ctorDriverEntry CSysCall::SetVirtualizationCallbacks CCRonos::SetVirtualizationCallbacks CCRonos::PerCoreAction - Lets Go start virtualizee cpu : 0 ! Hooked. procid [0] <=> syscall addr [FFFFF802EBE641C0] Virtualization is enabled! ~~~~~~~~~~~ CPUID (0) : PILL open gate! ~~~~~~~~~~~ RdmsrHook FFFFF88009647AE2 [pethread : FFFFFA8005DA7B00] -> dst = FFFFF802EBE641C0 II. procid [0] <=> syscall addr [FFFFF802EBE641C0] EXWORKER: worker exit with system affinity set, worker routine FFFFF802EC1B7478, parameter FFFFF88008797BE0, item FFFFF88008797BE0 @ProcessNotifyRoutineEx 9e0 FFFFFA80087B2940 start REMOTE!ThreadNotifyRoutine a5c FFFFFA8008810940 start @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000001170000 0000000000097000] @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [000007F9D5280000 00000000001C0000] @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000076EC0000 0000000000157000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070F168 000007F9D52A50B1 [000007F9D5282D6A] > @Epilogue 0000000000000000 260000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070F168 000007F9D52A50F5 [000007F9D5282DCA] > @Epilogue 0000000000840000 1e0000 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070F168 000007F9D52A5166 [000007F9D5282D6A] > @Epilogue 0000000000A20000 2000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070F308 000007F9D52A7D81 [000007F9D5282DCA] > @Epilogue 0000000000690000 0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070F1D8 000007F9D52A6440 [000007F9D5282E1A] > @Epilogue 000007F9D5280000 0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E448 000007F9D52B7BF4 [000007F9D5282D6A] > @Epilogue 0000000000A22000 1000 attempt to write @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000076E10000 0000000000045000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070EC08 000007F9D52A0FD4 [000007F9D52830EA] > @Epilogue 0000000076E4E0B0 1060 attempt to write @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000076E60000 000000000005A000] @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000076E00000 0000000000008000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070EC08 000007F9D52A0FD4 [000007F9D52830EA] > @Epilogue 0000000076EB6128 e8 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070EB28 000007F9D52A13F7 [000007F9D52830EA] > @Epilogue 0000000076EB6128 e8 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070EC08 000007F9D52A0FD4 [000007F9D52830EA] > @Epilogue 0000000076E050E0 a0 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070EB28 000007F9D52A13F7 [000007F9D52830EA] > @Epilogue 0000000076E050E0 a0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070EB28 000007F9D52A13F7 [000007F9D52830EA] > @Epilogue 0000000076E4E0B0 1060 easy RE+ attempt @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000000840000 0000000000136000] @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [00000000767E0000 0000000000130000] @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000000840000 0000000000136000] @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000000840000 000000000014C000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000000000 2e0000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3D8 0000000076E223FD [000007F9D5282DCA] > @Epilogue 0000000000AA0000 1e0000 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C80000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C81000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C82000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3D8 0000000076E223FD [000007F9D5282DCA] > @Epilogue 00000000006B0000 0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E2F8 0000000076E23AE7 [000007F9D5282E1A] > @Epilogue 0000000076EC0000 0 easy RE+ attempt @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [00000000767E0000 0000000000130000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 0000000076860000 1240 attempt to write @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [00000000748D0000 00000000000A6000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 0000000074968000 900 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 0000000074968000 900 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 0000000076860000 1240 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070DCC8 000007F9D52B7BF4 [000007F9D5282D6A] > @Epilogue 0000000000A23000 2000 attempt to write CHILD!ProcessNotifyRoutineEx c30 00000000000009E0 start 7d7bd80 @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C83000 1000 attempt to write @ImageNotifyRoutine 9e0 FFFFFA80087B2940 [0000000074320000 00000000000A7000] @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 00000000743A8000 44c attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 00000000743A8000 44c easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C84000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C85000 2000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C87000 3000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C8A000 2000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000000000 3c48 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 00000000006A0000 7e0 attempt to write ZwRaiseException 0000000076E40350 @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E2F8 0000000076E23AE7 [000007F9D5282E1A] > @Epilogue 00000000748E6731 0 easy RE+ attempt SHIMVIEW: ShimInfo(Complete) @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C8C000 2000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C8E000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C8F000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C90000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 00000000012011F8 1d0 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3B8 0000000076E2B46D [000007F9D52830EA] > @Epilogue 00000000012011F8 1d0 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C91000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C92000 1000 attempt to write @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E2F8 0000000076E23E97 [000007F9D5282E1A] > @Epilogue 0000000001199B18 0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E2F8 0000000076E23AE7 [000007F9D5282E1A] > @Epilogue 0000000001199B18 0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E2F8 0000000076E23BDB [000007F9D5282E1A] > @Epilogue 0000000001199B18 0 easy RE+ attempt @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000002340000 100 attempt to write rdmsrstack : 0000000012345678 rdmsrstack : 000000000119B44C rdmsrstack : 0000000000000003 rdmsrstack : 000000000119B441 rdmsrstack : 00000000BADF00D0 @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 > @Epilogue 0000000000C93000 2000 attempt to write rdmsrstack : 0000000000000000 @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E2F8 0000000076E23E97 [000007F9D5282E1A] rdmsrstack : 000000000119B45B > @Epilogue 00000000011994A1 0 easy RE+ attempt rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 ZwRaiseException 000000007490288D rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B443 rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B45B rdmsrstack : 00000000BADF00D0 rdmsrstack : 000000000119B45D rdmsrstack : 0000000000000000 rdmsrstack : 000000000119B450 rdmsrstack : 00000000BADF00D0 ********** >> in filter. @VirtualMemoryCallback 00000000000009E0 00000000000009E0 [thread : FFFFFA8007CD0700] > I. @Prologue 000000000070E3A8 0000000076E2031D [000007F9D5282D6A] > @Epilogue 0000000000C95000 1000 attempt to write ZwRaiseException 0000000000C95FF8 ********** >> ********** caught AV as expected.The context is partially valid. Only x86 user-mode context is available. WOW64 breakpoint - code 4000001f (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. 00000000`74959bfc cc int 3 32.kd:x86> g The context is partially valid. Only x86 user-mode context is available. The context is partially valid. Only x86 user-mode context is available. ZwRaiseException 0000000074959BFA ********** << finishThe context is partially valid. Only x86 user-mode context is available. WOW64 breakpoint - code 4000001f (first chance) First chance exceptions are reported before any exception handling. This exception may be expected and handled. 00000000`74959bfc cc int 3 32.kd:x86> g The context is partially valid. Only x86 user-mode context is available. The context is partially valid. Only x86 user-mode context is available. @ThreadNotifyRoutine 9e0 FFFFFA80087B2940 exit @ProcessNotifyRoutineEx 9e0 FFFFFA80087B2940 exit |
.. so first step is done! -> PoC of monitor.
Next part will be implementing callbacks and introduce communication with concept of python based fuzzer to demonstrate control over fuzzed process.
And also simplier sollution exist on older windows – f.e. winxp. In current using of VMM it is easy to omit VMM :
I. fast call can be replaced (also speed up!) by using callgates -> “call far fword ” (http://www.zer0mem.sk/?p=34 – ring3-ring0 callgate)
II. no patchguard, so IDT hooks and SYSCALL hook can leave unprotected
—–
but with VMM we have total control above system, and more over ring3 framework can be some kind of model to ring0 dbi fuzz framework which is even more promissing