Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/autogenerate attack tactics visualization 95 #613

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions v2/tools/generate-coverage-matrices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"fmt"
"os"
"path/filepath"
"sort"

"github.com/datadog/stratus-red-team/v2/pkg/stratus"
)

func GenerateCoverageMatrices(index map[stratus.Platform]map[string][]*stratus.AttackTechnique, docsDirectory string) error {
// Process each platform in the index
for platform, tacticsMap := range index {
// Initialize the HTML content
htmlContent := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<title>%s Coverage Matrix</title>
<link rel="icon" type="image/png" href="logo.png">
<style>
body { font-family: Arial, sans-serif; margin: 20px;}
table { width: 100%%; border-collapse: collapse; margin: 20px 0; font-size: 12px}
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; color: #7e56c2}
th { background-color: #f4f4f4; font-weight: bold; font-size: 14px; color: #333; }
tr:nth-child(even) { background-color: #f9f9f9; }
tr:hover { background-color: #f1f1f1; }
td:hover { background-color: #e9e9ff; color: #5a3ea8; cursor: pointer;}
caption { font-size: 1.5em; margin-bottom: 10px; font-weight: bold; }
</style>
</head>
<body>
<h1>Stratus Red Team</h1>
<table>
<caption>Coverage Matrix for %s</caption>
`, platform, platform)

// Extract unique tactics
tacticsSet := make(map[string]struct{})
for tactic := range tacticsMap {
tacticsSet[tactic] = struct{}{}
}
tactics := make([]string, 0, len(tacticsSet))
for tactic := range tacticsSet {
tactics = append(tactics, tactic)
}
sort.Strings(tactics)

// Add header row
htmlContent += "<thead><tr>"
for _, tactic := range tactics {
htmlContent += fmt.Sprintf("<th>%s</th>", tactic)
}
htmlContent += "</tr></thead>\n<tbody>\n"

rows := make([][]string, 0)
maxRows := 0

// Map tactic to techniques
tacticToTechniques := make(map[string][]string)
for _, tactic := range tactics {
techniques := tacticsMap[tactic]
for _, technique := range techniques {
tacticToTechniques[tactic] = append(tacticToTechniques[tactic], technique.FriendlyName)
}
if len(tacticToTechniques[tactic]) > maxRows {
maxRows = len(tacticToTechniques[tactic])
}
}

// Fill rows with Stratus techniques for each ATT&CK tactic
for i := 0; i < maxRows; i++ {
row := make([]string, len(tactics))
for j, tactic := range tactics {
if i < len(tacticToTechniques[tactic]) {
row[j] = tacticToTechniques[tactic][i]
} else {
row[j] = ""
}
}
rows = append(rows, row)
}

// Add rows to the HTML table
for _, row := range rows {
htmlContent += "<tr>"
for _, cell := range row {
if cell != "" {
htmlContent += fmt.Sprintf("<td>%s</td>", cell)
} else {
htmlContent += "<td></td>"
}
}
htmlContent += "</tr>\n"
}

// Close table and HTML document
htmlContent += `
</tbody>
</table>
</body>
</html>`
filePath := filepath.Join(docsDirectory, fmt.Sprintf("%s.html", platform))
if err := os.WriteFile(filePath, []byte(htmlContent), 0644); err != nil {
return fmt.Errorf("failed to write HTML file for platform %s: %w", platform, err)
}
fmt.Printf("Generated coverage matrix for platform: %s\n", platform)
}

return nil
}
7 changes: 7 additions & 0 deletions v2/tools/generate-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func main() {
os.Exit(1)
}

// Generate HTML files for each platform
if err := GenerateCoverageMatrices(index, docsDirectory); err != nil {
fmt.Fprintln(os.Stderr, "Could not generate coverage matrices")
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}

// Write a single index file with all techniques. File is enconded in YAML.
yamlIndex := filepath.Join(docsDirectory, "index.yaml")
if err := GenerateYAML(yamlIndex, index); err != nil {
Expand Down
Loading