public void ShouldThrowNullExceptionWhenSerializingWithNUllStreamAndNUllObject() { IUserDataFormatter formatter = new UserDataBinaryFormatter(); Stream serializationStream = null; object objectToSerialize = null; Assert.Throws <ArgumentNullException>(() => formatter.Serialize(serializationStream, objectToSerialize)); }
/// <summary> /// Initialize the plugin dependencies and execute its entry point. /// </summary> /// <param name="remoteParameters">Parameters containing the plugin to load /// and the parameters to pass to it's entry point.</param> /// <returns></returns> public static int Load(IntPtr remoteParameters) { try { if (remoteParameters == IntPtr.Zero) { throw new ArgumentOutOfRangeException(nameof(remoteParameters), "Remote arguments address was zero"); } // Extract the plugin initialization information // from the remote host loader arguments. var remoteInfoFormatter = new UserDataBinaryFormatter(); var pluginConfig = PluginConfiguration <RemoteEntryInfo, ManagedRemoteInfo> .LoadData( remoteParameters, remoteInfoFormatter ); IDependencyResolver resolver = CreateDependencyResolver( pluginConfig.RemoteInfo.UserLibrary); // Construct the parameter array passed to the plugin initialization function. var pluginParameters = new object[1 + pluginConfig.RemoteInfo.UserParams.Length]; pluginParameters[0] = pluginConfig.UnmanagedInfo; for (var i = 0; i < pluginConfig.RemoteInfo.UserParams.Length; ++i) { pluginParameters[i + 1] = pluginConfig.RemoteInfo.UserParams[i]; } DeserializeParameters(pluginParameters, remoteInfoFormatter); // Execute the plugin library's entry point and pass in the user arguments. pluginConfig.State = LoadPlugin( resolver.Assembly, pluginParameters, pluginConfig.RemoteInfo.ChannelName); return((int)pluginConfig.State); } catch (ArgumentOutOfRangeException outOfRangeEx) { Log(outOfRangeEx.ToString()); throw; } catch (Exception exception) { Log(exception.ToString()); } return((int)PluginInitializationState.Failed); }
public void ShouldSerializeClassToStream() { IUserDataFormatter formatter = new UserDataBinaryFormatter(); using (Stream serializationStream = new MemoryStream()) { var objectToSerialize = new UserDataFormatterTestClass { IntegerMember = 1 }; formatter.Serialize(serializationStream, objectToSerialize); Assert.NotEqual(0, serializationStream.Length); } }
public static int Load(IntPtr remoteParameters) { try { if (remoteParameters == IntPtr.Zero) { throw new ArgumentOutOfRangeException(nameof(remoteParameters), "Remote arguments address was zero"); } var remoteInfoFormatter = new UserDataBinaryFormatter(); var connection = ConnectionData <RemoteEntryInfo, ManagedRemoteInfo> .LoadData( remoteParameters, remoteInfoFormatter ); var resolver = new Resolver(connection.RemoteInfo.UserLibrary); var paramArray = new object[1 + connection.RemoteInfo.UserParams.Length]; paramArray[0] = connection.UnmanagedInfo; for (int i = 0; i < connection.RemoteInfo.UserParams.Length; ++i) { paramArray[i + 1] = connection.RemoteInfo.UserParams[i]; } // Execute the plugin's entry point and pass in the user arguments LoadUserLibrary( resolver.Assembly, paramArray, connection.RemoteInfo.ChannelName, remoteInfoFormatter); } catch (ArgumentOutOfRangeException outOfRangeEx) { Log(outOfRangeEx.ToString()); throw; } catch (Exception exception) { Log(exception.ToString()); } return(0); }
public void ShouldSerializeAndDeserializeClassWithStream() { IUserDataFormatter formatter = new UserDataBinaryFormatter(); const int integerMemberValue = 1; using (Stream serializationStream = new MemoryStream()) { var objectToSerialize = new UserDataFormatterTestClass { IntegerMember = integerMemberValue }; formatter.Serialize(serializationStream, objectToSerialize); Assert.NotEqual(0, serializationStream.Length); serializationStream.Position = 0; var deserializedObject = formatter.Deserialize <UserDataFormatterTestClass>(serializationStream); Assert.NotNull(deserializedObject); Assert.Equal(integerMemberValue, deserializedObject.IntegerMember); } }
/// <summary> /// Start CoreCLR and execute a .NET assembly in a target process. /// </summary> /// <param name="localProcessId">Process ID of the process communicating with the target process.</param> /// <param name="targetProcessId">The process ID of the process to inject the .NET assembly into.</param> /// <param name="remoteInjectorConfig">Configuration settings for starting CoreCLR and executing .NET assemblies.</param> /// <param name="pipePlatform">Class for creating pipes for communication with the target process.</param> /// <param name="passThruArguments">Arguments passed to the .NET hooking plugin once it is loaded in the target process.</param> public static void Inject( int localProcessId, int targetProcessId, RemoteInjectorConfiguration remoteInjectorConfig, IPipePlatform pipePlatform, params object[] passThruArguments) { if (string.IsNullOrWhiteSpace(remoteInjectorConfig.InjectionPipeName)) { throw new ArgumentException("Invalid injection pipe name"); } InjectionHelper.BeginInjection(targetProcessId); using (InjectionHelper.CreateServer(remoteInjectorConfig.InjectionPipeName, pipePlatform)) { try { var remoteInfoFormatter = new UserDataBinaryFormatter(); // Initialize the arguments passed to the CoreHook plugin. var remoteInfo = CreateRemoteInfo(localProcessId, remoteInfoFormatter, passThruArguments); using (var pluginArgumentsStream = new MemoryStream()) { // Serialize the plugin information such as the DLL path // and the plugin arguments, which are copied to the remote process. CreatePluginArguments( remoteInfo, remoteInfoFormatter, remoteInjectorConfig.PayloadLibrary, pluginArgumentsStream, remoteInjectorConfig.InjectionPipeName); // Inject the CoreCLR hosting module into the process, start the CoreCLR // and use the CoreLoad dll to resolve the dependencies of the hooking library // and then call the IEntryPoint.Run method located in the hooking library. try { var process = GetProcessById(targetProcessId); var pluginArgumentsLength = (int)pluginArgumentsStream.Length; using (var assemblyLoader = CreateAssemblyLoader(process)) { var pathConfig = GetPathConfig(); // Load the CoreCLR hosting module in the remote process. assemblyLoader.LoadModule(remoteInjectorConfig.HostLibrary); // Load the function detour module into remote process. assemblyLoader.LoadModule(remoteInjectorConfig.DetourLibrary); // Initialize CoreCLR in the remote process using the native CoreCLR hosting module. assemblyLoader.CreateThread( new RemoteFunctionCall { Arguments = new HostFunctionArguments(pathConfig, new HostArguments { Verbose = remoteInjectorConfig.VerboseLog, PayloadFileName = remoteInjectorConfig.ClrBootstrapLibrary, CoreRootPath = remoteInjectorConfig.ClrRootPath, CoreLibrariesPath = remoteInjectorConfig.ClrLibrariesPath }), FunctionName = new FunctionName { Module = remoteInjectorConfig.HostLibrary, Function = GetClrStartFunctionName() }, }); // Execute a .NET function in the remote process now that CoreCLR is started. assemblyLoader.CreateThread(new RemoteFunctionCall { Arguments = new AssemblyFunctionArguments( pathConfig, CoreHookLoaderDelegate, new PluginConfigurationArguments( process.Is64Bit(), assemblyLoader.CopyMemory(pluginArgumentsStream.GetBuffer(), pluginArgumentsLength), pluginArgumentsLength) ), FunctionName = new FunctionName { Module = remoteInjectorConfig.HostLibrary, Function = GetClrExecuteManagedFunctionName() } }, false); InjectionHelper.WaitForInjection(targetProcessId); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } finally { InjectionHelper.EndInjection(targetProcessId); } } }