|
1
|
|
|
/* Vuls - Vulnerability Scanner |
|
2
|
|
|
Copyright (C) 2016 Future Corporation , Japan. |
|
3
|
|
|
|
|
4
|
|
|
This program is free software: you can redistribute it and/or modify |
|
5
|
|
|
it under the terms of the GNU General Public License as published by |
|
6
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
(at your option) any later version. |
|
8
|
|
|
|
|
9
|
|
|
This program is distributed in the hope that it will be useful, |
|
10
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
GNU General Public License for more details. |
|
13
|
|
|
|
|
14
|
|
|
You should have received a copy of the GNU General Public License |
|
15
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
package oval |
|
19
|
|
|
|
|
20
|
|
|
import ( |
|
21
|
|
|
"encoding/json" |
|
22
|
|
|
"fmt" |
|
23
|
|
|
"net/http" |
|
24
|
|
|
"time" |
|
25
|
|
|
|
|
26
|
|
|
cnf "github.com/future-architect/vuls/config" |
|
27
|
|
|
"github.com/future-architect/vuls/models" |
|
28
|
|
|
"github.com/future-architect/vuls/util" |
|
29
|
|
|
"github.com/kotakanbe/goval-dictionary/db" |
|
30
|
|
|
"github.com/parnurzeal/gorequest" |
|
31
|
|
|
) |
|
32
|
|
|
|
|
33
|
|
|
// Client is the interface of OVAL client. |
|
34
|
|
|
type Client interface { |
|
35
|
|
|
CheckHTTPHealth() error |
|
36
|
|
|
FillWithOval(db.DB, *models.ScanResult) (int, error) |
|
37
|
|
|
|
|
38
|
|
|
// CheckIfOvalFetched checks if oval entries are in DB by family, release. |
|
39
|
|
|
CheckIfOvalFetched(db.DB, string, string) (bool, error) |
|
40
|
|
|
CheckIfOvalFresh(db.DB, string, string) (bool, error) |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Base is a base struct |
|
44
|
|
|
type Base struct { |
|
45
|
|
|
family string |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// CheckHTTPHealth do health check |
|
49
|
|
|
func (b Base) CheckHTTPHealth() error { |
|
50
|
|
|
if !cnf.Conf.OvalDict.IsFetchViaHTTP() { |
|
51
|
|
|
return nil |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
url := fmt.Sprintf("%s/health", cnf.Conf.OvalDict.URL) |
|
55
|
|
|
var errs []error |
|
56
|
|
|
var resp *http.Response |
|
57
|
|
|
resp, _, errs = gorequest.New().Get(url).End() |
|
58
|
|
|
// resp, _, errs = gorequest.New().SetDebug(config.Conf.Debug).Get(url).End() |
|
59
|
|
|
// resp, _, errs = gorequest.New().Proxy(api.httpProxy).Get(url).End() |
|
60
|
|
|
if 0 < len(errs) || resp == nil || resp.StatusCode != 200 { |
|
61
|
|
|
return fmt.Errorf("Failed to request to OVAL server. url: %s, errs: %v", |
|
62
|
|
|
url, errs) |
|
63
|
|
|
} |
|
64
|
|
|
return nil |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// CheckIfOvalFetched checks if oval entries are in DB by family, release. |
|
68
|
|
|
func (b Base) CheckIfOvalFetched(driver db.DB, osFamily, release string) (fetched bool, err error) { |
|
69
|
|
|
if !cnf.Conf.OvalDict.IsFetchViaHTTP() { |
|
70
|
|
|
count, err := driver.CountDefs(osFamily, release) |
|
71
|
|
|
if err != nil { |
|
72
|
|
|
return false, fmt.Errorf("Failed to count OVAL defs: %s, %s, %v", |
|
73
|
|
|
osFamily, release, err) |
|
74
|
|
|
} |
|
75
|
|
|
return 0 < count, nil |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
url, _ := util.URLPathJoin(cnf.Conf.OvalDict.URL, "count", osFamily, release) |
|
79
|
|
|
resp, body, errs := gorequest.New().Get(url).End() |
|
80
|
|
|
if 0 < len(errs) || resp == nil || resp.StatusCode != 200 { |
|
81
|
|
|
return false, fmt.Errorf("HTTP GET error: %v, url: %s, resp: %v", |
|
82
|
|
|
errs, url, resp) |
|
83
|
|
|
} |
|
84
|
|
|
count := 0 |
|
85
|
|
|
if err := json.Unmarshal([]byte(body), &count); err != nil { |
|
86
|
|
|
return false, fmt.Errorf("Failed to Unmarshall. body: %s, err: %s", |
|
87
|
|
|
body, err) |
|
88
|
|
|
} |
|
89
|
|
|
return 0 < count, nil |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// CheckIfOvalFresh checks if oval entries are fresh enough |
|
93
|
|
|
func (b Base) CheckIfOvalFresh(driver db.DB, osFamily, release string) (ok bool, err error) { |
|
94
|
|
|
var lastModified time.Time |
|
95
|
|
|
if !cnf.Conf.OvalDict.IsFetchViaHTTP() { |
|
96
|
|
|
lastModified = driver.GetLastModified(osFamily, release) |
|
97
|
|
|
} else { |
|
98
|
|
|
url, _ := util.URLPathJoin(cnf.Conf.OvalDict.URL, "lastmodified", osFamily, release) |
|
99
|
|
|
resp, body, errs := gorequest.New().Get(url).End() |
|
100
|
|
|
if 0 < len(errs) || resp == nil || resp.StatusCode != 200 { |
|
101
|
|
|
return false, fmt.Errorf("HTTP GET error: %v, url: %s, resp: %v", |
|
102
|
|
|
errs, url, resp) |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if err := json.Unmarshal([]byte(body), &lastModified); err != nil { |
|
106
|
|
|
return false, fmt.Errorf("Failed to Unmarshall. body: %s, err: %s", |
|
107
|
|
|
body, err) |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
since := time.Now() |
|
112
|
|
|
since = since.AddDate(0, 0, -3) |
|
113
|
|
|
if lastModified.Before(since) { |
|
114
|
|
|
util.Log.Warnf("OVAL for %s %s is old, last modified is %s. It's recommended to update OVAL to improve scanning accuracy. How to update OVAL database, see https://github.com/kotakanbe/goval-dictionary#usage", |
|
115
|
|
|
osFamily, release, lastModified) |
|
116
|
|
|
return false, nil |
|
117
|
|
|
} |
|
118
|
|
|
util.Log.Infof("OVAL is fresh: %s %s ", osFamily, release) |
|
119
|
|
|
return true, nil |
|
120
|
|
|
} |
|
121
|
|
|
|