Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 53 ยท Projects

Project: Server Hardening Checklist Checker

Build a configuration auditor in Node.js: parse a sample SSH and web server config, test it against a hardening baseline, score it and print fixes.

Hardening means reducing a system's attack surface: turn off what you do not need, tighten what remains, and keep it that way. Security benchmarks such as the CIS Benchmarks turn that advice into long checklists. Nobody enjoys ticking hundreds of boxes by hand, and manual checks drift, so teams automate them. Tools that do this for real include Lynis, OpenSCAP and cloud compliance scanners.

In this project you build a small checklist checker in Node.js. It parses a sample SSH server configuration and a few web server settings, compares them with a baseline of rules, gives each rule a severity, computes a score, and prints a fix for every failure. The configuration is made-up text held in strings, so the project cannot change any real machine. It is a stand-in for a real audit tool, and the settings it checks are real OpenSSH-style options. See Privilege Escalation Concepts and OS Hardening for the ideas behind the rules. Audit only systems you own or are authorized to assess, and test changes on a copy first: a hardening change that locks you out of your own server is a classic mistake.

How an auditor works

Read config Parse the settings as data.
Compare Check each rule in a baseline.
Score Weight rules by severity.
Fix Tell the admin what to change.
1 / 2

Step 1: Parse a configuration file

Many Unix services use simple Key value lines with # comments. A parser must handle comments, blank lines and keys that are repeated. Be careful about defaults: if a setting is missing from the file, the software's built-in default applies, and that default may be insecure or may differ between versions. A real auditor asks the running program for its effective configuration (for OpenSSH, sshd -T prints it). Our stand-in treats a missing key as "use the stated default" and says so in its output.

Node.js Parse a Key value config

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Keys are lowercased because OpenSSH keywords are case-insensitive. The "first value wins" rule matches how OpenSSH treats most options, which is why a later line can silently fail to override an earlier one. Knowing such quirks is exactly why you check the effective configuration on real systems.

Step 2: Define the baseline as data

Put rules in a table, not in a pile of if statements. Each rule has an ID, a description, a severity weight, a check function that returns pass or fail, a default for a missing key, and a fix. Data-driven rules are easy to review, extend and map to a published benchmark.

Node.js Run baseline rules and print fixes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The weights make the score honest: failing one high-severity rule costs five times as much as failing a low one. A raw "4 of 5 passed" would hide that. Also notice SSH-05 passes only through the default. That is why the output marks defaults: a setting that is correct by accident can break after an upgrade, so many benchmarks require it to be written down explicitly.

Step 3: Audit several hosts and rank them

Real environments have many machines. Run the same baseline against each configuration and rank the results, listing the most common failures so one fix to a shared template removes many findings at once. Group the failures by rule to see which template or image needs the change.

Node.js Compare hosts and find the common failures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The ranking tells you where to start (web-02, which fails every rule, then legacy-03) and the failure table tells you which shared template to fix first. In a real program you would store results over time so improvement, and regressions after a deployment, are visible.

What is real and what is the stand-in

The options audited here (PermitRootLogin, PasswordAuthentication, MaxAuthTries, X11Forwarding, PermitEmptyPasswords, server_tokens, ssl_protocols, autoindex) are real, widely used settings, and reducing exposure through them is standard advice. The parser is deliberately simplified: it ignores Match blocks, Include files, quoted values and version-specific defaults. The baseline and weights are our teaching examples, not an official benchmark. For real work, use a maintained baseline such as CIS or your vendor's guidance, run a proven scanner, and change production systems through reviewed configuration management, never by hand.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should a hardening auditor look at the effective configuration, not just the config file?

    What the running service actually uses may differ from what you read in one file. Defaults and includes matter.
  2. 2. Why give rules severity weights instead of counting passes?

    Equal counting hides critical failures behind many minor passes. Weights keep the score meaningful.
  3. 3. What is the main safety risk when applying a hardening change such as disabling password SSH logins?

    Always confirm the replacement access method works, keep a second session open, and test on a copy first.
  4. 4. Why is it useful to group failures by rule across many hosts?

    Common failures point to a root cause in a template or process, which is cheaper to fix than editing each host.

Summary

  • Hardening reduces attack surface by disabling what is unnecessary and tightening the rest; benchmarks like CIS list the rules.
  • Parse configuration as data, remember that defaults and overrides decide what really applies, and prefer the effective configuration from the running service.
  • Keep rules in a table with an ID, severity, check, explanation and fix, and score with weights.
  • Audit many hosts, rank them, and fix shared templates first.
  • Change production systems carefully: test first, keep a way back in, and use configuration management.

Next: Practice Exercises with Solutions.