Skip to content

Instantly share code, notes, and snippets.

@tetheredge
Last active June 21, 2016 22:39
Show Gist options
  • Save tetheredge/82a194be1b2d54d1bd0500d752fff7da to your computer and use it in GitHub Desktop.
Save tetheredge/82a194be1b2d54d1bd0500d752fff7da to your computer and use it in GitHub Desktop.
type Nvd struct {
Title string `xml:"nvd"`
}
type Entry struct {
XMLName xml.Name `xml:"entry"`
Type string `xml:"type,attr"`
Name string `xml:"name,attr"`
Sequence string `xml:"seq,attr"`
Published string `xml:"published,attr"`
Modified string `xml:"modified,attr"`
Severity string `xml:"severity,attr"`
CVSSVersion string `xml:"CVSS_version,attr"`
CVSSScore string `xml:"CVSS_score,attr"`
CVSSBaseScore string `xml:"CVSS_base_score,attr"`
CVSSImpactSubscore string `xml:"CVSS_impact_score,attr"`
CVSSExploitSubscore string `xml:"CVSS_exploit_subscore,attr"`
CVSSVector string `xml:"CVSS_vector,attr"`
LossTypes string `xml:"loss_types>avail>conf>int"`
Range string `xml:"range>network"`
Descriptions []string `xml:"desc>descript"`
References []Reference `xml:"refs>ref"`
Vulnerabilites []Vulnerability `xml:"vuln_soft"`
}
type Reference struct {
Source string `xml:"source,attr"`
Url string `xml:"url,attr"`
Adv string `xml:"adv,attr"`
Patch string `xml:"patch,attr"`
}
type Vulnerability struct {
Product []Product `xml:"prod"`
}
type Product struct {
Name string `xml:"name,attr"`
Vendor string `xml:"vendor,attr"`
Versions []Version `xml:"vers"`
}
type Version struct {
Number string `xml:"num,attr"`
Edition string `xml:"edition,attr"`
Prev string `xml:"prev,attr"`
}
type Config struct {
Nvd Nvd
Entrys []Entry `xml:"entry"`
}
func (c Config) GetFile(directory, name string) (string, error) {
os.Chdir(directory)
file, err := ioutil.ReadFile(name)
fileToString := string(file[:])
if err != nil {
return "", err
}
return fileToString, nil
}
func (c Config) ParseMetaFile(name string) (time.Time, error) {
output := string(name[:])
reader := csv.NewReader(strings.NewReader(output))
reader.Comma = ':'
reader.FieldsPerRecord = -1
records, err := reader.ReadAll()
timeStamp := records[0][1:]
timeString := strings.Join(timeStamp, ":")
parseTime, err := time.Parse(time.RFC3339, timeString)
if err != nil {
return time.Time{}, err
}
return parseTime, nil
}
func (c Config) XmlDecompose(name string) (Config, error) {
f, err := os.Open(name)
gzipFile, err := gzip.NewReader(f)
if err != nil {
return c, err
}
testXml := xml.NewDecoder(gzipFile)
err = testXml.Decode(&c)
if err != nil {
return c, err
}
return c, nil
}
func (c Config) GetNvdFiles(dir string) ([]string, error) {
nvdFiles := []string{}
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, file := range files {
name := file.Name()
if strings.HasPrefix(name, "nvdcve") {
nvdFiles = append(nvdFiles, name)
}
}
return nvdFiles, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment