Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Added node.js security working group as db
Browse files Browse the repository at this point in the history
  • Loading branch information
dgonzalez committed Feb 5, 2018
1 parent 940a0e5 commit f9175cc
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 49 deletions.
54 changes: 15 additions & 39 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@
[[constraint]]
name = "github.com/docker/docker"
version = "1.13.1"

[[constraint]]
branch = "v25"
name = "gopkg.in/libgit2/git2go.v25"

[[constraint]]
name = "github.com/mholt/archiver"
version = "2.0.0"
43 changes: 36 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"os"

"github.com/dgonzalez/gammaray/pathrunner"
"github.com/dgonzalez/gammaray/vulnfetcher/nodeswg"
"github.com/dgonzalez/gammaray/vulnfetcher/ossvulnfetcher"
)

// OSSIndexURL URL for OSSIndex. Is not a hardcoded value to facilitate testing.
const OSSIndexURL = "https://ossindex.net/v2.0/package"
const nodeswgURL = "https://github.com/nodejs/security-wg/archive/master.zip"

func main() {
if len(os.Args) < 2 {
Expand All @@ -23,19 +25,33 @@ func main() {
os.Exit(1)
}

fetcher := ossvulnfetcher.New(OSSIndexURL)
ossFetcher := ossvulnfetcher.New(OSSIndexURL)
err = ossFetcher.Fetch()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

nodeswgFetcher := nodeswg.New(nodeswgURL)
err = nodeswgFetcher.Fetch()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

for _, singlePackage := range packages {
vulnerabilities, err := fetcher.Test(singlePackage.Name, singlePackage.Version)
vulnerabilitiesOSS, err := ossFetcher.Test(singlePackage.Name, singlePackage.Version)
// vulnerabilitiesNodeSWG, err := nodeswgFetcher.Test()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

if len(vulnerabilities) > 0 {
fmt.Printf("Package: %s\n", singlePackage.Name)
for _, vulnerability := range vulnerabilities {
fmt.Printf("\t- Vulnerability:\n")
fmt.Printf("\t\t- CVE: %s\n\t\tTitle: %s\n\t\tVersions: %s\n\t\tMore Info: %s",
if len(vulnerabilitiesOSS) > 0 {
fmt.Printf("\tPackage: %s\n", singlePackage.Name)
for _, vulnerability := range vulnerabilitiesOSS {
fmt.Printf("\t\t- Vulnerability (OSS Index):\n")
fmt.Printf("\t\t\t- CVE: %s\n\t\tTitle: %s\n\t\tVersions: %s\n\t\tMore Info: [%s]\n",
vulnerability.CVE,
vulnerability.Title,
vulnerability.Versions,
Expand All @@ -44,6 +60,19 @@ func main() {
}
}

vulnerabilitiesNodeSWG, err := nodeswgFetcher.Test(singlePackage.Name, singlePackage.Version)
if len(vulnerabilitiesNodeSWG) > 0 {
fmt.Printf("\tPackage: %s\n", singlePackage.Name)
for _, vulnerability := range vulnerabilitiesNodeSWG {
fmt.Printf("\t\t- Vulnerability (Node Security Working Group):\n")
fmt.Printf("\t\t\t- CVE: %s\n\t\tTitle: %s\n\t\tVersions: %s\n\t\tMore Info: [%s]\n",
vulnerability.CVE,
vulnerability.Title,
vulnerability.Versions,
vulnerability.References,
)
}
}
}

}
105 changes: 105 additions & 0 deletions vulnfetcher/nodeswg/nodeswg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package nodeswg

import (
"encoding/json"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"

"github.com/dgonzalez/gammaray/vulnfetcher"
"github.com/mholt/archiver"
)

// Fetcher fetches node community vulnerabilities
type Fetcher struct {
DatabaseURL string
vulnerabilities []Vulnerability
}

// Vulnerability a single vulnerability
type Vulnerability struct {
Module string `json:"module_name"`
CVES []string `json:"cves"`
VulnerableVersions string `json:"vulnerable_versions"`
Title string `json:"title"`
References string `json:"references"`
Overview string `json:"overview"`
}

// New creates a NodeSWGFetcher
func New(URL string) *Fetcher {
return &Fetcher{URL, make([]Vulnerability, 0)}
}

// Fetch builds the database from nodeswg on github
func (n *Fetcher) Fetch() error {
destFilePath := path.Join(os.TempDir(), "nodeswg.zip")
unzipFolder := path.Join(os.TempDir(), "nodeswg")
vulnFolder := path.Join(unzipFolder, "security-wg-master", "vuln", "npm")

os.Mkdir(unzipFolder, os.ModePerm)

destFile, err := os.Create(destFilePath)
if err != nil {
return err
}
defer destFile.Close()

response, err := http.Get(n.DatabaseURL)
if err != nil {
return err
}
defer response.Body.Close()

_, err = io.Copy(destFile, response.Body)
if err != nil {
return err
}

archiver.Zip.Open(destFilePath, unzipFolder)

filepath.Walk(vulnFolder, func(path string, f os.FileInfo, err error) error {

if strings.HasSuffix(path, ".json") {
jsonFile, err := os.Open(path)
defer jsonFile.Close()
if err != nil {
return err
}

jsonParser := json.NewDecoder(jsonFile)
var nodeVulnerability Vulnerability
err = jsonParser.Decode(&nodeVulnerability)
if err != nil {
return err
}
n.vulnerabilities = append(n.vulnerabilities, nodeVulnerability)
}

return nil
})

return nil
}

// Test tests for a vulnerability
func (n *Fetcher) Test(module string, version string) ([]vulnfetcher.Vulnerability, error) {
var vulnerabilities []vulnfetcher.Vulnerability

for _, vulnerability := range n.vulnerabilities {
if module == vulnerability.Module {
vulnerabilities = append(vulnerabilities, vulnfetcher.Vulnerability{
CVE: strings.Join(vulnerability.CVES, " "),
Title: vulnerability.Title,
Description: vulnerability.Overview,
Versions: vulnerability.VulnerableVersions,
References: vulnerability.References,
})
}
}

return vulnerabilities, nil
}
5 changes: 3 additions & 2 deletions vulnfetcher/ossvulnfetcher/osindexfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func New(URL string) *OSSIndexFetcher {
}

// Fetch does nothing as it is API based. No need to download anything.
func (n *OSSIndexFetcher) Fetch() {
func (n *OSSIndexFetcher) Fetch() error {
// Nothing to do here. API based.
return nil
}

// Test tests for a package
Expand Down Expand Up @@ -81,7 +82,7 @@ func (n *OSSIndexFetcher) Test(name string, version string) ([]vulnfetcher.Vulne
Title: vulnerability.Title,
Description: vulnerability.Description,
Versions: strings.Join(vulnerability.Versions, " "),
References: "[ " + strings.Join(vulnerability.References, " ") + " ]\n",
References: strings.Join(vulnerability.References, " "),
}
vulnerabilities = append(vulnerabilities, processedVulnerability)
}
Expand Down
2 changes: 1 addition & 1 deletion vulnfetcher/vulnfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ type Vulnerability struct {

// VulnFetcher fetches vulnerabilities
type VulnFetcher interface {
Fetch()
Fetch() error
Test(component string, version string) ([]Vulnerability, error)
}

0 comments on commit f9175cc

Please sign in to comment.