Skip to content

Anatomy of a Template

The best way to learn what a template is is to read a real one slowly. This chapter takes apart http-missing-security-headers, a template from the installed collection (find it at ~/nuclei-templates/http/misconfiguration/http-missing-security-headers.yaml), and then runs it against the lab fixture, whose sloppy personality was practically designed for it. Everything quoted below is the file as installed, trimmed only for length where a cut is marked.

The name card

id: http-missing-security-headers

info:
  name: HTTP Missing Security Headers
  author: socketz,geeknik,G4L1T0,convisoappsec,kurohost,dawid-czarnecki,forgedhallpass,jub0bs,userdehghani,celbahraoui,safejulian
  severity: info
  description: |
    This template searches for missing HTTP security headers. The impact of these missing headers can vary.
  classification:
    cwe-id: CWE-693
  metadata:
    max-request: 1
  tags: misconfig,headers,generic,vuln
  • id is the template's handle — unique, and the name you select it by (-id http-missing-security-headers). By convention it matches the filename.
  • info is the name card every template carries: a human name, an author (eleven of them here — collection templates are crowd-maintained), and a severity from the fixed shelf: info, low, medium, high, critical (and unknown for the undecided). Those two fields are all a result line shows; the rest is for readers.
  • description says what the check looks for, in the author's words. Read it before you trust a finding; it is the claim the matchers below have to cash.
  • classification ties the check to a public weakness catalogue (here CWE-693, a missing security mechanism). Templates for known vulnerabilities carry CVE numbers in the same spirit.
  • tags are the selection handles chapter 6 is built on: misconfig, headers, generic, vuln.

The request

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

    host-redirects: true
    max-redirects: 3

The protocol block is named for what it speaks — http: here; the collection also holds dns:, network:, ssl:, and others. Inside is a list of requests; this template needs exactly one. {{BaseURL}} is a variable: at run time it becomes the target you gave nuclei, so the same template works against any host. The redirect lines tell the engine to follow the target's redirects, up to three, before judging the answer — a server that answers "moved, look over there" should be judged on what's over there.

A request block can hold more than this one shows: request headers and bodies, several paths (each is sent), and payloads — lists of values substituted into the request, for checks that have to try many spellings of a question. Chapter 5's template uses none of that, deliberately; most detection templates don't need it either.

The matchers

    matchers-condition: or
    matchers:
      - type: dsl
        name: strict-transport-security
        dsl:
          - "!regex('(?i)strict-transport-security', header)"
          - "status_code != 301 && status_code != 302"
        condition: and
      # …eleven more, one per header…

Three ideas are doing the work:

  • matchers-condition: or governs the list: any one matcher firing produces a finding. (The lab templates used and — all of them or none. Both are common; the condition is where a template says whether its evidence must agree or may accumulate.)
  • Each matcher here is type dsl — small expressions in nuclei's expression language, evaluated against parts of the response. Other types exist and chapter 5 uses two of them: status (the code equals one of these), word (this part contains one of these strings), regex (this part matches a pattern). A matcher aims at a part — header, body, or the whole response.
  • Inside this matcher, condition: and governs its own two expressions: the header block must not mention strict-transport-security (the ! is negation; (?i) asks for case-insensitive matching, because header names are case-insensitive in HTTP and a matcher that forgets that lies), and the answer must not be a redirect — a redirect response isn't the page whose headers matter.

Each of the twelve matchers is named, and the name becomes the suffix on the finding. One request, twelve tests, and potentially twelve findings, each saying precisely which header was missing. That is the whole design: ask once, judge carefully, report exactly.

The signature

The file ends with a long comment beginning # digest:. That is a cryptographic signature over the template's contents, present on official collection templates so the engine can tell an untouched template from an altered one. Your own templates don't need one (chapter 5's have none, and run fine); know that it exists so a template that someone hands you with a broken digest is a template to read twice before running.

Run it

$ nuclei -duc -nc -ni -id http-missing-security-headers -u http://127.0.0.1:18084

No -t this time: an -id with no template path is resolved against the installed collection, which is how the collection is normally used. Against the sloppy fixture:

Check: ten findings, one per missing header —

[http-missing-security-headers:strict-transport-security] [http] [info] http://127.0.0.1:18084
[http-missing-security-headers:content-security-policy] [http] [info] http://127.0.0.1:18084
[http-missing-security-headers:x-frame-options] [http] [info] http://127.0.0.1:18084
[…seven more, one per header…]
[INF] Scan completed in 10.927045ms. 10 matches found.

Ten, not twelve — and the two silent matchers are the interesting ones. missing-content-type and content-type-charset-specification test the Content-Type header, and the fixture sends a proper one (text/html; charset=utf-8), so they correctly found nothing to say. A template's silence is information of exactly the same kind as its noise: both mean a specific test was applied and came out a specific way. Chapter 5 closes the loop by making the fixture tidy — every header present — and watching all twelve fall silent at once.