EasyHook,.NET远程共享客户端和服务器之间的接口?(EasyHook, .NET Remo

2019-08-02 15:56发布

无论是IPC客户端和IPC服务器如何调用共享的远程接口(类继承MarshalByRefObject的)进行通信,而不必把接口类里面注入的应用? 例如,如果我把接口类在被注入到我的目标进程注入库项目,我的注入应用程序不能引用该接口。

编辑:我已经回答了以下问题。

Answer 1:

由于EasyHook提交66751 (绑EasyHook 2.7阿尔法),它似乎并不可能得到远程接口的实例同时在客户端(即过程发起您的DLL注入)和服务器( 注入的进程中运行你的注入DLL)。

这是什么意思?

那么,在FileMon的和的ProcessMonitor实例中,注意如何共享远程接口( FileMonInterface ,嵌入在Program.cs ,对于菲莱蒙 ,和DemoInterface ,在其自己的文件,为的ProcessMonitor )被放置在注入组件。 FileMonInterface是在FileMon的项目。 DemoInterface是在项目的ProcessMonitor。

为什么不是其他回合? 为什么不把FileMonInterface项目FileMonInject,并把DemoInterface在ProcMonInject? 因为这时的界面将不再给调用应用程序(FileMon和的ProcessMonitor)访问。

究其原因是因为EasyHook内部使用:

RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(TRemoteObject),
                ChannelName,
                InObjectMode);

这种远程调用允许客户打电话给你的(服务器)接口,但服务器本身(你的应用程序)不能调用它。

解决方案

相反,使用:

// Get the instance by simply calling `new RemotingInterface()` beforehand somewhere
RemotingServices.Marshal(instanceOfYourRemotingInterfaceHere, ChannelName);

我所做的是增加一个重载EasyHook的RemoteHook.IpcCreateServer()接受做NET远程处理我的新“办法”。

它的丑陋,但它的工作原理:

代码

更换整个IpcCreateServer方法(从支架来支撑)用下面的代码。 这里有所示的两种方法。 一个是更详细的过载。 第二个是“原始”的方法调用我们的超载。

public static IpcServerChannel IpcCreateServer<TRemoteObject>(
        ref String RefChannelName,
        WellKnownObjectMode InObjectMode,
        TRemoteObject ipcInterface, String ipcUri, bool useNewMethod,
        params WellKnownSidType[] InAllowedClientSIDs) where TRemoteObject : MarshalByRefObject
    {
        String ChannelName = RefChannelName ?? GenerateName();

        ///////////////////////////////////////////////////////////////////
        // create security descriptor for IpcChannel...
        System.Collections.IDictionary Properties = new System.Collections.Hashtable();

        Properties["name"] = ChannelName;
        Properties["portName"] = ChannelName;

        DiscretionaryAcl DACL = new DiscretionaryAcl(false, false, 1);

        if (InAllowedClientSIDs.Length == 0)
        {
            if (RefChannelName != null)
                throw new System.Security.HostProtectionException("If no random channel name is being used, you shall specify all allowed SIDs.");

            // allow access from all users... Channel is protected by random path name!
            DACL.AddAccess(
                AccessControlType.Allow,
                new SecurityIdentifier(
                    WellKnownSidType.WorldSid,
                    null),
                -1,
                InheritanceFlags.None,
                PropagationFlags.None);
        }
        else
        {
            for (int i = 0; i < InAllowedClientSIDs.Length; i++)
            {
                DACL.AddAccess(
                    AccessControlType.Allow,
                    new SecurityIdentifier(
                        InAllowedClientSIDs[i],
                        null),
                    -1,
                    InheritanceFlags.None,
                    PropagationFlags.None);
            }
        }

        CommonSecurityDescriptor SecDescr = new CommonSecurityDescriptor(false, false,
            ControlFlags.GroupDefaulted |
            ControlFlags.OwnerDefaulted |
            ControlFlags.DiscretionaryAclPresent,
            null, null, null,
            DACL);

        //////////////////////////////////////////////////////////
        // create IpcChannel...
        BinaryServerFormatterSinkProvider BinaryProv = new BinaryServerFormatterSinkProvider();
        BinaryProv.TypeFilterLevel = TypeFilterLevel.Full;

        IpcServerChannel Result = new IpcServerChannel(Properties, BinaryProv, SecDescr);

        if (!useNewMethod)
        {
            ChannelServices.RegisterChannel(Result, false);

            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(TRemoteObject),
                ChannelName,
                InObjectMode);
        }
        else
        {
            ChannelServices.RegisterChannel(Result, false);

            ObjRef refGreeter = RemotingServices.Marshal(ipcInterface, ipcUri);
        }

        RefChannelName = ChannelName;

        return Result;
    }

    /// <summary>
    /// Creates a globally reachable, managed IPC-Port.
    /// </summary>
    /// <remarks>
    /// Because it is something tricky to get a port working for any constellation of
    /// target processes, I decided to write a proper wrapper method. Just keep the returned
    /// <see cref="IpcChannel"/> alive, by adding it to a global list or static variable,
    /// as long as you want to have the IPC port open.
    /// </remarks>
    /// <typeparam name="TRemoteObject">
    /// A class derived from <see cref="MarshalByRefObject"/> which provides the
    /// method implementations this server should expose.
    /// </typeparam>
    /// <param name="InObjectMode">
    /// <see cref="WellKnownObjectMode.SingleCall"/> if you want to handle each call in an new
    /// object instance, <see cref="WellKnownObjectMode.Singleton"/> otherwise. The latter will implicitly
    /// allow you to use "static" remote variables.
    /// </param>
    /// <param name="RefChannelName">
    /// Either <c>null</c> to let the method generate a random channel name to be passed to 
    /// <see cref="IpcConnectClient{TRemoteObject}"/> or a predefined one. If you pass a value unequal to 
    /// <c>null</c>, you shall also specify all SIDs that are allowed to connect to your channel!
    /// </param>
    /// <param name="InAllowedClientSIDs">
    /// If no SID is specified, all authenticated users will be allowed to access the server
    /// channel by default. You must specify an SID if <paramref name="RefChannelName"/> is unequal to <c>null</c>.
    /// </param>
    /// <returns>
    /// An <see cref="IpcChannel"/> that shall be keept alive until the server is not needed anymore.
    /// </returns>
    /// <exception cref="System.Security.HostProtectionException">
    /// If a predefined channel name is being used, you are required to specify a list of well known SIDs
    /// which are allowed to access the newly created server.
    /// </exception>
    /// <exception cref="RemotingException">
    /// The given channel name is already in use.
    /// </exception>
    public static IpcServerChannel IpcCreateServer<TRemoteObject>(
        ref String RefChannelName,
        WellKnownObjectMode InObjectMode,
        params WellKnownSidType[] InAllowedClientSIDs) where TRemoteObject : MarshalByRefObject
    {
        return IpcCreateServer<TRemoteObject>(ref RefChannelName, InObjectMode, null, null, false, InAllowedClientSIDs);
    }

而已。 这就是你需要改变。 你不必改变IpcCreateClient()。

使用代码

这里是你将如何使用新的重载方法:

假设你有

public class IpcInterface : MarshalByRefObject { /* ... */ }

为您的共享远程接口。

创建它的一个新的实例,并保存其引用。 您将使用这与你的客户沟通。

var myIpcInterface = new IpcInterface(); // Keep this reference to communicate!

这里是你如何创造了远程通道:

        ipcServer = RemoteHooking.IpcCreateServer<IpcInterface>(ref IpcChannelName, WellKnownObjectMode.Singleton, WellKnownSidType.WorldSid);

这里是你会如何现在创建一个远程通道:

        ipcServer = RemoteHooking.IpcCreateServer<IpcInterface>(ref IpcChannelName, WellKnownObjectMode.Singleton, myIpcInterface, IpcChannelName, true, WellKnownSidType.WorldSid);

不要忘了...

我得到了这个解决方案这个StackOverflow的职位 。 请务必做,因为他说,并覆盖InitializeLifetimeService返回NULL:

public override object InitializeLifetimeService()
{
    // Live "forever"
    return null;
}

我觉得这是应该避免了客户端失去了远程接口。

用途

现在,而不是被迫把你的远程接口文件在同一目录下注入的项目,你可以为你的接口文件创建一个特定的库。

该解决方案可能是共同的知识,那些具有NET远程处理这样的经验,但我什么都不知道它(可能已经使用了Word界面错在这个岗位)。



文章来源: EasyHook, .NET Remoting sharing interface between both client and server?