-
Notifications
You must be signed in to change notification settings - Fork 200
/
revert_cmd.go
82 lines (73 loc) · 2.34 KB
/
revert_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"errors"
"github.com/datadog/stratus-red-team/v2/pkg/stratus"
"github.com/datadog/stratus-red-team/v2/pkg/stratus/runner"
"log"
"os"
"github.com/spf13/cobra"
)
var revertForce bool
func buildRevertCmd() *cobra.Command {
detonateCmd := &cobra.Command{
Use: "revert attack-technique-id [attack-technique-id]...",
Short: "Revert the detonation of an attack technique",
Example: "stratus revert aws.defense-evasion.cloudtrail-stop",
DisableFlagsInUseLine: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("you must specify at least one attack technique")
}
_, err := resolveTechniques(args)
return err
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getTechniquesCompletion(toComplete), cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
techniques, _ := resolveTechniques(args)
doRevertCmd(techniques)
},
}
detonateCmd.Flags().BoolVarP(&revertForce, "force", "f", false, "Force attempt to reverting even if the technique is not in the DETONATED state")
return detonateCmd
}
func doRevertCmd(techniques []*stratus.AttackTechnique) {
VerifyPlatformRequirements(techniques)
workerCount := len(techniques)
techniquesChan := make(chan *stratus.AttackTechnique, workerCount)
errorsChan := make(chan error, workerCount)
// Create workers
for i := 0; i < workerCount; i++ {
go revertCmdWorker(techniquesChan, errorsChan)
}
// Send attack techniques to revert
for i := range techniques {
techniquesChan <- techniques[i]
}
close(techniquesChan)
hadError := handleErrorsChannel(errorsChan, workerCount)
doStatusCmd(techniques)
if hadError {
os.Exit(1)
}
}
func revertCmdWorker(techniques <-chan *stratus.AttackTechnique, errors chan<- error) {
for technique := range techniques {
if technique.Revert == nil {
log.Println("Warning: " + technique.ID + " has no revert function and cannot be reverted.")
errors <- nil
continue
}
stratusRunner := runner.NewRunner(technique, revertForce)
err := stratusRunner.Revert()
errors <- err
}
}