예제 #1
0
        public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // Add any constructor code after InitializeComponent call
            //
            SetInitialMessage();

            // EXAMPLE CODE SECTION
            keyboardHook          = HookFactory.CreateKeyboardHookExt();
            keyboardHook.KeyDown += new KeyboardEventHandlerExt(keyboardHook_KeyDown);

            //
            // Note: Use IMouseHookExt for mouse move events.
            //
            mouseHook = HookFactory.CreateMouseHookLite();
            mouseHook.LeftButtonDown += new MouseEventHandlerExt(mouseHook_LeftButtonDown);
            mouseHook.LeftButtonUp   += new MouseEventHandlerExt(mouseHook_LeftButtonUp);
            mouseHook.MouseWheel     += new MouseEventHandlerExt(mouseHook_MouseWheel);

            UpdateEnabledStates();
        }
 public void DefaultCreationTest(TestMethodRecord tmr)
 {
     using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt())
     {
         tmr.RunTest(keyboard != null, "Keyboard hook created.");
     }
 }
        public void ReferenceCountingTest(TestMethodRecord tmr)
        {
            //
            // In this test we are observing how the various keyboard hook
            // classes are interacting with their implementation classes. They
            // should add references in their constructor and release them when
            // disposed. The implementation should only be disposed when the ref
            // count is zero.
            //
            KeyboardHookTestImpl testImpl = new KeyboardHookTestImpl();

            tmr.RunTest(testImpl.ReferenceCount == 0, "Now references count is 0.");
            tmr.RunTest(testImpl.Disposed == false, "Not initially disposed.");

            using (IKeyboardHook hook1 = HookFactory.CreateKeyboardHook(testImpl))
            {
                tmr.RunTest(testImpl.ReferenceCount == 1, "Now references count is 1.");
                tmr.RunTest(testImpl.Disposed == false, "Not disposed.");

                using (IKeyboardHookExt hook2 = HookFactory.CreateKeyboardHookExt(testImpl))
                {
                    tmr.RunTest(testImpl.ReferenceCount == 2, "Now references count is 2.");
                    tmr.RunTest(testImpl.Disposed == false, "Not disposed.");
                }

                tmr.RunTest(testImpl.ReferenceCount == 1, "Now references count is 1.");
                tmr.RunTest(testImpl.Disposed == false, "Not disposed.");
            }

            tmr.RunTest(testImpl.ReferenceCount == 0, "Now references count is 0.");
            tmr.RunTest(testImpl.Disposed == true, "Disposed.");
        }
예제 #4
0
        public void ShouldEnableHookWithProperty()
        {
            using (var hook = HookFactory.CreateHook <GetVersionDelegate>(
                       LocalHook.GetProcAddress("kernel32.dll", "GetVersion"),
                       GetVersionDetour, this))
            {
                // Enable the hook for all threads
                hook.Enabled = true;
                Assert.Equal <uint>(0, GetVersion());
                Assert.Equal <uint>(0, hook.Target());

                // Disable the hook for all threads
                hook.Enabled = false;
                Assert.NotEqual <uint>(0, GetVersion());
                Assert.NotEqual <uint>(0, hook.Target());
                Assert.NotEqual <uint>(0, hook.Original());

                // Enable the hook for the current thread
                hook.ThreadACL.SetInclusiveACL(new int[1]);
                Assert.Equal <uint>(0, GetVersion());
                Assert.Equal <uint>(0, hook.Target());
                Assert.NotEqual <uint>(0, hook.Original());

                // Disable the hook for the current thread
                hook.ThreadACL.SetExclusiveACL(new int[1]);
                Assert.NotEqual <uint>(0, GetVersion());
                Assert.NotEqual <uint>(0, hook.Target());
            }
        }
예제 #5
0
        /// <summary>
        /// Initialize hooks for our file I/O functions.
        /// </summary>
        /// <param name="context"></param>
        public void Run(IContext context)
        {
            _readFileHook  = HookFactory.CreateHook <ReadFileDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Kernel32, "ReadFile"), Detour_ReadFile, this);
            _writeFileHook = HookFactory.CreateHook <WriteFileDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Kernel32, "WriteFile"), Detour_WriteFile, this);

            DisplayFileAccess().GetAwaiter().GetResult();
        }
예제 #6
0
 public void CreationTest(TestMethodRecord tmr)
 {
     using (IMouseHookExt mouseHook = HookFactory.CreateMouseHookExt())
     {
         tmr.RunTest(mouseHook != null, "Mouse hook created.");
     }
 }
예제 #7
0
        public void DetourApiAndInternalFunctionUsingInterfaceBypassAddress()
        {
            using (var hookInternal = HookFactory.CreateHook <InternalFindAtom>(
                       LocalHook.GetProcAddress("kernel32.dll", "InternalFindAtom"),
                       InternalFindAtom_Hook,
                       this))
            {
                hookInternal.ThreadACL.SetInclusiveACL(new int[] { 0 });

                InternalFindAtomFunction = hookInternal.Original;

                _internalFindAtomCalled = false;

                string atomName = "TestLocalAtomName";
                ushort atomId   = AddAtomW(atomName);

                ushort foundAtomId = FindAtomW(atomName);

                Assert.NotEqual(0, atomId);
                Assert.True(_internalFindAtomCalled);
                Assert.Equal(atomId, foundAtomId);

                Assert.Equal <ushort>(0, DeleteAtom(atomId));
            }
        }
예제 #8
0
        public ReflectionDetourNotifier(HookFactory hookFactory, Delegate targetDelegate)
        {
            _targetDelegate = targetDelegate;
            _hookDelegate   = GenerateInterceptor(targetDelegate);

            Hook = hookFactory(Marshal.GetFunctionPointerForDelegate(_targetDelegate),
                               Marshal.GetFunctionPointerForDelegate(_hookDelegate));
            _detour = Hook.CreateDetour(_targetDelegate.GetType());
        }
        public ReflectionDetourNotifier(HookFactory hookFactory, Delegate targetDelegate)
        {
            _targetDelegate = targetDelegate;
            _hookDelegate = GenerateInterceptor(targetDelegate);

            Hook = hookFactory(Marshal.GetFunctionPointerForDelegate(_targetDelegate),
                Marshal.GetFunctionPointerForDelegate(_hookDelegate));
            _detour = Hook.CreateDetour(_targetDelegate.GetType());
        }
예제 #10
0
        public override unsafe void CreateHooks()
        {
            _d3DDeviceFunctions = new List <IntPtr>();

            using (var direct3D = new SharpDX.Direct3D9.Direct3D())
            {
                using (var device = new Device(direct3D, 0, DeviceType.NullReference, IntPtr.Zero,
                                               CreateFlags.HardwareVertexProcessing,
                                               new PresentParameters {
                    BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = IntPtr.Zero
                }))
                {
                    _d3DDeviceFunctions.AddRange(ReadVTableAddresses(device.NativePointer, D3DDevice9FunctionCount));
                }
            }

            // Create the hooks for our target Direct3D Device functions.
            _d3DEndSceneHook = HookFactory.CreateHook <IDirect3DDevice9_EndSceneDelegate>(
                _d3DDeviceFunctions[(int)FunctionOrdinals.EndScene],
                Detour_EndScene,
                this);

            _d3DPresentHook = HookFactory.CreateHook <IDirect3DDevice9_PresentDelegate>(
                _d3DDeviceFunctions[(int)FunctionOrdinals.Present],
                Detour_Present,
                this);

            _d3DResetHook = HookFactory.CreateHook <IDirect3DDevice9_ResetDelegate>(
                _d3DDeviceFunctions[(int)FunctionOrdinals.Reset],
                Detour_Reset,
                this);

            // Add the Frames Per Second overlay.
            Overlays = new List <IOverlay>
            {
                new Direct3D.Core.Drawing.Overlay
                {
                    Elements =
                    {
                        new FramesPerSecondOverlay(new System.Drawing.Font("Arial",  16, FontStyle.Bold))
                        {
                            Location    = new System.Drawing.Point(25,              25),
                            Color       = Color.Red,
                            AntiAliased = true,
                            Text        = "{0:N0} FPS"
                        }
                    },
                    Hidden = false
                }
            };

            // Enable the hooks for all threads except the current thread.
            _d3DEndSceneHook.ThreadACL.SetExclusiveACL(new int[1]);
            _d3DPresentHook.ThreadACL.SetExclusiveACL(new int[1]);
            _d3DResetHook.ThreadACL.SetExclusiveACL(new int[1]);
        }
        public void InstallHookTests(TestMethodRecord tmr)
        {
            using (IKeyboardHook kHook = HookFactory.CreateKeyboardHook())
            {
                TestInstallHook(tmr, kHook, "IKeyboardHook");
            }

            using (IKeyboardHookExt kHookExt = HookFactory.CreateKeyboardHookExt())
            {
                TestInstallHook(tmr, kHookExt, "IKeyboardHookExt");
            }
        }
예제 #12
0
        public void CreateKeyboardHookTests(TestMethodRecord tmr)
        {
            using (IKeyboardHook hook = HookFactory.CreateKeyboardHook())
            {
                tmr.RunTest(hook != null, "IKeyboardHook created successfully with default options.");
            }

            using (IKeyboardHookExt hookExt = HookFactory.CreateKeyboardHookExt())
            {
                tmr.RunTest(hookExt != null, "IKeyboardHookExt created successfully with default options.");
            }
        }
예제 #13
0
        public void InstallHookTests(TestMethodRecord tmr)
        {
            using (IMouseHook mHook = HookFactory.CreateMouseHook())
            {
                TestInstallHook(tmr, mHook, "IMouseHook");
            }

            using (IMouseHookExt mHookExt = HookFactory.CreateMouseHookExt())
            {
                TestInstallHook(tmr, mHookExt, "IMouseHookExt");
            }

            using (IMouseHookLite mHookLite = HookFactory.CreateMouseHookLite())
            {
                TestInstallHook(tmr, mHookLite, "IMouseHookLite");
            }
        }
예제 #14
0
        public void CreateMouseHookTests(TestMethodRecord tmr)
        {
            using (IMouseHook mouseHook = HookFactory.CreateMouseHook())
            {
                tmr.RunTest(mouseHook != null, "IMouseHook created successfully with default options.");
            }

            using (IMouseHookExt mouseHookExt = HookFactory.CreateMouseHookExt())
            {
                tmr.RunTest(mouseHookExt != null, "IMouseHookExt created successfully with default options.");
            }

            using (IMouseHookLite mouseHookLite = HookFactory.CreateMouseHookLite())
            {
                tmr.RunTest(mouseHookLite != null, "IMouseHookLite created successfully with default options.");
            }
        }
예제 #15
0
        public override void CreateHooks()
        {
            var renderForm = new SharpDX.Windows.RenderForm();

            Device.CreateWithSwapChain(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport,
                CreateSwapChainDescription(renderForm.Handle),
                out _device,
                out _swapChain);

            if (_swapChain != null)
            {
                _d3DDeviceFunctions.AddRange(ReadVTableAddresses(_swapChain.NativePointer, DXGI_SWAPCHAIN_METHOD_COUNT));
            }

            _d3DPresentHook = HookFactory.CreateHook <DXGISwapChain_PresentDelegate>(
                _d3DDeviceFunctions[(int)FunctionOrdinals.Present],
                Detour_Present,
                this);

            Overlays = new List <IOverlay>
            {
                // Add the Frames Per Second overlay
                new Direct3D.Core.Drawing.Overlay
                {
                    Elements =
                    {
                        new FramesPerSecondOverlay(new Font("Arial",  16, FontStyle.Bold))
                        {
                            Location    = new Point(25,              25),
                            Color       = Color.Red,
                            AntiAliased = true,
                            Text        = "{0:N0} FPS"
                        }
                    },
                    Hidden = false
                }
            };

            _d3DPresentHook.Enabled = true;
        }
예제 #16
0
        /// <summary>
        /// First method called during plugin load.
        /// Can be used to create hooks and initialize
        /// variables.
        /// </summary>
        /// <param name="context">Contains any standard information required for each plugin.</param>
        public unsafe void Run(IContext context)
        {
            // Create network function hooks
            _wsaRecvHook  = HookFactory.CreateHook <WSARecvDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Ws2_32, "WSARecv"), Detour_WSARecv, this);
            _wsaSendHook  = HookFactory.CreateHook <WSASendDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Ws2_32, "WSASend"), Detour_WsaSend, this);
            _recvHook     = HookFactory.CreateHook <RecvDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Ws2_32, "recv"), Detour_recv, this);
            _sendHook     = HookFactory.CreateHook <SendDelegaqte>(LocalHook.GetProcAddress(Interop.Libraries.Ws2_32, "send"), Detour_send, this);
            _recvfromHook = HookFactory.CreateHook <RecvfromDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Ws2_32, "recvfrom"), Detour_recvfrom, this);
            _sendtoHook   = HookFactory.CreateHook <SendtoDelegate>(LocalHook.GetProcAddress(Interop.Libraries.Ws2_32, "sendto"), Detour_sendto, this);

            // Enable hooks for all threads
            _wsaSendHook.Enabled  = true;
            _wsaRecvHook.Enabled  = true;
            _recvHook.Enabled     = true;
            _sendHook.Enabled     = true;
            _recvfromHook.Enabled = true;
            _sendtoHook.Enabled   = true;

            ProcessPackets().GetAwaiter().GetResult();
        }
        public void EventTest(TestMethodRecord tmr)
        {
            KeyboardHookEventArgs[] keyArgs = GenerateRandomKeyData();

            KeyboardHookTestImpl hookImpl = new KeyboardHookTestImpl();

            using (IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt(hookImpl))
            {
                keyboard.InstallHook();

                keyboard.KeyDown += new KeyboardEventHandlerExt(EventTest_KeyDown);

                //
                // Trigger a bunch of keyboard events. The event handler will
                // capture the output and we will then compare it here.
                //
                foreach (KeyboardHookEventArgs keyArg in keyArgs)
                {
                    hookImpl.TriggerKeyAction(KeyboardEvents.KeyDown, keyArg);
                }
            }

            tmr.RunTest(keyArgs.Length == eventTest_ArgsList.Count, "The proper number of events were caught.");

            bool same = true;

            for (int i = 0; i < keyArgs.Length; i++)
            {
                KeyboardHookEventArgs kea1 = keyArgs[i];
                KeyboardHookEventArgs kea2 = (KeyboardHookEventArgs)eventTest_ArgsList[i];

                if (!CompareKeyArgs(kea1, kea2))
                {
                    tmr.WriteLine("Failed event results comparison at index " + i);
                    same = false;
                    break;
                }
            }

            tmr.RunTest(same, "The event callback captured all the events successfully.");
        }
예제 #18
0
        public void EventSinkTest(TestMethodRecord tmr)
        {
            MouseHookEventArgs[] mouseArgs = GenerateRandomClickArgs();

            MouseHookTestImpl mouseImpl = new MouseHookTestImpl();

            using (IMouseHookExt mouseHook = HookFactory.CreateMouseHookExt(mouseImpl))
            {
                mouseHook.LeftButtonDown += new MouseEventHandlerExt(EventSinkTest_LeftButtonDown);
                mouseHook.InstallHook();

                //
                // Trigger a bunch of mouse down events. The event handler will
                // capture the output and we will then compare it here.
                //
                foreach (MouseHookEventArgs mea in mouseArgs)
                {
                    mouseImpl.TriggerMouseAction(MouseEvents.LeftButtonDown, mea);
                }
            }

            tmr.RunTest(mouseArgs.Length == eventTest_ArgsList.Count, "The proper number of events were caught.");

            bool same = true;

            for (int i = 0; i < mouseArgs.Length; i++)
            {
                MouseHookEventArgs mea1 = mouseArgs[i];
                MouseHookEventArgs mea2 = (MouseHookEventArgs)eventTest_ArgsList[i];

                if (!CompareMouseArgs(mea1, mea2))
                {
                    tmr.WriteLine("Failed event results comparison at index " + i);
                    same = false;
                    break;
                }
            }

            tmr.RunTest(same, "The event callback captured all the events successfully.");
        }
        public void SortedEventTest(TestMethodRecord tmr)
        {
            KeyboardHookTestImpl hookImpl = new KeyboardHookTestImpl();
            IKeyboardHookExt     keyboard = HookFactory.CreateKeyboardHookExt(hookImpl);

            keyboard.InstallHook();

            keyboard.KeyDown       += new KeyboardEventHandlerExt(SortedEventTest_KeyDown);
            keyboard.KeyUp         += new KeyboardEventHandlerExt(SortedEventTest_KeyUp);
            keyboard.SystemKeyUp   += new KeyboardEventHandlerExt(SortedEventTest_SystemKeyUp);
            keyboard.SystemKeyDown += new KeyboardEventHandlerExt(SortedEventTest_SystemKeyDownKeyUp);

            KeyboardHookEventArgs[] keyArgs = GenerateRandomKeyData();

            KeyboardHookEventArgs kea1 = keyArgs[0];
            KeyboardHookEventArgs kea2 = keyArgs[1];
            KeyboardHookEventArgs kea3 = keyArgs[2];
            KeyboardHookEventArgs kea4 = keyArgs[3];

            hookImpl.TriggerKeyAction(KeyboardEvents.KeyDown, kea1);
            hookImpl.TriggerKeyAction(KeyboardEvents.KeyUp, kea2);
            hookImpl.TriggerKeyAction(KeyboardEvents.SystemKeyDown, kea3);
            hookImpl.TriggerKeyAction(KeyboardEvents.SystemKeyUp, kea4);

            tmr.RunTest(sortedEventTest_ArgsListDown.Count == 1, "One KeyDown event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListUp.Count == 1, "One KeyUp event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListSysDown.Count == 1, "One SystemKeyDown event trigged.");
            tmr.RunTest(sortedEventTest_ArgsListSysUp.Count == 1, "One SystemKeyUp event trigged.");

            KeyboardHookEventArgs keaB1 = (KeyboardHookEventArgs)sortedEventTest_ArgsListDown[0];
            KeyboardHookEventArgs keaB2 = (KeyboardHookEventArgs)sortedEventTest_ArgsListUp[0];
            KeyboardHookEventArgs keaB3 = (KeyboardHookEventArgs)sortedEventTest_ArgsListSysDown[0];
            KeyboardHookEventArgs keaB4 = (KeyboardHookEventArgs)sortedEventTest_ArgsListSysUp[0];

            tmr.RunTest(CompareKeyArgs(kea1, keaB1), "Correct KeyDown event caught.");
            tmr.RunTest(CompareKeyArgs(kea2, keaB2), "Correct KeyUp event caught.");
            tmr.RunTest(CompareKeyArgs(kea3, keaB3), "Correct SysKeyDown event caught.");
            tmr.RunTest(CompareKeyArgs(kea4, keaB4), "Correct SysKeyUp event caught.");
        }
예제 #20
0
 public void Save()
 {
     HookFactory.AddHookSetupForItem(this);
 }
 public void NullImplementationTest(TestMethodRecord tmr)
 {
     tmr.RegisterException("Null implementation causes an exception.", typeof(ArgumentNullException));
     IKeyboardHookExt keyboard = HookFactory.CreateKeyboardHookExt(null);
 }