gpt4 book ai didi

c# - EasyHook、.NET Remoting 在客户端和服务器之间共享接口(interface)?

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:24 27 4
gpt4 key购买 nike

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

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

最佳答案

截至EasyHook Commit 66751 (绑定(bind)到 EasyHook 2.7 alpha),似乎不可能在客户端(启动 DLL 注入(inject)的进程)和服务器(injected 运行您注入(inject)的 DLL 的进程)。

我是什么意思?

好吧,在 FileMonProcessMonitor例如,请注意共享远程接口(interface)( FileMonInterface,嵌入在 Program.cs 中,用于 Filemon,以及 DemoInterface,在其自己的文件中,用于 ProcessMonitor )是如何放置在注入(inject)程序集。 FileMonInterface 在 FileMon 项目中。 DemoInterface 在 ProcessMonitor 项目中。

为什么不是另一轮?为什么不把FileMonInterface放在工程FileMonInject里,把DemoInterface放在ProcMonInject里呢? 因为调用应用程序(FileMon 和 ProcessMonitor)将无法再访问这些接口(interface)。

原因是因为EasyHook内部使用了:

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

此远程调用允许客户端调用您的(服务器)接口(interface),但服务器本身(您,应用程序)不能调用它。

解决方案

相反,使用:

// 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 { /* ... */ }

作为您的共享远程接口(interface)。

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

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

下面是您之前创建远程 channel 的方式:

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

以下是您现在创建远程 channel 的方法:

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

不要忘记...

我从 this StackOverflow post 得到了这个解决方案.请务必照他说的做,重写InitializeLifetimeService返回null:

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

我认为这应该可以防止客户端丢失远程接口(interface)。

使用

现在,您不必被迫将远程接口(interface)文件放在与注入(inject)项目相同的目录中,您可以专门为您的接口(interface)文件创建一个库。

这个解决方案对于那些有 .NET 远程处理经验的人来说可能是常识,但我对此一无所知(可能在这篇文章中使用了错误的接口(interface)这个词)。

关于c# - EasyHook、.NET Remoting 在客户端和服务器之间共享接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com