Beispiel #1
0
        public bool Execute(IIdentity identity)
        {
            if (identity is IEntity entity)
            {
                using (var dialog = new CreateEntityTemplateDialog(entity))
                {
                    if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                    {
                        entity.Model?.AddEntityTemplate(dialog.EntityName,
                                                        dialog.EntityDescription, dialog.BigImage, dialog.Image, dialog.SmallImage, entity);
                    }
                }
            }
            else if (identity is IDataFlow flow)
            {
                using (var dialog = new GenericIdentityCreationDialog())
                {
                    dialog.IdentityName        = flow.Name;
                    dialog.IdentityDescription = flow.Description;
                    dialog.IdentityTypeName    = "Flow Template";
                    if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                    {
                        var flowTemplate = flow.Model.AddFlowTemplate(dialog.IdentityName,
                                                                      dialog.IdentityDescription, flow);
                        flow.Model.AutoApplySchemas(flowTemplate);
                    }
                }
            }
            else if (identity is ITrustBoundary trustBoundary)
            {
                using (var dialog = new GenericIdentityCreationDialog())
                {
                    dialog.IdentityName        = trustBoundary.Name;
                    dialog.IdentityDescription = trustBoundary.Description;
                    dialog.IdentityTypeName    = "Trust Boundary Template";
                    if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                    {
                        var trustBoundaryTemplate = trustBoundary.Model.AddTrustBoundaryTemplate(dialog.IdentityName,
                                                                                                 dialog.IdentityDescription, trustBoundary);
                        trustBoundary.Model.AutoApplySchemas(trustBoundaryTemplate);
                    }
                }
            }

            return(true);
        }
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            string text    = null;
            bool   warning = false;

            try
            {
                switch (action.Name)
                {
                case "AddActor":
                    using (var dialog = new GenericIdentityCreationDialog())
                    {
                        dialog.IdentityTypeName = "Threat Actor";
                        if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                        {
                            text = "Add Threat Actor";
                            _model.AddThreatActor(dialog.IdentityName, dialog.IdentityDescription);
                        }
                    }
                    break;

                case "RemoveActor":
                    var selected = _grid.GetSelectedCells()?.OfType <GridCell>()
                                   .Select(x => x.GridRow)
                                   .Distinct()
                                   .ToArray();

                    if (_currentRow != null)
                    {
                        if ((selected?.Length ?? 0) > 1)
                        {
                            var outcome = MessageBox.Show(Form.ActiveForm,
                                                          $"You have selected {selected.Length} Threat Actors. Do you want to remove them all?\nPlease click 'Yes' to remove all selected Threat Actors,\nNo to remove only the last one you selected, '{_currentRow.Tag?.ToString()}'.\nPress Cancel to abort.",
                                                          "Remove Threat Actors", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
                                                          MessageBoxDefaultButton.Button3);
                            switch (outcome)
                            {
                            case DialogResult.Yes:
                                bool removed = true;
                                foreach (var row in selected)
                                {
                                    bool r = false;
                                    if (row.Tag is IThreatActor actor)
                                    {
                                        r = _model.RemoveThreatActor(actor.Id);
                                    }

                                    removed &= r;

                                    if (r && row == _currentRow)
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                    }
                                }

                                if (removed)
                                {
                                    text = "Remove Threat Actors";
                                }
                                else
                                {
                                    warning = true;
                                    text    = "One or more Threat Actors cannot be removed.";
                                }

                                break;

                            case DialogResult.No:
                                if (_currentRow != null && _currentRow.Tag is IThreatActor actor2)
                                {
                                    if (_model.RemoveThreatActor(actor2.Id))
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                        text             = "Remove Threat Actor";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Threat Actor cannot be removed.";
                                    }
                                }

                                break;
                            }
                        }
                        else if (_currentRow?.Tag is IThreatActor actor &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove Threat Actor '{actor.Name}'. Are you sure?",
                                                 "Remove Threat Actor", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (_model.RemoveThreatActor(actor.Id))
                            {
                                text             = "Remove Threat Actor";
                                _properties.Item = null;
                            }
                            else
                            {
                                warning = true;
                                text    = "The Threat Actor cannot be removed.";
                            }
                        }
                    }

                    break;

                case "Restore":
                    var initializer = new ActorsInitializer();
                    initializer.Initialize(_model);
                    text = "Threat Actors restore";
                    break;

                case "Refresh":
                    LoadModel();
                    break;
                }

                if (warning)
                {
                    ShowWarning?.Invoke(text);
                }
                else if (text != null)
                {
                    ShowMessage?.Invoke($"{text} has been executed successfully.");
                }
            }
            catch
            {
                ShowWarning?.Invoke($"An error occurred during the execution of the action.");
                throw;
            }
        }
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            string text    = null;
            bool   warning = false;

            try
            {
                switch (action.Name)
                {
                case "AddEntityTemplate":
                    using (var dialog = new CreateEntityTemplateDialog())
                    {
                        if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                        {
                            var entityTemplate = _model.AddEntityTemplate(dialog.EntityName,
                                                                          dialog.EntityDescription,
                                                                          dialog.BigImage,
                                                                          dialog.Image,
                                                                          dialog.SmallImage,
                                                                          dialog.EntityType);
                            _model.AutoApplySchemas(entityTemplate);
                            text = "Add Entity Template";
                        }
                    }
                    break;

                case "AddFlowTemplate":
                    using (var dialog = new GenericIdentityCreationDialog())
                    {
                        dialog.IdentityTypeName = "Flow Template";
                        if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                        {
                            var flowTemplate = _model.AddFlowTemplate(dialog.IdentityName,
                                                                      dialog.IdentityDescription);
                            _model.AutoApplySchemas(flowTemplate);
                            text = "Add Flow Template";
                        }
                    }
                    break;

                case "AddTrustBoundaryTemplate":
                    using (var dialog = new GenericIdentityCreationDialog())
                    {
                        dialog.IdentityTypeName = "Trust Boundary Template";
                        if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                        {
                            var trustBoundaryTemplate = _model.AddTrustBoundaryTemplate(dialog.IdentityName,
                                                                                        dialog.IdentityDescription);
                            _model.AutoApplySchemas(trustBoundaryTemplate);
                            text = "Add Trust Boundary Template";
                        }
                    }
                    break;

                case "RemoveItemTemplates":
                    var selected = _grid.GetSelectedCells()?.OfType <GridCell>()
                                   .Select(x => x.GridRow)
                                   .Distinct()
                                   .ToArray();

                    if (_currentRow != null)
                    {
                        if ((selected?.Length ?? 0) > 1)
                        {
                            var outcome = MessageBox.Show(Form.ActiveForm,
                                                          $"You have selected {selected.Length} Item Templates. Do you want to remove them all?\nPlease click 'Yes' to remove all selected Item Templates,\nNo to remove only the last one you selected, '{_currentRow.Tag?.ToString()}'.\nPress Cancel to abort.",
                                                          "Remove Item Templates", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
                                                          MessageBoxDefaultButton.Button3);
                            switch (outcome)
                            {
                            case DialogResult.Yes:
                                bool removed = true;
                                foreach (var row in selected)
                                {
                                    bool r = false;
                                    if (row.Tag is IEntityTemplate template)
                                    {
                                        r = _model.RemoveEntityTemplate(template.Id);
                                    }
                                    else if (row.Tag is IFlowTemplate flowTemplate)
                                    {
                                        r = _model.RemoveFlowTemplate(flowTemplate.Id);
                                    }
                                    else if (row.Tag is ITrustBoundaryTemplate trustBoundaryTemplate)
                                    {
                                        r = _model.RemoveTrustBoundaryTemplate(trustBoundaryTemplate.Id);
                                    }

                                    removed &= r;

                                    if (r && row == _currentRow)
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                    }
                                }

                                if (removed)
                                {
                                    text = "Remove Item Templates";
                                }
                                else
                                {
                                    warning = true;
                                    text    = "One or more Item Templates cannot be removed.";
                                }

                                break;

                            case DialogResult.No:
                                if (_currentRow?.Tag is IEntityTemplate template2)
                                {
                                    if (_model.RemoveEntityTemplate(template2.Id))
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                        text             = "Remove Entity Template";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Entity Template cannot be removed.";
                                    }
                                }
                                else if (_currentRow?.Tag is IFlowTemplate template3)
                                {
                                    if (_model.RemoveFlowTemplate(template3.Id))
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                        text             = "Remove Flow Template";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Flow Template cannot be removed.";
                                    }
                                }
                                else if (_currentRow?.Tag is ITrustBoundaryTemplate template4)
                                {
                                    if (_model.RemoveTrustBoundaryTemplate(template4.Id))
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                        text             = "Remove Trust Boundary Template";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Trust Boundary Template cannot be removed.";
                                    }
                                }

                                break;
                            }
                        }
                        else if (_currentRow?.Tag is IEntityTemplate template &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove Entity Template '{template.Name}'. Are you sure?",
                                                 "Remove Entity Template", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (_model.RemoveEntityTemplate(template.Id))
                            {
                                text             = "Remove Entity Template";
                                _properties.Item = null;
                            }
                            else
                            {
                                warning = true;
                                text    = "The Entity Template cannot be removed.";
                            }
                        }
                        else if (_currentRow?.Tag is IFlowTemplate flowTemplate &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove Flow Template '{flowTemplate.Name}'. Are you sure?",
                                                 "Remove Flow Template", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (_model.RemoveFlowTemplate(flowTemplate.Id))
                            {
                                text             = "Remove Flow Template";
                                _properties.Item = null;
                            }
                            else
                            {
                                warning = true;
                                text    = "The Flow Template cannot be removed.";
                            }
                        }
                        else if (_currentRow?.Tag is ITrustBoundaryTemplate trustBoundaryTemplate &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove Trust Boundary Template '{trustBoundaryTemplate.Name}'. Are you sure?",
                                                 "Remove Trust Boundary Template", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (_model.RemoveTrustBoundaryTemplate(trustBoundaryTemplate.Id))
                            {
                                text             = "Remove Trust Boundary Template";
                                _properties.Item = null;
                            }
                            else
                            {
                                warning = true;
                                text    = "The Trust Boundary Template cannot be removed.";
                            }
                        }
                    }
                    break;