Ejemplo n.º 1
0
        /// <summary>
        /// Create the config class that is passed to the CLR bootstrap library to be loaded.
        /// The <paramref name="remoteInfo"/> holds information such as what hooking module to load.
        /// </summary>
        /// <param name="remoteInfo">The configuration that is serialized and passed to CoreLoad.</param>
        /// <param name="userDataFormatter">Serializes the <paramref name="remoteInfo"/> data.</param>
        /// <param name="pluginPath">The plugin to be loaded and executed in the target process.</param>
        /// <param name="argumentsStream">The stream that holds the the serialized <paramref name="remoteInfo"/> class.</param>
        /// <param name="injectionPipeName">The pipe name used for notifying the host process that the hook plugin has been loaded in the target process.</param>
        private static void CreatePluginArguments(
            ManagedRemoteInfo remoteInfo,
            IUserDataFormatter userDataFormatter,
            string pluginPath,
            Stream argumentsStream,
            string injectionPipeName)
        {
            if (string.IsNullOrWhiteSpace(pluginPath))
            {
                throw new ArgumentException("The injection library was not valid");
            }

            if (File.Exists(pluginPath))
            {
                pluginPath = Path.GetFullPath(pluginPath);
            }

            remoteInfo.UserLibrary = pluginPath;

            if (File.Exists(remoteInfo.UserLibrary))
            {
                remoteInfo.UserLibraryName = AssemblyName.GetAssemblyName(remoteInfo.UserLibrary).FullName;
            }
            else
            {
                throw new FileNotFoundException($"The given assembly could not be found: '{remoteInfo.UserLibrary}'", remoteInfo.UserLibrary);
            }

            remoteInfo.ChannelName = injectionPipeName;

            userDataFormatter.Serialize(argumentsStream, remoteInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create the config class that is passed to the CLR bootstrap library to be loaded.
        /// The <paramref name="remoteInfo"/> holds information such as what hooking module to load.
        /// </summary>
        /// <param name="remoteInfo">The configuration that is serialized and passed to CoreLoad.</param>
        /// <param name="serializer">Serializes the <paramref name="remoteInfo"/> data.</param>
        /// <param name="library">The managed hooking library to be loaded and executed in the target process.</param>
        /// <param name="argumentsStream">The stream that holds the the serialized <paramref name="remoteInfo"/> class.</param>
        /// <param name="injectionPipeName">The pipe name used for notifying the host process that the hook plugin has been loaded in the target process.</param>
        private static void PrepareInjection(
            ManagedRemoteInfo remoteInfo,
            IUserDataFormatter serializer,
            ref string library,
            MemoryStream argumentsStream,
            string injectionPipeName)
        {
            if (string.IsNullOrWhiteSpace(library))
            {
                throw new ArgumentException("The injection library was not valid");
            }

            if ((library != null) && File.Exists(library))
            {
                library = Path.GetFullPath(library);
            }

            remoteInfo.UserLibrary = library;

            if (File.Exists(remoteInfo.UserLibrary))
            {
                remoteInfo.UserLibraryName = AssemblyName.GetAssemblyName(remoteInfo.UserLibrary).FullName;
            }
            else
            {
                throw new FileNotFoundException($"The given assembly could not be found: '{remoteInfo.UserLibrary}'", remoteInfo.UserLibrary);
            }

            remoteInfo.ChannelName = injectionPipeName;

            serializer.Serialize(argumentsStream, remoteInfo);
        }
Ejemplo n.º 3
0
        public void ShouldThrowNullExceptionWhenSerializingWithNullStreamAndNullObject()
        {
            IUserDataFormatter formatter           = CreateFormatter();
            Stream             serializationStream = null;
            object             objectToSerialize   = null;

            Assert.Throws <ArgumentNullException>(() => formatter.Serialize(serializationStream, objectToSerialize));
        }
Ejemplo n.º 4
0
        public void ShouldSerializeClassToStream()
        {
            IUserDataFormatter formatter = CreateFormatter();

            using (Stream serializationStream = new MemoryStream())
            {
                var objectToSerialize = new UserDataFormatterTestClass {
                    IntegerMember = 1
                };
                formatter.Serialize(serializationStream, objectToSerialize);

                Assert.NotEqual(0, serializationStream.Length);
            }
        }
Ejemplo n.º 5
0
        public void ShouldSerializeAndDeserializeClassWithStream()
        {
            IUserDataFormatter formatter          = CreateFormatter();
            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);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="processId">The process ID of the local process that is injecting into another remote process.</param>
        /// <param name="formatter">Serializer for the user arguments passed to the plugin.</param>
        /// <param name="passThruArguments">The arguments passed to the plugin during initialization.</param>
        /// <returns></returns>
        private static ManagedRemoteInfo CreateRemoteInfo(int processId, IUserDataFormatter formatter, params object[] passThruArguments)
        {
            var remoteInfo = new ManagedRemoteInfo {
                RemoteProcessId = processId
            };

            var arguments = new List <object>();

            if (passThruArguments != null)
            {
                foreach (var arg in passThruArguments)
                {
                    using (var ms = new MemoryStream())
                    {
                        formatter.Serialize(ms, arg);
                        arguments.Add(ms.ToArray());
                    }
                }
            }
            remoteInfo.UserParams = arguments.ToArray();

            return(remoteInfo);
        }