public void DetourInternalFunction() { using (var hook = LocalHook.Create( LocalHook.GetProcAddress("kernel32.dll", "InternalAddAtom"), new InternalAddAtomDelegate(InternalAddAtomHook), this)) { InternalAddAtomFunction = (InternalAddAtomDelegate) Marshal.GetDelegateForFunctionPointer( hook.HookBypassAddress, typeof(InternalAddAtomDelegate)); hook.ThreadACL.SetInclusiveACL(new int[] { 0 }); _internalAddAtomCalled = false; string atomName = "TestLocalAtomName"; ushort atomId = AddAtomW(atomName); Assert.NotEqual(0, atomId); Assert.True(_internalAddAtomCalled); StringBuilder atomBuffer = new StringBuilder(MaxPathLength); uint bufLength = GetAtomNameW(atomId, atomBuffer, MaxPathLength); string retrievedAtomName = atomBuffer.ToString(); Assert.Equal((uint)atomName.Length, bufLength); Assert.Equal(retrievedAtomName.Length, atomName.Length); Assert.Equal(retrievedAtomName, atomName); Assert.Equal <ushort>(0, DeleteAtom(atomId)); } }
public void ShouldDetourInternalFunction() { using (var hook = LocalHook.Create( LocalHook.GetProcAddress(Interop.Libraries.Kernel32, "InternalAddAtom"), new InternalAddAtomDelegate(Detour_InternalAddAtomHook), this)) { InternalAddAtomFunction = hook.OriginalAddress.ToFunction <InternalAddAtomDelegate>(); hook.ThreadACL.SetInclusiveACL(new int[] { 0 }); _internalAddAtomCalled = false; string atomName = "TestLocalAtomName"; ushort atomId = Interop.Kernel32.AddAtomW(atomName); Assert.NotEqual(0, atomId); Assert.True(_internalAddAtomCalled); StringBuilder atomBuffer = new StringBuilder(MaxPathLength); uint bufLength = Interop.Kernel32.GetAtomNameW(atomId, atomBuffer, MaxPathLength); string retrievedAtomName = atomBuffer.ToString(); Assert.Equal((uint)atomName.Length, bufLength); Assert.Equal(retrievedAtomName.Length, atomName.Length); Assert.Equal(retrievedAtomName, atomName); Assert.Equal <ushort>(0, Interop.Kernel32.DeleteAtom(atomId)); } }
public void DetourAPIAndInternalFunction() { var internalAddAtomFuncAddress = LocalHook.GetProcAddress("kernel32.dll", "InternalAddAtom"); // Create the internal function detour using (var hookInternal = LocalHook.Create( internalAddAtomFuncAddress, new InternalAddAtomDelegate(InternalAddAtomHook), this)) // Create the public API detour using (var hookAPI = LocalHook.Create( LocalHook.GetProcAddress("kernel32.dll", "AddAtomW"), new AddAtomWDelegate(AddAtomHook), this)) { hookInternal.ThreadACL.SetInclusiveACL(new int[] { 0 }); InternalAddAtomFunction = hookInternal.HookBypassAddress.ToFunction <InternalAddAtomDelegate>(); hookAPI.ThreadACL.SetInclusiveACL(new int[] { 0 }); _internalAddAtomCalled = false; _addAtomCalled = false; string atomName = "TestLocalAtomName"; ushort atomId = AddAtomW(atomName); Assert.NotEqual(0, atomId); Assert.True(_internalAddAtomCalled); Assert.True(_addAtomCalled); StringBuilder atomBuffer = new StringBuilder(MaxPathLength); uint bufLength = GetAtomNameW(atomId, atomBuffer, MaxPathLength); string retrievedAtomName = atomBuffer.ToString(); Assert.NotEqual <uint>(0, bufLength); Assert.Equal((uint)atomName.Length, bufLength); Assert.Equal(retrievedAtomName.Length, atomName.Length); Assert.Equal(retrievedAtomName, atomName); Assert.Equal <ushort>(0, DeleteAtom(atomId)); } }
public void ShouldDetourApiAndInternalFunctionCalledByAddAtom() { // Create the internal function and public API detours. using (var hookInternal = LocalHook.Create( LocalHook.GetProcAddress(Interop.Libraries.Kernel32, "InternalAddAtom"), new InternalAddAtomDelegate(Detour_InternalAddAtomHook), this)) using (var hookApi = LocalHook.Create( LocalHook.GetProcAddress(Interop.Libraries.Kernel32, "AddAtomW"), new AddAtomWDelegate(Detour_AddAtom), this)) { hookInternal.ThreadACL.SetInclusiveACL(new int[] { 0 }); InternalAddAtomFunction = hookInternal.TargetAddress.ToFunction <InternalAddAtomDelegate>(); hookApi.ThreadACL.SetInclusiveACL(new int[] { 0 }); _internalAddAtomCalled = false; _addAtomCalled = false; string atomName = "TestLocalAtomName"; ushort atomId = Interop.Kernel32.AddAtomW(atomName); Assert.NotEqual(0, atomId); Assert.True(_internalAddAtomCalled); Assert.True(_addAtomCalled); StringBuilder atomBuffer = new StringBuilder(MaxPathLength); uint bufLength = Interop.Kernel32.GetAtomNameW(atomId, atomBuffer, MaxPathLength); string retrievedAtomName = atomBuffer.ToString(); Assert.NotEqual <uint>(0, bufLength); Assert.Equal((uint)atomName.Length, bufLength); Assert.Equal(retrievedAtomName.Length, atomName.Length); Assert.Equal(retrievedAtomName, atomName); Assert.Equal <ushort>(0, Interop.Kernel32.DeleteAtom(atomId)); } }