示例#1
0
        // POST: /grunttasking/create
        public async Task <IActionResult> Create(GruntTasking tasking)
        {
            try
            {
                CovenantUser currentUser = await _context.GetCurrentUser(_userManager, HttpContext.User);

                tasking.Grunt = await _context.GetGrunt(tasking.GruntId);

                tasking.GruntTask = await _context.GetGruntTask(tasking.GruntTaskId);

                GruntCommand createdCommand = await _context.CreateGruntCommand(new GruntCommand
                {
                    Command         = GetCommand(tasking),
                    CommandTime     = DateTime.UtcNow,
                    CommandOutputId = 0,
                    CommandOutput   = new CommandOutput(),
                    User            = currentUser,
                    GruntId         = tasking.Grunt.Id,
                    Grunt           = tasking.Grunt
                }, _grunthub);

                tasking.GruntCommand   = createdCommand;
                tasking.GruntCommandId = createdCommand.Id;

                GruntTasking created = await _context.CreateGruntTasking(tasking);

                return(RedirectToAction(nameof(Interact), new { id = created.Id }));
            }
            catch (Exception e) when(e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
            {
                return(RedirectToAction(nameof(Index)));
            }
        }
示例#2
0
        // POST: /grunttasking/create
        public async Task <IActionResult> Create(GruntTasking tasking)
        {
            try
            {
                CovenantUser currentUser = await _context.GetCurrentUser(_userManager, HttpContext.User);

                tasking.Grunt = await _context.GetGrunt(tasking.GruntId);

                tasking.GruntTask = await _context.GetGruntTask(tasking.GruntTaskId);

                tasking.Type = tasking.GruntTask.TaskingType;
                for (int i = 0; i < Math.Min(tasking.Parameters.Count, tasking.GruntTask.Options.Count); i++)
                {
                    if (tasking.Parameters[i] == null)
                    {
                        tasking.Parameters[i] = "";
                        tasking.GruntTask.Options[i].Value = "";
                    }
                    else
                    {
                        tasking.GruntTask.Options[i].Value = tasking.Parameters[i];
                    }
                }
                for (int i = tasking.Parameters.Count; i < tasking.GruntTask.Options.Count; i++)
                {
                    tasking.GruntTask.Options[i].Value = "";
                }
                GruntCommand createdCommand = await _context.CreateGruntCommand(new GruntCommand
                {
                    Command         = GetCommand(tasking),
                    CommandTime     = DateTime.UtcNow,
                    CommandOutputId = 0,
                    CommandOutput   = new CommandOutput(),
                    User            = currentUser,
                    GruntId         = tasking.Grunt.Id,
                    Grunt           = tasking.Grunt
                }, _grunthub, _eventhub);

                tasking.GruntCommand   = createdCommand;
                tasking.GruntCommandId = createdCommand.Id;

                GruntTasking created = await _context.CreateGruntTasking(tasking, _grunthub);

                return(RedirectToAction(nameof(Interact), new { id = created.Id }));
            }
            catch (Exception e) when(e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
            {
                return(RedirectToAction(nameof(Index)));
            }
        }
示例#3
0
        public async Task <ActionResult <GruntCommand> > CreateGruntCommand([FromBody] GruntCommand gruntCommand)
        {
            try
            {
                gruntCommand.Grunt = await _context.GetGrunt(gruntCommand.GruntId);

                GruntCommand createdCommand = await _context.CreateGruntCommand(gruntCommand, _grunthub, _eventhub);

                return(CreatedAtRoute(nameof(GetGruntCommand), new { id = createdCommand.Id }, createdCommand));
            }
            catch (ControllerNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (ControllerBadRequestException e)
            {
                return(BadRequest(e.Message));
            }
        }
示例#4
0
        public async Task <GruntCommand> Input(CovenantUser user, Grunt grunt, string UserInput)
        {
            GruntCommand GruntCommand = await _context.CreateGruntCommand(new GruntCommand
            {
                Command         = GetCommandFromInput(UserInput),
                CommandTime     = DateTime.UtcNow,
                User            = user,
                GruntId         = grunt.Id,
                Grunt           = grunt,
                CommandOutputId = 0,
                CommandOutput   = new CommandOutput()
            }, _grunthub, _eventhub);

            List <ParsedParameter> parameters = ParseParameters(UserInput).ToList();
            GruntTask commandTask             = null;

            try
            {
                commandTask = await _context.GetGruntTaskByName(parameters.FirstOrDefault().Value);

                if (commandTask.Options.Count == 1 && new List <string> {
                    "Command", "ShellCommand", "PowerShellCommand", "Code"
                }.Contains(commandTask.Options[0].Name))
                {
                    parameters = new List <ParsedParameter>
                    {
                        new ParsedParameter
                        {
                            Value = commandTask.Name, Label = "", IsLabeled = false, Position = 0
                        },
                        new ParsedParameter
                        {
                            Value = UserInput.Substring(UserInput.IndexOf(" ", StringComparison.Ordinal) + 1).Trim('"'),
                            Label = "", IsLabeled = false, Position = 0
                        }
                    };
                }
            }
            catch (ControllerNotFoundException) { }

            string output = "";

            if (parameters.FirstOrDefault().Value.ToLower() == "show")
            {
                output = await Show(grunt);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "help")
            {
                output = await Help(parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "sharpshell")
            {
                output = await SharpShell(grunt, GruntCommand, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "kill")
            {
                output = await Kill(grunt, GruntCommand, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "set")
            {
                output = await Set(grunt, GruntCommand, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "history")
            {
                output = await History(grunt, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "connect")
            {
                output = await Connect(grunt, GruntCommand, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "disconnect")
            {
                output = await Disconnect(grunt, GruntCommand, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "jobs")
            {
                output = await Jobs(grunt, GruntCommand, parameters);
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "note")
            {
                grunt.Note = string.Join(" ", parameters.Skip(1).Select(P => P.Value).ToArray());
                await _context.EditGrunt(grunt, user, _grunthub, _eventhub);

                output = "Note: " + grunt.Note;
            }
            else if (parameters.FirstOrDefault().Value.ToLower() == "powershellimport")
            {
                grunt.PowerShellImport = parameters.Count >= 2 ? parameters[1].Value : "";
                await _context.EditGrunt(grunt, user, _grunthub, _eventhub);

                output = "PowerShell Imported";
            }
            else if (commandTask != null)
            {
                parameters = parameters.Skip(1).ToList();
                if (parameters.Count() < commandTask.Options.Count(O => !O.Optional))
                {
                    _context.Entry(GruntCommand).State = EntityState.Detached;
                    GruntCommand.CommandOutput.Output  = EliteConsole.PrintFormattedErrorLine(GetUsage(commandTask));
                    return(await _context.EditGruntCommand(GruntCommand, _grunthub, _eventhub));
                }
                // All options begin unassigned
                List <bool> OptionAssignments = commandTask.Options.Select(O => false).ToList();
                commandTask.Options.ForEach(O => O.Value = "");
                for (int i = 0; i < parameters.Count; i++)
                {
                    if (parameters[i].IsLabeled)
                    {
                        var option = commandTask.Options.FirstOrDefault(O => O.Name.Equals(parameters[i].Label, StringComparison.OrdinalIgnoreCase));
                        option.Value = parameters[i].Value;
                        OptionAssignments[commandTask.Options.IndexOf(option)] = true;
                    }
                    else
                    {
                        GruntTaskOption nextOption = null;
                        // Find next unassigned option
                        for (int j = 0; j < commandTask.Options.Count; j++)
                        {
                            if (!OptionAssignments[j])
                            {
                                nextOption           = commandTask.Options[j];
                                OptionAssignments[j] = true;
                                break;
                            }
                        }
                        if (nextOption == null)
                        {
                            // This is an extra parameter
                            _context.Entry(GruntCommand).State = EntityState.Detached;
                            GruntCommand.CommandOutput.Output  = EliteConsole.PrintFormattedErrorLine(GetUsage(commandTask));
                            return(await _context.EditGruntCommand(GruntCommand, _grunthub, _eventhub));
                        }
                        nextOption.Value = parameters[i].Value;
                    }
                }

                // Check for unassigned required options
                for (int i = 0; i < commandTask.Options.Count; i++)
                {
                    if (!OptionAssignments[i] && !commandTask.Options[i].Optional)
                    {
                        // This is an extra parameter
                        StringBuilder toPrint = new StringBuilder();
                        toPrint.Append(EliteConsole.PrintFormattedErrorLine(commandTask.Options[i].Name + " is required."));
                        toPrint.Append(EliteConsole.PrintFormattedErrorLine(GetUsage(commandTask)));
                        _context.Entry(GruntCommand).State = EntityState.Detached;
                        GruntCommand.CommandOutput.Output  = toPrint.ToString();
                        return(await _context.EditGruntCommand(GruntCommand, _grunthub, _eventhub));
                    }
                }
                // Parameters have parsed successfully
                commandTask = await _context.EditGruntTask(commandTask);
                await StartTask(grunt, commandTask, GruntCommand);
            }
            else
            {
                output = EliteConsole.PrintFormattedErrorLine("Unrecognized command");
            }
            _context.Entry(GruntCommand).State = EntityState.Detached;
            GruntCommand.CommandOutput.Output  = output;
            return(await _context.EditGruntCommand(GruntCommand, _grunthub, _eventhub));
        }