関数をエクスポートせずに呼び出す方法を見つけようとしています。
さて、「add」が定義されたexeファイルがあります。このexeはwin32コンソールアプリケーションで、DLLをロードします。DLL は、exe ファイルから追加機能を使用することも目的としています (エクスポートなし)。
これが私のメインの win32 コンソール アプリケーション ファイルです。
#include <windows.h>
#include <stdio.h>
#pragma auto_inline ( off )
int add ( int a, int b )
{
printf( "Adding some ints\n" );
return a + b;
}
int main ( )
{
HMODULE module = NULL;
if ( (module = LoadLibrary( L"hook.dll" )) == NULL )
{
printf( "Could not load library: %ld\n", GetLastError() );
return 0;
}
add( 3, 5 );
FreeLibrary( module );
return 0;
}
hook.dll のコードは次のとおりです。
#include <windows.h>
#include <stdio.h>
#include <detours.h>
static int (*add) ( int a, int b ) = ( int (*)( int a, int b ) ) 0x401000;
int Detoured_add ( int a, int b )
{
return add( a, b );
}
BOOL WINAPI DllMain ( HINSTANCE hDll, DWORD reason, LPVOID reserved )
{
if ( reason == DLL_PROCESS_ATTACH )
{
DetourTransactionBegin();
DetourAttach( (PVOID*) &add, Detoured_add );
DetourTransactionCommit();
}
else if ( reason == DLL_PROCESS_DETACH )
{
DetourTransactionBegin();
DetourDetach( (PVOID*) &add, Detoured_add );
DetourTransactionCommit();
}
return TRUE;
}
add 関数のアドレスを見つけるために、win32 コンソール アプリケーションを逆アセンブルしました。
.text:00401000 ; ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ S U B R O U T I N E ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
.text:00401000
.text:00401000
.text:00401000 sub_401000 proc near ; CODE XREF: sub_401020:loc_40104Bp
.text:00401000 push offset aAddingSomeInts ; "Adding some ints\n"
.text:00401005 call ds:printf
.text:0040100B add esp, 4
.text:0040100E mov eax, 8
.text:00401013 retn
.text:00401013 sub_401000 endp
問題は、LoadLibrary を呼び出すと、エラー コード アクセス違反であると思われる 998 が返されることです。そのメモリ領域はおそらく保護されているので、これは理にかなっていると思います。
任意のヒント?
(また、私が使用した逆アセンブラは Ida Pro フリー版で、迂回ライブラリは Microsoft から提供されています。)