Click here to Skip to main content
15,891,864 members
Articles / Web Development / HTML

.NET CLR Injection: Modify IL Code during Run-time

Rate me:
Please Sign up or sign in to vote.
4.98/5 (240 votes)
7 Aug 2014LGPL310 min read 598K   18.4K   352  
Modify methods' IL codes on runtime even if they have been JIT-compiled, supports release mode / x64 & x86, and variants of .NET versions, from 2.0 to 4.5.
/*
    EasyHook - The reinvention of Windows API hooking
 
    Copyright (C) 2009 Christoph Husse

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    Please visit http://www.codeplex.com/easyhook for more information
    about the project and latest updates.
*/
#include "stdafx.h"


EASYHOOK_NT_EXPORT LhUninstallHook(TRACED_HOOK_HANDLE InHandle)
{
/*
Description:

    Removes the given hook. To also release associated resources,
    you will have to call LhWaitForPendingRemovals(). In any case
    your hook handler will never be executed again, after calling this
    method.

Parameters:

    - InHandle

        A traced hook handle. If the hook is already removed, this method
        will still return STATUS_SUCCESS.
*/
    LOCAL_HOOK_INFO*        Hook = NULL;
    LOCAL_HOOK_INFO*        List;
    LOCAL_HOOK_INFO*        Prev;
    NTSTATUS                NtStatus;
    BOOLEAN                 IsAllocated = FALSE;

     if(!IsValidPointer(InHandle, sizeof(HOOK_TRACE_INFO)))
        return FALSE;

    RtlAcquireLock(&GlobalHookLock);
    {
        if((InHandle->Link != NULL) && LhIsValidHandle(InHandle, &Hook))
        {
            InHandle->Link = NULL;

            if(Hook->HookProc != NULL)
            {
                Hook->HookProc = NULL;  

                IsAllocated = TRUE;
            }
        }

        if(!IsAllocated)
        {
            RtlReleaseLock(&GlobalHookLock);

            RETURN;
        }

        // remove from global list
        List = GlobalHookListHead.Next;
        Prev = &GlobalHookListHead;

        while(List != NULL)
        {
            if(List == Hook)
            {
                Prev->Next = Hook->Next;

                break;
            }

            List = List->Next;
        }

        // add to removal list
        Hook->Next = GlobalRemovalListHead.Next;
        GlobalRemovalListHead.Next = Hook;
    }
    RtlReleaseLock(&GlobalHookLock);

    RETURN(STATUS_SUCCESS);

THROW_OUTRO:
FINALLY_OUTRO:
    return NtStatus;
}






EASYHOOK_NT_EXPORT LhUninstallAllHooks()
{
/*
Description:

    Will remove ALL hooks. To also release associated resources,
    you will have to call LhWaitForPendingRemovals().
*/
    LOCAL_HOOK_INFO*        Hook;
    LOCAL_HOOK_INFO*        List;
    NTSTATUS                NtStatus;

    RtlAcquireLock(&GlobalHookLock);
    {
        // remove from global list
        List = GlobalHookListHead.Next;

        while(List != NULL)
        {
            Hook = List;
            List = List->Next;

            // remove tracking
            if(LhIsValidHandle(Hook->Tracking, NULL))
            {
                Hook->Tracking->Link = NULL;
            }

            // add to removal list
            Hook->HookProc = NULL;
            Hook->Next = GlobalRemovalListHead.Next;

            GlobalRemovalListHead.Next = Hook;
        }

		GlobalHookListHead.Next = NULL;
    }
    RtlReleaseLock(&GlobalHookLock);

    RETURN(STATUS_SUCCESS);

FINALLY_OUTRO:
    return NtStatus;
}






EASYHOOK_NT_EXPORT LhWaitForPendingRemovals()
{
/*
Descriptions:

    For stability reasons, all resources associated with a hook
    have to be released if no thread is currently executing the
    handler. Separating this wait loop from the uninstallation
    method is a great performance gain, because you can release
    all hooks first, and then wait for all removals simultaneously.

*/
    PLOCAL_HOOK_INFO        Hook;
    NTSTATUS                NtStatus;

    while(TRUE)
    {
        // pop from removal list
        RtlAcquireLock(&GlobalHookLock);
        {
            Hook = GlobalRemovalListHead.Next;
            
            if(Hook == NULL)
            {
                RtlReleaseLock(&GlobalHookLock);

                break;
            }

            GlobalRemovalListHead.Next = Hook->Next;
        }
        RtlReleaseLock(&GlobalHookLock);

        // restore entry point...
        if(Hook->HookCopy == *((ULONGLONG*)Hook->TargetProc))
        {
            *((ULONGLONG*)Hook->TargetProc) = Hook->TargetBackup;

#ifdef X64_DRIVER
			*((ULONGLONG*)(Hook->TargetProc + 8)) = Hook->TargetBackup_x64;
#endif

            // release memory...
            while(*Hook->IsExecutedPtr > 0)
            {
                RtlSleep(100);
            }

            LhFreeMemory(&Hook);
        }
        else
        {
            // hook was changed... no chance to release resources
        }
    }

    RETURN(STATUS_SUCCESS);

FINALLY_OUTRO:
    return NtStatus;
}




void LhCriticalFinalize()
{
/*
Description:

    Will be called in the DLL_PROCESS_DETACH event and just uninstalls
    all hooks. If it is possible also their memory is released. 
*/
    LhUninstallAllHooks();

    LhWaitForPendingRemovals();

	RtlDeleteLock(&GlobalHookLock);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions