2017-09-01 1 views
0

Impossible de comprendre comment appeler une fonction dans un fichier dll géré à l'aide de la DLL non managée.appelant une DLL dll à partir d'un fichier dll non géré (qui est injecté dans le processus en cours d'exécution)

Actuellement, j'ai été capable d'injecter une DLL non managée dans un processus en cours et d'appeler une DLL gérée (et surtout je suis un débutant en C++) en utilisant cela comme indiqué ci-dessous.

#include "stdafx.h" 
#include <Windows.h> 
#include "dllmain.h" 

BOOL APIENTRY DllMain(HMODULE hModule, 
        DWORD ul_reason_for_call, 
        LPVOID lpReserved ) 
{ 
switch (ul_reason_for_call) 
    { 
    case DLL_PROCESS_ATTACH: 
    { 
    LoadManagedProject(L"C:\\Users\\nagaganesh.kurcheti\\Desktop\\ExampleProject.dll"); 
    DisplayPid(); 
    break; 
    } 
    case DLL_THREAD_ATTACH: 
    case DLL_THREAD_DETACH: 
    case DLL_PROCESS_DETACH: 
    break; 
} 
return TRUE; 
} 

void DisplayPid() 
{ 
DWORD pid = GetCurrentProcessId(); 
wchar_t buf[64]; 
wsprintf(buf, L"Hey, it worked! Pid is %d", pid); 
MessageBox(NULL, buf, L"Injected NEW MessageBox", NULL); 
} 

ET DE MAIN DLL JE SUIS appel d'une fonction POIGNEES LE PROCESSUS D'INJECTION QUI RESSEMBLE: -

DllExport void LoadManagedProject(const wchar_t * managedDllLocation) 
{ 
HRESULT hr; 
ICLRMetaHost* pClrMetaHost = NULL; 
ICLRRuntimeInfo* pClrRuntimeInfo = NULL; 
ICLRRuntimeHost* pClrRuntimeHost = NULL; 
hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pClrMetaHost); 
if (hr == S_OK) 
{ 


    hr = pClrMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pClrRuntimeInfo)); 
    if (hr == S_OK) 
    {   
     BOOL fLoadable; 
     hr = pClrRuntimeInfo->IsLoadable(&fLoadable); 
     if ((hr == S_OK) && fLoadable) 
     {     
      hr = pClrRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, 
       IID_PPV_ARGS(&pClrRuntimeHost)); 
      if (hr == S_OK) 
      { 
       hr = pClrRuntimeHost->Start(); 
       if (hr == S_OK) 
       { 
        MessageBox(NULL, L"HR=SOK45STTIME", L"Injected MessageBox", NULL); 

        DWORD result; 
        hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(
         managedDllLocation, 
         L"ExampleProject.Example", 
         L"EntryPoint", 
         L"Argument", 
         &result); 
        if (hr == S_OK) 
        { 
         MessageBox(NULL, L"HR=SOK6STTIME", L"Injected MessageBox", NULL); 
        } 

       } 
      } 
     } 
    } 
} 
} 

je n'étais pas en mesure d'injecter ce processus après plusieurs essais. Puis-je obtenir quelle erreur j'ai faite ou suggérer une meilleure approche d'appeler un DLL géré (C#) en utilisant dll non géré qui est injecté dans un processus en cours d'exécution. Merci d'avance.

MISE À JOUR:

S'il est impossible de cette façon, pourriez-vous suggérer une meilleure approche de l'injection dll gérée dans un processus en cours d'exécution. Merci

+0

Google « DllMain chargeur verrouillage » à Découvrez pourquoi cela ne peut jamais fonctionner. –

+0

Pourriez-vous élaborer s'il vous plaît. Je suis un débutant à cette situation.Thankyou –

Répondre

1

Vous pouvez y parvenir en injectant dll géré dans le processus non géré à l'aide EasyHook ici est un exemple de code:

#include <easyhook.h> 
#include <string> 
#include <iostream> 
#include <Windows.h> 

DWORD gFreqOffset = 0; 
BOOL WINAPI myBeepHook(DWORD dwFreq, DWORD dwDuration) 
{ 
    std::cout << "\n BeepHook: ****All your beeps belong to us!\n\n"; 
    return Beep(dwFreq + gFreqOffset, dwDuration); 
} 

// EasyHook will be looking for this export to support DLL injection. If not found then 
// DLL injection will fail. 
extern "C" void __declspec(dllexport) __stdcall NativeInjectionEntryPoint(REMOTE_ENTRY_INFO* inRemoteInfo); 

void __stdcall NativeInjectionEntryPoint(REMOTE_ENTRY_INFO* inRemoteInfo) 
{ 
    std::cout << "\n\nNativeInjectionEntryPointt(REMOTE_ENTRY_INFO* inRemoteInfo)\n\n" << 
     "IIIII   jjj    tt    dd !!! \n" 
     " III nn nnn   eee cccc tt  eee  dd !!! \n" 
     " III nnn nn jjj ee e cc  tttt ee e dddddd !!! \n" 
     " III nn nn jjj eeeee cc  tt eeeee dd dd  \n" 
     "IIIII nn nn jjj eeeee ccccc tttt eeeee dddddd !!! \n" 
     "    jjjj           \n\n"; 

    std::cout << "Injected by process Id: " << inRemoteInfo->HostPID << "\n"; 
    std::cout << "Passed in data size: " << inRemoteInfo->UserDataSize << "\n"; 
    if (inRemoteInfo->UserDataSize == sizeof(DWORD)) 
    { 
     gFreqOffset = *reinterpret_cast<DWORD *>(inRemoteInfo->UserData); 
     std::cout << "Adjusting Beep frequency by: " << gFreqOffset << "\n"; 
    } 

    // Perform hooking 
    HOOK_TRACE_INFO hHook = { NULL }; // keep track of our hook 

    std::cout << "\n"; 
    std::cout << "Win32 Beep found at address: " << GetProcAddress(GetModuleHandle(TEXT("kernel32")), "Beep") << "\n"; 

    // Install the hook 
    NTSTATUS result = LhInstallHook(
     GetProcAddress(GetModuleHandle(TEXT("kernel32")), "Beep"), 
     myBeepHook, 
     NULL, 
     &hHook); 
    if (FAILED(result)) 
    { 
     std::wstring s(RtlGetLastErrorString()); 
     std::wcout << "Failed to install hook: "; 
     std::wcout << s; 
    } 
    else 
    { 
     std::cout << "Hook 'myBeepHook installed successfully."; 
    } 

    // If the threadId in the ACL is set to 0, 
    // then internally EasyHook uses GetCurrentThreadId() 
    ULONG ACLEntries[1] = { 0 }; 

    // Disable the hook for the provided threadIds, enable for all others 
    LhSetExclusiveACL(ACLEntries, 1, &hHook); 

    return; 
} 

Vous pouvez trouver plus de détails à original source