C#挂钩vmtable

时间:2016-06-20 04:18:05

标签: c# c++ hook native vtable

作为了解更多关于c#本机端的侧面项目,我想在虚拟方法表中挂钩虚拟方法。

我成功地可以调用该函数,但是当调用该函数时,将指向虚方法的指针更改为我自己的方法会崩溃。

我为这个学习目的制作了一个小的c ++应用程序,这就是它



#include <iostream>
#include <conio.h>
using namespace std;

class Class
{
	public:
	virtual void Function ( ) = 0;
	virtual void Function2 ( ) = 0;
	virtual void Function3 ( ) = 0;
};


class ClassI : Class
{
	public:
	void Function ( )
	{
		cout << "Function1" << endl;
	}

	void Function2 ( )
	{
		cout << "Function2" << endl;
	}

	void Function3 ( )
	{
		cout << "Function3" << endl;
	}


};



int main ( )
{
	ClassI* a = new ClassI ( );
	int aaaa = 10;

	int* aaa = &aaaa;

	cout << "AddressOfClass: " << &a << endl;
	getch ( );

	a->Function ( );

	getch ( );
	delete a;
	return 0;
}
&#13;
&#13;
&#13;

我得到一个Class指针的实例并输出它并等待输入。

输入后我运行我要挂钩的功能。

现在在我的c#方面,我创建了一个dll,并且dll正在通过clr注入进入内存空间。

我通过多种方式验证了注射效果。

&#13;
&#13;
 public class EntryPoint
    {
        #region Delegates
        private delegate void orgFunction();
        private static orgFunction oFunction;
        #endregion




        public static void Hooked()
        {
            Console.WriteLine("HookedFunction");
            oFunction();
        }



        [DllExport("DllMain",CallingConvention.Cdecl)]
        public static void DllMain()
        {

            unsafe
            {
                Delegate Hook = new Action(Hooked);
                IntPtr* vtable = (IntPtr*)*(IntPtr*)0x00F3FE10;
                oFunction = Marshal.GetDelegateForFunctionPointer<orgFunction>(*(IntPtr*)vtable[0]);
                uint OldProtection;
                MUtil.MEMORY_BASIC_INFORMATION mbi;
                MUtil.VirtualQuery((IntPtr)vtable, out mbi, (IntPtr)sizeof(MUtil.MEMORY_BASIC_INFORMATION));
                MUtil.VirtualProtect(mbi.BaseAddress, (uint)mbi.RegionSize, 0x04, out OldProtection);
                vtable[0] = Marshal.GetFunctionPointerForDelegate(Hook);
                MUtil.VirtualProtect(mbi.BaseAddress, (uint)mbi.RegionSize, OldProtection, out OldProtection);
            }
        }
    }
&#13;
&#13;
&#13;

这就是我在c#侧做的事情。

获得oFunction后,为了测试我确实调用了它并删除了其他代码并且它有效。

但挂钩它一切都很好但是在c ++程序中我发送输入并运行我们挂钩的功能,应用程序崩溃。

每次运行c ++程序时,我都会更新地址。

&#13;
&#13;
        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_BASIC_INFORMATION
        {
            public IntPtr BaseAddress;
            public IntPtr AllocationBase;
            public uint AllocationProtect;
            public IntPtr RegionSize;
            public uint State;
            public uint Protect;
            public uint Type;
        }

        [DllImport("kernel32.dll")]
        public static extern UIntPtr VirtualQuery(IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, IntPtr dwLength);


        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool VirtualProtect(IntPtr address, uint size, uint newProtect, out uint oldProtect);
&#13;
&#13;
&#13;

编辑:

我决定做一个简单的小测试,我将oFunction改为vmtable [2]

oFunction = Marshal.GetDelegateForFunctionPointer<orgFunction>(*(IntPtr*) vtable[1]  );

上面的代码让我崩溃了,而且我很乖,这让我想到获取ClassI实例的地址可能会给出第一个函数的地址而不是vmtable,我认为不是这样的

-

感谢您阅读本文,祝您日夜愉快。

最好的问候。

1 个答案:

答案 0 :(得分:0)

要做一个vmtable钩子,要做的就是用一个新的vmtable钩子覆盖vtable内的函数指针,要做的就是只使用VirtualProtect()来获得写权限,然后使用WriteProcessMemory()来覆盖它。 / p>

您需要手动反转vtable并使用WriteProcessMemory()手动覆盖它。

  1. 获取vtable的地址
  2. 添加vTableIndex * 4或vTableIndex * 8(取决于x86和x64)
  3. 使用WriteProcessMemory覆盖此地址