> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/cortex-cloud-runtime-security/rules-and-policies/cloud-workload-policies-and-rules/cloud-workload-rules/create-a-new-custom-detection-rule/configure-xdr-agent-rule.md).

# Configure XDR agent rule

The **XDR agent** scanner runs a Python script on the host and evaluates the script's return value to determine whether a compliance violation is present.

{% hint style="warning" %}
**Prerequisites**

The custom Python scripts are intended to be executed exclusively for compliance checks and validations. To ensure the scripts are used properly and no security risks or unintended changes occur, the system implements the following restrictions and safeguards:

* Only a predefined set of Python libraries and functions required for compliance checks are available for use.&#x20;
* Scripts are designed to perform **read-only** operations. Libraries or functions that enable writing, deleting, or creating operations are excluded.
* Only authorized users with specific permissions can create or update custom scripts. This ensures that only trusted individuals can define compliance checks. Only the following roles can enable this capability:
  * Account Admin
  * Instance Administrator
  * Deployment Admin
  * Privileged Security Admin
    {% endhint %}

### Rule structure

Your script must define a top-level function named `run` that accepts positional and keyword arguments:

```python
def run(*args, **kwargs):
    ...
```

The scanner evaluates the return value of the `run` function as follows:

| Return value                                  | Result                                                                      |
| --------------------------------------------- | --------------------------------------------------------------------------- |
| `None`                                        | Pass. No violation is reported.                                             |
| `{"result": True}`                            | Pass. No violation is reported.                                             |
| `{"result": False, "cause": "<description>"}` | Fail. A finding is created, and `cause` is used as the finding description. |

{% hint style="warning" %}
**Note**

If the script does not define a `run` function, or if the script raises an exception during execution, the rule reports an error.
{% endhint %}

### Example

The following script checks the Secure Shell (SSH) configuration on the host and reports a violation if `PermitRootLogin yes` is configured.

```
def run(*args, **kwargs):
    try:
        with open("/etc/ssh/sshd_config", "r") as f:
            contents = f.read()
    except FileNotFoundError:
        return {
            "result": False,
            "cause": "/etc/ssh/sshd_config not found",
        }

    for line in contents.splitlines():
        stripped = line.strip()

        if stripped.startswith("#"):
            continue

        if stripped.replace(" ", "").lower() == "permitrootloginyes":
            return {
                "result": False,
                "cause": "SSH allows root login (PermitRootLogin yes)",
            }

    return {"result": True}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cortex-docs.paloaltonetworks.com/cortex-cloud-runtime-security/rules-and-policies/cloud-workload-policies-and-rules/cloud-workload-rules/create-a-new-custom-detection-rule/configure-xdr-agent-rule.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
