2023-01-18 03:04:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Classification struct {
|
|
|
|
CVSSScore string `yaml:"cvss-score,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Info struct {
|
2023-09-26 04:37:37 +00:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Severity string `yaml:"severity"`
|
|
|
|
Description string `yaml:"description"`
|
2023-01-18 03:04:11 +00:00
|
|
|
Classification Classification `yaml:"classification,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Data struct {
|
2023-09-26 04:37:37 +00:00
|
|
|
ID string `yaml:"id"`
|
|
|
|
Info Info `yaml:"info"`
|
|
|
|
FilePath string `json:"file_path"`
|
2023-01-18 03:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) != 3 {
|
2023-09-26 04:37:37 +00:00
|
|
|
fmt.Println("Usage: go run main.go <directory1[,directory2,...]> <output_file>")
|
2023-01-18 03:04:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-09-26 04:37:37 +00:00
|
|
|
input := os.Args[1]
|
2023-01-18 03:04:11 +00:00
|
|
|
outputFile := os.Args[2]
|
2023-09-26 04:37:37 +00:00
|
|
|
var directories []string
|
|
|
|
|
|
|
|
// Check if the input contains a comma
|
|
|
|
if strings.Contains(input, ",") {
|
|
|
|
directories = strings.Split(input, ",")
|
|
|
|
} else {
|
|
|
|
directories = []string{input}
|
|
|
|
}
|
2023-01-18 03:04:11 +00:00
|
|
|
|
|
|
|
var data []Data
|
|
|
|
|
2023-09-26 04:37:37 +00:00
|
|
|
for _, directory := range directories {
|
|
|
|
fmt.Println("Generating data for", directory)
|
|
|
|
|
|
|
|
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error accessing path %s: %v\n", path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
|
|
|
|
yamlFile, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error reading YAML file %s: %v\n", path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var d Data
|
|
|
|
err = yaml.Unmarshal(yamlFile, &d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error unmarshalling YAML file %s: %v\n", path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.Info.Classification.CVSSScore == "" {
|
|
|
|
d.Info.Classification.CVSSScore = "N/A"
|
|
|
|
}
|
|
|
|
if d.Info.Classification == (Classification{}) {
|
|
|
|
d.Info.Classification.CVSSScore = "N/A"
|
|
|
|
}
|
|
|
|
fpath := strings.Replace(path, "/home/runner/work/nuclei-templates/nuclei-templates/", "", 1)
|
|
|
|
d.FilePath = fpath
|
|
|
|
|
|
|
|
data = append(data, d)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error reading directory: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonData []byte
|
|
|
|
for _, d := range data {
|
|
|
|
temp, err := json.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error marshalling JSON: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
jsonData = append(jsonData, temp...)
|
|
|
|
jsonData = append(jsonData, byte('\n'))
|
|
|
|
}
|
|
|
|
err := ioutil.WriteFile(outputFile, jsonData, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error writing JSON data to file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("JSON data written to", outputFile)
|
|
|
|
}
|