CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2014
    Posts
    61

    Question IAT Hooking without success. Some help me please?

    Hi,

    I'm making a dll file for inject in a specific remote process and then make hook IAT inside this remote process in TerminateProcess function.

    Have this code below, dll is injected with success in a simple executable portable, but i'm not receiving any message for success or fails.

    PS: Code below was compiled Dev C++ and dll injection made using PH ( Process Hacker ).

    Where is wrong?

    thanks in advance for any suggestion.

    main.cpp
    Code:
    #include "dll.h"
    #include <windows.h>
    
    BOOL WINAPI MyTerminateProcess(HANDLE hProcess,UINT uExitCode)
    {
         SetLastError(5);
         return FALSE;
    }
    
    typedef BOOL (WINAPI *TERMINATEPROCESS_PROC)(HANDLE, UINT); 
    
    TERMINATEPROCESS_PROC HookFunction(char *UserDll,TERMINATEPROCESS_PROC pfn,TERMINATEPROCESS_PROC HookFunc) 
    
    {
        DWORD dwSizeofExportTable=0;
        DWORD dwRelativeVirtualAddress=0;
        HMODULE hm=GetModuleHandle(NULL);
        PIMAGE_DOS_HEADER pim=(PIMAGE_DOS_HEADER)hm;
        PIMAGE_NT_HEADERS pimnt=(PIMAGE_NT_HEADERS)((DWORD)pim + 
    (DWORD)pim->e_lfanew); 
        PIMAGE_DATA_DIRECTORY 
    pimdata=(PIMAGE_DATA_DIRECTORY)&(pimnt->OptionalHeader.DataDirectory);
    
        PIMAGE_OPTIONAL_HEADER pot=&(pimnt->OptionalHeader);
        PIMAGE_DATA_DIRECTORY 
    pim2=(PIMAGE_DATA_DIRECTORY)((DWORD)pot+(DWORD)104);
        dwSizeofExportTable=pim2->Size;
        dwRelativeVirtualAddress=pim2->VirtualAddress;
        char *ascstr;
        PIMAGE_IMPORT_DESCRIPTOR 
    pimexp=(PIMAGE_IMPORT_DESCRIPTOR)(pim2->VirtualAddress + (DWORD)pim);
        while(pimexp->Name)
        {
            ascstr=(char *)((DWORD)pim + (DWORD)pimexp->Name);
            if(_strcmpi(ascstr,UserDll) == 0)
            {
                break;
            }
            pimexp++;
        }
        PIMAGE_THUNK_DATA 
    pname=(PIMAGE_THUNK_DATA)((DWORD)pim+(DWORD)pimexp->FirstThunk);
        LPDWORD lpdw=&(pname->u1.Function);
        DWORD dwError=0;
        DWORD OldProtect=0;
        while(pname->u1.Function)
        {
            if((DWORD)pname->u1.Function == (DWORD)pfn)
            {
                lpdw=&(pname->u1.Function);
    
    VirtualProtect((LPVOID)lpdw,sizeof(DWORD),PAGE_READWRITE,&OldProtect);
    
    
                pname->u1.Function=(DWORD)HookFunc;
    
    VirtualProtect((LPVOID)lpdw,sizeof(DWORD),PAGE_READONLY,&OldProtect);
    
                return pfn;
            }
            pname++;
    
        }
        return (TERMINATEPROCESS_PROC)0;
    }
    
    TERMINATEPROCESS_PROC CallHook(void) 
    {
        TERMINATEPROCESS_PROC AddOfTerminateProcess;
    
        HMODULE hm=GetModuleHandle(TEXT("Kernel32.dll"));
        TERMINATEPROCESS_PROC fp=(TERMINATEPROCESS_PROC)GetProcAddress(hm,"TerminateProcess");
        
         AddOfTerminateProcess= HookFunction("Kernel32.dll",fp,MyTerminateProcess);
        
    	 if(AddOfTerminateProcess == 0)
        {
            MessageBox(NULL,TEXT("Unable TO Hook Function."),TEXT("Parth"),MB_OK);
        }
        else
        {
            MessageBox(NULL,TEXT("Success Hooked."),TEXT("Parth"),MB_OK);
        }
        return 0;
    }
    
    BOOL APIENTRY DllMain (HINSTANCE hInst,
                           DWORD reason,
                           LPVOID reserved)
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
               CallHook();
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        return TRUE;
    }
    dll.h
    Code:
    #ifndef _DLL_H_
    #define _DLL_H_
    
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else 
    # define DLLIMPORT __declspec (dllimport)
    #endif 
    
    
    class DLLIMPORT DllClass
    {
      public:
        DllClass();
        virtual ~DllClass(void);
    
      private:
    
    };
    
    
    #endif

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IAT Hooking without success. Some help me please?

    but i'm not receiving any message for success or fails.
    Try writing to a file rather than using MesageBox(). If you are injecting into a process, for MessageBox() to have a chance of working the process needs to be configured to allow interaction with the desktop.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2014
    Posts
    61

    Re: IAT Hooking without success. Some help me please?

    @2Kaud, This code above is correct? mainly this part? =>

    Code:
    AddOfTerminateProcess= HookFunction("Kernel32.dll",fp,MyTerminateProcess); // REFERENCE TO MyTerminateProcess in HookFunction() call.
    And also my APIENTRY DllMain not is recognized on DLL_PROCESS_ATTACH. Why? i think that the first error is this.
    Last edited by FL4SHC0D3R; December 14th, 2016 at 07:48 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IAT Hooking without success. Some help me please?

    dll injection made using PH ( Process Hacker )
    How do you know dll was successfully injected into the .exe?

    my APIENTRY DllMain not is recognized on DLL_PROCESS_ATTACH
    How do you know? Is DLL_THEAD_ATTACH called?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2014
    Posts
    61

    Re: IAT Hooking without success. Some help me please?

    Quote Originally Posted by 2kaud View Post
    How do you know dll was successfully injected into the .exe?



    How do you know? Is DLL_THEAD_ATTACH called?

    How do you know dll was successfully injected into the .exe?
    Name:  9a735a8a94454949bbfd1cc8f9d2a332.jpg
Views: 232
Size:  46.0 KB

    My trouble is only because code inside DLL_PROCESS_ATTACH not is executed

    How do you know? Is DLL_THEAD_ATTACH called?
    To say true, i'm don't know, only know that code of DLL_THEAD_ATTACH not is executed.

    Here is my example in attachment.
    Last edited by FL4SHC0D3R; December 14th, 2016 at 10:36 AM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IAT Hooking without success. Some help me please?

    Is DLLMain() being called at all for any reason? If not, then the issue is with the hooking which is PH.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2014
    Posts
    61

    Re: IAT Hooking without success. Some help me please?

    Quote Originally Posted by 2kaud View Post
    Is DLLMain() being called at all for any reason? If not, then the issue is with the hooking which is PH.
    Eg:

    This code below don't works on my example attached above.

    Code:
    DWORD WINAPI MyFunction1(LPVOID pData) 
    { 
    
        std::ofstream out("output.txt");
        out << "Testing...";
        out.close();
        return 0;
        
    } 
    
    HANDLE hThread;         
    DWORD nThread; 
    
    ...
    
    case DLL_PROCESS_ATTACH:       
          
          if((hThread = CreateThread(NULL, 0, MyFunction1, NULL, 0, &nThread)) != NULL) 
          { 
             CloseHandle(hThread); 
          }

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IAT Hooking without success. Some help me please?

    You shouldn't call CreateThread() from within DllMain(). See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx. This also provides a details of others which shouldn't be used within DllMain().

    Why not first try
    Code:
    BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
    {
       std::ofstream out("output.txt");
       out << "Testing...";
       out.close();
       return TRUE;
    }
    as file operations are allowed within DllMain().

    If this doesn't work then the issue is with PH injecting the dll.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2014
    Posts
    61

    Re: IAT Hooking without success. Some help me please?

    @2kaud,

    i'm have compiled with VC++ 2008 and now it's okay about Dll Entry Point, my problem now is because is failing the hook :-(

    "Unable TO Hook Function."

    Some suggestion?

    My actual code below:

    All this code was adapted from here

    Code:
    // dllmain.cpp : Defines the entry point for the DLL application.
    #include "stdafx.h"
    #include <windows.h>
    #include <fstream>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    BOOL WINAPI MyTerminateProcess(HANDLE hProcess,UINT uExitCode)
    {
         SetLastError(5);
         return FALSE;
    }
    
    typedef BOOL (WINAPI *TERMINATEPROCESS_PROC)(HANDLE, UINT); 
    
    TERMINATEPROCESS_PROC HookFunction(char *UserDll,TERMINATEPROCESS_PROC pfn,TERMINATEPROCESS_PROC HookFunc) 
    
    {
                            
        DWORD dwSizeofExportTable=0;
        DWORD dwRelativeVirtualAddress=0;
        HMODULE hm=GetModuleHandle(NULL);
        PIMAGE_DOS_HEADER pim=(PIMAGE_DOS_HEADER)hm;
        PIMAGE_NT_HEADERS pimnt=(PIMAGE_NT_HEADERS)((DWORD)pim + 
    (DWORD)pim->e_lfanew); 
        PIMAGE_DATA_DIRECTORY 
    pimdata=(PIMAGE_DATA_DIRECTORY)&(pimnt->OptionalHeader.DataDirectory);
    
        PIMAGE_OPTIONAL_HEADER pot=&(pimnt->OptionalHeader);
        PIMAGE_DATA_DIRECTORY 
    pim2=(PIMAGE_DATA_DIRECTORY)((DWORD)pot+(DWORD)104);
        dwSizeofExportTable=pim2->Size;
        dwRelativeVirtualAddress=pim2->VirtualAddress;
        char *ascstr;
        PIMAGE_IMPORT_DESCRIPTOR 
    pimexp=(PIMAGE_IMPORT_DESCRIPTOR)(pim2->VirtualAddress + (DWORD)pim);
        while(pimexp->Name)
        {
            ascstr=(char *)((DWORD)pim + (DWORD)pimexp->Name);
            if(_strcmpi(ascstr,UserDll) == 0)
            {
                break;
            }
            pimexp++;
        }
        PIMAGE_THUNK_DATA 
    pname=(PIMAGE_THUNK_DATA)((DWORD)pim+(DWORD)pimexp->FirstThunk);
        LPDWORD lpdw=&(pname->u1.Function);
        DWORD dwError=0;
        DWORD OldProtect=0;
        while(pname->u1.Function)
        {
            if((DWORD)pname->u1.Function == (DWORD)pfn)
            {
                lpdw=&(pname->u1.Function);
    
    VirtualProtect((LPVOID)lpdw,sizeof(DWORD),PAGE_READWRITE,&OldProtect);
    
    
                pname->u1.Function=(DWORD)HookFunc;
    
    VirtualProtect((LPVOID)lpdw,sizeof(DWORD),PAGE_READONLY,&OldProtect);
    
                return pfn;
            }
            pname++;
    
        }
        return (TERMINATEPROCESS_PROC)0;
    }
    
    TERMINATEPROCESS_PROC CallHook(void) 
    {
        TERMINATEPROCESS_PROC AddOfTerminateProcess;
    
        HMODULE hm=GetModuleHandle(TEXT("Kernel32.dll"));
        TERMINATEPROCESS_PROC fp=(TERMINATEPROCESS_PROC)GetProcAddress(hm,"TerminateProcess");
        
         AddOfTerminateProcess= HookFunction("Kernel32.dll",fp,&MyTerminateProcess);
        
    	 if(AddOfTerminateProcess == 0)
        {
            MessageBox(NULL,TEXT("Unable TO Hook Function."),TEXT("Parth"),MB_OK);
        }
        else
        {
            MessageBox(NULL,TEXT("Success Hooked."),TEXT("Parth"),MB_OK);
        }
        return 0;
    }
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
    					 )
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    	     CallHook();
    	   break;
    	case DLL_THREAD_ATTACH:
    	case DLL_THREAD_DETACH:
    	case DLL_PROCESS_DETACH:
    		break;
    	}
    	return TRUE;
    }
    Last edited by FL4SHC0D3R; December 14th, 2016 at 05:23 PM.

  10. #10
    Join Date
    Apr 2014
    Posts
    61

    Re: IAT Hooking without success. Some help me please?

    I updated my code on last answer. See.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: IAT Hooking without success. Some help me please?

    Quote Originally Posted by FL4SHC0D3R View Post
    I updated my code on last answer. See.
    But you are still calling MessageBox (indiretly) from within DllMain (case DLL_PROCESS_ATTACH).
    As 2kaud already mentioned in his post#2 you should not do it!
    Try some type of logging instead!
    Victor Nijegorodov

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: IAT Hooking without success. Some help me please?

    All this code was adapted from here
    Yes, but that question was started because the posted code didn't work!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured