【问题标题】:Application hooking (winsock) from c++/c#来自 c++/c# 的应用程序挂钩 (winsock)
【发布时间】:2010-11-17 08:39:53
【问题描述】:

我有一个调用 winsocket 函数的(旧)应用程序:

struct hostent* FAR gethostbyname(
  __in  const char *name
);

它目前将其导入为 ws32_dll.#52 而不是正常的名称调用。

我的目的只是为了能够在主机搜索发生时(应该在应用程序启动时)打开消息框。

我尝试创建一个 c++ dll,其中 pragma cmets 指向 #52 并将其放在应用程序目录(包括“exe.local”和“exe.manifest”文件以尝试重定向它),但它加载了c:\windows\system32 代替。

之后,我创建了一个启动进程本身的 c# 项目(因此从 Process 对象中获取 PID),并将 easyhook dll 添加到其中。

我检查了这个例子:http://www.codeproject.com/KB/DLL/EasyHook64.aspx

将调用更改为:

    FileMon.FileMonInterface Interface;
    LocalHook CreateFileHook;
    Stack<String> Queue = new Stack<String>();

    public Main(
        RemoteHooking.IContext InContext,
        String InChannelName)
    {
        // connect to host...

        Interface = 
          RemoteHooking.IpcConnectClient<FileMon.FileMonInterface>(InChannelName);
    }

    public void Run(
        RemoteHooking.IContext InContext,
        String InChannelName)
    {
        // install hook...
        try
        {
            CreateFileHook = LocalHook.Create(
                LocalHook.GetProcAddress("ws2_32.dll", "gethostbyname"),
                new DCreateFile(GetHostByName_Hooked),
                this);

            CreateFileHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
        }
        catch (Exception ExtInfo)
        {
            Interface.ReportException(ExtInfo);

            return;
        }

        Interface.IsInstalled(RemoteHooking.GetCurrentProcessId());

        // wait for host process termination...
        try
        {
            while (true)
            {
                Thread.Sleep(500);

                // transmit newly monitored file accesses...
                if (Queue.Count > 0)
                {
                    String[] Package = null;

                    lock (Queue)
                    {
                        Package = Queue.ToArray();

                        Queue.Clear();
                    }

                    Interface.OnCreateFile(RemoteHooking.GetCurrentProcessId(), Package);
                }
                else
                    Interface.Ping();
            }
        }
        catch
        {
            // NET Remoting will raise an exception if host is unreachable
        }
    }


    [UnmanagedFunctionPointer(CallingConvention.StdCall,
        CharSet = CharSet.Auto,
        SetLastError = true)]
    delegate IntPtr DGetHostByName(
        String name);

    // just use a P-Invoke implementation to get native API access
    // from C# (this step is not necessary for C++.NET)
    [DllImport("ws2_32.dll",
        CharSet = CharSet.Auto,
        SetLastError = true,
        CallingConvention = CallingConvention.StdCall)]
    static extern IntPtr gethostbyname(
        String name);

    // this is where we are intercepting all file accesses!
    static IntPtr GetHostByName_Hooked(
        String name)
    {
        try
        {
            Main This = (Main)HookRuntimeInfo.Callback;
            MessageBox.Show("hi!");


        }
        catch
        {
        }

        // call original API...
        return GetHostByName(
            name);
    }
}

}

(可能在这里写错了,但项目编译成功@home)。

问题是我不知道我需要做什么才能挂钩这些方法 应用程序本身。

我的意思是.. 剩下的就是用 c# easyhook 进行挂钩(假设应用程序是“foo.exe”)? 我需要为easyhook创建一个自定义dll吗?(在这种情况下,我需要在里面定义什么内容?)

我发现它有点...“复杂”的 helloworld 钩子,呵呵。

提前致谢;)

【问题讨论】:

    标签: c# hook winsock easyhook


    【解决方案1】:

    最后,使用“Gray hack python”一书教会了我如何用更少的行数来完成这一切,而这正是我想要的。

    还没有 exe,但是……就是这样。

    使用 pydbg + 钩子。

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多