0

プログラム内でLdrLoadDll api が呼び出されたときに、迂回してアンチ Dll インジェクションを作成するために、JMP 命令テクニックを使用しています。完全に動作する Delphi コードを見つけましたが、このコードのこの VC++ 2013 バージョンは、

「アクセス違反書き込み場所 0x0000000000」

以下に示します。

では、どうすればこの問題を解決できますか? 誰かが私を助けてくれますか?

前もって感謝します。

Delphi のバージョン:

procedure hook(target, newfunc:pointer); 
var 
  jmpto:dword; 
    OldProtect: Cardinal; // old protect in memory 
begin 
  jmpto:=dword(newfunc)-dword(target)-5; 
  VirtualProtect(target, 5, PAGE_EXECUTE_READWRITE, @OldProtect); 
  pbyte(target)^:=$e9; 
  pdword(dword(target)+1)^:=jmpto; 
end; 

procedure myLdrLoadDll(PathToFile:PAnsiChar; Flags:variant; ModuleFileName:PAnsiChar; var ModuleHandle:THandle); 
begin 
  MessageBox(0, 'I have blocked your attempt to inject a dll file!!', 'WARNING!', MB_OK); 
  ModuleHandle:=0; 
end; 

procedure Main; 
begin 
Hook(GetProcAddress(GetModuleHandle('ntdll.dll'), 'LdrLoadDll'), @myLdrLoadDll); 
end; 

begin 
end.

VC++ 2013 バージョンの翻訳を試みています:

 BOOL TrampolineAPI(HMODULE hModule, LPCWSTR DllName, LPCSTR ProcName, DWORD dwReplaced)
    {
        DWORD dwReturn;
        DWORD dwOldProtect;
        DWORD dwAddressToHook = (DWORD)GetProcAddress(GetModuleHandle(DllName), ProcName);
        BYTE *pbTargetCode = (BYTE *)dwAddressToHook;
        BYTE *pbReplaced = (BYTE *)dwReplaced;
        VirtualProtect((LPVOID)dwAddressToHook, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect);
        *pbTargetCode++ = 0xE9;   // My trouble is here
        *((signed int*)(pbTargetCode)) = pbReplaced - (pbTargetCode + 4);
        VirtualProtect((LPVOID)dwAddressToHook, 5, PAGE_EXECUTE, &dwOldProtect);
        dwReturn = dwAddressToHook + 5;
        FlushInstructionCache(GetCurrentProcess(), NULL, NULL);
        return TRUE;
    }

    void WINAPI Replaced(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
    {
        printf("Invasion!!");
    }



    int _tmain(int argc, _TCHAR* argv[])
    {

        while (true)
        {
 TrampolineAPI(GetModuleHandle(0), (LPCWSTR)"ntdll.DLL","LdrLoadDLL"(DWORD)Replaced);


        }


        return 0;
    }
4

1 に答える 1