BlitzPointer: Testing new MACRO based function implementation.

This commit is contained in:
Michael Fabian Dirks
2015-05-14 00:42:38 +02:00
parent d69e0aba0b
commit 57cb1642c4
3 changed files with 126 additions and 42 deletions
+49 -29
View File
@@ -3,32 +3,31 @@
#include "BlitzPointer.h" #include "BlitzPointer.h"
DLL_EXPORT uint32_t BlitzPointer_GetReturnAddress() { DLL_EXPORT int32_t BlitzPointer_GetReturnAddress() {
unsigned int StackPointer, ReturnAddress; int32_t StackPointer, ReturnAddress;
__asm { //ASM. Do touch if suicidal. __asm { //ASM. Do touch if suicidal.
mov StackPointer, esp // Store current Stack Pointer mov StackPointer, esp; // Store current Stack Pointer
mov esp, ebp // On X86, EBP[0] is our own function and EBP[1] is the return address. mov esp, ebp; // On X86, EBP[0] is our own function and EBP[1] is the return address.
add esp, 4 // Which means that we can just take it from there into our own variable. add esp, 4; // Which means that we can just take it from there into our own variable.
pop ReturnAddress // Just like this. pop ReturnAddress; // Just like this.
mov esp, [StackPointer] // And then reset the Stack Pointer. mov esp, [StackPointer]; // And then reset the Stack Pointer.
} }
return ReturnAddress; return ReturnAddress;
} }
DLL_EXPORT int32_t BlitzPointer_GetFunctionPointer() {
DLL_EXPORT uint32_t BlitzPointer_GetFunctionPointer() { int32_t StackPointer, ReturnAddress;
unsigned int StackPointer, ReturnAddress;
__asm { //ASM. Do touch if suicidal. __asm { //ASM. Do touch if suicidal.
mov StackPointer, esp // Store current Stack Pointer mov StackPointer, esp; // Store current Stack Pointer
mov esp, ebp // On X86, EBP[0] is our own function and EBP[1] is the return address. mov esp, ebp; // On X86, EBP[0] is our own function and EBP[1] is the return address.
add esp, 4 // Which means that we can just take it from there into our own variable. add esp, 4; // Which means that we can just take it from there into our own variable.
pop ReturnAddress // Just like this. pop ReturnAddress; // Just like this.
mov esp, [StackPointer] // And then reset the Stack Pointer. mov esp, [StackPointer]; // And then reset the Stack Pointer.
} }
// let's look backwards in memory for the function signature (0x53 0x56 0x57 0x55 0x89 0xE5) for at most one megabyte. // Let's look backwards in memory for the function signature (0x53 0x56 0x57 0x55 0x89 0xE5) for at most one megabyte.
uint8_t* startPtr = (uint8_t*)ReturnAddress; uint8_t* startPtr = (uint8_t*)ReturnAddress;
uint8_t* endPtr = (uint8_t*)(ReturnAddress - 1048576); uint8_t* endPtr = (uint8_t*)(ReturnAddress - 1048576);
for (uint8_t* curPtr = startPtr; curPtr != endPtr; curPtr--) { for (uint8_t* curPtr = startPtr; curPtr != endPtr; curPtr--) {
@@ -37,21 +36,45 @@ DLL_EXPORT uint32_t BlitzPointer_GetFunctionPointer() {
if (*(curPtr + 2) == 0x57) // push edi if (*(curPtr + 2) == 0x57) // push edi
if (*(curPtr + 3) == 0x55) // push ebp if (*(curPtr + 3) == 0x55) // push ebp
if (*(curPtr + 4) == 0x89 && *(curPtr + 5) == 0xE5) // mov ebp,esp if (*(curPtr + 4) == 0x89 && *(curPtr + 5) == 0xE5) // mov ebp,esp
return (uint32_t)curPtr; return (int32_t)curPtr;
} }
return 0; return 0;
} }
__declspec(naked) uint32_t __BlitzPointer_CallFunction() { // Defines for easier function generation.
__asm { #define CALLFUNCTION_DECL_BEGIN(NAME) DLL_EXPORT int32_t BlitzPointer_CallFunction##NAME(intptr_t ipFunctionPointer
jmp eax #define CALLFUNCTION_DECL_PARAMETER(TYPE, NAME) , TYPE NAME
} #define CALLFUNCTION_DECL_END() )
} #define CALLFUNCTION_DECL_IMPLEMENT() ) { int32_t result;
#define CALLFUNCTION_IMPL_SAFEGUARD() if (!ipFunctionPointer) return NULL;
#define CALLFUNCTION_IMPL_PREPARE(COUNT) __asm { sub esp, COUNT * 4; }
#define CALLFUNCTION_IMPL_PARAMETER(INDEX, NAME) __asm { mov eax, [ NAME ]; mov [esp - INDEX * 4], eax; }
#define CALLFUNCTION_IMPL_CALL() __asm { call dword ptr[ipFunctionPointer]; }
#define CALLFUNCTION_IMPL_RETURN() __asm { mov [result], eax }; return result; }
DLL_EXPORT uint32_t BlitzPointer_CallFunction0(uint32_t fpFunctionPointer) { // Call Function with 0 parameter.
CALLFUNCTION_DECL_BEGIN(I0)
CALLFUNCTION_DECL_IMPLEMENT()
CALLFUNCTION_IMPL_SAFEGUARD()
CALLFUNCTION_IMPL_CALL()
CALLFUNCTION_IMPL_RETURN()
// Call Function with 1 parameter.
/*CALLFUNCTION_DECL_BEGIN(I1)
CALLFUNCTION_DECL_PARAMETER(int32_t, p1)
CALLFUNCTION_DECL_IMPLEMENT()
CALLFUNCTION_IMPL_SAFEGUARD()
//CALLFUNCTION_IMPL_PREPARE(1)
//CALLFUNCTION_IMPL_PARAMETER(0, p1)
CALLFUNCTION_IMPL_CALL()
CALLFUNCTION_IMPL_RETURN()*/
/*
DLL_EXPORT uint32_t BlitzPointer_CallFunction0(intptr_t fpFunctionPointer) {
if (!fpFunctionPointer) if (!fpFunctionPointer)
return 0; return NULL;
__asm { __asm {
call dword ptr[fpFunctionPointer]; call dword ptr[fpFunctionPointer];
@@ -62,10 +85,9 @@ DLL_EXPORT uint32_t BlitzPointer_CallFunction0(uint32_t fpFunctionPointer) {
} }
return rv; return rv;
} }
DLL_EXPORT uint32_t BlitzPointer_CallFunction1(uint32_t fpFunctionPointer, uint32_t p1) { DLL_EXPORT uint32_t BlitzPointer_CallFunction1(uint32_t fpFunctionPointer, uint32_t p1) {
if (!fpFunctionPointer) if (!fpFunctionPointer)
return 0; return NULL;
__asm { __asm {
sub esp, 0x4; sub esp, 0x4;
@@ -79,7 +101,6 @@ DLL_EXPORT uint32_t BlitzPointer_CallFunction1(uint32_t fpFunctionPointer, uint3
} }
return rv; return rv;
} }
DLL_EXPORT uint32_t BlitzPointer_CallFunction2(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2) { DLL_EXPORT uint32_t BlitzPointer_CallFunction2(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2) {
if (!fpFunctionPointer) if (!fpFunctionPointer)
return 0; return 0;
@@ -98,7 +119,6 @@ DLL_EXPORT uint32_t BlitzPointer_CallFunction2(uint32_t fpFunctionPointer, uint3
} }
return rv; return rv;
} }
DLL_EXPORT uint32_t BlitzPointer_CallFunction3(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3) { DLL_EXPORT uint32_t BlitzPointer_CallFunction3(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3) {
if (!fpFunctionPointer) if (!fpFunctionPointer)
return 0; return 0;
@@ -119,7 +139,6 @@ DLL_EXPORT uint32_t BlitzPointer_CallFunction3(uint32_t fpFunctionPointer, uint3
} }
return rv; return rv;
} }
DLL_EXPORT uint32_t BlitzPointer_CallFunction4(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) { DLL_EXPORT uint32_t BlitzPointer_CallFunction4(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4) {
if (!fpFunctionPointer) if (!fpFunctionPointer)
return 0; return 0;
@@ -207,3 +226,4 @@ DLL_EXPORT uint32_t BlitzPointer_CallFunctionS4(uint32_t fpFunctionPointer, uint
} }
return *((uint32_t*)(returnvalue + 4)); return *((uint32_t*)(returnvalue + 4));
} }
*/
+2 -13
View File
@@ -1,15 +1,4 @@
#include "dllmain.h" #include "dllmain.h"
DLL_EXPORT uint32_t BlitzPointer_GetReturnAddress(); DLL_EXPORT int32_t BlitzPointer_GetReturnAddress();
DLL_EXPORT uint32_t BlitzPointer_GetFunctionPointer(); DLL_EXPORT int32_t BlitzPointer_GetFunctionPointer();
DLL_EXPORT uint32_t BlitzPointer_CallFunction0(uint32_t fpFunctionPointer);
DLL_EXPORT uint32_t BlitzPointer_CallFunction1(uint32_t fpFunctionPointer, uint32_t p1);
DLL_EXPORT uint32_t BlitzPointer_CallFunction2(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2);
DLL_EXPORT uint32_t BlitzPointer_CallFunction3(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3);
DLL_EXPORT uint32_t BlitzPointer_CallFunction4(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
DLL_EXPORT uint32_t BlitzPointer_CallFunctionS0(uint32_t fpFunctionPointer);
DLL_EXPORT uint32_t BlitzPointer_CallFunctionS1(uint32_t fpFunctionPointer, uint32_t p1);
DLL_EXPORT uint32_t BlitzPointer_CallFunctionS2(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2);
DLL_EXPORT uint32_t BlitzPointer_CallFunctionS3(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3);
DLL_EXPORT uint32_t BlitzPointer_CallFunctionS4(uint32_t fpFunctionPointer, uint32_t p1, uint32_t p2, uint32_t p3, uint32_t p4);
+75
View File
@@ -5,6 +5,14 @@
<Configuration>Debug</Configuration> <Configuration>Debug</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
</ProjectConfiguration> </ProjectConfiguration>
<ProjectConfiguration Include="Preprocess-Preview|Win32">
<Configuration>Preprocess-Preview</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Preprocess|Win32">
<Configuration>Preprocess</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32"> <ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>Win32</Platform> <Platform>Win32</Platform>
@@ -21,6 +29,12 @@
<PlatformToolset>v120</PlatformToolset> <PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
@@ -28,12 +42,18 @@
<WholeProgramOptimization>false</WholeProgramOptimization> <WholeProgramOptimization>false</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Preprocess-Preview|Win32'">
<PlatformToolset>v120</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings"> <ImportGroup Label="ExtensionSettings">
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess|Win32'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
@@ -44,12 +64,21 @@
<TargetExt>.dll</TargetExt> <TargetExt>.dll</TargetExt>
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess|Win32'">
<OutDir>$(SolutionDir)\Build\$(ProjectName)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)\Intermediate\$(ProjectName)\$(Configuration)\</IntDir>
<TargetExt>.dll</TargetExt>
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)\Build\$(ProjectName)\$(Configuration)\</OutDir> <OutDir>$(SolutionDir)\Build\$(ProjectName)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)\Intermediate\$(ProjectName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)\Intermediate\$(ProjectName)\$(Configuration)\</IntDir>
<TargetExt>.dll</TargetExt> <TargetExt>.dll</TargetExt>
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess-Preview|Win32'">
<TargetExt>.dll</TargetExt>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
@@ -73,6 +102,40 @@
<ForcedIncludeFiles> <ForcedIncludeFiles>
</ForcedIncludeFiles> </ForcedIncludeFiles>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<PreprocessToFile>true</PreprocessToFile>
<PreprocessKeepComments>true</PreprocessKeepComments>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<Version>1.0</Version>
<LinkStatus>
</LinkStatus>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>
</SDLCheck>
<AdditionalIncludeDirectories>$(SolutionDir);$(ProjectDir)</AdditionalIncludeDirectories>
<MultiProcessorCompilation>false</MultiProcessorCompilation>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<BufferSecurityCheck>false</BufferSecurityCheck>
<FunctionLevelLinking>true</FunctionLevelLinking>
<CompileAsManaged>false</CompileAsManaged>
<CompileAsWinRT>false</CompileAsWinRT>
<StructMemberAlignment>4Bytes</StructMemberAlignment>
<EnableParallelCodeGeneration>true</EnableParallelCodeGeneration>
<CreateHotpatchableImage>true</CreateHotpatchableImage>
<RuntimeTypeInfo>
</RuntimeTypeInfo>
<OpenMPSupport>false</OpenMPSupport>
<ForcedIncludeFiles>
</ForcedIncludeFiles>
<MinimalRebuild>false</MinimalRebuild>
<PreprocessToFile>false</PreprocessToFile>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
@@ -106,6 +169,7 @@
</ForcedIncludeFiles> </ForcedIncludeFiles>
<BasicRuntimeChecks>Default</BasicRuntimeChecks> <BasicRuntimeChecks>Default</BasicRuntimeChecks>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<PreprocessToFile>false</PreprocessToFile>
</ClCompile> </ClCompile>
<Link> <Link>
<GenerateDebugInformation>false</GenerateDebugInformation> <GenerateDebugInformation>false</GenerateDebugInformation>
@@ -118,6 +182,17 @@
</LinkStatus> </LinkStatus>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess-Preview|Win32'">
<ClCompile>
<PreprocessToFile>true</PreprocessToFile>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Preprocess-Preview|Win32'">
<ClCompile>
<PreprocessKeepComments>true</PreprocessKeepComments>
<PreprocessorDefinitions>_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="BlitzPointer.cpp" /> <ClCompile Include="BlitzPointer.cpp" />
<ClCompile Include="dllmain.cpp" /> <ClCompile Include="dllmain.cpp" />