Skip to content

Writing Your Own Template

Fourteen thousand templates sounds like coverage of everything, until the thing you need to check is yours: a header your own appliance sends, a page your own application should never expose, a string your own company's software prints. Writing a template is a small skill with an outsized return, and the lab's appliance template — the one that has been firing since chapter 3 — is this chapter's specimen. It began as a question: how would I know, from outside, that the imaginary appliance is the thing answering? The answer: it announces itself, with an X-Lab-Appliance header nothing else sends.

The template, whole

examples/lab-appliance-detect.yaml, complete:

id: lab-appliance-detect

info:
  name: LabWidget Appliance Console Detected
  author: mere-mortals-lab
  severity: info
  description: |
    The target answers with an X-Lab-Appliance header. Only the lab
    fixture sends that header, so a match means "the imaginary
    appliance console is here" and nothing more.
  tags: lab,detection

http:
  - method: GET
    path:
      - "{{BaseURL}}/"

    matchers-condition: and
    matchers:
      - type: status
        status:
          - 200

      - type: word
        part: header
        words:
          - "X-Lab-Appliance:"

    extractors:
      - type: regex
        name: appliance_banner
        part: header
        group: 1
        regex:
          - "(?i)x-lab-appliance:\\s*([^\\r\\n]+)"

Nothing in it should be new after chapter 4: the name card, one request, matchers under a condition. The choices worth noticing are the judgement calls:

  • matchers-condition: and. Either test alone is too weak — plenty of things answer 200, and a header word should be believed only on a healthy answer. Together they say one precise thing.
  • The description says what a match does not mean. "…and nothing more" is doing real work: whoever reads this finding in a report, possibly months later, is told the size of the claim.
  • The extractor pulls the header's value into the finding, so the report shows the banner text itself. It is a regex extractor with a name (appliance_banner — the name that appeared in chapter 3's result line) and a group, because the pattern wraps the interesting part in parentheses.

The other two lab templates are variations on the same skeleton and are in examples/ to read: lab-server-banner swaps the word for a different header, and lab-status-page asks for a different path and matches a word in the body instead of the header.

Validate, then prove

$ nuclei -duc -nc -ni -t examples/lab-appliance-detect.yaml -validate
[INF] All templates validated successfully

Validation checks that the file is a well-formed template — fields spelled right, types where types belong. The lab also fed it a deliberately broken copy (the id: field renamed to idd:), and validation named the fault precisely:

[ERR] Error occurred loading template …: yaml: unmarshal errors:
  line 1: field idd not found in type templates.Alias
[FTL] Could not validate templates: errors occurred during template validation

But hold validation to its actual promise: it proves the template is grammatical, not that it is right. This guide's own first draft of the appliance template is the evidence. It matched on the word x-lab-appliance: — lowercase. The fixture sends X-Lab-Appliance:, word matchers are case-sensitive unless told otherwise, and so the template validated cleanly, ran cleanly, and matched nothing, while its two sibling templates fired around it. Two more lessons came from the same small file: the extractor's first pattern, (.+), happily captured the header line's trailing carriage return into the reported value (visible the moment the output was read as JSON), which is why the shipped pattern excludes \r and \n from what it captures.

The only cure for a grammatical-but-wrong template is a target with a known answer, in both directions:

Check: against the sloppy fixture, the template fires and shows its extraction —

[lab-appliance-detect:appliance_banner] [http] [info] http://127.0.0.1:18084/ ["LabWidget/2.1 (lab fixture)"]

— and against the same fixture restarted as tidy, with the header removed, it does not:

$ python3 examples/fixture_server.py tidy   # (restart it)
$ nuclei -duc -nc -ni -t examples/ -u http://127.0.0.1:18084
[INF] Scan completed in 2.739773ms. No results found.

The tidy personality is a small act of hardening, and every lab check respects it: the appliance header is gone, the Server banner has shed its version, /status now answers 401, and — the collection's template from chapter 4, same treatment —

$ nuclei -duc -nc -ni -id http-missing-security-headers -u http://127.0.0.1:18084
[INF] Scan completed in 2.695927ms. No results found.

That pair of runs, trait present and trait removed, is the whole method of this guide in miniature, and the honest way to bring any template of your own into service:

  1. Write the smallest template that asks your one question.
  2. Validate it — grammar first.
  3. Fire it at a target you know has the trait. It must match.
  4. Fire it at the same target with the trait removed. It must not.
  5. Only then let it anywhere near a target whose answers you don't already know.