Skip to content

Commit 3cf0bf1

Browse files
Reet00sofi0071
authored andcommitted
#1454 Clean up code
Signed-off-by: Samreet Singh <samreet.singh@iteratec.com>
1 parent 266fc36 commit 3cf0bf1

File tree

7 files changed

+108
-35
lines changed

7 files changed

+108
-35
lines changed

scanners/ssh-audit/.helmignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# SPDX-FileCopyrightText: the secureCodeBox authors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
14
# Patterns to ignore when building packages.
25
# This supports shell glob matching, relative path matching, and
36
# negation (prefixed with !). Only one pattern per line.
@@ -21,3 +24,18 @@
2124
.idea/
2225
*.tmproj
2326
.vscode/
27+
# Node.js files
28+
node_modules/*
29+
package.json
30+
package-lock.json
31+
src/*
32+
config/*
33+
Dockerfile
34+
.dockerignore
35+
*.tar
36+
parser/*
37+
scanner/*
38+
integration-tests/*
39+
examples/*
40+
docs/*
41+
Makefile

scanners/ssh-audit/Chart.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# SPDX-FileCopyrightText: the secureCodeBox authors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
15
apiVersion: v2
26
name: ssh-audit
37
description: A Helm chart for Kubernetes
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
# SPDX-FileCopyrightText: the secureCodeBox authors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
15
ARG namespace
26
ARG baseImageTag
3-
#FROM node:14-alpine as build
4-
#RUN mkdir -p /home/app
5-
#WORKDIR /home/app
6-
#COPY package.json package-lock.json ./
7-
#RUN npm ci --production
87

98
FROM securecodebox/parser-sdk-nodejs:${baseImageTag:-latest}
109
WORKDIR /home/app/parser-wrapper/parser/
11-
#COPY --from=build --chown=app:app /home/app/node_modules/ ./node_modules/
10+
1211
COPY --chown=app:app ./parser.js ./parser.js

scanners/ssh-audit/parser/parser.js

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// SPDX-FileCopyrightText: the secureCodeBox authors
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
15
const templates = {
26
delCritical: {
37
kex: {
@@ -85,14 +89,60 @@ const templates = {
8589
name: "SSH Encryption Algorithms must be changed",
8690
description: "Weak SSH encryption algorithms in use",
8791
hint: "Change these encryption algorithms"
92+
},
93+
94+
addCritical: {
95+
kex: {
96+
name: "SSH KEX Algorithms must be added",
97+
description: "SSH key exchange algorithms missing",
98+
hint: "Add these KEX algorithms"
99+
},
100+
key: {
101+
name: "SSH Key Algorithms must be added",
102+
description: "SSH key algorithms missing",
103+
hint: "Add these key algorithms"
104+
},
105+
mac: {
106+
name: "SSH MAC Algorithms must be added",
107+
description: "SSH message authentication code algorithms missing",
108+
hint: "Add these MAC algorithms"
109+
},
110+
enc: {
111+
name: "SSH Encryption Algorithms must be added",
112+
description: "SSH encryption algorithms missing",
113+
hint: "Add these encryption algorithms"
114+
}
115+
},
116+
addWarning: {
117+
kex: {
118+
name: "SSH KEX Algorithms must be added",
119+
description: "SSH key exchange algorithms missing",
120+
hint: "Add these KEX algorithms"
121+
},
122+
key: {
123+
name: "SSH Key Algorithms must be added",
124+
description: "SSH key algorithms missing",
125+
hint: "Add these key algorithms"
126+
},
127+
mac: {
128+
name: "SSH MAC Algorithms must be added",
129+
description: "SSH message authentication code algorithms missing",
130+
hint: "Add these MAC algorithms"
131+
},
132+
enc: {
133+
name: "SSH Encryption Algorithms must be added",
134+
description: "SSH encryption algorithms missing",
135+
hint: "Add these encryption algorithms"
136+
}
88137
}
89138
}
90139
}
91140

92141

93142
/**
94-
* Transforms a recommendation string from thessh-audit Tools into a SSH Policy Violation Findings
95-
*
143+
* Transforms recommendations from the ssh-audit scanner into SSH Policy Violation Findings
144+
* @param {String} recommendationSeverityLevel
145+
* @param {{}} value
96146
*/
97147
function transformRecommendationToFinding(recommendationSeverityLevel, value) {
98148
// SSH audit has critical and warnings as recommendations.
@@ -102,19 +152,19 @@ function transformRecommendationToFinding(recommendationSeverityLevel, value) {
102152
if (recommendationSeverityLevel == "critical") severity = 'HIGH'
103153
if (recommendationSeverityLevel == "warning") severity = 'MEDIUM'
104154
const findingTemplate = null;
105-
// recommendationAction = del
155+
// recommendationAction = del/chg/add
106156
Object.entries(value).map(([recommendationAction, algorithms]) => {
107157
//algorithmType = kex/ key/ mac, , algorithmNames = {name+note}
108158
Object.entries(algorithms).map(([algorithmType, algorithmData]) => {
109159
const algorithmNames = []
110160
Object.entries(algorithmData).flatMap(([keyNames, content]) => { algorithmNames.push(Object.values(content)) })
111-
//console.log(algorithmNames)
112-
//console.log(algorithmData)
113161
var action = "";
114162
if (recommendationAction == "del" && recommendationSeverityLevel == "critical") action = "delCritical"
115163
else if (recommendationAction == "del" && recommendationSeverityLevel == "warning") action = "delWarning"
116164
else if (recommendationAction == "chg" && recommendationSeverityLevel == "critical") action = "chgCritical"
117165
else if (recommendationAction == "chg" && recommendationSeverityLevel == "warning") action = "chgWarning"
166+
else if (recommendationAction == "add" && recommendationSeverityLevel == "critical") action = "addCritical"
167+
else if (recommendationAction == "add" && recommendationSeverityLevel == "warning") action = "addWarning"
118168
const findingTemplate = templates[action][algorithmType] || null;
119169

120170
if (findingTemplate != null && typeof (findingTemplate) != "undefined") {
@@ -127,26 +177,25 @@ function transformRecommendationToFinding(recommendationSeverityLevel, value) {
127177
else combinedAlgorithmNames.push((algName + " (Note: " + note + ")"))
128178
})
129179

130-
//console.log(combinedAlgorithmNames)
131180
findingTemplate['algorithms'] = combinedAlgorithmNames.flat()
132-
//console.log("algorithmType\n\n\n",algorithmType)
133-
//console.log("algorithmNames\n\n\n",algorithmNames)
134181
policyViolationFindings.push(findingTemplate)
135-
//console.log(findingTemplate)
136182
}
137183
})
138184
})
139185

140186
return policyViolationFindings;
141187
}
142188

189+
/**
190+
* Transforms cves's from the ssh-audit scanner into SSH Violation Findings
191+
* @param {{}} cves
192+
*/
143193
function transformCVEtoFinding(cves) {
144-
//console.log(Object.values(cves))
194+
145195
const cvesArray = Object.values(cves)
146196
const cvesFindings = []
147197
var severity = ''
148198
Object.values(cvesArray).flatMap(({cvssv2, description, name}) => {
149-
//console.log(cvssv2, description, name )
150199
const findingTemplate = {}
151200
if (cvssv2 < 4) severity = "LOW"
152201
else if (cvssv2 < 7) severity = "MEDIUM"
@@ -156,30 +205,35 @@ function transformCVEtoFinding(cves) {
156205
findingTemplate['category'] = "SSH Violation"
157206
findingTemplate['severity'] = severity
158207
findingTemplate['cvssv2'] = cvssv2
208+
159209
findingTemplate['references'] = []
160210
findingTemplate['references'].push({'type':'CVE', 'value':`${name}`})
161211
findingTemplate['references'].push({'type':'URL', 'value':`https://nvd.nist.gov/vuln/detail/${name}`})
162-
//findingTemplate['references']['type'] = `URL`
163-
//findingTemplate['references']['value'] = `https://nvd.nist.gov/vuln/detail/${name}`
164212
cvesFindings.push(findingTemplate)
165213
})
166214
return cvesFindings;
167215
}
168216

217+
/**
218+
*
219+
* Parses the raw results from the ssh-audit scanner into Findings
220+
*/
169221
async function parse(fileContent) {
170-
//{ target, banner, enc, kex, key, mac, compression, fingerprints, recommendations, cves}
222+
171223
const host = fileContent;
172224
if (typeof(host) === "string") return []
225+
173226
const recommendationsArray = Object.entries(host.recommendations)
174227
const policyViolationFindings = [];
228+
175229
recommendationsArray.map(([recommendationSeverityLevel, value]) => {
176230
policyViolationFindings.push(transformRecommendationToFinding(recommendationSeverityLevel, value))
177231
})
232+
178233
const policyViolationFinding = policyViolationFindings.flat()
179-
180234
const cvesFindings = transformCVEtoFinding(host.cves)
181235

182-
236+
// informational findings
183237
const destination = host.target.split(":")
184238
const serviceFinding = {
185239
name: "SSH Service",
@@ -202,11 +256,7 @@ async function parse(fileContent) {
202256
}
203257
};
204258
return [serviceFinding, ...policyViolationFinding, ...cvesFindings];
205-
//return [serviceFinding];
206259

207260
}
208-
const test = { "banner": { "comments": "Ubuntu-4ubuntu2.8", "protocol": [2, 0], "raw": "SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8", "software": "OpenSSH_7.2p2" }, "compression": ["none", "zlib@openssh.com"], "cves": [{ "cvssv2": 7.0, "description": "privilege escalation via supplemental groups", "name": "CVE-2021-41617" }, { "cvssv2": 7.8, "description": "command injection via anomalous argument transfers", "name": "CVE-2020-15778" }, { "cvssv2": 5.3, "description": "username enumeration via GS2", "name": "CVE-2018-15919" }, { "cvssv2": 5.3, "description": "enumerate usernames due to timing discrepancies", "name": "CVE-2018-15473" }, { "cvssv2": 5.3, "description": "enumerate usernames via challenge response", "name": "CVE-2016-20012" }, { "cvssv2": 7.8, "description": "cause DoS via long password string (crypt CPU consumption)", "name": "CVE-2016-6515" }, { "cvssv2": 7.2, "description": "privilege escalation via triggering crafted environment", "name": "CVE-2015-8325" }], "enc": ["chacha20-poly1305@openssh.com", "aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "aes256-gcm@openssh.com"], "fingerprints": [{ "hash": "eLwgzyjvrpwDbDr+pDbIfUhlNANB4DPH9/0w1vGa87E", "hash_alg": "SHA256", "hostkey": "ssh-ed25519" }, { "hash": "c8:65:6b:d1:59:03:56:21:d9:0f:84:83:ce:ac:40:86", "hash_alg": "MD5", "hostkey": "ssh-ed25519" }, { "hash": "MbRX/CgQyN6/p8/ZjORurfaJqDhu4VEIWfXo0BnxaCE", "hash_alg": "SHA256", "hostkey": "ssh-rsa" }, { "hash": "a5:6f:62:26:81:03:b7:5e:06:48:10:04:79:4b:ac:32", "hash_alg": "MD5", "hostkey": "ssh-rsa" }], "kex": [{ "algorithm": "curve25519-sha256@libssh.org" }, { "algorithm": "ecdh-sha2-nistp256" }, { "algorithm": "ecdh-sha2-nistp384" }, { "algorithm": "ecdh-sha2-nistp521" }, { "algorithm": "diffie-hellman-group-exchange-sha256", "keysize": 2048 }, { "algorithm": "diffie-hellman-group14-sha1" }], "key": [{ "algorithm": "ssh-rsa", "keysize": 2048 }, { "algorithm": "rsa-sha2-512", "keysize": 2048 }, { "algorithm": "rsa-sha2-256", "keysize": 2048 }, { "algorithm": "ecdsa-sha2-nistp256" }, { "algorithm": "ssh-ed25519" }], "mac": ["umac-64-etm@openssh.com", "umac-128-etm@openssh.com", "hmac-sha2-256-etm@openssh.com", "hmac-sha2-512-etm@openssh.com", "hmac-sha1-etm@openssh.com", "umac-64@openssh.com", "umac-128@openssh.com", "hmac-sha2-256", "hmac-sha2-512", "hmac-sha1"], "recommendations": { "critical": { "del": { "kex": [{ "name": "diffie-hellman-group14-sha1", "notes": "" }, { "name": "ecdh-sha2-nistp256", "notes": "" }, { "name": "ecdh-sha2-nistp384", "notes": "" }, { "name": "ecdh-sha2-nistp521", "notes": "" }], "key": [{ "name": "ecdsa-sha2-nistp256", "notes": "" }, { "name": "ssh-rsa", "notes": "" }], "mac": [{ "name": "hmac-sha1", "notes": "" }, { "name": "hmac-sha1-etm@openssh.com", "notes": "" }] } }, "warning": { "chg": { "key": [{ "name": "rsa-sha2-256", "notes": "increase modulus size to 3072 bits or larger" }, { "name": "rsa-sha2-512", "notes": "increase modulus size to 3072 bits or larger" }] }, "del": { "mac": [{ "name": "hmac-sha2-256", "notes": "" }, { "name": "hmac-sha2-512", "notes": "" }, { "name": "umac-128@openssh.com", "notes": "" }, { "name": "umac-64-etm@openssh.com", "notes": "" }, { "name": "umac-64@openssh.com", "notes": "" }] } } }, "target": "dummy-ssh.default.svc:22" }
209261

210-
//console.log(parse(test))
211-
//parse(test)
212262
module.exports.parse = parse;

scanners/ssh-audit/parser/parser.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ const {
88
validateParser,
99
} = require("@securecodebox/parser-sdk-nodejs/parser-utils");
1010

11-
// eslint-disable-next-line security/detect-non-literal-fs-filename
11+
1212
const readFile = util.promisify(fs.readFile);
1313

1414
const {parse} = require("./parser");
1515

16-
test("ssh-audit parser parses a result into proper findings", async () => {
16+
test("ssh-audit parser parses a result into proper findings for dummy-ssh", async () => {
1717
const hosts = JSON.parse(
1818
await readFile(__dirname + "/__testFiles__/dummy-ssh.json", {
1919
encoding: "utf8",
@@ -321,7 +321,7 @@ test("should properly parse empty json file", async () => {
321321

322322

323323

324-
test("ssh-audit parser parses a result into proper findings dfdg", async () => {
324+
test("ssh-audit parser parses a result into proper findings for an example", async () => {
325325
const hosts = JSON.parse(
326326
await readFile(__dirname + "/__testFiles__/example.json", {
327327
encoding: "utf8",

scanners/ssh-audit/templates/ssh-audit-parse-definition.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# SPDX-FileCopyrightText: the secureCodeBox authors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
15
apiVersion: execution.securecodebox.io/v1
26
kind: ParseDefinition
37
metadata:

scanners/ssh-audit/templates/ssh-audit-scan-type.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# SPDX-FileCopyrightText: the secureCodeBox authors
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
15
apiVersion: "execution.securecodebox.io/v1"
26
kind: ScanType
37
metadata:
@@ -21,12 +25,6 @@ spec:
2125
command:
2226
- "sh"
2327
- "/wrapper.sh"
24-
#- "python3"
25-
#- "ssh-audit/ssh-audit.py"
26-
# Remove any user-interation
27-
#- "--no-interaction"
28-
# Output in json format
29-
#- "-j"
3028

3129
resources:
3230
{{- toYaml .Values.scanner.resources | nindent 16 }}

0 commit comments

Comments
 (0)