@hardkeel/quantum-scan 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/LICENSE +202 -0
- package/README.md +161 -2
- package/action.yml +44 -0
- package/dist/badge.js +60 -0
- package/dist/cbom.js +59 -0
- package/dist/cli.js +305 -0
- package/dist/community-rules.js +30 -0
- package/dist/community.js +28 -0
- package/dist/ignore.js +75 -0
- package/dist/infra.js +557 -0
- package/dist/map-activity.js +22 -0
- package/dist/map-areas.js +50 -0
- package/dist/map-cloud-keys.js +108 -0
- package/dist/map-host.js +333 -0
- package/dist/map-network.js +518 -0
- package/dist/map-people.js +77 -0
- package/dist/map-rank.js +48 -0
- package/dist/map-report.js +178 -0
- package/dist/map-run.js +271 -0
- package/dist/map-types.js +16 -0
- package/dist/map-web.js +640 -0
- package/dist/pqc.js +32 -0
- package/dist/report.js +179 -0
- package/dist/rules.js +135 -0
- package/dist/sarif.js +43 -0
- package/dist/scan.js +220 -0
- package/dist/score.js +122 -0
- package/dist/secrets.js +108 -0
- package/dist/share-config.js +111 -0
- package/dist/share.js +114 -0
- package/dist/types.js +2 -0
- package/dist/walk.js +63 -0
- package/package.json +51 -4
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Who holds the keys to your cloud data. Reads Terraform/OpenTofu and CloudFormation for storage,
|
|
2
|
+
// databases and backups, and notes where the cloud provider holds the encryption keys.
|
|
3
|
+
// Provider-held keys protect against a stolen disk, not against the provider, an insider there,
|
|
4
|
+
// or a legal demand made to them. For data no third party should be able to read, encrypt it
|
|
5
|
+
// before it leaves your hands, or keep the keys yourself.
|
|
6
|
+
// Advice only: it is listed in the map report and does not change the readiness score.
|
|
7
|
+
export const CUSTODY_FIX = {
|
|
8
|
+
provider: "The provider holds the keys, so it can read this data. Encrypt sensitive data before it's uploaded (client-side encryption), or use keys you hold: an external key store such as AWS KMS External Key Store, Google Cloud External Key Manager or Azure Key Vault Managed HSM with your own keys.",
|
|
9
|
+
"provider-kms": "You manage the key, but it lives in the provider's key service, so the provider could still use it. For data no third party should read, encrypt it before upload or move the key to an external key store you hold.",
|
|
10
|
+
};
|
|
11
|
+
/** Resource types we know, what they hold, and which keys name a key you chose. */
|
|
12
|
+
const TF = {
|
|
13
|
+
aws_s3_bucket_server_side_encryption_configuration: { kind: "storage", keyAttr: /kms_master_key_id\s*=/, defaultOn: true },
|
|
14
|
+
aws_db_instance: { kind: "database", keyAttr: /kms_key_id\s*=/, needsOn: /storage_encrypted\s*=\s*true/ },
|
|
15
|
+
aws_rds_cluster: { kind: "database", keyAttr: /kms_key_id\s*=/, needsOn: /storage_encrypted\s*=\s*true/ },
|
|
16
|
+
aws_docdb_cluster: { kind: "database", keyAttr: /kms_key_id\s*=/, needsOn: /storage_encrypted\s*=\s*true/ },
|
|
17
|
+
aws_neptune_cluster: { kind: "database", keyAttr: /kms_key_arn\s*=/, needsOn: /storage_encrypted\s*=\s*true/ },
|
|
18
|
+
aws_redshift_cluster: { kind: "database", keyAttr: /kms_key_id\s*=/, needsOn: /encrypted\s*=\s*true/ },
|
|
19
|
+
aws_dynamodb_table: { kind: "database", keyAttr: /kms_key_arn\s*=/, defaultOn: true },
|
|
20
|
+
aws_ebs_volume: { kind: "disk", keyAttr: /kms_key_id\s*=/, needsOn: /encrypted\s*=\s*true/ },
|
|
21
|
+
aws_efs_file_system: { kind: "storage", keyAttr: /kms_key_id\s*=/, needsOn: /encrypted\s*=\s*true/ },
|
|
22
|
+
aws_backup_vault: { kind: "backup", keyAttr: /kms_key_arn\s*=/, defaultOn: true },
|
|
23
|
+
aws_sqs_queue: { kind: "storage", keyAttr: /kms_master_key_id\s*=/, needsOn: /sqs_managed_sse_enabled\s*=\s*true|kms_master_key_id/ },
|
|
24
|
+
google_storage_bucket: { kind: "storage", keyAttr: /default_kms_key_name\s*=/, defaultOn: true },
|
|
25
|
+
google_sql_database_instance: { kind: "database", keyAttr: /encryption_key_name\s*=/, defaultOn: true },
|
|
26
|
+
google_bigquery_dataset: { kind: "database", keyAttr: /kms_key_name\s*=/, defaultOn: true },
|
|
27
|
+
google_compute_disk: { kind: "disk", keyAttr: /kms_key_self_link\s*=|raw_key\s*=|rsa_encrypted_key\s*=/, defaultOn: true },
|
|
28
|
+
azurerm_storage_account: { kind: "storage", keyAttr: /customer_managed_key\s*\{/, defaultOn: true },
|
|
29
|
+
azurerm_mssql_database: { kind: "database", keyAttr: /transparent_data_encryption_key_vault_key_id\s*=/, defaultOn: true },
|
|
30
|
+
azurerm_postgresql_flexible_server: { kind: "database", keyAttr: /customer_managed_key\s*\{/, defaultOn: true },
|
|
31
|
+
azurerm_cosmosdb_account: { kind: "database", keyAttr: /key_vault_key_id\s*=/, defaultOn: true },
|
|
32
|
+
};
|
|
33
|
+
const KIND_WORD = { storage: "Cloud storage", database: "Cloud database", backup: "Cloud backups", disk: "Cloud disk" };
|
|
34
|
+
function title(kind, c) {
|
|
35
|
+
return c === "provider" ? `${KIND_WORD[kind]} encrypted with keys the provider holds` : `${KIND_WORD[kind]} keys live in the provider's key service`;
|
|
36
|
+
}
|
|
37
|
+
function advice(path, line, resource, kind, custody) {
|
|
38
|
+
return { target: `${path}:${line}`, resource, kind, custody, ruleId: custody === "provider" ? "cloud-provider-held-keys" : "cloud-keys-in-provider-kms", title: title(kind, custody), fix: CUSTODY_FIX[custody] };
|
|
39
|
+
}
|
|
40
|
+
const strip = (s) => s.replace(/"(?:[^"\\]|\\.)*"/g, '""').replace(/#.*$|\/\/.*$/, "");
|
|
41
|
+
/** Terraform resource blocks: type, name, first line (1-based) and body text. */
|
|
42
|
+
export function tfResources(text) {
|
|
43
|
+
const lines = text.split("\n");
|
|
44
|
+
const out = [];
|
|
45
|
+
for (let i = 0; i < lines.length; i++) {
|
|
46
|
+
const m = lines[i].match(/^\s*resource\s+"([a-z0-9_]+)"\s+"([A-Za-z0-9_-]+)"\s*\{/);
|
|
47
|
+
if (!m)
|
|
48
|
+
continue;
|
|
49
|
+
let depth = 0;
|
|
50
|
+
let j = i;
|
|
51
|
+
for (; j < lines.length; j++) {
|
|
52
|
+
for (const ch of strip(lines[j])) {
|
|
53
|
+
if (ch === "{")
|
|
54
|
+
depth++;
|
|
55
|
+
else if (ch === "}")
|
|
56
|
+
depth--;
|
|
57
|
+
}
|
|
58
|
+
if (depth <= 0)
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
out.push({ type: m[1], name: m[2], line: i + 1, body: lines.slice(i + 1, j).map((l) => l.replace(/^\s*(#|\/\/).*$/, "")).join("\n") });
|
|
62
|
+
i = j;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
export function cloudKeyAdvice(path, text) {
|
|
67
|
+
const p = path.toLowerCase();
|
|
68
|
+
const out = [];
|
|
69
|
+
if (/\.(tf|hcl)$/.test(p)) {
|
|
70
|
+
const res = tfResources(text);
|
|
71
|
+
// An Azure storage account can get its key from a separate resource in the same file.
|
|
72
|
+
const separateAzureKey = res.some((r) => r.type === "azurerm_storage_account_customer_managed_key");
|
|
73
|
+
for (const r of res) {
|
|
74
|
+
const spec = TF[r.type];
|
|
75
|
+
if (!spec)
|
|
76
|
+
continue;
|
|
77
|
+
if (spec.needsOn && !spec.needsOn.test(r.body))
|
|
78
|
+
continue; // encryption off is already a finding of its own
|
|
79
|
+
const id = `${r.type}.${r.name}`;
|
|
80
|
+
if (r.type === "aws_s3_bucket_server_side_encryption_configuration") {
|
|
81
|
+
const own = !/sse_algorithm\s*=\s*"AES256"/.test(r.body) && spec.keyAttr.test(r.body);
|
|
82
|
+
out.push(advice(path, r.line, id, spec.kind, own ? "provider-kms" : "provider"));
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// A customer-supplied key (raw_key or rsa_encrypted_key) is one you hold: Google keeps only a hash of it.
|
|
86
|
+
if (r.type === "google_compute_disk" && /raw_key\s*=|rsa_encrypted_key\s*=/.test(r.body))
|
|
87
|
+
continue;
|
|
88
|
+
if (r.type === "azurerm_storage_account" && separateAzureKey) {
|
|
89
|
+
out.push(advice(path, r.line, id, spec.kind, "provider-kms"));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
out.push(advice(path, r.line, id, spec.kind, spec.keyAttr.test(r.body) ? "provider-kms" : "provider"));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (/\.(ya?ml|json|template)$/.test(p) && /AWSTemplateFormatVersion|AWS::/.test(text)) {
|
|
96
|
+
const lines = text.split("\n");
|
|
97
|
+
lines.forEach((l, i) => {
|
|
98
|
+
const m = l.match(/["']?SSEAlgorithm["']?\s*:\s*["']?(AES256|aws:kms)["']?/);
|
|
99
|
+
if (!m)
|
|
100
|
+
return;
|
|
101
|
+
// The key can sit just before or just after (JSON tools often sort keys, putting KMSMasterKeyID first).
|
|
102
|
+
const near = lines.slice(Math.max(0, i - 3), i + 4).join("\n");
|
|
103
|
+
const custody = m[1] === "AES256" || !/KMSMasterKeyID/.test(near) ? "provider" : "provider-kms";
|
|
104
|
+
out.push(advice(path, i + 1, "S3 bucket encryption", "storage", custody));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
package/dist/map-host.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// This machine: reads local encryption settings only. No network connections, no changes,
|
|
2
|
+
// and private keys are never opened (only .pub files and certificates).
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { X509Certificate } from "node:crypto";
|
|
5
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { ALGORITHMS } from "./rules.js";
|
|
9
|
+
import { emptyResult } from "./map-types.js";
|
|
10
|
+
import { certKey, certSignature, sshFindings } from "./map-web.js";
|
|
11
|
+
export const realIO = {
|
|
12
|
+
platform: process.platform,
|
|
13
|
+
home: homedir(),
|
|
14
|
+
read(p) {
|
|
15
|
+
try {
|
|
16
|
+
return readFileSync(p, "utf8");
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
list(d) {
|
|
23
|
+
try {
|
|
24
|
+
return readdirSync(d);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
run(cmd, args) {
|
|
31
|
+
try {
|
|
32
|
+
return execFileSync(cmd, args, { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 15000, windowsHide: true });
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
// ssh -V prints to stderr and some tools exit non-zero while still answering.
|
|
36
|
+
const err = e;
|
|
37
|
+
const out = `${err.stdout ?? ""}${err.stderr ?? ""}`;
|
|
38
|
+
return out || null;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
/** First value of each setting wins, as in sshd. Follows Include lines one level deep. */
|
|
43
|
+
export function parseSshd(text, io, dir, out = {}) {
|
|
44
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
45
|
+
const line = raw.trim();
|
|
46
|
+
if (!line || line.startsWith("#"))
|
|
47
|
+
continue;
|
|
48
|
+
if (/^match\s/i.test(line))
|
|
49
|
+
break; // settings after Match apply only to some users
|
|
50
|
+
const m = line.match(/^(\S+)\s+(.+)$/);
|
|
51
|
+
if (!m)
|
|
52
|
+
continue;
|
|
53
|
+
const key = m[1].toLowerCase();
|
|
54
|
+
const val = m[2].trim();
|
|
55
|
+
if (key === "include" && io && dir) {
|
|
56
|
+
for (const pattern of val.split(/\s+/)) {
|
|
57
|
+
const full = pattern.startsWith("/") || /^[A-Za-z]:/.test(pattern) ? pattern : join(dir, pattern);
|
|
58
|
+
const star = full.lastIndexOf("*");
|
|
59
|
+
if (star >= 0) {
|
|
60
|
+
const folder = full.slice(0, full.lastIndexOf("/", star));
|
|
61
|
+
const suffix = full.slice(star + 1);
|
|
62
|
+
for (const f of (io.list(folder) ?? []).filter((n) => n.endsWith(suffix)).sort()) {
|
|
63
|
+
const t = io.read(join(folder, f));
|
|
64
|
+
if (t)
|
|
65
|
+
parseSshd(t, undefined, undefined, out);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const t = io.read(full);
|
|
70
|
+
if (t)
|
|
71
|
+
parseSshd(t, undefined, undefined, out);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (key === "kexalgorithms")
|
|
76
|
+
out.kex ??= val;
|
|
77
|
+
if (key === "ciphers")
|
|
78
|
+
out.ciphers ??= val;
|
|
79
|
+
if (key === "macs")
|
|
80
|
+
out.macs ??= val;
|
|
81
|
+
if (key === "hostkeyalgorithms")
|
|
82
|
+
out.hostKeyAlgorithms ??= val;
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/** OpenSSH version from `ssh -V`, e.g. 9.6. */
|
|
87
|
+
export function opensshVersion(text) {
|
|
88
|
+
const m = text?.match(/OpenSSH_for_Windows_(\d+)\.(\d+)|OpenSSH_(\d+)\.(\d+)/);
|
|
89
|
+
if (!m)
|
|
90
|
+
return null;
|
|
91
|
+
return Number(m[1] ?? m[3]) + Number(m[2] ?? m[4]) / 10;
|
|
92
|
+
}
|
|
93
|
+
const explicit = (v) => (v && !/^[+\-^]/.test(v) ? v.split(",").map((s) => s.trim()) : null);
|
|
94
|
+
function checkSshd(io, log, res) {
|
|
95
|
+
const dir = io.platform === "win32" ? "C:/ProgramData/ssh" : "/etc/ssh";
|
|
96
|
+
const path = `${dir}/sshd_config`;
|
|
97
|
+
const text = io.read(path);
|
|
98
|
+
if (text === null)
|
|
99
|
+
return; // no SSH server here
|
|
100
|
+
log.record("host", path, "read SSH server settings");
|
|
101
|
+
res.checked.push(path);
|
|
102
|
+
const s = parseSshd(text, io, dir);
|
|
103
|
+
const version = opensshVersion(io.run("ssh", ["-V"]));
|
|
104
|
+
log.record("host", "ssh -V", "read the installed OpenSSH version");
|
|
105
|
+
const kex = explicit(s.kex);
|
|
106
|
+
// Defaults: OpenSSH 9.0 added sntrup761x25519 by default, 9.9 added mlkem768x25519.
|
|
107
|
+
const defaultPqc = version !== null && version >= 9.0;
|
|
108
|
+
const lists = {
|
|
109
|
+
kex: kex ?? (defaultPqc ? ["sntrup761x25519-sha512"] : ["curve25519-sha256"]),
|
|
110
|
+
ciphers: explicit(s.ciphers) ?? [],
|
|
111
|
+
macs: explicit(s.macs) ?? [],
|
|
112
|
+
hostKeys: explicit(s.hostKeyAlgorithms) ?? [],
|
|
113
|
+
};
|
|
114
|
+
const r = sshFindings(lists, "host", path, false);
|
|
115
|
+
// Host keys are judged from the key files below, not the algorithm list.
|
|
116
|
+
res.findings.push(...r.findings.filter((f) => f.ruleId !== "ssh-host-key"));
|
|
117
|
+
if (r.pqc)
|
|
118
|
+
res.strengths.push({ area: "host", target: path, algorithm: "Hybrid SSH key exchange", title: kex ? "SSH server allows quantum-safe hybrid key exchange" : `SSH server uses quantum-safe hybrid key exchange by default (OpenSSH ${version})` });
|
|
119
|
+
if (!kex && version === null)
|
|
120
|
+
res.skipped.push({ target: path, reason: "couldn't read the OpenSSH version, so the default key exchange is a guess" });
|
|
121
|
+
}
|
|
122
|
+
// ---------- public key files ----------
|
|
123
|
+
/** Type and size of an OpenSSH public key line. Never reads private keys. */
|
|
124
|
+
export function sshPublicKey(line) {
|
|
125
|
+
const m = line.trim().match(/^(ssh-rsa|ssh-dss|ssh-ed25519|ssh-ed448|ecdsa-sha2-nistp\d+|sk-[a-z0-9@.-]+)\s+([A-Za-z0-9+/=]+)/);
|
|
126
|
+
if (!m)
|
|
127
|
+
return null;
|
|
128
|
+
if (m[1] !== "ssh-rsa")
|
|
129
|
+
return { type: m[1] };
|
|
130
|
+
try {
|
|
131
|
+
const b = Buffer.from(m[2], "base64");
|
|
132
|
+
let off = 0;
|
|
133
|
+
const next = () => {
|
|
134
|
+
const len = b.readUInt32BE(off);
|
|
135
|
+
const v = b.subarray(off + 4, off + 4 + len);
|
|
136
|
+
off += 4 + len;
|
|
137
|
+
return v;
|
|
138
|
+
};
|
|
139
|
+
next(); // "ssh-rsa"
|
|
140
|
+
next(); // e
|
|
141
|
+
const n = next();
|
|
142
|
+
const lead = n[0] === 0 ? 1 : 0;
|
|
143
|
+
const bytes = n.length - lead;
|
|
144
|
+
const topBits = 8 - Math.clz32(n[lead]) + 24;
|
|
145
|
+
return { type: "ssh-rsa", bits: (bytes - 1) * 8 + topBits };
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return { type: "ssh-rsa" };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function keyFinding(path, k, role) {
|
|
152
|
+
const base = { area: "host", target: path, internetFacing: false };
|
|
153
|
+
if (k.type === "ssh-dss")
|
|
154
|
+
return { ...base, ruleId: "host-key-dsa", title: `${role} uses DSA`, algorithm: "DSA", primitive: "signature", risk: "weak-now", severity: "high", fix: ALGORITHMS.DSA.fix };
|
|
155
|
+
if (k.type === "ssh-rsa" && k.bits && k.bits < 2048)
|
|
156
|
+
return { ...base, ruleId: "host-key-small-rsa", title: `${role} uses a ${k.bits}-bit RSA key`, algorithm: "RSA", primitive: "signature", risk: "weak-now", severity: "high", keySize: k.bits, fix: "Replace with an Ed25519 key, or RSA of at least 3072 bits." };
|
|
157
|
+
const alg = k.type === "ssh-rsa" ? "RSA signature" : k.type.includes("ed25519") ? "Ed25519" : k.type.includes("ed448") ? "Ed448" : "ECDSA";
|
|
158
|
+
return { ...base, ruleId: "host-key-quantum", title: `${role} uses ${alg === "RSA signature" ? `${k.bits ?? ""}-bit RSA`.trim() : alg}`, algorithm: alg, primitive: "signature", risk: "quantum-vulnerable", severity: "medium", keySize: k.bits, fix: "Keep it for now; plan to move to post-quantum signatures once OpenSSH supports them." };
|
|
159
|
+
}
|
|
160
|
+
function checkKeyFiles(io, log, res) {
|
|
161
|
+
const dirs = [
|
|
162
|
+
[io.platform === "win32" ? "C:/ProgramData/ssh" : "/etc/ssh", "SSH host key"],
|
|
163
|
+
[join(io.home, ".ssh"), "Your SSH key"],
|
|
164
|
+
];
|
|
165
|
+
for (const [dir, role] of dirs) {
|
|
166
|
+
const names = io.list(dir);
|
|
167
|
+
if (!names)
|
|
168
|
+
continue;
|
|
169
|
+
for (const name of names.filter((n) => n.endsWith(".pub")).sort()) {
|
|
170
|
+
const path = join(dir, name).split("\\").join("/");
|
|
171
|
+
const text = io.read(path);
|
|
172
|
+
if (!text)
|
|
173
|
+
continue;
|
|
174
|
+
log.record("host", path, "read public key type and size");
|
|
175
|
+
const k = sshPublicKey(text);
|
|
176
|
+
if (!k)
|
|
177
|
+
continue;
|
|
178
|
+
res.checked.push(path);
|
|
179
|
+
res.findings.push(keyFinding(path, k, role));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// ---------- OpenSSL and system policy ----------
|
|
184
|
+
export function opensslPolicyFindings(text, path) {
|
|
185
|
+
const out = [];
|
|
186
|
+
const base = { area: "host", target: path, internetFacing: false };
|
|
187
|
+
const min = text.match(/^\s*MinProtocol\s*=\s*(\S+)/im)?.[1];
|
|
188
|
+
if (min && /^(TLSv1|TLSv1\.1|SSLv3)$/i.test(min)) {
|
|
189
|
+
const a = ALGORITHMS["TLS 1.0/1.1"];
|
|
190
|
+
out.push({ ...base, ruleId: "host-openssl-old-tls", title: `OpenSSL allows ${min} system-wide`, algorithm: "TLS 1.0/1.1", primitive: a.primitive, risk: a.risk, severity: a.severity, fix: "Set MinProtocol = TLSv1.2 in the OpenSSL config." });
|
|
191
|
+
}
|
|
192
|
+
const level = text.match(/^\s*CipherString\s*=.*@SECLEVEL=(\d)/im)?.[1];
|
|
193
|
+
if (level !== undefined && Number(level) < 2) {
|
|
194
|
+
out.push({ ...base, ruleId: "host-openssl-seclevel", title: `OpenSSL security level lowered to ${level}`, algorithm: "Weak TLS ciphers", primitive: "protocol", risk: "weak-now", severity: "medium", fix: "Use @SECLEVEL=2 or higher, which blocks SHA-1 and keys under 2048 bits." });
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
function checkOpenssl(io, log, res) {
|
|
199
|
+
if (io.platform === "win32")
|
|
200
|
+
return;
|
|
201
|
+
for (const path of ["/etc/ssl/openssl.cnf", "/usr/lib/ssl/openssl.cnf", "/etc/pki/tls/openssl.cnf", "/opt/homebrew/etc/openssl@3/openssl.cnf"]) {
|
|
202
|
+
const text = io.read(path);
|
|
203
|
+
if (text === null)
|
|
204
|
+
continue;
|
|
205
|
+
log.record("host", path, "read OpenSSL settings");
|
|
206
|
+
res.checked.push(path);
|
|
207
|
+
res.findings.push(...opensslPolicyFindings(text, path));
|
|
208
|
+
}
|
|
209
|
+
const policy = io.read("/etc/crypto-policies/config");
|
|
210
|
+
if (policy !== null) {
|
|
211
|
+
log.record("host", "/etc/crypto-policies/config", "read the system encryption policy");
|
|
212
|
+
res.checked.push("/etc/crypto-policies/config");
|
|
213
|
+
if (/^\s*LEGACY/m.test(policy)) {
|
|
214
|
+
res.findings.push({ area: "host", target: "/etc/crypto-policies/config", internetFacing: false, ruleId: "host-crypto-policy-legacy", title: "System encryption policy is LEGACY", algorithm: "Weak TLS ciphers", primitive: "protocol", risk: "weak-now", severity: "medium", fix: "Run update-crypto-policies --set DEFAULT (or FUTURE)." });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// ---------- certificates ----------
|
|
219
|
+
function certFromPem(pem) {
|
|
220
|
+
try {
|
|
221
|
+
return new X509Certificate(pem);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
export function localCertFindings(c, now = Date.now()) {
|
|
228
|
+
const base = { area: "host", target: c.target, internetFacing: false };
|
|
229
|
+
const out = [];
|
|
230
|
+
if (c.notAfter < now)
|
|
231
|
+
out.push({ ...base, ruleId: "host-cert-expired", title: `${c.label} has expired`, algorithm: "Expired certificate", primitive: "protocol", risk: "weak-now", severity: "medium", fix: "Renew or remove the expired certificate." });
|
|
232
|
+
if (c.sigWeak)
|
|
233
|
+
out.push({ ...base, ruleId: "host-cert-weak-signature", title: `${c.label} is signed with SHA-1`, algorithm: "SHA-1", primitive: "hash", risk: "weak-now", severity: "high", fix: "Reissue it signed with SHA-256 or stronger." });
|
|
234
|
+
if (c.keyType === "RSA" && c.bits && c.bits < 2048)
|
|
235
|
+
out.push({ ...base, ruleId: "host-cert-small-rsa", title: `${c.label} uses a ${c.bits}-bit RSA key`, algorithm: "RSA", primitive: "signature", risk: "weak-now", severity: "high", keySize: c.bits, fix: "Reissue with at least a 2048-bit RSA key, or ECDSA P-256." });
|
|
236
|
+
else if (c.keyType === "RSA" || c.keyType === "ECDSA" || c.keyType === "Ed25519") {
|
|
237
|
+
out.push({ ...base, ruleId: c.isCa ? "host-ca-quantum" : "host-cert-quantum", title: `${c.label} uses ${c.keyType === "RSA" ? `${c.bits ?? ""}-bit RSA`.trim() : c.keyType}`, algorithm: c.keyType === "RSA" ? "RSA signature" : c.keyType, primitive: "signature", risk: "quantum-vulnerable", severity: "medium", keySize: c.bits, longLived: c.isCa, fix: c.isCa ? "Certificate authorities stay trusted for years: plan an ML-DSA authority by 2030." : "Plan to move to ML-DSA certificates once your certificate authority offers them." });
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
function checkLinuxCerts(io, log, res) {
|
|
242
|
+
if (io.platform === "win32")
|
|
243
|
+
return;
|
|
244
|
+
const live = "/etc/letsencrypt/live";
|
|
245
|
+
for (const name of io.list(live) ?? []) {
|
|
246
|
+
const path = `${live}/${name}/cert.pem`;
|
|
247
|
+
const pem = io.read(path);
|
|
248
|
+
if (!pem)
|
|
249
|
+
continue;
|
|
250
|
+
log.record("host", path, "read certificate details");
|
|
251
|
+
const cert = certFromPem(pem);
|
|
252
|
+
if (!cert)
|
|
253
|
+
continue;
|
|
254
|
+
res.checked.push(path);
|
|
255
|
+
const k = certKey(cert);
|
|
256
|
+
res.findings.push(...localCertFindings({ keyType: k.type, bits: k.bits, sigWeak: Boolean(certSignature(Buffer.from(cert.raw))?.weak), notAfter: Date.parse(cert.validTo), label: `Certificate for ${name}`, target: path }));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// ---------- Windows ----------
|
|
260
|
+
/** Protocols and ciphers explicitly turned on in the Schannel registry. Missing keys mean the Windows default. */
|
|
261
|
+
export function parseSchannel(text) {
|
|
262
|
+
const enabledProtocols = [];
|
|
263
|
+
const enabledCiphers = [];
|
|
264
|
+
let key = "";
|
|
265
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
266
|
+
const line = raw.trim();
|
|
267
|
+
if (/^HKEY_/i.test(line)) {
|
|
268
|
+
key = line;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const m = line.match(/^Enabled\s+REG_DWORD\s+0x([0-9a-f]+)/i);
|
|
272
|
+
if (!m || Number.parseInt(m[1], 16) === 0)
|
|
273
|
+
continue;
|
|
274
|
+
const proto = key.match(/\\Protocols\\(SSL 2\.0|SSL 3\.0|TLS 1\.0|TLS 1\.1)\\Server$/i);
|
|
275
|
+
if (proto)
|
|
276
|
+
enabledProtocols.push(proto[1]);
|
|
277
|
+
const cipher = key.match(/\\Ciphers\\(Triple DES 168|DES 56\/56|RC4 [0-9/]+|NULL)$/i);
|
|
278
|
+
if (cipher)
|
|
279
|
+
enabledCiphers.push(cipher[1]);
|
|
280
|
+
}
|
|
281
|
+
return { enabledProtocols, enabledCiphers };
|
|
282
|
+
}
|
|
283
|
+
const CERT_PS = "Get-ChildItem Cert:\\LocalMachine\\My | ForEach-Object { $s = $null; try { $s = $_.PublicKey.Key.KeySize } catch {}; [pscustomobject]@{ Subject = $_.Subject; NotAfter = $_.NotAfter.ToUniversalTime().ToString('o'); Sig = $_.SignatureAlgorithm.FriendlyName; Key = $_.PublicKey.Oid.FriendlyName; Size = $s } } | ConvertTo-Json -Compress";
|
|
284
|
+
export function parseWindowsCerts(json) {
|
|
285
|
+
try {
|
|
286
|
+
const v = JSON.parse(json.trim() || "[]");
|
|
287
|
+
const list = Array.isArray(v) ? v : [v];
|
|
288
|
+
return list.map((c) => ({ subject: String(c.Subject ?? ""), notAfter: Date.parse(c.NotAfter), sig: String(c.Sig ?? ""), key: String(c.Key ?? ""), size: typeof c.Size === "number" ? c.Size : undefined }));
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
return [];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function checkWindows(io, log, res) {
|
|
295
|
+
if (io.platform !== "win32")
|
|
296
|
+
return;
|
|
297
|
+
const reg = "HKLM\\SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\SCHANNEL";
|
|
298
|
+
const text = io.run("reg", ["query", reg, "/s"]);
|
|
299
|
+
log.record("host", reg, "read Windows TLS (Schannel) settings");
|
|
300
|
+
if (text) {
|
|
301
|
+
res.checked.push(reg);
|
|
302
|
+
const s = parseSchannel(text);
|
|
303
|
+
const base = { area: "host", target: reg, internetFacing: false };
|
|
304
|
+
if (s.enabledProtocols.length) {
|
|
305
|
+
const a = ALGORITHMS["TLS 1.0/1.1"];
|
|
306
|
+
res.findings.push({ ...base, ruleId: "host-schannel-old-tls", title: `Windows has ${s.enabledProtocols.join(" and ")} turned on for servers`, algorithm: "TLS 1.0/1.1", primitive: a.primitive, risk: a.risk, severity: a.severity, fix: "Set Enabled to 0 for these protocols under SCHANNEL\\Protocols, or remove the keys on Windows Server 2022 and later." });
|
|
307
|
+
}
|
|
308
|
+
if (s.enabledCiphers.length) {
|
|
309
|
+
res.findings.push({ ...base, ruleId: "host-schannel-weak-cipher", title: `Windows has ${s.enabledCiphers.join(" and ")} turned on`, algorithm: "Weak TLS ciphers", primitive: "protocol", risk: "weak-now", severity: "high", fix: "Set Enabled to 0 for these ciphers under SCHANNEL\\Ciphers." });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const json = io.run("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", CERT_PS]);
|
|
313
|
+
log.record("host", "Cert:\\LocalMachine\\My", "read this machine's certificate details");
|
|
314
|
+
for (const c of parseWindowsCerts(json ?? "")) {
|
|
315
|
+
const keyType = /RSA/i.test(c.key) ? "RSA" : /EC|ECC|ECDSA/i.test(c.key) ? "ECDSA" : "other";
|
|
316
|
+
const cn = c.subject.match(/CN=([^,]+)/)?.[1] ?? "a certificate";
|
|
317
|
+
res.findings.push(...localCertFindings({ keyType, bits: keyType === "RSA" ? c.size : undefined, sigWeak: /sha1|md5/i.test(c.sig), notAfter: c.notAfter, label: `Certificate for ${cn}`, target: "Cert:\\LocalMachine\\My" }));
|
|
318
|
+
}
|
|
319
|
+
if (json !== null)
|
|
320
|
+
res.checked.push("Cert:\\LocalMachine\\My");
|
|
321
|
+
}
|
|
322
|
+
export function runHost(log, io = realIO) {
|
|
323
|
+
const res = emptyResult("host");
|
|
324
|
+
for (const step of [checkSshd, checkKeyFiles, checkOpenssl, checkLinuxCerts, checkWindows]) {
|
|
325
|
+
try {
|
|
326
|
+
step(io, log, res);
|
|
327
|
+
}
|
|
328
|
+
catch (e) {
|
|
329
|
+
res.errors.push({ area: "host", target: step.name, kind: "read", message: e instanceof Error ? e.message : String(e) });
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return res;
|
|
333
|
+
}
|