1

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

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

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

我的意图只是能够在主机搜索发生时(应该在应用程序启动时)执行类似打开消息框的操作。

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

之后,我创建了启动进程本身的 ac# 项目(因此从进程对象中获取 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 钩子,呵呵。

提前致谢 ;)

4

1 回答 1

0

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

还没有exe但是......就是这样。

使用 pydbg + 钩子。

于 2010-11-18T11:30:20.487 回答