public async Task ExecuteCommandFromApi(string mode, string requestID, string command, string commandID, string senderUserName, HubConnection hubConnection) { try { switch (mode.ToLower()) { case "pscore": var psCoreResult = PSCore.GetCurrent(senderUserName).WriteInput(command, commandID); await SendResultsViaAjax("PSCore", psCoreResult); break; case "winps": if (EnvironmentHelper.IsWindows) { var result = WindowsPS.GetCurrent(senderUserName).WriteInput(command, commandID); await SendResultsViaAjax("WinPS", result); } break; case "cmd": if (EnvironmentHelper.IsWindows) { var result = CMD.GetCurrent(senderUserName).WriteInput(command, commandID); await SendResultsViaAjax("CMD", result); } break; case "bash": if (EnvironmentHelper.IsLinux) { var result = Bash.GetCurrent(senderUserName).WriteInput(command, commandID); await SendResultsViaAjax("Bash", result); } break; default: break; } await hubConnection.SendAsync("CommandResultViaApi", commandID, requestID); } catch (Exception ex) { Logger.Write(ex); } }
public static Bash GetCurrent(string connectionID) { if (Sessions.ContainsKey(connectionID)) { var bash = Sessions[connectionID]; bash.ProcessIdleTimeout.Stop(); bash.ProcessIdleTimeout.Start(); return(bash); } else { var bash = new Bash(); bash.ConnectionID = connectionID; bash.ProcessIdleTimeout = new System.Timers.Timer(600000); // 10 minutes. bash.ProcessIdleTimeout.AutoReset = false; bash.ProcessIdleTimeout.Elapsed += bash.ProcessIdleTimeout_Elapsed; Sessions.AddOrUpdate(connectionID, bash, (id, b) => bash); bash.ProcessIdleTimeout.Start(); return(bash); } }
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.UninstallAgent(); 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 device.", senderConnectionID); } }
public async Task ExecuteCommand(string mode, string command, string commandID, string senderConnectionID, HubConnection hubConnection) { try { switch (mode.ToLower()) { case "pscore": { var psCoreResult = PSCore.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonSerializer.Serialize(psCoreResult); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { await SendResultsViaAjax("PSCore", psCoreResult); await hubConnection.SendAsync("PSCoreResultViaAjax", commandID); } else { await hubConnection.SendAsync("PSCoreResult", psCoreResult); } break; } case "winps": if (EnvironmentHelper.IsWindows) { var result = WindowsPS.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonSerializer.Serialize(result); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { await SendResultsViaAjax("WinPS", result); await hubConnection.SendAsync("WinPSResultViaAjax", commandID); } else { await hubConnection.SendAsync("CommandResult", result); } } break; case "cmd": if (EnvironmentHelper.IsWindows) { var result = CMD.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonSerializer.Serialize(result); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { await SendResultsViaAjax("CMD", result); await hubConnection.SendAsync("CMDResultViaAjax", commandID); } else { await hubConnection.SendAsync("CommandResult", result); } } break; case "bash": if (EnvironmentHelper.IsLinux) { var result = Bash.GetCurrent(senderConnectionID).WriteInput(command, commandID); var serializedResult = JsonSerializer.Serialize(result); if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { await SendResultsViaAjax("Bash", result); await hubConnection.SendAsync("BashResultViaAjax", commandID); } else { await hubConnection.SendAsync("CommandResult", result); } } break; default: break; } } catch (Exception ex) { Logger.Write(ex); await hubConnection.SendAsync("DisplayMessage", "There was an error executing the command. It has been logged on the client device.", "Error executing command.", senderConnectionID); } }