Automate Vulnerability Scanning with Nuclei: Custom YAML Templates for Web and API Security

Security tutorial - IT technology blog
Security tutorial - IT technology blog

Real-World Problem: The 2 AM Exposed Staging API Alert

At 2:14 AM, my phone buzzed with an anomaly alert from our API gateway. Someone from an unfamiliar IP range was scraping /api/v1/debug/config on one of our production microservices. A quick curl confirmed the worst: database hostnames and internal feature flags were sitting there in plain JSON.

Ever since an SSH brute-force scare on an older server, I have treated baseline hardening as non-negotiable. SSH was locked down, firewall rules were tight, and IAM roles followed least privilege. None of that mattered when a rushed Friday deploy shipped a debug route meant only for local testing. I had 14 other microservices to check—fast—and I needed a way to keep this bug out of production for good.

Root Cause Analysis: Why Manual Checks Miss Custom API Bugs

Once we killed the exposed route and pulled the deploy logs, the gap was obvious. Our security checks only looked at infrastructure and known CVEs, completely ignoring our own application logic.

  • Rapid API drift: When a team ships 20 or more pull requests a week, new routes, headers, and query parameters appear daily. Static spreadsheets go stale before the sprint ends.
  • Blind spots in generic scanners: Off-the-shelf tools know how to probe for default WordPress logins or decade-old Apache bugs. They have zero context for your internal /debug/config route or custom X-Internal-Admin headers.
  • No automated pre-release gate: A full DAST scan took nearly two hours in our old setup. Because it slowed down releases, engineers skipped it on pull requests and staging deploys.

At 2 AM, nobody has two hours to wait for a bloated GUI scanner to crawl a subnet. You need targeted checks that finish in seconds.

Solutions Compared: Choosing the Right Vulnerability Scanner

To fix this permanently, I evaluated three common ways teams test Web and API endpoints.

1. Ad-Hoc Bash and curl Loops

A quick for loop with curl and grep can save you during an active incident. Beyond a one-off check, though, handling redirects, custom HTTP methods, timeouts, and structured reporting turns into brittle spaghetti code. Sharing those scripts across a 10-person engineering team only makes maintenance worse.

2. Traditional Network Scanners

Full-suite network scanners earn their keep during quarterly infrastructure audits and OS-level CVE sweeps. Pointing one at a live API at 2 AM is a different story. Firing thousands of generic probes can trip WAF rate limits, flood your logs, and spike p99 latency for real users.

3. Nuclei with Custom YAML Templates

Nuclei works differently. Built by ProjectDiscovery, it fires fast, concurrent HTTP requests driven by simple YAML files. Instead of guessing what your app does, you define the exact paths, headers, payloads, and response patterns to match. Those templates live in Git right next to your service code, run in seconds, and plug straight into CI/CD.

Best Approach: Automating Web and API Scans with Nuclei

That night, I put together a small library of custom YAML templates tailored to our stack. You can set up the same workflow in about 15 minutes.

Step 1: Install Nuclei and Run a Baseline Scan

Got Go 1.21+ on your workstation or CI runner? Install Nuclei with one command:

go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest
nuclei -version

Pull the latest community templates first, then run a scoped baseline scan. Filtering by tags and severity keeps you from blasting production with thousands of irrelevant probes:

nuclei -update-templates
nuclei -u https://api.example.com -tags exposure,misconfig -severity medium,high,critical

In roughly 10 to 20 seconds, Nuclei flags common leaks like exposed .env files, .git directories, public Swagger UI panels, and permissive CORS headers.

Step 2: Write a Custom Nuclei YAML Template for Your API

Community templates will never know about your internal debug routes or custom auth headers. Create a custom-templates/ directory and add a 32-line file named exposed-debug-config.yaml:

id: internal-debug-config-exposure

info:
  name: Internal Debug Config Endpoint Exposed
  author: secops-team
  severity: high
  description: Detects exposed /api/v1/debug/config endpoints leaking environment details.
  tags: api,exposure,custom

http:
  - method: GET
    path:
      - "{{BaseURL}}/api/v1/debug/config"
      - "{{BaseURL}}/debug/config"

    headers:
      User-Agent: "Internal-Security-Check/1.0"

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

      - type: word
        part: body
        words:
          - "db_host"
          - "feature_flags"
        condition: or

      - type: word
        part: header
        words:
          - "application/json"

Three details make this template reliable in production:

  • {{BaseURL}} expands automatically for every target URL you pass to Nuclei.
  • matchers-condition: and requires all matchers to pass, which eliminates false positives from custom 200 OK error pages.
  • Combining the application/json header check with specific body keys like db_host prevents noisy alerts on HTML fallback routes.

Step 3: Scan Multiple Microservices Safely in Production

During an active incident across multiple services, drop your service URLs into targets.txt (one per line) and enforce strict rate limits:

nuclei -l targets.txt \
  -t ./custom-templates/ \
  -rl 25 \
  -c 10 \
  -timeout 5 \
  -jsonl -o scan-results.json

Each flag protects your live traffic while keeping the scan fast:

  • -rl 25 caps outbound traffic at 25 requests per second.
  • -c 10 limits concurrency to 10 templates in parallel.
  • -timeout 5 drops hanging connections after 5 seconds so one slow service cannot stall the run.
  • -jsonl -o scan-results.json writes newline-delimited JSON ready for Slack webhooks or your SIEM.

Running this against all 14 microservices at 2:30 AM took under four seconds. It confirmed that only one service had leaked the debug route.

Step 4: Integrate Nuclei into Your CI/CD Pipeline

Finding a leak at 2 AM beats missing it altogether, but blocking the deploy in staging is the real win. Drop this short script into your pipeline to fail builds automatically when a custom template triggers:

#!/usr/bin/env bash
set -euo pipefail

TARGET_URL="https://staging-api.example.com"
REPORT_FILE="nuclei-ci-report.json"

nuclei -u "$TARGET_URL" \
  -t ./custom-templates/ \
  -severity high,critical \
  -rl 30 \
  -silent \
  -jsonl -o "$REPORT_FILE"

if [ -s "$REPORT_FILE" ]; then
  echo "Security check failed! Vulnerabilities detected:"
  cat "$REPORT_FILE"
  exit 1
fi

echo "All custom Nuclei API checks passed."

Whenever $REPORT_FILE contains a match, the script exits with code 1 and halts the deploy—adding less than 10 seconds to your CI run. Make it a team rule: every fixed security bug or new internal admin route gets a matching YAML template in custom-templates/.

Start with three templates covering your highest-risk endpoints today. Test them against staging with rate limiting enabled, wire the check into CI, and save yourself the next 2 AM wake-up call.

Share: