Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/shellcode/windows/x86/src/migrate/executex64.asm
Views: 11791
;-----------------------------------------------------------------------------;1; Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)2; Compatible: Windows 7, 2008, Vista, 2003, XP3; Architecture: wow644; Version: 1.0 (Jan 2010)5; Size: 75 bytes6; Build: >build.py executex647;-----------------------------------------------------------------------------;89; A simple function to execute native x64 code from a wow64 (x86) process.10; Can be called from C using the following prototype:11; typedef DWORD (WINAPI * EXECUTEX64)( X64FUNCTION pFunction, DWORD dwParameter );12; The native x64 function you specify must be in the following form (as well as being x64 code):13; typedef BOOL (WINAPI * X64FUNCTION)( DWORD dwParameter );1415; Clobbers: EAX, ECX and EDX (ala the normal stdcall calling convention)16; Un-Clobbered: EBX, ESI, EDI, ESP and EBP can be expected to remain un-clobbered.1718[BITS 32]1920WOW64_CODE_SEGMENT EQU 0x2321X64_CODE_SEGMENT EQU 0x332223start:24push ebp ; prologue, save EBP...25mov ebp, esp ; and create a new stack frame26push esi ; save the registers we shouldn't clobber27push edi ;28mov esi, [ebp+8] ; ESI = pFunction29mov ecx, [ebp+12] ; ECX = dwParameter30call delta ;31delta:32pop eax ;33add eax, (native_x64-delta) ; get the address of native_x643435sub esp, 8 ; alloc some space on stack for far jump36mov edx, esp ; EDX will be pointer our far jump37mov dword [edx+4], X64_CODE_SEGMENT ; set the native x64 code segment38mov dword [edx], eax ; set the address we want to jump to (native_x64)3940call go_all_native ; perform the transition into native x64 and return here when done.4142mov ax, ds ; fixes an elusive bug on AMD CPUs, http://blog.rewolf.pl/blog/?p=148443mov ss, ax ; found and fixed by ReWolf, incorporated by RaMMicHaeL4445add esp, (8+4+8) ; remove the 8 bytes we allocated + the return address which was never popped off + the qword pushed from native_x6446pop edi ; restore the clobbered registers47pop esi ;48pop ebp ; restore EBP49retn (4*2) ; return to caller (cleaning up our two function params)5051go_all_native:52mov edi, [esp] ; EDI is the wow64 return address53jmp dword far [edx] ; perform the far jump, which will return to the caller of go_all_native5455native_x64:56[BITS 64] ; we are now executing native x64 code...57xor rax, rax ; zero RAX58push rdi ; save RDI (EDI being our wow64 return address)59call rsi ; call our native x64 function (the param for our native x64 function is allready in RCX)60pop rdi ; restore RDI (EDI being our wow64 return address)61push rax ; simply push it to alloc some space62mov dword [rsp+4], WOW64_CODE_SEGMENT ; set the wow64 code segment63mov dword [rsp], edi ; set the address we want to jump to (the return address from the go_all_native call)64jmp dword far [rsp] ; perform the far jump back to the wow64 caller...656667