Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Abstract
Two days ago I discussed with @TheWover and @FuzzySecurity about contributing to this project, and they both mentioned that D/Invoke wasn't supporting Windows on Windows x64 (hereby as "WOW64"). I therefore made a custom stub to implement this feature.
WOW64 is a feature that was introduced with Windows XP and that allow the execution of x86 applications on x64 systems. This is done via three main DLLs (hereby as "module"):
wow64.dll
for non-GUI-related API calls translation;wow64win.dll
for GUI-related API translation; andwow64cpu.dll
for x86 emulation.I don't know the inner mechanisms and how all of that work; however I know that there is an insane number of things that happen for each system calls and re-implementing everything would be a huge piece of work.
Implementation
A normal WOW64 stub from NTDLL is as follows:
As shown, there is no system call executed from here, instead the stub call
ntdll!Wow64SystemServiceCall
. The problem is that this is a private (i.e. not exported) function and I still did not find a reliable way to retrieve the address of the function.But this is not a big deal because this is only pointing to a
JMP
instruction to an exported function:Wow64Transition
. This function is responsible for switching the segment register (i.e. CPU long mode) to transition from x86 to x64 and therefore to thewhNt*
functions.The address of the
Wow64Transition
exported function can be easily found by usingGetExportAddress()
fromSharpSploit.Execution.DynamicInvoke
class. With the address, a custom stub can be created:If you are executig
NtAllocateVirtualMemory
, this should look like that:Final Notes
Target Framework: .NET Framework 4.7.1
Build and Run: Dubug, Release, Release with optimisation
Test Program: https://gist.github.com/am0nsec/dc81efa478b7d6c8d948177bcb5a276d