diff --git a/README.md b/README.md index 8d43741..815b285 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ phronesis/ | Skill | Version | Category | Purpose | | --- | --- | --- | --- | | [fiona](https://gitea.nolivaldeziii.com/nvaldez/phronesis/src/branch/master/skill/fiona/SKILL.md) | 2.2.0 | productivity | Operate an llm-wiki with the fiona-assistant CLI | +| [linux-server-hardening](https://gitea.nolivaldeziii.com/nvaldez/phronesis/src/branch/master/skill/linux-server-hardening/SKILL.md) | 1.2.0 | security | Numbered checklist to audit, harden, and verify an exposed Debian-family Linux server (SSH, accounts, firewall, fail2ban, updates) | | [memory](https://gitea.nolivaldeziii.com/nvaldez/phronesis/src/branch/master/skill/memory/SKILL.md) | 1.0.1 | productivity | Give a skill its own MEMORIES.md beside its SKILL.md | | [python-tdd](https://gitea.nolivaldeziii.com/nvaldez/phronesis/src/branch/master/skill/python-tdd/SKILL.md) | 1.2.0 | development | Minimalist TDD architect for Python work | | [sectionize-markdown](https://gitea.nolivaldeziii.com/nvaldez/phronesis/src/branch/master/skill/sectionize-markdown/SKILL.md) | 1.2.0 | documentation | Number headings and list items so each rule can be cited | diff --git a/skill/linux-server-hardening/MEMORIES.md b/skill/linux-server-hardening/MEMORIES.md new file mode 100644 index 0000000..a8ad281 --- /dev/null +++ b/skill/linux-server-hardening/MEMORIES.md @@ -0,0 +1,173 @@ +--- +name: skill-memories +version: 1.0.1 +title: linux-server-hardening Memories +created: 2026-11-19 +updated: 2026-11-19 +type: summary +--- + +# linux-server-hardening Memories + + + +- [1. How to use this file](#1-how-to-use-this-file) + - [1.1 When to read](#11-when-to-read) + - [1.2 When to write](#12-when-to-write) + - [1.3 How to write](#13-how-to-write) + - [1.4 How the memory evolves](#14-how-the-memory-evolves) +- [2. Output and wording](#2-output-and-wording) + - [2.1 Style and format](#21-style-and-format) + - [2.2 Placement and naming](#22-placement-and-naming) +- [3. Inputs and sources](#3-inputs-and-sources) + - [3.1 Where inputs come from](#31-where-inputs-come-from) + - [3.2 Quirks and parsing](#32-quirks-and-parsing) +- [4. Steps and tools](#4-steps-and-tools) + - [4.1 Commands and flags](#41-commands-and-flags) + - [4.2 Machine and shell](#42-machine-and-shell) +- [5. Recurring requests](#5-recurring-requests) + - [5.1 Patterns](#51-patterns) + + + +What users want from the linux-server-hardening skill, kept across sessions: +standing preferences, corrections, repeated request patterns, and +facts a run verified while using this skill. Preferences for the +whole wiki go to the wiki's `MEMORY.md`. Knowledge from sources goes +to pages under `wiki/`. + +## 1. How to use this file + +### 1.1 When to read + +- **1.1.1** Read this file once [the skill](./SKILL.md) is loaded, + before its first step of the session. When it is already in + context, do not read it again. +- **1.1.2** Apply every entry that matches the request. + - **1.1.2.1** An entry overrides a default of the skill and an + entry of the wiki's `MEMORY.md`, for work done with this skill + only. + - **1.1.2.2** An entry never overrides the boundaries in the wiki's + `AGENTS.md`; when the two conflict, follow the boundary and ask + the user. + - **1.1.2.3** An entry never changes a tool contract: a command, a + flag, or a required frontmatter field. +- **1.1.3** Every heading and list item is numbered as + [sectionize-markdown](../../skills/sectionize-markdown/SKILL.md) + describes. Cite an item as `linux-server-hardening MEMORIES.md item 2.1.3`; a + new item takes the next free number in its subsection. + +### 1.2 When to write + +- **1.2.1** Write an entry when one of these happens while the skill + is in use: + - **1.2.1.1** The user states a requirement or preference for this + skill that outlasts the current request ("always", "never", "from + now on", "when you do X"). + - **1.2.1.2** The user corrects the skill's output or a step it + took. + - **1.2.1.3** The same kind of request comes for the second time; + record the pattern and what the user wanted. + - **1.2.1.4** A run verifies a reusable fact about the skill's + inputs, hosts, or tools that the skill does not state. + - **1.2.1.5** The user explicitly asks you to remember something + for this skill. +- **1.2.2** Do not write: + - **1.2.2.1** One-off values that only apply to the current + request. + - **1.2.2.2** Passwords, tokens, cookies, personal data, or + anything the user asks to keep out. + - **1.2.2.3** Preferences that hold for any wiki work; those go to + the wiki's `MEMORY.md`. + - **1.2.2.4** Facts the skill or a wiki page already states; cite + them instead. + - **1.2.2.5** Guesses; only what the user said or a run showed. + +### 1.3 How to write + +- **1.3.1** Put the entry under the `###` subsection that fits. + - **1.3.1.1** When none fits, add a `###` subsection with the next + free number under the matching `##`, or a new `##` category with + the next free number. + - **1.3.1.2** Never put an entry directly under a `##`; its id + would collide with a later subsection. +- **1.3.2** Use the knowledge-entry shape of sectionize-markdown + section 4: a header line with the source tag and a short title, + one child per fact, and a last child with the ref and the date. +- **1.3.3** Source tags: `user` (the user said it), `run` (a command + or tool run showed it), `source` (a file under `raw/` states it), + joined with `+` when several agree. +- **1.3.4** The ref says where to check the fact: the user's request + and its date, the command and its output line, or a page or raw + path. +- **1.3.5** Write the entry in the same turn, then tell the user + which item was added or changed, for example "Saved to linux-server-hardening + MEMORIES.md item 2.1.1". +- **1.3.6** Bump `version` (patch for a changed entry, minor for a + new entry or subsection) and set `updated` in the frontmatter on + every change. +- **1.3.7** Append `- YYYY-MM-DD - linux-server-hardening memory: ` + to `wiki/log.md`, and bump that file too. +- **1.3.8** The change lands in the save of the current ingest cycle + or housekeeping pass. + - **1.3.8.1** With no cycle running, save it on its own with + `fiona-assistant git save --description "linux-server-hardening memory: <item id> <title>"`. + +An entry looks like this: + +```text +- **2.1.1** [user] "Reports open with the verdict" + - **2.1.1.1** The report starts with pass or fail, then the evidence. + - **2.1.1.2** Applies to every run of this skill. + - **2.1.1.3** Ref: user request YYYY-MM-DD; added YYYY-MM-DD. +``` + +### 1.4 How the memory evolves + +- **1.4.1** Before adding an entry, look for a near-duplicate in the + same category; extend it instead and keep its id and its earliest + date. +- **1.4.2** A finding that contradicts an entry becomes its next + child, titled `OVERRIDE: ...`, with the new facts and its own ref. +- **1.4.3** An entry that no longer applies is marked + **Superseded YYYY-MM-DD** with the reason; it is not deleted. +- **1.4.4** An entry that held for several uses graduates; mark it + **Promoted YYYY-MM-DD** with the target. + - **1.4.4.1** A rule for every run of the skill moves into + [the skill](./SKILL.md). + - **1.4.4.2** A preference that turns out to hold for all wiki + work moves to the wiki's `MEMORY.md`. +- **1.4.5** Never renumber ids; pages, log lines, and chats may cite + them. +- **1.4.6** Git is the memory's history: `git log -p MEMORIES.md` in + the skill's folder shows when each entry changed and why. + +## 2. Output and wording + +What the skill produces and how it reads. + +### 2.1 Style and format + +### 2.2 Placement and naming + +## 3. Inputs and sources + +What the skill reads and where it comes from. + +### 3.1 Where inputs come from + +### 3.2 Quirks and parsing + +## 4. Steps and tools + +How the skill's commands and the machine behave. + +### 4.1 Commands and flags + +### 4.2 Machine and shell + +## 5. Recurring requests + +Requests that come back, and what the user wanted each time. + +### 5.1 Patterns diff --git a/skill/linux-server-hardening/SKILL.md b/skill/linux-server-hardening/SKILL.md new file mode 100644 index 0000000..35b5c72 --- /dev/null +++ b/skill/linux-server-hardening/SKILL.md @@ -0,0 +1,215 @@ +--- +name: linux-server-hardening +version: 1.2.0 +description: Load this skill when a Linux server needs hardening before or after exposure to a network (Debian or Raspberry Pi homelab) - a numbered, human-followable checklist that audits listeners and accounts, enforces SSH key-only, closes unused accounts, scopes the firewall, sets up fail2ban and automatic security updates, and verifies every change with a real probe. +category: security +--- + +# Linux Server Hardening + +<!-- toc-start: headings 2..6 --> + +- [1. How to use this checklist](#1-how-to-use-this-checklist) +- [2. Audit the exposure](#2-audit-the-exposure) +- [3. Harden SSH](#3-harden-ssh) +- [4. Close unused accounts](#4-close-unused-accounts) +- [5. Firewall the ingress](#5-firewall-the-ingress) +- [6. Brute force protection](#6-brute-force-protection) +- [7. Automatic security updates](#7-automatic-security-updates) +- [8. Reduce the listening surface](#8-reduce-the-listening-surface) +- [9. Verify and record](#9-verify-and-record) +- [10. Memory](#10-memory) + +<!-- toc-end --> + +A hardening checklist for a Debian-family Linux server, written +so a human can follow it top to bottom. Every heading and list +item has a stable id; cite one as `item 3.4`. Items are facts to +hold and gotchas; boxes are steps to do and verify. The ground +rule throughout: a probe decides, memory and recollection do not. + +## 1. How to use this checklist + +- **1.1** Work in order 2 to 9: audit first (read-only), then + change, then verify. Never batch two changes between probes. +- **1.2** The skill assumes a Debian-family host with sudo; adapt + names and paths to the distribution in front of you. +- **1.3** Before editing any live config, copy it aside: + `sudo cp -a <file> /tmp/<file>.bak-$(date +%Y%m%d)`. +- **1.4** Edit live config in place; never reconstruct it from a + template or memory. Diff against the backup after the edit. +- **1.5** Every change gets a recorded one-line undo (the backup + path, the prior shell, the old rule set). +- **1.6** `sudo` is assumed on every command below; do not run a + shell as root. + +## 2. Audit the exposure + +Read-only; change nothing here. Build the picture before touching +anything. + +- [ ] **2.1** Inventory the TCP and UDP listeners with their + owning processes: + `sudo ss -tlnp` and `sudo ss -ulnp`. +- [ ] **2.2** For every listener, name the service, why it runs, + and who may reach it (public, LAN, loopback). No unexplained + listener stays unexplained. +- [ ] **2.3** Inventory accounts that can log in: + `awk -F: '$7 ~ /(sh|bash)$/ {print $1":"$3}' /etc/passwd`. +- [ ] **2.4** Inventory accounts with a usable password: + `sudo awk -F: '$2 ~ /^\$/ {print $1}' /etc/shadow`. +- [ ] **2.5** Inventory sudo members: `getent group sudo`, and + check `authorized_keys` of every human account for content. +- [ ] **2.6** Record the firewall state: `sudo ufw status + verbose` (or the nftables equivalent). +- [ ] **2.7** Record pending updates: `apt list --upgradable + 2>/dev/null | tail -n +2 | wc -l` and flag the security + ones. +- **2.8** A listener is not an open port: cross the listener list + with the firewall rules. "Open" means reachable from a given + source, and only the firewall says which sources those are. + +## 3. Harden SSH + +- **3.1** Keep everything in a drop-in, not the main file: + `/etc/ssh/sshd_config.d/10-no-password.conf` (included at the + top of `sshd_config` on Debian). +- [ ] **3.2** Write the drop-in with: + `PasswordAuthentication no`, + `KbdInteractiveAuthentication no`, + `PermitRootLogin no`, `MaxAuthTries 3`, + `X11Forwarding no`, `LoginGraceTime 30`. +- [ ] **3.3** Validate before activating: + `sudo sshd -t` must print nothing. +- [ ] **3.4** Activate gracefully: `sudo systemctl reload ssh`. + A reload drops no existing session; a restart can. +- [ ] **3.5** Verify the effective config, not the file: + `sudo sshd -T | grep -Ei '^(passwordauthentication| + kbdinteractiveauthentication|permitrootlogin)'`. +- [ ] **3.6** Verify behavior with a live probe: + `ssh -o PreferredAuthentications=none -o ConnectTimeout=5 + localhost` must answer `Permission denied (publickey)`. +- **3.7** GOTCHA: if VNC or other services tunnel over SSH + (`ssh -L`), `AllowTcpForwarding` must stay `yes`. Turning it + off silently kills the tunnels and looks like "server not + responding". +- **3.8** GOTCHA: `PermitRootLogin without-password` (the + default) still allows root login with a key. An empty + `/root/.ssh/authorized_keys` saves you today; say `no` anyway. +- **3.9** Check the protocol only on museum-grade hosts: SSH + protocol 2 is enforced by the software itself on OpenSSH + 7.6 and later - the `Protocol` directive was removed from + the config language and modern sshd cannot speak protocol 1. + Verify with `ssh -v` (`Remote protocol version 2.0`). A + "force protocol 2" item in a modern guide is a staleness + marker for the guide, not a gap in the host. + +## 4. Close unused accounts + +- [ ] **4.1** For each candidate account, verify first that + nothing uses it: `ps -eo user,args | grep <name>`, `sudo + crontab -l -u <name>`, enabled units, recent logins + (`sudo last | grep <name>`). +- [ ] **4.2** Lock the password: `sudo passwd -l <name>`. +- [ ] **4.3** Set the shell to `/usr/sbin/nologin`: + `sudo usermod -s /usr/sbin/nologin <name>`. +- [ ] **4.4** Verify: `sudo passwd -S <name>` shows `L`, and + `getent passwd <name>` shows the nologin shell. +- **4.5** GOTCHA: a password lock does NOT block SSH key login. + An account with content in `authorized_keys` stays reachable + until its shell is nologin (or the keys are removed). +- **4.6** Record the undo: `sudo passwd -u <name>` plus + `sudo usermod -s /bin/bash <name>` (or the prior shell). + +## 5. Firewall the ingress + +- [ ] **5.1** Set the policy: `sudo ufw default deny incoming` + and `sudo ufw default allow outgoing`. +- [ ] **5.2** Add allow rules scoped to a source, never to + `Anywhere` without a reason; write a comment per rule: + `sudo ufw allow from 192.168.178.0/24 to any port 22 proto + tcp comment 'SSH from LAN'`. +- [ ] **5.3** If access arrives through a proxy or jump host, + allow exactly that one IP for that port. +- [ ] **5.4** Enable and verify: `sudo ufw --force enable; + sudo ufw status verbose`. The current SSH session must + survive the enable; if it does not, you locked yourself + out. +- **5.5** Cover both stacks: repeat rules for the IPv6 prefixes + in use, and note that an ISP global prefix can rotate. + +## 6. Brute force protection + +- [ ] **6.1** Install and enable fail2ban with the sshd jail + (Debian default ships it); verify it counts probes: + `sudo fail2ban-client status sshd`. +- **6.2** GOTCHA: if public access arrives through a stream or + reverse proxy, every internet probe reaches sshd with the + proxy's IP. A ban can then only hit the proxy - cutting the + owner's own public path (a self-DoS an attacker can trigger) + while real attacker IPs stay invisible. +- [ ] **6.3** In that topology, add the proxy to ignoreip + (`/etc/fail2ban/jail.local`, `[DEFAULT]` section: + `ignoreip = 127.0.0.1/8 ::1 <proxy-ip>`), reload, and + verify with `sudo fail2ban-client get sshd ignoreip`. +- **6.4** Per-client banning in that topology belongs on the + proxy host, against its own (real-client-IP) logs. Do not + pretend it works on the backend box. + +## 7. Automatic security updates + +- [ ] **7.1** Install: `sudo apt-get install -y + unattended-upgrades`. +- [ ] **7.2** Enable: write `/etc/apt/apt.conf.d/20auto-upgrades` + with `APT::Periodic::Update-Package-Lists "1";` and + `APT::Periodic::Unattended-Upgrade "1";`. +- [ ] **7.3** Verify the timers are enabled: + `systemctl is-enabled apt-daily.timer + apt-daily-upgrade.timer`. +- [ ] **7.4** Verify the origins with `sudo unattended-upgrades + --dry-run --debug`: the distribution's security origin must + be in the allowed list. +- **7.5** GOTCHA: third-party repositories (vendor repos such as + the Raspberry Pi Foundation origin) are not in the default + allow list and stay manual; re-check `apt list --upgradable` + occasionally. + +## 8. Reduce the listening surface + +- [ ] **8.1** For every listener that survived the audit without + a reason, disable both the service and its socket: + `sudo systemctl disable --now <name>.service <name>.socket` + (example: rpcbind, when no NFS or rpc is in use). +- [ ] **8.2** Verify the port is gone from `sudo ss -tlnp` and + `sudo ss -ulnp`; a socket unit can re-activate a service. +- **8.3** Prefer not running a service over filtering it: a + disabled service is closed to every source, forever. + +## 9. Verify and record + +- [ ] **9.1** Re-run the probes of section 2; compare listener + by listener and rule by rule against the pre-hardening + picture. +- [ ] **9.2** Test the public path from a real outside leg (a + phone on mobile data), not from inside the LAN, and not + through a VPN. +- **9.3** GOTCHA: consumer VPN exits (verified with Mullvad) + silently drop non-web ports; a failed TCP test through such a + tunnel says nothing about the port. If a question remains, use + `https://check-host.net/check-tcp?host=<ip>:<port>` or a phone + on mobile data. +- [ ] **9.4** Record what changed and how to undo it on the + host's wiki entity page, then save the change set as one + commit. A hardening step that is not written down did not + happen. + +## 10. Memory + +- **10.1** After loading this skill, read [MEMORIES](./MEMORIES.md) + once per session. When it is already in context, do not read it + again. +- **10.2** Update it whenever an important memory about this skill + comes up: a lasting preference, a correction, a repeated + request, or a fact a run verified. Also update it whenever the + user explicitly asks you to remember something for this skill. + Section 1 of that file says how. diff --git a/test/test_registry.py b/test/test_registry.py index 2ff4415..1373d36 100644 --- a/test/test_registry.py +++ b/test/test_registry.py @@ -42,10 +42,24 @@ def test_skills_loads_the_bundled_python_tdd(): assert (skill.source_dir / "SKILL.md").is_file() +def test_skills_loads_the_bundled_linux_server_hardening(): + assert "linux-server-hardening" in registry.SKILLS + skill = registry.SKILLS["linux-server-hardening"] + assert skill.version == "1.2.0" + assert skill.category == "security" + assert skill.description + assert (skill.source_dir / "SKILL.md").is_file() + assert (skill.source_dir / "MEMORIES.md").is_file() + + def test_categories_list_the_packed_skills(): assert "python-tdd" in registry.CATEGORIES[registry.SKILLS["python-tdd"].category] + assert "linux-server-hardening" in registry.CATEGORIES["security"] def test_skills_in_category_filters_by_label(): assert registry.skills_in_category("development") == {"python-tdd": registry.SKILLS["python-tdd"]} + assert registry.skills_in_category("security") == { + "linux-server-hardening": registry.SKILLS["linux-server-hardening"], + } assert registry.skills_in_category("nonesuch") == {}