Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-在没有导出函数的情况下从外部进程调用函数_C_Windows_Winapi_External Process_Detours - Fatal编程技术网

C-在没有导出函数的情况下从外部进程调用函数

C-在没有导出函数的情况下从外部进程调用函数,c,windows,winapi,external-process,detours,C,Windows,Winapi,External Process,Detours,我试图弄清楚如何在不导出函数的情况下调用函数 好的,我有一个定义了“add”的exe文件,这个exe是一个win32控制台应用程序,加载一个DLL。DLL还旨在使用exe文件中的add函数(无导出) 以下是我的主要win32 console应用程序文件: #include <windows.h> #include <stdio.h> #pragma auto_inline ( off ) int add ( int a, int b ) { printf( &

我试图弄清楚如何在不导出函数的情况下调用函数

好的,我有一个定义了“add”的exe文件,这个exe是一个win32控制台应用程序,加载一个DLL。DLL还旨在使用exe文件中的add函数(无导出)

以下是我的主要win32 console应用程序文件:

#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;
}
问题是当我调用LoadLibrary时,它返回998,我认为这是错误代码访问冲突。我想这是有道理的,因为内存区域可能受到保护

有什么建议吗


(另外,我使用的反汇编程序是Ida Pro免费版,迂回库由Microsoft提供。)

模块在加载时会重新定位。您应该找到加载模块的基址,然后自己重新定位该地址。此外,您还可以使用[DebugHelp][]库按符号名检索函数地址,而不是对其进行硬编码。

我对迂回不太了解,也不确定它是否可以用这种方式使用。但是您确定add()的地址在运行时实际上是0x401000吗?模块不能重新定位吗?对,我也不太确定地址,因为我对这类东西不熟悉。我会继续努力,并报告我的发现。你给自己带来了很多麻烦和悲伤。从实际的角度来看,您有两种选择:要么导出函数,要么让可执行文件传递指向需要调用的函数的指针。+1我想可以使用DebugHelp。但是,您必须使用调试信息编译模块。我发现,将原始的hexcode值作为函数指针进行反汇编可以工作,但对于win32控制台应用程序则不行。Windows DEP会阻止它。所以int(add)(inta,intb)=(int()(int,int))0x2329381;//一些值如果它仅仅来自主可执行文件,那么这似乎可以正常工作,但是如果它来自主可执行文件使用的外部模块,我认为这可以工作:hmodulehandle=GetModuleHandleA(“另一个.dll”);int(add)(inta,intb)=(int()(int,int))((DWORD)句柄+(DWORD)0x2329381);
#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;
}
.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