public static WindowsPS GetCurrent(string connectionID) { if (Sessions.ContainsKey(connectionID)) { var winPS = Sessions[connectionID]; winPS.ProcessIdleTimeout.Stop(); winPS.ProcessIdleTimeout.Start(); return(winPS); } else { var winPS = new WindowsPS(); winPS.ConnectionID = connectionID; winPS.ProcessIdleTimeout = new System.Timers.Timer(600000); // 10 minutes. winPS.ProcessIdleTimeout.AutoReset = false; winPS.ProcessIdleTimeout.Elapsed += winPS.ProcessIdleTimeout_Elapsed; while (!Sessions.TryAdd(connectionID, winPS)) { Thread.Sleep(1000); } winPS.ProcessIdleTimeout.Start(); return(winPS); } }
private static async Task ExecuteCommand(string mode, string command, string commandID, string senderConnectionID) { if (!IsServerVerified) { Logger.Write($"Command attempted before server was verified. Mode: {mode}. Command: {command}. Sender: {senderConnectionID}"); Uninstaller.UninstallClient(); return; } try { switch (mode.ToLower()) { case "pscore": { var psCoreResult = PSCore.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonConvert.SerializeObject(psCoreResult); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { SendResultsViaAjax("PSCore", psCoreResult); await HubConnection.InvokeAsync("PSCoreResultViaAjax", commandID); } else { await HubConnection.InvokeAsync("PSCoreResult", psCoreResult); } break; } case "winps": if (OSUtils.IsWindows) { var result = WindowsPS.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonConvert.SerializeObject(result); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { SendResultsViaAjax("WinPS", result); await HubConnection.InvokeAsync("WinPSResultViaAjax", commandID); } else { await HubConnection.InvokeAsync("CommandResult", result); } } break; case "cmd": if (OSUtils.IsWindows) { var result = CMD.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonConvert.SerializeObject(result); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { SendResultsViaAjax("CMD", result); await HubConnection.InvokeAsync("CMDResultViaAjax", commandID); } else { await HubConnection.InvokeAsync("CommandResult", result); } } break; case "bash": if (OSUtils.IsLinux) { var result = Bash.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonConvert.SerializeObject(result); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { SendResultsViaAjax("Bash", result); } else { await HubConnection.InvokeAsync("CommandResult", result); } } break; default: break; } } catch (Exception ex) { Logger.Write(ex); await HubConnection.InvokeAsync("DisplayConsoleMessage", $"There was an error executing the command. It has been logged on the client machine.", senderConnectionID); } }