在CefSharp.Wp下钩入ws2_32.dll的recv函数失败

时间:2019-03-24 15:00:40

标签: c# easyhook

当我尝试连接到CefSharp.Wpf下的ws2_32.dll的recv函数时。当我开始调试程序时,在接收到一些数据包后我将不再钩子,但是我可以在Winsock数据包编辑器中看到recv数据包,看起来钩子停止了工作。我不知道发生了什么看一下代码:

enter image description here

using CefSharp;
using EasyHook;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;

namespace HookTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // Thread.Sleep(20 * 1000);
            InitBrowser();
            InstallHook();
        }

        void InitBrowser()
        {
            var settings = new CefSharp.Wpf.CefSettings();

            Cef.Initialize(settings);
            var cefBrowser = new CefSharp.Wpf.ChromiumWebBrowser
            {
                Address = "https://github.com"
            };
            panel.Children.Add(cefBrowser);
        }

        void InstallHook()
        {
            List<LocalHook> hooks = new List<LocalHook>
            {
                LocalHook.Create(LocalHook.GetProcAddress("Ws2_32.dll", "recv"), new Ws2_32.Drecv(RecvHook), null),
            };
            foreach (LocalHook hook in hooks)
            {
                hook.ThreadACL.SetExclusiveACL(new int[] { 0 });
            }
        }

        public int RecvHook(IntPtr s, IntPtr buf, int len, int flags)
        {
            int num = 0;
            try
            {
                num = Ws2_32.recv(s, buf, len, flags);
                Debug.WriteLine("recv buffer:" + num);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            return num;
        }
    }

    class Ws2_32
    {
        [DllImport("WS2_32.dll")]
        public static extern int recv(IntPtr s, IntPtr buf, int len, int flags);

        [UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
        public delegate int Drecv(IntPtr s, IntPtr buf, int len, int flags);
    }
}

0 个答案:

没有答案
相关问题