|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/datadog/stratus-red-team/v2/pkg/stratus" |
| 9 | +) |
| 10 | + |
| 11 | +// Define MITRE ATT&CK tactic order from MITRE website |
| 12 | +var mitreTacticOrder = []string{ |
| 13 | + "Reconnaissance", |
| 14 | + "Resource Development", |
| 15 | + "Initial Access", |
| 16 | + "Execution", |
| 17 | + "Persistence", |
| 18 | + "Privilege Escalation", |
| 19 | + "Defense Evasion", |
| 20 | + "Credential Access", |
| 21 | + "Discovery", |
| 22 | + "Lateral Movement", |
| 23 | + "Collection", |
| 24 | + "Command and Control", |
| 25 | + "Exfiltration", |
| 26 | + "Impact", |
| 27 | +} |
| 28 | + |
| 29 | +// GenerateCoverageMatrices generates a single static .md file containing MITRE ATT&CK coverage tables split by platform |
| 30 | +func GenerateCoverageMatrices(index map[stratus.Platform]map[string][]*stratus.AttackTechnique, docsDirectory string) error { |
| 31 | + outputFilePath := filepath.Join(docsDirectory, "attack-techniques", "mitre-attack-coverage-matrices.md") |
| 32 | + |
| 33 | + if err := os.MkdirAll(filepath.Dir(outputFilePath), 0755); err != nil { |
| 34 | + return fmt.Errorf("failed to create docs directory: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + if _, err := os.Stat(outputFilePath); err == nil { |
| 38 | + if err := os.Remove(outputFilePath); err != nil { |
| 39 | + return fmt.Errorf("failed to delete existing file: %w", err) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + file, err := os.Create(outputFilePath) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed to create file: %w", err) |
| 46 | + } |
| 47 | + defer file.Close() |
| 48 | + |
| 49 | + htmlContent := ` |
| 50 | +<style> |
| 51 | + .table-container { |
| 52 | + overflow-x: auto; /* Enables horizontal scrolling */ |
| 53 | + max-width: 80%; /* Ensures it doesn't go beyond the page */ |
| 54 | + border: 1px solid #ddd; |
| 55 | + padding: 10px; |
| 56 | + margin-bottom: 20px; |
| 57 | + } |
| 58 | + table { |
| 59 | + width: 100%; |
| 60 | + border-collapse: collapse; |
| 61 | + margin: 20px 0; |
| 62 | + font-size: 16px; |
| 63 | + white-space: nowrap; /* Prevents text wrapping in cells */ |
| 64 | + } |
| 65 | + th, td { |
| 66 | + border: 1px solid #ddd; |
| 67 | + padding: 8px; |
| 68 | + text-align: center; |
| 69 | + } |
| 70 | + .md-sidebar.md-sidebar--secondary { display: none; } |
| 71 | + .md-content { min-width: 100%; } |
| 72 | +</style> |
| 73 | +
|
| 74 | +# MITRE ATT&CK Coverage by Platform |
| 75 | +
|
| 76 | +This provides coverage matrices of MITRE ATT&CK tactics and techniques currently covered by Stratus Red Team for different cloud platforms. |
| 77 | +` |
| 78 | + |
| 79 | + // Loop through each platform and generate tables |
| 80 | + for platform, tacticsMap := range index { |
| 81 | + htmlContent += fmt.Sprintf("<h2 style=\"text-transform: uppercase;\">%s</h2>\n", platform) |
| 82 | + htmlContent += `<div class="table-container">` // Add scrollable div |
| 83 | + |
| 84 | + // Start the table |
| 85 | + htmlContent += "<table>\n" |
| 86 | + |
| 87 | + // Sort tactics based on MITRE ATT&CK order |
| 88 | + sortedTactics := []string{} |
| 89 | + tacticSet := make(map[string]bool) |
| 90 | + |
| 91 | + // Collect tactics present for specific platform |
| 92 | + for tactic := range tacticsMap { |
| 93 | + tacticSet[tactic] = true |
| 94 | + } |
| 95 | + |
| 96 | + // Append tactics in the correct order |
| 97 | + for _, tactic := range mitreTacticOrder { |
| 98 | + if tacticSet[tactic] { |
| 99 | + sortedTactics = append(sortedTactics, tactic) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Add table header with sorted tactics |
| 104 | + htmlContent += "<thead><tr>" |
| 105 | + for _, tactic := range sortedTactics { |
| 106 | + htmlContent += fmt.Sprintf("<th>%s</th>", tactic) |
| 107 | + } |
| 108 | + htmlContent += "</tr></thead>\n<tbody>\n" |
| 109 | + |
| 110 | + // Create a list of rows |
| 111 | + rows := make([][]string, 0) |
| 112 | + maxRows := 0 |
| 113 | + |
| 114 | + // Map each tactic to its respective techniques (column by column) |
| 115 | + tacticToTechniques := make(map[string][]string) |
| 116 | + for _, tactic := range sortedTactics { |
| 117 | + techniques := tacticsMap[tactic] |
| 118 | + for _, technique := range techniques { |
| 119 | + platform, _ := technique.Platform.FormatName() |
| 120 | + cellText := fmt.Sprintf("<a href=\"%s\">%s</a>", fmt.Sprintf("../%s/%s", platform, technique.ID), technique.FriendlyName) |
| 121 | + tacticToTechniques[tactic] = append(tacticToTechniques[tactic], cellText) |
| 122 | + } |
| 123 | + if len(tacticToTechniques[tactic]) > maxRows { |
| 124 | + maxRows = len(tacticToTechniques[tactic]) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Fill rows with techniques for each tactic |
| 129 | + for i := 0; i < maxRows; i++ { |
| 130 | + row := make([]string, len(sortedTactics)) |
| 131 | + for j, tactic := range sortedTactics { |
| 132 | + if i < len(tacticToTechniques[tactic]) { |
| 133 | + row[j] = tacticToTechniques[tactic][i] |
| 134 | + } else { |
| 135 | + row[j] = "" |
| 136 | + } |
| 137 | + } |
| 138 | + rows = append(rows, row) |
| 139 | + } |
| 140 | + |
| 141 | + // Add rows to the HTML table |
| 142 | + for _, row := range rows { |
| 143 | + htmlContent += "<tr>" |
| 144 | + for _, cell := range row { |
| 145 | + if cell != "" { |
| 146 | + htmlContent += fmt.Sprintf("<td>%s</td>", cell) |
| 147 | + } else { |
| 148 | + htmlContent += "<td></td>" |
| 149 | + } |
| 150 | + } |
| 151 | + htmlContent += "</tr>\n" |
| 152 | + } |
| 153 | + |
| 154 | + htmlContent += "</tbody>\n</table>\n</div>\n" // Close scrollable div |
| 155 | + } |
| 156 | + |
| 157 | + // Write to Markdown file |
| 158 | + if _, err := file.WriteString(htmlContent); err != nil { |
| 159 | + return fmt.Errorf("failed to write to file: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + fmt.Printf("Generated MITRE ATT&CK coverage markdown file: %s\n", outputFilePath) |
| 163 | + return nil |
| 164 | +} |
0 commit comments