Skip to content

Commit

Permalink
fixed errors
Browse files Browse the repository at this point in the history
  • Loading branch information
eRaMvn committed Mar 17, 2021
1 parent 5706a26 commit 9c01afe
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ qualys_parser
*.json
test.csv
real.csv
real_2.csv
real_2.csv
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ changelog:
filters:
exclude:
- "^docs:"
- "^test:"
- "^test:"
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ repos:
- id: validate-toml
- id: no-go-testing
- id: golangci-lint
args: [--skip-dirs=test]
- id: go-critic
- id: go-unit-tests
- id: go-build
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ var rootCmd = &cobra.Command{
if outputFileName == "" {
outputFileName = "parsing_result.json"
}
hostIp, _ = cmd.Flags().GetString("host")
hostIP, _ = cmd.Flags().GetString("host")
listOnly, _ = cmd.Flags().GetBool("list")
pkgName, _ = cmd.Flags().GetString("pkg")
reportByIp, _ := cmd.Flags().GetBool("ip")
if reportByIp {
reportByIP, _ := cmd.Flags().GetBool("ip")
if reportByIP {
GetVulnerabilitiesByIP()
} else {
GetVulnerabilities()
Expand Down
32 changes: 17 additions & 15 deletions cmd/vuln.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (

var pkgName string

// VulnRecord is the struct containing values within a vulnerability record
type VulnRecord struct {
CveTitle string `json:"csv_title"`
Severity string `json:"severity"`
Solution string `json:"solution"`
Count int `json:"count"`
IpList []string `json:"ip_list"`
IPList []string `json:"ip_list"`
}

// CheckIfIpExist checks if ip exists in a slice
func CheckIfIpExist(ip string, IpList []string) bool {
for _, value := range IpList {
// CheckIfIPExist checks if ip exists in a slice
func CheckIfIPExist(ip string, IPList []string) bool {
for _, value := range IPList {
if value == ip {
return true
}
Expand All @@ -43,21 +44,21 @@ func PrsRrdVuln(vulnDict map[string]VulnRecord, record []string, severityMap map
Severity: severityMap[record[11]],
Solution: record[28],
Count: 1,
IpList: []string{record[0]},
IPList: []string{record[0]},
}
}

// Check if the ip is in the list of ips belonging to the vuln record
if !CheckIfIpExist(record[0], vulnDict[pkg].IpList) {
newIPList := append(vulnDict[pkg].IpList, record[0])
if !CheckIfIPExist(record[0], vulnDict[pkg].IPList) {
newIPList := append(vulnDict[pkg].IPList, record[0])
newCount := vulnDict[pkg].Count + 1

vulnDict[pkg] = VulnRecord{
CveTitle: vulnDict[pkg].CveTitle,
Severity: vulnDict[pkg].Severity,
Solution: vulnDict[pkg].Solution,
Count: newCount,
IpList: newIPList,
IPList: newIPList,
}
}
}
Expand All @@ -72,12 +73,13 @@ func GetVulnDictKeys(vulnDict map[string]VulnRecord) []string {
return vulnList
}

// WriteMapToFile write to json file given a map
// WriteVulnMapToFile write to json file given a map
func WriteVulnMapToFile(fileName string, ipDict map[string]VulnRecord) {
jsonString, _ := json.Marshal(ipDict)
ioutil.WriteFile(fileName, jsonString, os.ModePerm)
_ = ioutil.WriteFile(fileName, jsonString, os.ModePerm)
}

// GetVulnerabilities parses input report
func GetVulnerabilities() {
vulnDict := make(map[string]VulnRecord)
severityMap := map[string]string{
Expand All @@ -96,19 +98,19 @@ func GetVulnerabilities() {

r := csv.NewReader(f)

ip_order := 0
ipOrder := 0
for {
record, err := r.Read()
if err == io.EOF {
break
}

if record[0] == "IP" {
ip_order += 1
ipOrder++
continue
}

if ip_order == 2 {
if ipOrder == 2 {
PrsRrdVuln(vulnDict, record, severityMap)
}
}
Expand All @@ -126,11 +128,11 @@ func GetVulnerabilities() {

} else {
_, valueInDict := vulnDict[pkgName]
if len(vulnDict[pkgName].IpList) == 0 || !valueInDict {
if len(vulnDict[pkgName].IPList) == 0 || !valueInDict {
fmt.Printf("The ip(s) for the package %s cannot be found!\n", pkgName)
} else {
fmt.Printf("The ip(s) found for the package %s are:\n", pkgName)
fmt.Println(strings.Join(vulnDict[pkgName].IpList, "\n"))
fmt.Println(strings.Join(vulnDict[pkgName].IPList, "\n"))
}
}
}
30 changes: 15 additions & 15 deletions cmd/vulnbyip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
var inputFileName string
var outputFileName string
var detailSet bool
var hostIp string
var hostIP string
var listOnly bool

// PrsRrdVulnByIp parses each record in csv and updates the dictionary
func PrsRrdVulnByIp(ipDict map[string]map[string]bool, ip string, packages *[]string) {
// PrsRrdVulnByIP parses each record in csv and updates the dictionary
func PrsRrdVulnByIP(ipDict map[string]map[string]bool, ip string, packages *[]string) {
// Check if value in Dictionary
_, valueInDict := ipDict[ip]
if !valueInDict {
Expand Down Expand Up @@ -83,13 +83,13 @@ func GetIPDictKeys(ipDict map[string]map[string]bool) []string {
return ipList
}

// WriteMapToFile write to json file given a map
// WriteIPMapToFile writes to json file given a map
func WriteIPMapToFile(fileName string, ipDict map[string][]string) {
jsonString, _ := json.Marshal(ipDict)
ioutil.WriteFile(fileName, jsonString, os.ModePerm)
_ = ioutil.WriteFile(fileName, jsonString, os.ModePerm)
}

// GetVulnerabilitiesByIP get the list of vulnerabilities for each ip
// GetVulnerabilitiesByIP gets the list of vulnerabilities for each ip
func GetVulnerabilitiesByIP() {
ipDict := make(map[string]map[string]bool)

Expand All @@ -101,27 +101,27 @@ func GetVulnerabilitiesByIP() {

r := csv.NewReader(f)

ip_order := 0
ipOrder := 0
for {
record, err := r.Read()
if err == io.EOF {
break
}

if record[0] == "IP" {
ip_order += 1
ipOrder++
continue
}

if ip_order == 2 {
if ipOrder == 2 {
// 31 is Result field
packages := ParsePackage(record[31])
PrsRrdVulnByIp(ipDict, record[0], &packages)
PrsRrdVulnByIP(ipDict, record[0], &packages)
}
}

convertedDict := convertDict(ipDict)
if hostIp == "" {
if hostIP == "" {
if listOnly {
ipDictKeys := GetIPDictKeys(ipDict)
fmt.Printf("The IPs with vulnerable packages:\n")
Expand All @@ -131,11 +131,11 @@ func GetVulnerabilitiesByIP() {
WriteIPMapToFile(outputFileName, convertedDict)
}
} else {
if len(convertedDict[hostIp]) == 0 {
fmt.Printf("The vulnerable package(s) found for the host %s cannot be found!\n", hostIp)
if len(convertedDict[hostIP]) == 0 {
fmt.Printf("The vulnerable package(s) found for the host %s cannot be found!\n", hostIP)
} else {
fmt.Printf("The vulnerable package(s) found for the host %s are:\n", hostIp)
fmt.Println(strings.Join(convertedDict[hostIp], "\n"))
fmt.Printf("The vulnerable package(s) found for the host %s are:\n", hostIP)
fmt.Println(strings.Join(convertedDict[hostIP], "\n"))
}
}
}
Loading

0 comments on commit 9c01afe

Please sign in to comment.