コード例 #1
0
ファイル: ClientSocket.cs プロジェクト: resonancellc/DoXM
        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);
            }
        }