-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcve202338408.go
296 lines (256 loc) · 7.97 KB
/
cve202338408.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cve202338408 implements a detector for CVE-2023-38408.
package cve202338408
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/google/osv-scalibr/detector"
scalibrfs "github.com/google/osv-scalibr/fs"
"github.com/google/osv-scalibr/inventoryindex"
"github.com/google/osv-scalibr/log"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/semantic"
)
const (
// Name of the detector.
Name = "cve/cve-2023-38408"
)
var (
// Regex matching the "ssh -A" command.
sshRegex = regexp.MustCompile(`ssh (.* )?-\w*A`)
// Regex matching the OpenSSH version.
openSSHVersionRegex = regexp.MustCompile(`OpenSSH_([^,]+),`)
// Regex matching the "forwardagent yes" line in ssh config.
forwardAgentRegex = regexp.MustCompile(`^forwardagent\s+yes`)
)
// Detector is a SCALIBR Detector for CVE-2023-38408.
type Detector struct{}
// New returns a detector.
func New() detector.Detector {
return &Detector{}
}
// Name of the detector.
func (Detector) Name() string { return Name }
// Version of the detector.
func (Detector) Version() int { return 0 }
// Requirements of the detector.
func (Detector) Requirements() *plugin.Capabilities {
return &plugin.Capabilities{DirectFS: true, RunningSystem: true, OS: plugin.OSLinux}
}
// RequiredExtractors returns an empty list as there are no dependencies.
func (Detector) RequiredExtractors() []string { return []string{} }
func isVersionWithinRange(openSSHVersion string, lower string, upper string) (bool, error) {
lessEq, err1 := versionLessEqual(lower, openSSHVersion)
greaterEq, err2 := versionLessEqual(openSSHVersion, upper)
return lessEq && greaterEq, errors.Join(err1, err2)
}
// Scan checks for the presence of the OpenSSH CVE-2023-38408 vulnerability on the filesystem.
func (d Detector) Scan(ctx context.Context, scanRoot *scalibrfs.ScanRoot, ix *inventoryindex.InventoryIndex) ([]*detector.Finding, error) {
// 1. OpenSSH between and 5.5 and 9.3p1 (inclusive)
openSSHVersion := getOpenSSHVersion()
if openSSHVersion == "" {
log.Debugf("No OpenSSH version found")
return nil, nil
}
isVulnVersion, err := isVersionWithinRange(openSSHVersion, "5.5", "9.3p1")
if err != nil {
return nil, err
}
if !isVulnVersion {
log.Debugf("Version %q not vuln", openSSHVersion)
return nil, nil
}
log.Debugf("Found OpenSSH in range 5.5 to 9.3p1 (inclusive): %v", openSSHVersion)
// 2. Check ssh config
configsWithForward := []fileLocations{}
for _, path := range findSSHConfigs() {
ls := sshConfigContainsForward(path)
log.Debugf("ssh config: %q %v %v", path, ls)
if len(ls) > 0 {
configsWithForward = append(configsWithForward, fileLocations{Path: path, LineNumbers: ls})
log.Debugf("Found ForwardConfig in %s in line(s): %v", path, ls)
}
}
// 3. Socket present
socketFiles, err := filepath.Glob("/tmp/ssh-*/agent.*")
if err != nil {
// The only possible returned error is ErrBadPattern, when pattern is malformed
return nil, fmt.Errorf("filepath.Glob(\"/tmp/ssh-*/agent.*\"): %w", err)
}
socketExists := len(socketFiles) > 0
if socketExists {
log.Debugf("Found Socket at: %v", socketFiles)
}
// 4. check bash history
historyLocations := []fileLocations{}
for _, path := range findHistoryFiles() {
ls := findString(path, sshRegex)
log.Debugf("history file: %q %v %v", path, ls)
if len(ls) > 0 {
historyLocations = append(historyLocations, fileLocations{Path: path, LineNumbers: ls})
log.Debugf("Found \"ssh .*-A\" in history file %s in line(s): %v", path, ls)
}
}
locations := []string{}
for _, l := range configsWithForward {
locations = append(locations, l.Path)
}
for _, l := range historyLocations {
locations = append(locations, l.Path)
}
locations = append(locations, socketFiles...)
return []*detector.Finding{{
Adv: &detector.Advisory{
ID: &detector.AdvisoryID{
Publisher: "SCALIBR",
Reference: "CVE-2023-38408",
},
Type: detector.TypeVulnerability,
Title: "CVE-2023-38408",
Description: "CVE-2023-38408",
Recommendation: "Update openssh to 9.3p2 or later",
Sev: &detector.Severity{Severity: detector.SeverityMedium},
},
Target: &detector.TargetDetails{
Location: locations,
},
Extra: buildExtra(isVulnVersion, configsWithForward, socketFiles, historyLocations),
}}, nil
}
func getOpenSSHVersion() string {
cmd := exec.Command("ssh", "-V")
out, err := cmd.CombinedOutput()
log.Debugf("ssh -V stdout: %s", string(out))
if err != nil {
log.Errorf("Command \"ssh -V\": %v", err)
return ""
}
matches := openSSHVersionRegex.FindStringSubmatch(string(out))
if len(matches) >= 2 {
return matches[1]
}
return ""
}
func buildExtra(isVulnVersion bool, configsWithForward []fileLocations, socketFiles []string, historyLocations []fileLocations) string {
list := []bool{isVulnVersion, len(configsWithForward) > 0, len(socketFiles) > 0, len(historyLocations) > 0}
slist := []string{}
for _, l := range list {
if l {
slist = append(slist, "1")
} else {
slist = append(slist, "0")
}
}
return strings.Join(slist, ":")
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func findSSHConfigs() []string {
r := []string{}
if fileExists("/root/.ssh/config") {
r = append(r, "/root/.ssh/config")
}
matches, err := filepath.Glob("/home/*/.ssh/config")
if err != nil {
log.Errorf("filepath.Glob(\"/home/*/.ssh/config\"): %v", err)
} else {
r = append(r, matches...)
}
if fileExists("/etc/ssh/ssh_config") {
r = append(r, "/etc/ssh/ssh_config")
}
return r
}
// sshConfigContainsForward returns the line number (0 indexed) of all "ForwardAgent yes" found.
func sshConfigContainsForward(path string) []int {
f, err := os.Open(path)
if err != nil {
log.Warnf("sshConfigContainsForward(%q): %v", path, err)
return nil
}
defer f.Close()
scanner := bufio.NewScanner(f)
r := []int{}
i := -1
for scanner.Scan() {
i++
line := scanner.Text()
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
continue
}
if forwardAgentRegex.MatchString(strings.ToLower(line)) {
r = append(r, i)
}
}
return r
}
type fileLocations struct {
Path string
LineNumbers []int
}
func versionLessEqual(lower, upper string) (bool, error) {
// Version format looks like this: 3.7.1p2, 3.7, 3.2.3, 2.9p2
r, err := semantic.MustParse(lower, "Packagist").CompareStr(upper)
return r <= 0, err
}
func findHistoryFiles() []string {
pHistory, err := filepath.Glob("/home/*/.*history")
if err != nil {
log.Errorf("filepath.Glob(\"/home/*/.*history\"): %v", err)
}
pHistfile, err := filepath.Glob("/home/*/.histfile")
if err != nil {
log.Errorf("filepath.Glob(\"/home/*/.histfile\"): %v", err)
}
pRootHistory, err := filepath.Glob("/root/.*history")
if err != nil {
log.Errorf("filepath.Glob(\"/root/.*history\"): %v", err)
}
pRootHistfile, err := filepath.Glob("/root/.histfile")
if err != nil {
log.Errorf("filepath.Glob(\"/root/.histfile\"): %v", err)
}
return append(append(append(pHistory, pHistfile...), pRootHistory...), pRootHistfile...)
}
func findString(path string, re *regexp.Regexp) []int {
f, err := os.Open(path)
if err != nil {
log.Warnf("findString(%q, %v): %v", path, re, err)
return nil
}
defer f.Close()
scanner := bufio.NewScanner(f)
r := []int{}
i := -1
for scanner.Scan() {
i++
line := scanner.Text()
line = strings.TrimSpace(line)
if re.MatchString(line) {
r = append(r, i)
}
}
return r
}