// Check security: four basics of this computer, each yes, no or couldn't tell, with how it was told. It only asks the // system, with the system's own commands, and changes nothing. Run it from CLIDE, or here: // bun check-security.ts import { readFileSync } from "node:fs"; import { hostname, release, version } from "node:os"; type Answer = { says: "yes" | "no" | "couldn't tell"; how: string }; type Check = { name: string; answer: Answer }; /** A command's output, or null when it isn't there, fails or takes too long. */ function run(command: string[]): string | null { try { const done = Bun.spawnSync(command, { stdout: "pipe", stderr: "pipe", timeout: 15_000 }); return done.exitCode === 0 ? done.stdout.toString().trim() : null; } catch { return null; } } const powershell = (script: string) => run(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script]); const yes = (how: string): Answer => ({ says: "yes", how }); const no = (how: string): Answer => ({ says: "no", how }); const unsure = (how: string): Answer => ({ says: "couldn't tell", how }); function mac(): Check[] { const vault = run(["fdesetup", "status"]); const wall = run(["/usr/libexec/ApplicationFirewall/socketfilterfw", "--getglobalstate"]); const updates = run(["defaults", "read", "/Library/Preferences/com.apple.SoftwareUpdate", "AutomaticallyInstallMacOSUpdates"]); const name = run(["sw_vers", "-productName"]) ?? "macOS"; const os = run(["sw_vers", "-productVersion"]); return [ { name: "Disk encrypted", answer: vault === null ? unsure("FileVault didn't say: look in System Settings → Privacy & Security → FileVault.") : /FileVault is On/i.test(vault) ? yes("FileVault is on.") : no("FileVault is off.") }, { name: "Firewall on", answer: wall === null ? unsure("The firewall didn't say: look in System Settings → Network → Firewall.") : /enabled|State = [12]/i.test(wall) ? yes("The macOS firewall is on.") : no("The macOS firewall is off.") }, { name: "Updates automatic", answer: updates === null ? unsure("Not set on this Mac: look in System Settings → General → Software Update → Automatic updates.") : updates === "1" ? yes("macOS updates install by themselves.") : no("macOS updates don't install by themselves.") }, { name: "System", answer: os ? yes(`${name} ${os}.`) : unsure("sw_vers didn't answer.") }, ]; } function windows(): Check[] { // Read the way Explorer does, which needs no administrator: 1, 3 and 5 are on, 2 is off. const locker = powershell("(New-Object -ComObject Shell.Application).NameSpace($env:SystemDrive).Self.ExtendedProperty('System.Volume.BitLockerProtection')"); const walls = powershell("Get-NetFirewallProfile | ForEach-Object { \"$($_.Name)=$($_.Enabled)\" }"); const noAuto = powershell("(Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Policies\\Microsoft\\Windows\\WindowsUpdate\\AU' -Name NoAutoUpdate -ErrorAction SilentlyContinue).NoAutoUpdate"); const product = powershell("(Get-CimInstance Win32_OperatingSystem).Caption"); const off = (walls ?? "").split(/\r?\n/).filter((line) => /=False$/i.test(line)).map((line) => line.split("=")[0]); return [ { name: "Disk encrypted", answer: locker === null || locker === "" ? unsure("BitLocker didn't say: look in Settings → Privacy & security → Device encryption.") : ["1", "3", "5"].includes(locker) ? yes("BitLocker is on for the system drive.") : no("BitLocker is off for the system drive.") }, { name: "Firewall on", answer: walls === null ? unsure("The firewall didn't say: look in Windows Security → Firewall & network protection.") : off.length === 0 ? yes("Windows Firewall is on for every network.") : no(`Windows Firewall is off for: ${off.join(", ")}.`) }, { name: "Updates automatic", answer: noAuto === "1" ? no("A policy on this computer turns automatic updates off.") : yes("Windows Update installs by itself, as Windows does unless told not to.") }, { name: "System", answer: yes(`${product || "Windows"} ${version()} (${release()}).`) }, ]; } function linux(): Check[] { const blocks = run(["lsblk", "-rno", "TYPE"]); const active = (unit: string) => run(["systemctl", "is-active", unit]) === "active"; const enabled = (unit: string) => (run(["systemctl", "is-enabled", unit]) ?? "").startsWith("enabled"); const wall = ["ufw", "firewalld", "nftables"].find(active); const auto = ["unattended-upgrades", "dnf-automatic.timer", "dnf-automatic-install.timer", "packagekit-offline-update"].find(enabled); let system = "Linux"; try { system = /^PRETTY_NAME="?([^"\n]+)"?/m.exec(readFileSync("/etc/os-release", "utf8"))?.[1] ?? system; } catch {} return [ { name: "Disk encrypted", answer: blocks === null ? unsure("lsblk didn't answer.") : /^crypt$/m.test(blocks) ? yes("A disk is encrypted with LUKS.") : no("No disk is encrypted with LUKS.") }, { name: "Firewall on", answer: wall ? yes(`${wall} is running.`) : unsure("None of ufw, firewalld or nftables is running as a service: one may be set up another way.") }, { name: "Updates automatic", answer: auto ? yes(`${auto} is on.`) : unsure("No automatic-update service is on: your system may update another way, or not at all.") }, { name: "System", answer: yes(`${system}, kernel ${release()}.`) }, ]; } const checks = process.platform === "darwin" ? mac() : process.platform === "win32" ? windows() : linux(); const lines = checks.map((check) => (check.name === "System" && check.answer.says === "yes" ? `System: ${check.answer.how}` : `${check.name}: ${check.answer.says}. ${check.answer.how}`)); const nos = checks.filter((check) => check.answer.says === "no").length; console.log([`Security basics of ${hostname()}, ${new Date().toLocaleString()}`, "", ...lines, "", nos === 0 ? "Nothing here says no." : `${nos} of ${checks.length - 1} say no.`].join("\n"));