internal MessageHandler()
 {
     _pipeServer = new NamedPipeServer(ConstantsEnums.DllToClientNamedPipeName);                                 // for connection from dll to this client. We create and own the pipe
     _pipeClient = new NamedPipeClient(ConstantsEnums.ClientToDllNamedPipeName);                                 // for connection from this client to dll. Dll creates and owns the pipe
     _pipeServer.MessageReceived             += _pipeServer_MessageReceived;
     _pipeServer.ClientConnectionEstablished += _pipeServer_ClientConnectionEstablished;
     _pipeClient.ConnectedToPipe             += _pipeClient_ConnectedToPipe;
     LogHandlerSingleton.Instance().LogLine("Named pipe enabled.", "System", true, true);
 }
        private void HandleNamedPipeMessageReceived(ContainerEventArgs <byte[]> e)
        {
            if (e.Value.Length < 2)
            {
                // not usable
                return;
            }

            // data starts at offset 1 or later, as the first byte is the message type.
            var asciiEncoding = new ASCIIEncoding();

            switch (e.Value[0])
            {
            case MessageType.Action:
                break;

            case MessageType.Notification:
                var notificationText = asciiEncoding.GetString(e.Value, 1, e.Value.Length - 1);
                LogHandlerSingleton.Instance().LogLine(notificationText, string.Empty);
                if (this.DisplayNotifications)
                {
                    this.NotificationLogFunc?.Invoke(notificationText);
                }
                break;

            case MessageType.NormalTextMessage:
                LogHandlerSingleton.Instance().LogLine(asciiEncoding.GetString(e.Value, 1, e.Value.Length - 1), string.Empty);
                break;

            case MessageType.DebugTextMessage:
                LogHandlerSingleton.Instance().LogLine(asciiEncoding.GetString(e.Value, 1, e.Value.Length - 1), "Camera dll", true);
                break;

            case MessageType.ErrorTextMessage:
                LogHandlerSingleton.Instance().LogLine(asciiEncoding.GetString(e.Value, 1, e.Value.Length - 1), "Camera dll", false, true);
                break;
                // rest are ignored.
            }
        }
Beispiel #3
0
 /// <inheritdoc/>
 public virtual void SendValueAsMessage()
 {
     MessageHandlerSingleton.Instance().SendSettingMessage(this.ID, GeneralUtils.ConvertToByteArray(this.Value));
     LogHandlerSingleton.Instance().LogLine("Setting value sent: ID: {0}. Value: {1}", "Setting", true, true, this.ID, this.GetValueAsString());
 }
Beispiel #4
0
        /// <summary>
        /// Injects the dll with the path specified into the process with the id specified.
        /// </summary>
        /// <param name="processId">the PID of the process to inject the dll into.</param>
        /// <param name="dllPathNameToInject">The full path + filename of the dll to inject</param>
        /// <returns>true if succeeded, false otherwise. If false is returned, <see cref="LastError"/> is set with the error code.</returns>
        public bool PerformInjection(int processId, string dllPathNameToInject)
        {
            this.LastError = 0;

            uint dllLengthToPassInBytes = (uint)((dllPathNameToInject.Length + 1) * Marshal.SizeOf(typeof(char)));

            this.LastActionPerformed = "Opening the host process";
            LogHandlerSingleton.Instance().LogLine("Opening the host process", "DllInjector", true);
            IntPtr processHandle = Win32Wrapper.OpenProcess(ProcessAccessFlags.CreateThread | ProcessAccessFlags.QueryInformation | ProcessAccessFlags.VirtualMemoryOperation |
                                                            ProcessAccessFlags.VirtualMemoryWrite | ProcessAccessFlags.VirtualMemoryRead,
                                                            false, (uint)processId);

            if (processHandle == IntPtr.Zero)
            {
                // failed, so set the error code and return
                this.LastError = Marshal.GetLastWin32Error();
                return(false);
            }

            this.LastActionPerformed = "Obtaining the address of LoadLibraryA";
            LogHandlerSingleton.Instance().LogLine("Obtaining the address of LoadLibraryA", "DllInjector", true);
            IntPtr loadLibraryAddress = Win32Wrapper.GetProcAddress(Win32Wrapper.GetModuleHandle("kernel32.dll"), "LoadLibraryA");

            if (loadLibraryAddress == IntPtr.Zero)
            {
                this.LastError = Marshal.GetLastWin32Error();
                return(false);
            }

            this.LastActionPerformed = "Allocating memory in the host process for the dll filename";
            LogHandlerSingleton.Instance().LogLine("Allocating memory in the host process for the dll filename", "DllInjector", true);
            IntPtr memoryInTargetProcess = Win32Wrapper.VirtualAllocEx(processHandle, IntPtr.Zero, dllLengthToPassInBytes, AllocationType.Commit | AllocationType.Reserve,
                                                                       MemoryProtection.ReadWrite);

            if (memoryInTargetProcess == IntPtr.Zero)
            {
                this.LastError = Marshal.GetLastWin32Error();
                return(false);
            }

            Thread.Sleep(500);

            this.LastActionPerformed = "Writing dll filename into memory allocated in host process";
            LogHandlerSingleton.Instance().LogLine("Writing dll filename into memory allocated in host process", "DllInjector", true);
            var  bytesToWrite = Encoding.Default.GetBytes(dllPathNameToInject);
            bool result       = Win32Wrapper.WriteProcessMemory(processHandle, memoryInTargetProcess, bytesToWrite, dllLengthToPassInBytes, out var bytesWritten);

            if (!result || (bytesWritten.ToInt32() != bytesToWrite.Length + 1))
            {
                this.LastError = Marshal.GetLastWin32Error();
                return(false);
            }

            this.LastActionPerformed = "Creating a thread in the host process to load the dll";
            LogHandlerSingleton.Instance().LogLine("Creating a thread in the host process to load the dll", "DllInjector", true);
            IntPtr remoteThreadHandle = Win32Wrapper.CreateRemoteThread(processHandle, IntPtr.Zero, 0, loadLibraryAddress, memoryInTargetProcess, 0, IntPtr.Zero);

            if (remoteThreadHandle == IntPtr.Zero)
            {
                this.LastError = Marshal.GetLastWin32Error();
                return(false);
            }
            // no clean up of the memory, we're not going to 'unload' the dll...
            result = Win32Wrapper.CloseHandle(processHandle);
            if (!result)
            {
                this.LastError = Marshal.GetLastWin32Error();
                return(false);
            }

            this.LastActionPerformed = "Done";
            LogHandlerSingleton.Instance().LogLine("Injection completed", "DllInjector", true);
            return(true);
        }