
CyberPatriot Linux hardening in Bash: dry-run mode, backups, bats tests and a containerized dry-run check on Ubuntu 22.04 and 24.04.
CyberPatriot hardening for Linux: a Bash framework that runs the scriptable part of a hardening pass during a timed round. It logs every change and backs up each file before it edits it. Judgment calls stay with the operator.
Built by Andrew Melek for the Lakeside High School CyberPatriot team.
CI runs ShellCheck, the 27 bats tests on Ubuntu 24.04, and the dry-run check on Ubuntu 22.04 and 24.04.
Real output from make dry-run in an Ubuntu 24.04 container, trimmed. The clip is also at docs/media/cphlf-demo.mp4.
[!warning] A real run purges packages, resets the passwords of the accounts you review, and rewrites sshd_config, PAM and firewall rules. Use it on a competition image or a throwaway VM, and start with
--dry-run.
You need Docker and make. Each target starts a throwaway --rm container with the repo mounted read-only, so the script never runs on your machine.
git clone https://gitlab.com/WhereIsXuezugi/cphlf.git
cd cphlf
make dry-run IMAGE=ubuntu:24.04
[00:00] INFO === harden.sh starting on Ubuntu 24.04 ===
[00:00] INFO DRY-RUN MODE: no changes will be made
[...]
[00:01] INFO DRY-RUN: default deny incoming -- would execute: ufw default deny incoming
[00:01] INFO DRY-RUN: default allow outgoing -- would execute: ufw default allow outgoing
[...]
[00:01] INFO === Hardening run complete ===
[...]
check: harden.sh exit 0, stderr empty
check: files changed in /etc /home /usr /var /root: 1
check: /root/hardening-run/harden.log
check: a config with DRY_RUN=0 still dry-runs
check: passed
The check records the sha256 sum, mode and owner of every file under /etc, /home, /usr, /var and /root (skipping /var/cache and /var/log) before and after the run. It fails if the script exits non-zero, writes to stderr, or changes anything outside /root/hardening-run. A second pass uses a config that sets DRY_RUN=0 and checks that the flag still wins.
Targets Ubuntu 22.04 and 24.04 (apt, ufw, PAM).
cp examples/config.env.example my-machine.env
sudo ./bin/harden.sh --config my-machine.env --dry-run
sudo ./bin/harden.sh --config my-machine.env --auto-approve
Set the WANT_* role answers and HARDEN_DEFAULT_PASSWORD in your copy first. examples/config.env.example lists every variable.
| Flag | Effect |
|---|---|
--dry-run | Logs each command instead of running it. Writes only under /root/hardening-run. |
--auto-approve | Purges the reviewed dual-use and legacy packages without asking. |
--config FILE | Presets the role answers. Root sources the file as shell code. |
Flags on the command line win over the config file. These steps still prompt: the account review, new accounts, a password for each reviewed account, the media sweep and the reboot. With stdin closed (</dev/null) each one takes its default: no accounts reviewed, no media deleted, no reboot.
Output goes to /root/hardening-run: harden.log (appended on each run), backups/ and baseline/ (overwritten on each run). Set HARDEN_STATE_DIR to move it.
flowchart LR
cli["bin/harden.sh<br/>flags, config, role answers"] --> mods["lib/*.sh<br/>10 modules in a fixed order"]
mods -->|commands| run["run()<br/>dry run: log it<br/>real run: execute, WARN on failure"]
mods -->|file edits| guard["DRY_RUN check<br/>backup_file first"]
run --> state["/root/hardening-run<br/>log, backups, baseline"]
guard --> state
Commands go through run() in lib/common.sh. A failed command is logged as WARN and the run moves on. File edits that are not a single command check the same DRY_RUN flag and back the file up first. ensure_line_in_file and ensure_block_in_file stop a second run from adding duplicate lines. Reviewed passwords are set again on every run.
| Module | What it does |
|---|---|
packages.sh | Updates the system, runs autoremove and sets apt to update daily. Removes 18 offensive tools and 7 games. Asks before purging 12 dual-use tools and 14 legacy services, default keep. |
firewall.sh | ufw: deny inbound by default, allow outbound, enable. |
ssh.sh | Writes sshd_config to a temp file, checks it with sshd -t, then moves it in. No root login, 3 auth tries, chacha20-poly1305, AES-GCM and AES-CTR ciphers, sftp kept. |
services.sh | Firewall rules for 8 services by role. When a role is off it purges Samba, vsftpd, telnetd, MySQL, Apache and BIND. When on it installs Samba and leaves the shares for you to review, and it installs and hardens vsftpd, MySQL and Apache. Mail and printing get port rules only. |
users.sh | Reviews the accounts you name: delete, admin rights, a password reset and 90/1/7 day aging. Creates new accounts. Locks the password of extra UID 0 accounts and of accounts with an empty password. |
kernel.sh | 24 sysctl keys in one marked block: 15 IPv4 network keys and 9 kernel and filesystem keys. IPv6 goes off only with DISABLE_IPV6=yes. |
pam.sh | Password quality (12 characters, all 4 classes), a history of 5, lockout after 5 failures for 30 minutes on the stock common-auth layout, and login.defs aging of 90/1/7 days. Also holds kernel_login_defs. |
filesystem.sh | Permissions on shadow, passwd, group, home directories and /root. Root-only cron and at, a login banner, a reset of an existing rc.local. Optional: USB, FireWire and Thunderbolt storage off, and a media sweep under /home. Writes SUID, world-writable, unowned and PHP file lists. |
monitoring.sh | Installs fail2ban, auditd (distribution default rules), rkhunter and chkrootkit without recommended packages. ClamAV and the rootkit scan are opt-in. |
forensics.sh | Saves 11 baseline files at the end of the run: system info, users, groups, last logins, processes, services, listening ports, installed and manually installed packages, firewall status and sshd_config. |
tools/ has two helpers: find-port-owner.sh names the process on a port, and list-nonstandard-users.sh lists accounts you did not expect.
| Decision | Default | Why |
|---|---|---|
| SSH password login | On (SSH_PASSWORD_AUTH=yes) | A team without SSH keys keeps a way in. Turn it off once keys are in place. |
| Deleting accounts | Asks, default no | A wrong delete cannot be undone. Your own account is never deleted, demoted or reset. |
| Account passwords | Reset to HARDEN_DEFAULT_PASSWORD | The value in the repo is a placeholder, and the log warns when it is used. Set your own. |
| Dual-use and legacy packages | Asks, default keep | nmap or a telnet server can be part of a machine's role. |
| Media sweep | Off unless ALLOW_MEDIA_FILES=no and you type DELETE | It deletes image, audio and video files under /home. It prints the count first. |
| Account lockout | 5 failures, 30 minutes | Teammates can lock themselves out too. Root is not locked. |
| ClamAV and rootkit scans | Off (INSTALL_CLAMAV, RUN_BASELINE_SCAN) | They are slow. Run them when the round allows. |
| IPv6 | Left on (DISABLE_IPV6=no) | Turn it off only if your checklist asks. |
| GRUB password | Not automated | A mistake can leave the machine unable to boot. |
Inbound traffic is default-deny, with allows for the roles you pick. If the image reports to a scoring agent, run sudo ss -tulpn before hardening and check that it still connects after.