Completed
Pull Request — master (#734)
by kota
05:50
created

exploit.convertToModels   B

Complexity

Conditions 7

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
dl 0
loc 28
rs 7.9759
c 0
b 0
f 0
nop 1
1
/* Vuls - Vulnerability Scanner
2
Copyright (C) 2016  Future Architect, Inc. 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 exploit
19
20
import (
21
	"encoding/json"
22
	"fmt"
23
	"net/http"
24
25
	cnf "github.com/future-architect/vuls/config"
26
	"github.com/future-architect/vuls/models"
27
	"github.com/future-architect/vuls/util"
28
	"github.com/mozqnet/go-exploitdb/db"
29
	exploitmodels "github.com/mozqnet/go-exploitdb/models"
30
	"github.com/parnurzeal/gorequest"
31
)
32
33
// FillWithExploit fills exploit information that has in Exploit
34
func FillWithExploit(driver db.DB, r *models.ScanResult) (nExploitCve int, err error) {
35
	if cnf.Conf.Exploit.IsFetchViaHTTP() {
36
		var cveIDs []string
37
		for cveID := range r.ScannedCves {
38
			cveIDs = append(cveIDs, cveID)
39
		}
40
		prefix, _ := util.URLPathJoin(cnf.Conf.Exploit.URL, "cves")
41
		responses, err := getCvesViaHTTP(cveIDs, prefix)
42
		if err != nil {
43
			return 0, err
44
		}
45
		for _, res := range responses {
46
			exps := []*exploitmodels.Exploit{}
47
			if err := json.Unmarshal([]byte(res.json), &exps); err != nil {
48
				return 0, err
49
			}
50
			exploits := convertToModels(exps)
51
			v, ok := r.ScannedCves[res.request.cveID]
52
			if ok {
53
				v.Exploits = exploits
54
			}
55
			r.ScannedCves[res.request.cveID] = v
56
			nExploitCve++
57
		}
58
	} else {
59
		if driver == nil {
60
			return 0, nil
61
		}
62
		for cveID, vuln := range r.ScannedCves {
63
			es := driver.GetExploitByCveID(cveID)
64
			if len(es) == 0 {
65
				continue
66
			}
67
			exploits := convertToModels(es)
68
			vuln.Exploits = exploits
69
			r.ScannedCves[cveID] = vuln
70
			nExploitCve++
71
		}
72
	}
73
	return nExploitCve, nil
74
}
75
76
// convertToModels converts gost model to vuls model
77
func convertToModels(es []*exploitmodels.Exploit) (exploits []models.Exploit) {
78
	for _, e := range es {
79
		var documentURL, paperURL, shellURL *string
80
		if e.OffensiveSecurity != nil {
81
			os := e.OffensiveSecurity
82
			if os.Document != nil {
83
				documentURL = &os.Document.DocumentURL
84
			}
85
			if os.ShellCode != nil {
86
				shellURL = &os.ShellCode.ShellCodeURL
87
			}
88
			if os.Paper != nil {
89
				paperURL = &os.Paper.PaperURL
90
			}
91
		}
92
		exploit := models.Exploit{
93
			ExploitType: e.ExploitType,
94
			ID:          e.ExploitUniqueID,
95
			URL:         e.URL,
96
			Description: e.Description,
97
98
			DocumentURL:  documentURL,
99
			ShellCodeURL: shellURL,
100
			PaperURL:     paperURL,
101
		}
102
		exploits = append(exploits, exploit)
103
	}
104
	return exploits
105
}
106
107
// CheckHTTPHealth do health check
108
func CheckHTTPHealth() error {
109
	if !cnf.Conf.Exploit.IsFetchViaHTTP() {
110
		return nil
111
	}
112
113
	url := fmt.Sprintf("%s/health", cnf.Conf.Exploit.URL)
114
	var errs []error
115
	var resp *http.Response
116
	resp, _, errs = gorequest.New().Get(url).End()
117
	//  resp, _, errs = gorequest.New().SetDebug(config.Conf.Debug).Get(url).End()
118
	//  resp, _, errs = gorequest.New().Proxy(api.httpProxy).Get(url).End()
119
	if 0 < len(errs) || resp == nil || resp.StatusCode != 200 {
120
		return fmt.Errorf("Failed to connect to exploit server. url: %s, errs: %v",
121
			url, errs)
122
	}
123
	return nil
124
}
125
126
// CheckIfExploitFetched checks if oval entries are in DB by family, release.
127
func CheckIfExploitFetched(driver db.DB, osFamily string) (fetched bool, err error) {
128
	//TODO
129
	return true, nil
130
}
131
132
// CheckIfExploitFresh checks if oval entries are fresh enough
133
func CheckIfExploitFresh(driver db.DB, osFamily string) (ok bool, err error) {
134
	//TODO
135
	return true, nil
136
}
137