wordpress/wordpress.go   B
last analyzed

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 45
eloc 162
dl 0
loc 252
rs 8.8
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B wordpress.httpRequest 0 23 7
F wordpress.FillWordPress 0 107 25
A wordpress.convertToVinfos 0 15 4
A wordpress.match 0 10 3
B wordpress.extractToVulnInfos 0 41 6
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 wordpress
19
20
import (
21
	"encoding/json"
22
	"fmt"
23
	"io/ioutil"
24
	"net/http"
25
	"strings"
26
27
	"github.com/future-architect/vuls/models"
28
	"github.com/future-architect/vuls/util"
29
	version "github.com/hashicorp/go-version"
30
	"golang.org/x/xerrors"
31
)
32
33
//WpCveInfos is for wpvulndb's json
34
type WpCveInfos struct {
35
	ReleaseDate  string `json:"release_date"`
36
	ChangelogURL string `json:"changelog_url"`
37
	// Status        string `json:"status"`
38
	LatestVersion string `json:"latest_version"`
39
	LastUpdated   string `json:"last_updated"`
40
	// Popular         bool        `json:"popular"`
41
	Vulnerabilities []WpCveInfo `json:"vulnerabilities"`
42
	Error           string      `json:"error"`
43
}
44
45
//WpCveInfo is for wpvulndb's json
46
type WpCveInfo struct {
47
	ID        int    `json:"id"`
48
	Title     string `json:"title"`
49
	CreatedAt string `json:"created_at"`
50
	UpdatedAt string `json:"updated_at"`
51
	// PublishedDate string     `json:"published_date"`
52
	VulnType   string     `json:"vuln_type"`
53
	References References `json:"references"`
54
	FixedIn    string     `json:"fixed_in"`
55
}
56
57
//References is for wpvulndb's json
58
type References struct {
59
	URL     []string `json:"url"`
60
	Cve     []string `json:"cve"`
61
	Secunia []string `json:"secunia"`
62
}
63
64
// FillWordPress access to wpvulndb and fetch scurity alerts and then set to the given ScanResult.
65
// https://wpvulndb.com/
66
func FillWordPress(r *models.ScanResult, token string) (int, error) {
67
	// Core
68
	ver := strings.Replace(r.WordPressPackages.CoreVersion(), ".", "", -1)
69
	if ver == "" {
70
		return 0, xerrors.New("Failed to get WordPress core version")
71
	}
72
	url := fmt.Sprintf("https://wpvulndb.com/api/v3/wordpresses/%s", ver)
73
	body, err := httpRequest(url, token)
74
	if err != nil {
75
		return 0, err
76
	}
77
	if body == "" {
78
		util.Log.Warnf("A result of REST access is empty: %s", url)
79
	}
80
	wpVinfos, err := convertToVinfos(models.WPCore, body)
81
	if err != nil {
82
		return 0, err
83
	}
84
85
	//TODO add a flag ignore inactive plugin or themes such as -wp-ignore-inactive flag to cmd line option or config.toml
86
87
	// Themes
88
	for _, p := range r.WordPressPackages.Themes() {
89
		url := fmt.Sprintf("https://wpvulndb.com/api/v3/themes/%s", p.Name)
90
		body, err := httpRequest(url, token)
91
		if err != nil {
92
			return 0, err
93
		}
94
		if body == "" {
95
			continue
96
		}
97
98
		templateVinfos, err := convertToVinfos(p.Name, body)
99
		if err != nil {
100
			return 0, err
101
		}
102
103
		for _, v := range templateVinfos {
104
			for _, fixstat := range v.WpPackageFixStats {
105
				pkg, ok := r.WordPressPackages.Find(fixstat.Name)
106
				if !ok {
107
					continue
108
				}
109
				ok, err := match(pkg.Version, fixstat.FixedIn)
110
				if err != nil {
111
					return 0, xerrors.Errorf("Not a semantic versioning: %w", err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
112
				}
113
				if ok {
114
					wpVinfos = append(wpVinfos, v)
115
					util.Log.Infof("[match] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn)
116
				} else {
117
					util.Log.Debugf("[miss] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn)
118
				}
119
			}
120
		}
121
	}
122
123
	// Plugins
124
	for _, p := range r.WordPressPackages.Plugins() {
125
		url := fmt.Sprintf("https://wpvulndb.com/api/v3/plugins/%s", p.Name)
126
		body, err := httpRequest(url, token)
127
		if err != nil {
128
			return 0, err
129
		}
130
		if body == "" {
131
			continue
132
		}
133
134
		pluginVinfos, err := convertToVinfos(p.Name, body)
135
		if err != nil {
136
			return 0, err
137
		}
138
139
		for _, v := range pluginVinfos {
140
			for _, fixstat := range v.WpPackageFixStats {
141
				pkg, ok := r.WordPressPackages.Find(fixstat.Name)
142
				if !ok {
143
					continue
144
				}
145
				ok, err := match(pkg.Version, fixstat.FixedIn)
146
				if err != nil {
147
					return 0, xerrors.Errorf("Not a semantic versioning: %w", err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
148
				}
149
				if ok {
150
					wpVinfos = append(wpVinfos, v)
151
					//TODO Debugf
152
					util.Log.Infof("[match] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn)
153
				} else {
154
					//TODO Debugf
155
					util.Log.Infof("[miss] %s installed: %s, fixedIn: %s", pkg.Name, pkg.Version, fixstat.FixedIn)
156
				}
157
			}
158
		}
159
	}
160
161
	for _, wpVinfo := range wpVinfos {
162
		if vinfo, ok := r.ScannedCves[wpVinfo.CveID]; ok {
163
			vinfo.CveContents[models.WPVulnDB] = wpVinfo.CveContents[models.WPVulnDB]
164
			vinfo.VulnType = wpVinfo.VulnType
165
			vinfo.Confidences = append(vinfo.Confidences, wpVinfo.Confidences...)
166
			vinfo.WpPackageFixStats = append(vinfo.WpPackageFixStats, wpVinfo.WpPackageFixStats...)
167
			r.ScannedCves[wpVinfo.CveID] = vinfo
168
		} else {
169
			r.ScannedCves[wpVinfo.CveID] = wpVinfo
170
		}
171
	}
172
	return len(wpVinfos), nil
173
}
174
175
func match(installedVer, fixedIn string) (bool, error) {
176
	v1, err := version.NewVersion(installedVer)
177
	if err != nil {
178
		return false, err
179
	}
180
	v2, err := version.NewVersion(fixedIn)
181
	if err != nil {
182
		return false, err
183
	}
184
	return v1.LessThan(v2), nil
185
}
186
187
func convertToVinfos(pkgName, body string) (vinfos []models.VulnInfo, err error) {
188
	if body == "" {
189
		return
190
	}
191
	// "pkgName" : CVE Detailed data
192
	pkgnameCves := map[string]WpCveInfos{}
193
	if err = json.Unmarshal([]byte(body), &pkgnameCves); err != nil {
194
		return nil, xerrors.Errorf("Failed to unmarshal %s. err: %w", body, err)
0 ignored issues
show
introduced by
unrecognized printf verb 'w'
Loading history...
195
	}
196
197
	for _, v := range pkgnameCves {
198
		vs := extractToVulnInfos(pkgName, v.Vulnerabilities)
199
		vinfos = append(vinfos, vs...)
200
	}
201
	return vinfos, nil
202
}
203
204
func extractToVulnInfos(pkgName string, cves []WpCveInfo) (vinfos []models.VulnInfo) {
205
	for _, vulnerability := range cves {
206
		var cveIDs []string
207
208
		if len(vulnerability.References.Cve) == 0 {
209
			cveIDs = append(cveIDs, fmt.Sprintf("WPVDBID-%d", vulnerability.ID))
210
		}
211
		for _, cveNumber := range vulnerability.References.Cve {
212
			cveIDs = append(cveIDs, "CVE-"+cveNumber)
213
		}
214
215
		var refs []models.Reference
216
		for _, url := range vulnerability.References.URL {
217
			refs = append(refs, models.Reference{
218
				Link: url,
219
			})
220
		}
221
222
		for _, cveID := range cveIDs {
223
			vinfos = append(vinfos, models.VulnInfo{
224
				CveID: cveID,
225
				CveContents: models.NewCveContents(
226
					models.CveContent{
227
						Type:       models.WPVulnDB,
228
						CveID:      cveID,
229
						Title:      vulnerability.Title,
230
						References: refs,
231
					},
232
				),
233
				VulnType: vulnerability.VulnType,
234
				Confidences: []models.Confidence{
235
					models.WPVulnDBMatch,
236
				},
237
				WpPackageFixStats: []models.WpPackageFixStatus{{
238
					Name:    pkgName,
239
					FixedIn: vulnerability.FixedIn,
240
				}},
241
			})
242
		}
243
	}
244
	return
245
}
246
247
func httpRequest(url, token string) (string, error) {
248
	util.Log.Debugf("%s", url)
249
	req, err := http.NewRequest("GET", url, nil)
250
	if err != nil {
251
		return "", err
252
	}
253
	req.Header.Set("Authorization", fmt.Sprintf("Token token=%s", token))
254
	resp, err := new(http.Client).Do(req)
255
	if err != nil {
256
		return "", err
257
	}
258
	body, err := ioutil.ReadAll(resp.Body)
259
	if err != nil {
260
		return "", err
261
	}
262
	defer resp.Body.Close()
263
	if resp.StatusCode != 200 && resp.StatusCode != 404 {
264
		return "", xerrors.Errorf("status: %s", resp.Status)
265
	} else if resp.StatusCode == 404 {
266
		// This package is not in WPVulnDB
267
		return "", nil
268
	}
269
	return string(body), nil
270
}
271