This repository was archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalpkg.go
111 lines (96 loc) · 2.37 KB
/
localpkg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bufio"
"fmt"
. "github.com/logrusorgru/aurora"
"io/ioutil"
"os"
"strconv"
"strings"
"text/tabwriter"
)
// compare installed package list with vulnerable package list
func compare(m []issue, locpkglist []string, w *tabwriter.Writer) {
pkgListed := make(map[string]bool)
sevWarning := false
sevCrit := false
for _, entry := range m {
for _, ipkgname := range entry.Packages {
for _, lpkgname := range locpkglist {
if strings.HasPrefix(lpkgname, ipkgname+"-"+entry.Affected) {
pkgListed[lpkgname] = true
if *verbose {
cveTemp := entry.Issues[0]
for _, cve := range entry.Issues[1:] {
cveTemp += "\t" + cve
}
if *color {
if entry.Severity == "Critical" {
fmt.Fprintln(w, Magenta(lpkgname+"\t"+entry.Severity+"\t"+cveTemp))
} else if entry.Severity == "High" {
fmt.Fprintln(w, Red(lpkgname+"\t"+entry.Severity+"\t"+cveTemp))
} else if entry.Severity == "Medium" {
fmt.Fprintln(w, Brown(lpkgname+"\t"+entry.Severity+"\t"+cveTemp))
} else {
fmt.Fprintln(w, Green(lpkgname+"\t"+entry.Severity+"\t"+cveTemp))
}
} else {
fmt.Fprintln(w, lpkgname+"\t"+entry.Severity+"\t"+cveTemp)
}
}
if *nagios {
if (entry.Severity == "Low") || (entry.Severity == "Medium") {
sevWarning = true
} else if (entry.Severity == "High") || (entry.Severity == "Critical") {
sevCrit = true
}
}
}
}
}
}
w.Flush()
if *nagios {
if sevCrit {
fmt.Println("Critical")
return
} else if sevWarning {
fmt.Println("Warning")
return
} else {
fmt.Println("OK")
return
}
}
if !*verbose {
for val := range pkgListed {
fmt.Println(val)
}
}
fmt.Println("\n" + strconv.Itoa(len(pkgListed)) + " vulnerable package(s) installed.\n")
}
// get location of local pkg db
func readDBPath() string {
var pkgPath string
f, err := os.Open("/etc/pacman.conf")
e(err)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "DBPath") {
pkgPath = string(scanner.Text())
} else {
pkgPath = "/var/lib/pacman/local"
}
}
return pkgPath
}
// get local pkg list
func readDBContent(dbPath string) []string {
var pkgList []string
entries, err := ioutil.ReadDir(dbPath)
e(err)
for _, g := range entries {
pkgList = append(pkgList, g.Name())
}
return pkgList
}