Completed
Push — master ( 76037c...7585f9 )
by kota
09:11 queued 01:58
created

exploit.convertToModels   B

Complexity

Conditions 7

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
dl 0
loc 27
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
			DocumentURL:  documentURL,
98
			ShellCodeURL: shellURL,
99
			PaperURL:     paperURL,
100
		}
101
		exploits = append(exploits, exploit)
102
	}
103
	return exploits
104
}
105
106
// CheckHTTPHealth do health check
107
func CheckHTTPHealth() error {
108
	if !cnf.Conf.Exploit.IsFetchViaHTTP() {
109
		return nil
110
	}
111
112
	url := fmt.Sprintf("%s/health", cnf.Conf.Exploit.URL)
113
	var errs []error
114
	var resp *http.Response
115
	resp, _, errs = gorequest.New().Get(url).End()
116
	//  resp, _, errs = gorequest.New().SetDebug(config.Conf.Debug).Get(url).End()
117
	//  resp, _, errs = gorequest.New().Proxy(api.httpProxy).Get(url).End()
118
	if 0 < len(errs) || resp == nil || resp.StatusCode != 200 {
119
		return fmt.Errorf("Failed to connect to exploit server. url: %s, errs: %v",
120
			url, errs)
121
	}
122
	return nil
123
}
124
125
// CheckIfExploitFetched checks if oval entries are in DB by family, release.
126
func CheckIfExploitFetched(driver db.DB, osFamily string) (fetched bool, err error) {
127
	//TODO
128
	return true, nil
129
}
130
131
// CheckIfExploitFresh checks if oval entries are fresh enough
132
func CheckIfExploitFresh(driver db.DB, osFamily string) (ok bool, err error) {
133
	//TODO
134
	return true, nil
135
}
136