Automated GRC-SOC Compliance Audits

Automated GRC & SOC Compliance Audits: Wazuh SCA, OpenSearch, LiteLLM & Telegram

Continuous compliance shouldn't depend on remembering to open a dashboard or manually checking every server after a config change. In this walkthrough, we build an automated GRC and SOC reporting workflow that evaluates custom Wazuh SCA checks for Docker and Nginx, queries OpenSearch, summarizes ISO 27001 controls through LiteLLM, and delivers clean HTML digests to Telegram.

This guide documents how to design, deploy, troubleshoot, and automate a continuous Governance, Risk, and Compliance (GRC) and Security Operations Center (SOC) reporting pipeline. It covers custom Wazuh Security Configuration Assessment (SCA) policies, centralized group targeting, remote command authorization, OpenSearch querying, LLM-assisted compliance summarization via LiteLLM, and HTML-safe Telegram alerting.

1. Architecture Overview & What Was Achieved

Phase Objective Key Outcome
Custom Docker SCA Policy Audit Docker socket ownership (CIS 3.15) Verified socket permissions and used valid Docker path requirements so scans execute cleanly.
OpenSearch API Querying Extract compliance check results programmatically Authenticated queries against wazuh-alerts-* return structured JSON under data.sca.check.result.
Unified Python Pipeline Combine SCA status checks + ISO 27001 LLM narrative + Telegram Built a native Python script using standard-library HTTP clients and HTML sanitization.
Centralized Group & Remote Execution Roll out policies to Linux hosts without affecting Windows nodes Created a dedicated agent group, moved policies into group shared storage, and enabled sca.remote_commands=1 on Linux agents.
Nginx SCA Expansion Audit Nginx server_tokens (CIS 5.2.1) across fleet & manager Deployed policy to both the Linux agent group folder and local manager directory so all hosts report status.
Sanitization Notice: Replace all angle-bracket placeholders such as <INDEXER_PASSWORD>, <LITELLM_URL>, <LITELLM_API_KEY>, <TELEGRAM_BOT_TOKEN>, <TELEGRAM_CHAT_ID>, <REMOTE_AGENT_HOST>, and <REMOTE_AGENT_ID> with your own environment values before running commands.

2. Step-by-Step Implementation Guide

Step 1: Verify the Docker Socket File Type on the Manager

In Linux, /var/run/docker.sock (and /run/docker.sock) is a socket, not a regular file. Check ownership and permissions before writing the check.

sudo ls -l /var/run/docker.sock /run/docker.sock

Step 2: Create the Custom Docker SCA Policy on the Manager

Use standard existing Docker paths in the requirements block, preserve exact YAML indentation, and map the check to CIS and ISO 27001 compliance metadata.

sudo bash -c "cat << 'EOF' > /var/ossec/etc/sca_docker_custom.yml
policy:
  id: \"custom_docker_grc\"
  file: \"sca_docker_custom.yml\"
  name: \"Custom GRC Audit - Docker Security\"
  description: \"Auditing Docker daemon socket security.\"
  references:
    - https://www.cisecurity.org/benchmark/docker

requirements:
  title: \"Check if Docker is installed\"
  description: \"Only run this policy if Docker is present.\"
  condition: any
  rules:
    - 'f:/usr/bin/docker'
    - 'd:/var/lib/docker'

checks:
  - id: 15000
    title: \"CIS 3.15: Ensure Docker socket is owned by root:docker\"
    description: \"The Docker socket grants root-level privileges and must be strictly controlled.\"
    remediation: \"Run: sudo chown root:docker /var/run/docker.sock\"
    compliance:
      - cis: [\"3.15\"]
      - iso_27001: [\"A.9.2.3\"]
    condition: all
    rules:
      - 'c:stat -c %U:%G /var/run/docker.sock -> r:^root:docker'
EOF"

Step 3: Restart Wazuh Manager and Confirm SCA Evaluation

Restart the manager service and check the SCA logs to verify the custom policy is loaded and evaluated without being skipped.

sudo systemctl restart wazuh-manager
sleep 5
sudo grep sca /var/ossec/logs/ossec.log | tail -n 15

Step 4: Query OpenSearch with Authentication to Inspect JSON Structure

Always pass valid indexer credentials and avoid blind piping into grep when debugging, as authentication failures can produce empty output. Inspect the JSON path _source.data.sca.check.result.

curl -s -k -u admin:<INDEXER_PASSWORD> -X GET "https://127.0.0.1:9200/wazuh-alerts-*/_search" -H "Content-Type: application/json" -d'
{
  "size": 1,
  "sort": [{"@timestamp": {"order": "desc"}}],
  "query": {
    "bool": {
      "must": [
        { "match": { "agent.id": "000" } },
        { "match": { "data.sca.check.id": "15000" } }
      ]
    }
  }
}' | jq .

Step 5: Test Extraction in a Standalone Python File (Not Directly in Bash)

Do not paste raw Python statements directly into an interactive Bash prompt. Save the script to a .py file and execute it with python3.

cat << 'EOF' > ~/check_compliance.py
import json
import ssl
import urllib.request
import base64

url = "https://127.0.0.1:9200/wazuh-alerts-*/_search"
query = {
    "size": 1,
    "sort": [{"@timestamp": {"order": "desc"}}],
    "query": {
        "bool": {
            "must": [
                {"match": {"agent.id": "000"}},
                {"match": {"data.sca.check.id": "15000"}}
            ]
        }
    }
}

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

req = urllib.request.Request(
    url,
    data=json.dumps(query).encode("utf-8"),
    headers={"Content-Type": "application/json"}
)
token = base64.b64encode(f"admin:<INDEXER_PASSWORD>".encode("utf-8")).decode("utf-8")
req.add_header("Authorization", f"Basic {token}")

with urllib.request.urlopen(req, context=ctx) as resp:
    data = json.loads(resp.read().decode("utf-8"))

hits = data.get("hits", {}).get("hits", [])
if hits:
    status = hits[0]["_source"]["data"]["sca"]["check"]["result"]
    print(f"CIS 3.15 Docker Socket Status: {status}")
EOF
python3 ~/check_compliance.py

Step 6: Create a Dedicated Linux Agent Group and Migrate Policies

Segment Linux container/web nodes into a dedicated group (for example, Linux-Docker) and remove the remote Linux agent from default so non-Linux endpoints do not process Linux-specific policies.

sudo /var/ossec/bin/agent_groups -a -g Linux-Docker
sudo /var/ossec/bin/agent_groups -a -i <REMOTE_AGENT_ID> -g Linux-Docker
sudo /var/ossec/bin/agent_groups -r -i <REMOTE_AGENT_ID> -g default
sudo /var/ossec/bin/agent_groups -s -i <REMOTE_AGENT_ID>

sudo mkdir -p /var/ossec/etc/shared/Linux-Docker
sudo mv /var/ossec/etc/sca_docker_custom.yml /var/ossec/etc/shared/Linux-Docker/sca_docker_custom.yml
sudo cp /var/ossec/etc/shared/Linux-Docker/sca_docker_custom.yml /var/ossec/etc/sca_docker_custom.yml

Step 7: Create the Nginx SCA Policy in the Group Folder and Locally on Manager

Create sca_nginx_custom.yml with proper YAML list indentation to verify server_tokens off; (CIS 5.2.1). Place it in the Linux-Docker shared folder for remote agents and also in /var/ossec/etc/ if the manager host itself runs Nginx.

sudo bash -c "cat << 'EOF' > /var/ossec/etc/shared/Linux-Docker/sca_nginx_custom.yml
policy:
  id: \"custom_nginx_grc\"
  file: \"sca_nginx_custom.yml\"
  name: \"Custom GRC Audit - Nginx Security\"
  description: \"Auditing Nginx reverse proxy security configuration.\"
  references:
    - https://www.cisecurity.org/benchmark/nginx

requirements:
  title: \"Check if Nginx is installed\"
  description: \"Only run if Nginx configuration directory exists.\"
  condition: any
  rules:
    - 'd:/etc/nginx'

checks:
  - id: 15001
    title: \"CIS 5.2.1: Ensure Server Tokens are Disabled\"
    description: \"Disabling server tokens prevents Nginx from broadcasting its version number to attackers.\"
    remediation: \"Add 'server_tokens off;' to your nginx.conf http block.\"
    compliance:
      - cis: [\"5.2.1\"]
      - iso_27001: [\"A.8.2.3\"]
    condition: all
    rules:
      - 'c:grep -Ri \"server_tokens\" /etc/nginx/ -> r:off'
EOF"

sudo cp /var/ossec/etc/shared/Linux-Docker/sca_nginx_custom.yml /var/ossec/etc/sca_nginx_custom.yml

Step 8: Configure Centralized agent.conf and Validate Syntax

Reference the policies under /var/ossec/etc/shared/ inside the group's agent.conf, fix file ownership, and run verify-agent-conf. (Note: warnings about missing files during local manager verification are expected when the policy resides inside the group subdirectory on the manager).

sudo bash -c "cat << 'EOF' > /var/ossec/etc/shared/Linux-Docker/agent.conf
<agent_config>
  <sca>
    <policies>
      <policy>/var/ossec/etc/shared/sca_docker_custom.yml</policy>
      <policy>/var/ossec/etc/shared/sca_nginx_custom.yml</policy>
    </policies>
  </sca>
</agent_config>
EOF"

sudo chown -R wazuh:wazuh /var/ossec/etc/shared/Linux-Docker/
sudo chown wazuh:wazuh /var/ossec/etc/sca_docker_custom.yml /var/ossec/etc/sca_nginx_custom.yml
sudo /var/ossec/bin/verify-agent-conf
sudo systemctl restart wazuh-manager

Step 9: Enable SCA Remote Commands on Remote Linux Agents

Remote Wazuh agents block command rules (c:) in SCA policies unless explicitly enabled locally in local_internal_options.conf.

ssh <AGENT_USER>@<REMOTE_AGENT_HOST>
echo "sca.remote_commands=1" | sudo tee -a /var/ossec/etc/local_internal_options.conf
tail -n 1 /var/ossec/etc/local_internal_options.conf
sudo systemctl restart wazuh-agent
sleep 10
sudo grep sca /var/ossec/logs/ossec.log | tail -n 12
exit

Step 10: Deploy the Unified Fleet Compliance & LLM Telegram Script

Install /usr/local/bin/wazuh-compliance.py to query Docker (15000) and Nginx (15001) results across configured Linux agents, aggregate 7-day ISO 27001 alerts for LLM summarization via LiteLLM, sanitize HTML characters, and send the digest to Telegram.

sudo bash -c "cat << 'EOF' > /usr/local/bin/wazuh-compliance.py
#!/usr/bin/env python3
import base64
import json
import ssl
import sys
import urllib.request
from collections import Counter

INDEXER_URL = \"https://127.0.0.1:9200/wazuh-alerts-*/_search\"
INDEXER_USER = \"admin\"
INDEXER_PASS = \"<INDEXER_PASSWORD>\"

LITELLM_URL = \"<LITELLM_URL>\"
LITELLM_KEY = \"<LITELLM_API_KEY>\"
LITELLM_MODEL = \"qwen-cluster\"

TELEGRAM_BOT_TOKEN = \"<TELEGRAM_BOT_TOKEN>\"
TELEGRAM_CHAT_ID = \"<TELEGRAM_CHAT_ID>\"

LINUX_FLEET = {
    \"000\": \"Manager-Node\",
    \"<REMOTE_AGENT_ID>\": \"Remote-Linux-Node\"
}

def get_opensearch_data(query):
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    req = urllib.request.Request(
        INDEXER_URL,
        data=json.dumps(query).encode(\"utf-8\"),
        headers={\"Content-Type\": \"application/json\"}
    )
    auth_b64 = base64.b64encode(f\"{INDEXER_USER}:{INDEXER_PASS}\".encode(\"utf-8\")).decode(\"utf-8\")
    req.add_header(\"Authorization\", f\"Basic {auth_b64}\")
    with urllib.request.urlopen(req, context=ctx, timeout=30) as resp:
        return json.loads(resp.read().decode(\"utf-8\"))

def check_policy(agent_id, check_id):
    query = {
        \"size\": 1,
        \"sort\": [{\"@timestamp\": {\"order\": \"desc\"}}],
        \"query\": {
            \"bool\": {
                \"must\": [
                    {\"match\": {\"agent.id\": agent_id}},
                    {\"match\": {\"data.sca.check.id\": check_id}}
                ]
            }
        }
    }
    try:
        data = get_opensearch_data(query)
        hits = data.get(\"hits\", {}).get(\"hits\", [])
        if hits:
            return hits[0][\"_source\"][\"data\"][\"sca\"][\"check\"][\"result\"]
    except Exception as exc:
        return f\"Error: {exc}\"
    return \"Unknown\"

def fetch_iso_alerts():
    query = {
        \"query\": {
            \"bool\": {
                \"must\": [
                    {\"range\": {\"timestamp\": {\"gte\": \"now-7d\", \"lte\": \"now\"}}},
                    {\"wildcard\": {\"rule.compliance.iso_27001\": \"*\"}}
                ]
            }
        },
        \"size\": 1000
    }
    return get_opensearch_data(query)

def minify_data(data):
    hits = data.get(\"hits\", {}).get(\"hits\", [])
    if not hits:
        return None
    counts = Counter()
    for hit in hits:
        source = hit.get(\"_source\", {})
        agent = source.get(\"agent\", {}).get(\"name\", \"Unknown\")
        iso_tags = source.get(\"rule\", {}).get(\"compliance\", {}).get(\"iso_27001\", [])
        for tag in iso_tags:
            counts[f\"ISO 27001 Control {tag} triggered on host '{agent}'\"] += 1

    lines = [\"=== 7-DAY ISO 27001 ALERTS ===\"]
    for item, count in counts.most_common(20):
        lines.append(f\"- {item} (Detected {count} times)\")
    return \"\\n\".join(lines)

def sanitize_html(text):
    return (
        text.replace(\"&\", \"&amp;\")
        .replace(\"<\", \"&lt;\")
        .replace(\">\", \"&gt;\")
        .replace(\"*\", \"\")
        .replace(\"#\", \"\")
    )

def analyze_with_llm(minified_data):
    system_prompt = (
        \"You are an IT Security Auditor. Review the triggered ISO 27001 control alerts from the last 7 days. \"
        \"Write a brief executive compliance narrative detailing which security domains experienced the most issues. \"
        \"Output plain text without asterisks or markdown headers.\"
    )
    payload = {
        \"model\": LITELLM_MODEL,
        \"messages\": [
            {\"role\": \"system\", \"content\": system_prompt},
            {\"role\": \"user\", \"content\": minified_data}
        ],
        \"temperature\": 0.2
    }
    req = urllib.request.Request(
        LITELLM_URL,
        data=json.dumps(payload).encode(\"utf-8\"),
        headers={
            \"Content-Type\": \"application/json\",
            \"Authorization\": f\"Bearer {LITELLM_KEY}\"
        }
    )
    with urllib.request.urlopen(req, timeout=300) as resp:
        result = json.loads(resp.read().decode(\"utf-8\"))
        return result[\"choices\"][0][\"message\"][\"content\"]

def send_telegram(msg):
    url = f\"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage\"
    payload = {
        \"chat_id\": TELEGRAM_CHAT_ID,
        \"text\": msg,
        \"parse_mode\": \"HTML\"
    }
    req = urllib.request.Request(
        url,
        data=json.dumps(payload).encode(\"utf-8\"),
        headers={\"Content-Type\": \"application/json\"}
    )
    with urllib.request.urlopen(req, timeout=15) as resp:
        resp.read()

def main():
    try:
        docker_lines = []
        nginx_lines = []

        for ag_id, ag_name in LINUX_FLEET.items():
            d_status = check_policy(ag_id, \"15000\")
            n_status = check_policy(ag_id, \"15001\")
            docker_lines.append(f\"<b>{sanitize_html(ag_name)}:</b> {sanitize_html(str(d_status).upper())}\")
            nginx_lines.append(f\"<b>{sanitize_html(ag_name)}:</b> {sanitize_html(str(n_status).upper())}\")

        docker_msg = \"<b>CIS 3.15 (Docker Socket)</b>\\n\" + \"\\n\".join(docker_lines) + \"\\n\\n\"
        nginx_msg = \"<b>CIS 5.2.1 (Nginx Tokens)</b>\\n\" + \"\\n\".join(nginx_lines) + \"\\n\\n\"

        raw_data = fetch_iso_alerts()
        minified = minify_data(raw_data)

        if minified:
            analysis = analyze_with_llm(minified)
            iso_msg = f\"<b>Weekly ISO 27001 Summary:</b>\\n{sanitize_html(analysis)}\"
        else:
            iso_msg = \"<b>Weekly ISO 27001 Status:</b>\\nNo compliance violations detected.\"

        final_msg = f\"<b>Unified GRC Audit Update</b>\\n\\n{docker_msg}{nginx_msg}{iso_msg}\"
        send_telegram(final_msg)
        print(\"Successfully sent unified fleet compliance digest to Telegram.\")
    except Exception as exc:
        err_msg = f\"<b>GRC Audit Pipeline Error</b>\\n<code>{sanitize_html(str(exc))}</code>\"
        try:
            send_telegram(err_msg)
        except Exception:
            pass
        sys.stderr.write(f\"Script failed: {exc}\\n\")
        sys.exit(1)

if __name__ == \"__main__\":
    main()
EOF"

sudo chmod +x /usr/local/bin/wazuh-compliance.py
/usr/local/bin/wazuh-compliance.py

Step 11: Schedule Automated Execution with Cron

Add a daily cron entry so the compliance check and Telegram summary run automatically without manual intervention.

(sudo crontab -l 2>/dev/null; echo "0 8 * * * /usr/local/bin/wazuh-compliance.py") | sudo crontab -
sudo crontab -l

3. Troubleshooting FAQ (SCA, OpenSearch & Telegram)

Q1. Why does an SCA check return not applicable with sca.remote_commands disabled?

Wazuh agents block command-based SCA rules (c:) by default. Enable sca.remote_commands=1 inside /var/ossec/etc/local_internal_options.conf on each remote agent and restart wazuh-agent.

grep "sca.remote_commands" /var/ossec/etc/local_internal_options.conf
sudo systemctl restart wazuh-agent
sudo grep sca /var/ossec/logs/ossec.log | tail -n 10

Q2. Why does a host show UNKNOWN for a check like Nginx (15001) in the script?

Either the agent has not yet evaluated and indexed a matching SCA alert for that check ID, or the policy file was placed only in a shared agent group folder and not locally in /var/ossec/etc/ on the manager host itself.

sudo ls -la /var/ossec/etc/sca_*_custom.yml /var/ossec/etc/shared/Linux-Docker/
sudo systemctl restart wazuh-manager
sleep 10
/usr/local/bin/wazuh-compliance.py

Q3. Why does verify-agent-conf warn that the policy file is not found?

When verifying locally on the manager, verify-agent-conf looks for paths inside /var/ossec/etc/shared/ rather than group subdirectories such as /var/ossec/etc/shared/Linux-Docker/. As long as YAML/XML syntax is valid and the tool prints verify-agent-conf: OK, agents download the policy into their local shared folder.

sudo /var/ossec/bin/verify-agent-conf
sudo /var/ossec/bin/agent_groups -l

Q4. Why does an OpenSearch curl query return empty output or fail authentication?

Missing credentials or piping into a filter that drops HTTP status codes can hide errors. Test with -i (include headers) and valid credentials against port 9200.

curl -i -s -k -u admin:<INDEXER_PASSWORD> "https://127.0.0.1:9200/_cluster/health"
curl -s -k -u admin:<INDEXER_PASSWORD> -X GET "https://127.0.0.1:9200/wazuh-alerts-*/_search?size=1"

Q5. Why does Telegram reject messages with HTTP 400 Bad Request or why does LiteLLM time out?

With parse_mode="HTML", Telegram rejects unescaped <, >, or & characters in model output. Also, local LLMs can take longer on cold starts, so use a generous request timeout (such as 300 seconds) on the LiteLLM call and test the Telegram endpoint directly.

curl -s -X POST "https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/sendMessage" \
  -H "Content-Type: application/json" \
  -d '{"chat_id":"<TELEGRAM_CHAT_ID>","text":"<b>Test Alert</b>","parse_mode":"HTML"}'
Explore More GRC & Homelab Guides: Browse additional configuration walkthroughs, Linux commands, and security notes on the Project Pages hub, or explore more articles across Blog Post Collections and Linux-CMD.

Comments

Popular Posts

Links for useful websites, job portals, cyber security, and more

Chennai :MTC complaint cell Customer Care No.:+91-9445030516 /Toll Free : 18005991500

Marriage Registration Online steps [Tamil Nadu]