-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(cwe, cti): update dictionary #1553
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for CWE en package main
import (
"archive/zip"
"bytes"
"encoding/xml"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
)
type weaknessCatalog struct {
Weaknesses []weakness `xml:"Weaknesses>Weakness"`
}
type weakness struct {
ID string `xml:"ID,attr"`
Name string `xml:"Name,attr"`
Description string `xml:"Description"`
ExtendedDescription string `xml:"Extended_Description"`
}
func main() {
if err := exec(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func exec() error {
resp, err := http.Get("https://cwe.mitre.org/data/xml/cwec_latest.xml.zip")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to fetch https://cwe.mitre.org/data/xml/cwec_latest.xml.zip")
}
bs, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
r, err := zip.NewReader(bytes.NewReader(bs), int64(len(bs)))
if err != nil {
return err
}
f, err := r.File[0].Open()
if err != nil {
return err
}
defer f.Close()
var catalog weaknessCatalog
if err := xml.NewDecoder(f).Decode(&catalog); err != nil {
return err
}
fmt.Println("// CweDictEn is the Cwe dictionary (https://cwe.mitre.org/data/xml/cwec_latest.xml.zip)")
fmt.Println("var CweDictEn = map[string]Cwe{")
for _, w := range catalog.Weaknesses {
fmt.Printf(" %s: {\n", strconv.Quote(w.ID))
fmt.Printf(" CweID: %s,\n", strconv.Quote(w.ID))
fmt.Printf(" Name: %s,\n", strconv.Quote(w.Name))
fmt.Printf(" Description: %s,\n", strconv.Quote(strip(w.Description)))
fmt.Printf(" ExtendedDescription: %s,\n", strconv.Quote(strip(w.ExtendedDescription)))
fmt.Printf(" Lang: %s,\n", strconv.Quote("en"))
fmt.Println(" },")
}
fmt.Println("}")
return nil
}
var rep = regexp.MustCompile(`\s{2,}`)
func strip(s string) string {
return strings.TrimSpace(rep.ReplaceAllString(strings.NewReplacer("\t", " ", "\n", " ").Replace(s), " "))
} |
for CWE ja package main
import (
"encoding/xml"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
type feed struct {
Item []struct {
References []struct {
Source string `xml:"source,attr"`
ID string `xml:"id,attr"`
Title string `xml:"title,attr"`
} `xml:"references"`
} `xml:"item"`
}
func main() {
if err := exec(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func exec() error {
urls := []string{"https://jvndb.jvn.jp/ja/rss/jvndb.rdf", "https://jvndb.jvn.jp/ja/rss/jvndb_new.rdf"}
for y := 2002; y <= time.Now().Year(); y++ {
urls = append(urls, fmt.Sprintf("https://jvndb.jvn.jp/ja/rss/years/jvndb_%d.rdf", y))
}
cwes := map[string]string{}
for _, u := range urls {
if err := func() error {
resp, err := http.Get(u)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to fetch %s", u)
}
var f feed
if err := xml.NewDecoder(resp.Body).Decode(&f); err != nil {
return err
}
for _, i := range f.Item {
for _, r := range i.References {
if !strings.HasPrefix(r.ID, "CWE-") {
continue
}
cwes[strings.TrimPrefix(r.ID, "CWE-")] = r.Title
}
}
return nil
}(); err != nil {
return err
}
}
fmt.Println("// CweDictJa is the Cwe dictionary")
fmt.Println("var CweDictJa = map[string]Cwe{")
ids := maps.Keys(cwes)
slices.Sort(ids)
for _, id := range ids {
fmt.Printf(" %s: {\n", strconv.Quote(id))
fmt.Printf(" CweID: %s,\n", strconv.Quote(id))
fmt.Printf(" Name: %s,\n", strconv.Quote(cwes[id]))
fmt.Printf(" Description: %s,\n", strconv.Quote(""))
fmt.Printf(" ExtendedDescription: %s,\n", strconv.Quote(""))
fmt.Printf(" Lang: %s,\n", strconv.Quote("ja"))
fmt.Println(" },")
}
fmt.Println("}")
return nil
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What did you implement:
update dictionary. (en: CWE v4.9, ja: 2022-10-31, MITRE ATT&CK: v12)
Type of change
How Has This Been Tested?
Checklist:
You don't have to satisfy all of the following.
make fmt
make test
Is this ready for review?: YES
Reference