Skip to content

Host Posture: Writing Rules

A Host Posture rule is a YAML document in the lc-posture/v1 language. It says what to read on a host (the probes), when the host has the problem (the violation), what to report (the evidence), and how to check the rule itself (the tests). The sensor runs the reads and evaluates the rule on the host. A rule only reads: it cannot run a command or change anything.

This page walks through writing one rule from start to finish. Two other pages go with it:

Every rule is equally trusted. A rule you write can do everything a rule of LimaCharlie's default pack can do, and the same safety rules apply to both: reads only, bounded in time and memory, and a deny list that keeps secrets out of findings.

Before you start

  • Sensors 5.4.4 or later. Older sensors do not run rules from your hive correctly. See Sensor and evaluator versions.
  • Permissions. Saving a rule needs host_posture.set and sensor.task. Scanning a host and reading results go through the extension and need ext.request.
  • CLI 5.14.0 or later for the commands on this page (pip install --upgrade limacharlie). You can also write rules in the web app: on the Host Posture page, open Rules and choose Create rule. The editor takes the same YAML, and saving runs the same checks.
  • The default pack as a model. limacharlie host-posture install-defaults --disabled writes LimaCharlie's rules into your hive without turning them on, and limacharlie host-posture rules get --key <id> shows one. Reading two or three that check something close to your question is the fastest way to learn the patterns.

How a rule is decided

Keep these four facts in mind. Most mistakes come from forgetting one of them.

  1. Every probe has a status: ok, absent, unreadable, invalid or unsupported. Values (value, files, mode, ...) exist only when the status is ok. Reading a value that is not there is an error.
  2. A pass has to be proven. If anything the conditions read was unreadable or cut short, the result is error, never pass and never not_applicable. Only a violation found anyway, elsewhere, still gives fail. The evaluator enforces this however you write your conditions.
  3. Absence is not proof. If a pass depended on something being absent (a file, a registry value), it does not close a finding unless the rule declares, in absent_is_pass, that the absence really means "not exposed".
  4. Everything read from a host is a string. "1" is not 1. Use num() to compare numbers.

Each rule ends in one of five results on each host: pass, fail, error, not_applicable or unsupported. How a result is decided gives the exact steps.

Tutorial: flag SSH servers that allow too many authentication attempts

MaxAuthTries in sshd_config limits how many passwords or keys a client may try on one SSH connection. OpenSSH defaults to 6, and CIS benchmarks ask for 4 or less. We will write a rule that fails when a Linux host's SSH server allows more than 4.

1. Write down the question

Before any YAML, answer four questions in plain words. They become the rule.

Question Answer
When does the host have the problem? No configuration file sets a global MaxAuthTries of 4 or less. That includes "not set at all", since the default is 6.
When does the rule not apply? sshd is not installed: no configuration file exists.
What does absence mean? A missing sshd_config means sshd is not installed, so it is safe to close old findings. A missing MaxAuthTries line is the problem (the default applies).
What does the person fixing it need? The value found, and which file it came from.

The third question matters most. If absence can hide the problem (a setting whose default is unsafe), the rule must not treat it as a pass.

2. Choose the probes

The setting lives in /etc/ssh/sshd_config and in drop-in files under /etc/ssh/sshd_config.d/. A config probe with the sshd format reads both:

probes:
  sshd:
    config:
      format: sshd
      paths: [/etc/ssh/sshd_config]
      discover:
        - { dir: /etc/ssh/sshd_config.d, file_regex: '\.conf$', max: 16 }
      selectors: [MaxAuthTries]

On a host, this probe returns one entry per file, each with its matches. For a file that sets MaxAuthTries 10 at the top level and MaxAuthTries 2 inside a Match User backup block, the evaluator reports:

sshd:
  status: ok
  truncated: false
  discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
  files:
    - path: /etc/ssh/sshd_config
      status: ok
      parse_status: ok
      matches:
        - { selector: MaxAuthTries, found: true, key: MaxAuthTries, value: "10", kind: scalar, line: 3, value_truncated: false }
        - { selector: MaxAuthTries, found: true, key: MaxAuthTries, value: "2", kind: scalar, line: 5, value_truncated: false, context: "Match User backup" }

The value is the string "10". A match inside a Match block carries a context, and a global one has none. The helper global_first(f, "MaxAuthTries") returns the first match with no context, which is the value sshd uses outside Match blocks.

3. Write the conditions

applies_when says when the rule is relevant: some configuration file exists.

applies_when: '!sshd.files.all(f, absent(f))'

violation is true when the host has the problem: no file sets a global value of 4 or less.

violation: >
  !sshd.files.exists(f, ok(f) &&
    global_first(f, "MaxAuthTries") != null &&
    num(global_first(f, "MaxAuthTries").value) <= 4)

num() turns the string into an integer. Without it, value <= 4 compares a string with an integer, which is an error on every host.

4. Find the bugs: the tests catch them

Tests mock what the probes return and state the result you expect. The hive runs them when you save the rule and refuses the rule if one fails. Write one test per answer in step 1, and always one where a file cannot be read:

tests:
  - name: not set, so the default of 6 applies
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files:
          - path: /etc/ssh/sshd_config
            status: ok
            parse_status: ok
            matches: [{ selector: MaxAuthTries, found: false, line: 0 }]
    expect: fail
  - name: sshd_config cannot be read
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files: [{ path: /etc/ssh/sshd_config, status: unreadable }]
    expect: error

Saving the rule with these conditions is refused. The refusal starts with host_posture_rule: the rule is invalid: and lists each problem, separated by semicolons. Here it includes:

FAIL rule.tests[sshd_config cannot be read]: expect error, got fail

The test found a real bug. When sshd_config is unreadable, ok(f) skips it, no file "sets a safe value", and the rule fails the host, claiming a problem it never saw. For a condition of the form "no file proves the fix", select files with !absent(f) instead. An unreadable file is then read, the read is an error, and the result is error, which is honest:

violation: >
  !sshd.files.exists(f, !absent(f) &&
    global_first(f, "MaxAuthTries") != null &&
    num(global_first(f, "MaxAuthTries").value) <= 4)

The opposite holds when violation collects hits (a list of failing files): select with ok(f), so one unreadable file does not hide a real hit in another. See ok(f) or !absent(f).

Now add the test for a host without sshd:

  - name: sshd is not installed
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files: [{ path: /etc/ssh/sshd_config, status: absent }]
    expect: not_applicable

The hive refuses it, with:

ABS rule.tests[sshd is not installed]: the result rests on absent items absent_is_pass does not cover (sshd: file /etc/ssh/sshd_config, sshd: discovered /etc/ssh/sshd_config.d)

The result rests on a missing file, and the rule has not said that a missing file means "not exposed". Step 1 answered that question: no sshd_config means sshd is not installed. Say so:

absent_is_pass: [sshd]

Had the answer been "a missing file can hide the problem", the right fix would be to keep the declaration out and state the result in the test with by_absence: true. A not_applicable by absence closes no finding, which is what you want when the default without the file is unsafe.

Other mistakes the tests catch, with the problem the hive lists (a mistake can produce one line per affected test):

Mistake Message
A misspelled field: .valeu FAIL rule.tests[set to 10]: expect fail, got error
A string compared with an integer: value <= 4 FAIL rule.tests[set to 10]: expect fail, got error
A selector that is not declared: global_first(f, "MaxAuthTrie") LANG rule.violation: global_first(): selector "MaxAuthTrie" is not listed in any config probe's selectors

The first two are only caught by tests. The hive does not check field names or types, so a rule without a test that reaches the field saves fine and reports error on every host.

5. Add evidence and the rest

Evidence is sent only on fail. With the instance strings and the message of an expression error, it is the only host content that leaves the host. Report what the person fixing the host needs:

evidence:
  set_to: >
    sshd.files.filter(f, ok(f) && global_first(f, "MaxAuthTries") != null)
      .map(f, f.path + ": " + global_first(f, "MaxAuthTries").value)

Then fill in title, description, severity, remediation.text and limitations, and standards if the check maps to a control of a benchmark you follow (for example cis: ["<control id>"], from your own copy of the benchmark). The finding shows the first four. limitations tells the next reader what the rule cannot see. Here is the finished rule:

apiVersion: lc-posture/v1
kind: Signature
id: acme.linux.sshd.max-auth-tries
version: 1
title: SSH allows more than 4 authentication attempts per connection
description: >
  MaxAuthTries sets how many passwords or keys a client may try on one SSH connection.
  The OpenSSH default is 6. A lower value slows down password guessing and stops a client
  from cycling through a large set of stolen keys.
severity: low
platforms: [linux]
tags: [baseline:sshd]
requires:
  evaluator: "1.1"
probes:
  sshd:
    config:
      format: sshd
      paths: [/etc/ssh/sshd_config]
      discover:
        - { dir: /etc/ssh/sshd_config.d, file_regex: '\.conf$', max: 16 }
      selectors: [MaxAuthTries]
applies_when: '!sshd.files.all(f, absent(f))'
violation: >
  !sshd.files.exists(f, !absent(f) &&
    global_first(f, "MaxAuthTries") != null &&
    num(global_first(f, "MaxAuthTries").value) <= 4)
evidence:
  set_to: >
    sshd.files.filter(f, ok(f) && global_first(f, "MaxAuthTries") != null)
      .map(f, f.path + ": " + global_first(f, "MaxAuthTries").value)
absent_is_pass: [sshd]
remediation:
  text: Add "MaxAuthTries 4" near the top of /etc/ssh/sshd_config and reload sshd.
limitations: >
  Passes when any file sets a global value of 4 or less. It does not work out which file
  sshd reads first, and it ignores values inside Match blocks.
tests:
  - name: not set, so the default of 6 applies
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files:
          - path: /etc/ssh/sshd_config
            status: ok
            parse_status: ok
            matches: [{ selector: MaxAuthTries, found: false, line: 0 }]
    expect: fail
    evidence: { set_to: [] }
  - name: set to 3 in a drop-in file
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: ok, count: 1 }]
        files:
          - path: /etc/ssh/sshd_config
            status: ok
            parse_status: ok
            matches: [{ selector: MaxAuthTries, found: false, line: 0 }]
          - path: /etc/ssh/sshd_config.d/50-hardening.conf
            status: ok
            parse_status: ok
            matches: [{ selector: MaxAuthTries, found: true, key: MaxAuthTries, value: "3", kind: scalar, line: 1 }]
    expect: pass
  - name: set to 10
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files:
          - path: /etc/ssh/sshd_config
            status: ok
            parse_status: ok
            matches: [{ selector: MaxAuthTries, found: true, key: MaxAuthTries, value: "10", kind: scalar, line: 40 }]
    expect: fail
    evidence: { set_to: ["/etc/ssh/sshd_config: 10"] }
  - name: 2 only inside a Match block does not count
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files:
          - path: /etc/ssh/sshd_config
            status: ok
            parse_status: ok
            matches: [{ selector: MaxAuthTries, found: true, key: MaxAuthTries, value: "2", kind: scalar, line: 120, context: "Match User backup" }]
    expect: fail
  - name: sshd is not installed
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files: [{ path: /etc/ssh/sshd_config, status: absent }]
    expect: not_applicable
  - name: sshd_config cannot be read
    probes:
      sshd:
        status: ok
        discovered: [{ dir: /etc/ssh/sshd_config.d, status: absent, count: 0 }]
        files: [{ path: /etc/ssh/sshd_config, status: unreadable }]
    expect: error

A few rules about the mocks, which the hive enforces:

  • Every probe in every test. A probe left out reads as unsupported.
  • Values in quotes: value: "3", never value: 3. line is an integer.
  • A config mock lists every literal path of paths and has a discovered entry for every discover directory.
  • A file that is not ok has no matches.

6. Validate and save

A hive record holds the rule under rule:, and optionally a scope: that limits it to some of your sensors by tag. Save this as max-auth-tries.yaml, with the finished rule indented under rule::

rule:
  apiVersion: lc-posture/v1
  kind: Signature
  id: acme.linux.sshd.max-auth-tries
  # ... the rest of the rule from step 5, indented by two spaces
scope:
  exclude_sensor_tags: [lab]

Check it without saving, then save it enabled:

limacharlie hive validate --hive-name host_posture_rule \
  --key acme.linux.sshd.max-auth-tries --input-file max-auth-tries.yaml

limacharlie host-posture rules set \
  --key acme.linux.sshd.max-auth-tries --input-file max-auth-tries.yaml --enabled

hive validate runs the same checks and tests as a save. It prints Record 'acme.linux.sshd.max-auth-tries' is valid. or the list of problems, each naming its place in the record, such as rule.violation or rule.tests[set to 10]. Only the problems of the first stage that fails are listed (required fields and their types, then the record name and scope, then everything else), so fix them and validate again. The record name (--key) must equal rule.id.

Pass --enabled. A new record saved with --comment or --tag but without --enabled is stored disabled.

7. Run it on a host

Scan one online Linux host and read its results:

limacharlie host-posture scan --sid <sensor id>
limacharlie host-posture results host --sid <sensor id>

Find acme.linux.sshd.max-auth-tries in the host's results. A fail shows the evidence you declared. An error shows a reason, which tells you what to fix:

Reason What it means What to do
expression:violation:probe is unreadable The condition read a file entry that could not be read: for this rule, an unreadable sshd_config. Check the file's permissions. For a sensor in a container, see Sensors in containers.
expression:violation:probe is invalid The condition read a file entry that did not parse, or a path that was refused (a symlink leaving its directory, a file over 10 MB). Check the file on the host.
unreadable:sshd The conditions finished, but a file or directory they depend on could not be read or parsed, so a pass could not be proven. As above.
truncated:sshd The probe hit a cap: more drop-in files than the discover max (16 here), or more than 32 files in total. Raise max, or narrow paths and discover.
expression:violation:no such key: valeu The condition read a field that is not in the result: a misspelled field, or matches of a file entry that is not ok. Fix the field name or the guard, and add a test that reaches it.
expression:violation:no such overload: string < int A string compared with an integer. Use num().
evaluator_timeout The scan ran out of time before this rule. Look for a rule that reads too much.

unsupported means the host cannot run the rule: a platform not in platforms, sensor 5.4.2, a probe type the platform lacks (packages on Windows), or a probe spec the sensor cannot parse. Sensors older than 5.4.2 send nothing and show as not assessed. On sensor 5.4.3, a rule whose id does not start with lc. can report error or not_applicable where 5.4.4 reports pass.

Results arrive a few seconds after the scan. Scheduled sweeps then check every host the rule applies to on the extension's schedule, daily by default. Rule changes reach the next sweep within about a minute.

8. Change it later

Raise version whenever you change the probes or the conditions. Open findings keep their history across versions. A finding closes only when a later scan proves the problem gone: a pass, or a not_applicable decided on evidence. Disabling or deleting the rule closes its findings as retired, on each host at its next scan.

Checklist

Use this list when you write a rule, or when you ask an AI assistant to write one. It covers what the hive checks and what it cannot.

  1. apiVersion: lc-posture/v1, kind: Signature, an id of at least two lowercase parts under your own prefix (acme.linux.sshd.max-auth-tries), version, title, severity, platforms, probes and violation.
  2. requires: {evaluator: "1.1"}. Never "1.2".
  3. Only the probe types of the reference, with their keys spelled exactly as there (paths, selectors, discover, file_regex). The hive does not check keys inside a probe, and a misspelled one can make a rule pass without reading anything. yara is not evaluated by sensors, and packages does not run on Windows. Probe names in lowercase.
  4. Sysctl keys under kernel., fs., net., user., vm. or dev.. Registry keys under the allowed roots. Catalog names copied from the catalog. The hive does not check these three.
  5. At most 32 files per config probe on a host, and a discover max above the number of files a real host has.
  6. A status check before every value: !absent(p) && p.value == "1", or ok(f) / !absent(f) on file entries as explained above.
  7. Strings compared with strings: p.value == "1", num(p.value) >= 2. Never p.value == 1.
  8. has() before a field that can be missing (context, blocks, value in registry_values), and a null check before has() on a helper's result (innermost(m) != null && has(innermost(m).args)). Never read truncated or value_truncated.
  9. Literals only for matches() patterns, split() separators, selectors in found/first/last/global_first, and probe indexes.
  10. absent_is_pass for each probe whose absence really means "not exposed", and nowhere else.
  11. Instances that stay the same across scans (paths, hashes), at most 64.
  12. Small evidence, never a secret.
  13. Tests: a fail, a pass, an error with a probe unreadable, a not_applicable when there is an applies_when, and an instances check when violation returns a list. Every probe mocked in every test, every value quoted.
  14. hive validate before saving.

Writing rules with an AI assistant

An assistant writes good rules when it has the language in front of it. Give it the reference, one or two examples close to your question, and the four answers of step 1. Ask it to follow the checklist and to include the tests. Then run limacharlie hive validate on what it returns and paste back any problem it reports. The problems name the field or test at fault, which is usually enough for the assistant to fix the rule.

Watch for three mistakes assistants tend to make: comparing host values with integers, guarding file entries with the wrong one of ok(f) and !absent(f), and putting probes in absent_is_pass whose absence can hide the problem.

Tips

  • One question per rule. Results, severity and standards are per rule.
  • Do not flag a stock configuration unless the default itself is the problem, and then say so in the description.
  • Keep platform targeting in platforms and sensor targeting in the record's scope.
  • YAML text that contains a colon followed by a space must be quoted or written as a > block.
  • Registry keys in single quotes, so backslashes stay literal.

See Also