Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 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;
         Sessions.AddOrUpdate(connectionID, winPS, (id, w) => winPS);
         winPS.ProcessIdleTimeout.Start();
         return(winPS);
     }
 }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        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);
            }
        }