<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[刘新修]]></title> 
<link>http://pic1.liuxinxiu.com:80/index.php</link> 
<description><![CDATA[刘新修的个人博客 (Liuxinxiu'S Blog)]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[刘新修]]></copyright>
<item>
<link>http://pic1.liuxinxiu.com:80/redis_bug/</link>
<title><![CDATA[Redis 未授权访问缺陷可轻易导致系统被黑]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[数据库技术]]></category>
<pubDate>Thu, 11 May 2017 03:29:11 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/redis_bug/</guid> 
<description>
<![CDATA[ 
	<p>&nbsp;<strong style="color: rgb(37, 37, 37); font-family: tahoma, arial, 宋体; text-indent: 2em;">注：</strong><span style="color: rgb(37, 37, 37); font-family: tahoma, arial, 宋体; text-indent: 2em;">近日曝出大规模利用 Redis 漏洞进行入侵的事件，会给用户的 Redis 运行环境以及 Linux 主机造成安全风险。若用户的Linux服务器中安装了Redis并对公网开放了Redis端口，则可能导致Redis数据丢失，服务器则存在被植入公钥用于SSH远程登录的风险。</span></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>关于Redis</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis是一个高性能的key-value数据库。 redis的出现，很大程度补偿了memcached这类keyvalue存储的不足，在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python，Ruby，Erlang，PHP客户端，使用很方便。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>容易遭受入侵的环境</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">用户自建的运行了 Redis 服务的 Linux 主机，依root身份运行Redis服务，并在公网上开放了 6379 的 Redis 端口。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>入侵现象</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">1）Redis 可能被执行过 flushall 命令</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">2）Redis 内建了名为 crackit 的 key</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">3）Redis 的 dir 参数指向了 /root/.ssh</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">4）/root/.ssh/authorized_keys 被覆盖或者包含 Redis 相关的内容</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>修复办法</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">5）以非 root 权限启动 Redis</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">6）增加 Redis 密码验证</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">7）禁止公网开放 Redis 端口, 例如可以在青云防火墙上禁用 6379 Redis 的端口</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">8）检查 authorized_keys 是否非法</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>详细内容请看下文：</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>漏洞概要</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis 默认情况下，会绑定在 0.0.0.0:6379，这样将会将Redis服务暴露到公网上，如果在没有开启认证的情况下，可以导致任意用户在可以访问目标服务器的情况下未授权访问Redis以及读取Redis的数据。攻击者在未授权访问Redis的情况下可以利用Redis的相关方法，可以成功将自己的公钥写入目标服务器的 /root/.ssh 文件夹的authotrized_keys 文件中，进而可以直接登录目标服务器。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>漏洞描述</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis 安全模型的观念是: &ldquo;请不要将Redis暴露在公开网络中, 因为让不受信任的客户接触到Redis是非常危险的&rdquo; 。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis 作者之所以放弃解决未授权访问导致的不安全性是因为, 99.99%使用Redis的场景都是在沙盒化的环境中, 为了0.01%的可能性增加安全规则的同时也增加了复杂性, 虽然这个问题的并不是不能解决的, 但是这在他的设计哲学中仍是不划算的。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">因为其他受信任用户需要使用Redis或者因为运维人员的疏忽等原因，部分Redis 绑定在0.0.0.0:6379，并且没有开启认证（这是Redis的默认配置），如果没有进行采用相关的策略，比如添加防火墙规则避免其他非信任来源ip访问等，将会导致Redis服务直接暴露在公网上，导致其他用户可以直接在非授权情况下直接访问Redis服务并进行相关操作。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">利用Redis自身的相关方法，可以进行写文件操作，攻击者可以成功将自己的公钥写入目标服务器的 /root/.ssh 文件夹的authotrized_keys 文件中，进而可以直接登录目标服务器。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>漏洞影响</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis 暴露在公网（即绑定在0.0.0.0:6379，目标IP公网可访问），并且没有开启相关认证和添加相关安全策略情况下可受影响而导致被利用。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">通过ZoomEye 的搜索结果显示，有97700在公网可以直接访问的Redis服务。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><img src="http://at.liuxinxiu.com/2017/05/image/a1.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a1.jpg" width="865" height="389" /></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">根据 ZoomEye 最新于2015年11月12日0点探测结果显示：</p><blockquote style="margin: 1.5em 10px; padding: 0.5em 10px; background: rgb(249, 249, 249); border-left: 6px solid rgb(204, 204, 204); quotes: &quot;&ldquo;&quot; &quot;&rdquo;&quot; &quot;&lsquo;&quot; &quot;&rsquo;&quot;; font-family: tahoma, arial, 宋体;"><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em;">总的存在无验证可直接利用 Redis 服务的目标全球有49099，其中中国有16477。其中被明着写入crackit的，也就是已经被黑的比例分别是全球65%（3.1万），中国67.5%（1.1万）。</p></blockquote><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>1.1. &nbsp; &nbsp;漏洞分析与利用</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">首先在本地生产公私钥文件:</p><div class="codeText"><br /><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>?&nbsp;1&nbsp;$ssh-keygen&nbsp;&ndash;t&nbsp;rsa&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><img src="http://at.liuxinxiu.com/2017/05/image/a2.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a2.jpg" width="865" height="583" style="color: rgb(0, 0, 0); font-family: &quot;Courier New&quot;, monospace; font-size: 12px; text-indent: 0px;" /></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">然后将公钥写入foo.txt文件</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>?&nbsp;1&nbsp;$&nbsp;(echo&nbsp;-e&nbsp;</span><span class="string">&quot;&#92;n&#92;n&quot;</span><span>;&nbsp;cat&nbsp;id_rsa.pub;&nbsp;echo&nbsp;-e&nbsp;</span><span class="string">&quot;&#92;n&#92;n&quot;</span><span>)&nbsp;&gt;&nbsp;foo.txt&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">再连接Redis写入文件</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><img src="http://at.liuxinxiu.com/2017/05/image/a3.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a3.jpg" width="864" height="241" /></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">这样就可以成功的将自己的公钥写入/root/.ssh文件夹的authotrized_keys文件里，然后攻击者直接执行：</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>?&nbsp;1&nbsp;$&nbsp;ssh&nbsp;&ndash;i&nbsp;&nbsp;id_rsa&nbsp;root@192.168.1.11&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">即可远程利用自己的私钥登录该服务器。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">当然，写入的目录不限于/root/.ssh 下的authorized_keys，也可以写入用户目录，不过Redis很多以root权限运行，所以写入root目录下，可以跳过猜用户的步骤。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>Redis 未授权的其他危害与利用</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>数据库数据泄露</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis 作为数据库，保存着各种各样的数据，如果存在未授权访问的情况，将会导致数据的泄露，其中包含保存的用户信息等</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><img src="http://at.liuxinxiu.com/2017/05/image/a4.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a4.jpg" width="865" height="198" /></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>代码执行</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">Redis可以嵌套Lua脚本的特性将会导致代码执行, 危害同其他服务器端的代码执行, 样例如下</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><img src="http://at.liuxinxiu.com/2017/05/image/a5.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a5.jpg" width="864" height="96" /></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">一旦攻击者能够在服务器端执行任意代码, 攻击方式将会变得多且复杂, 这是非常危险的.</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">通过Lua代码攻击者可以调用 redis.sha1hex() 函数，恶意利用 Redis 服务器进行 SHA-1 的破解。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>敏感信息泄露</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">通过 Redis 的 INFO 命令, 可以查看服务器相关的参数和敏感信息, 为攻击者的后续渗透做铺垫</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><img src="http://at.liuxinxiu.com/2017/05/image/a6.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a6.jpg" width="864" height="655" /></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;">可以看到泄露了很多 Redis 服务器的信息, 有当前 Redis 版本, 内存运行状态, 服务端个数等等敏感信息。</p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong>全球无验证可直接利用 Redis 分布情况</strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong><img src="http://at.liuxinxiu.com/2017/05/image/a7.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a7.jpg" width="800" height="475" /></strong></p><p style="margin-bottom: 15px; color: rgb(37, 37, 37); line-height: 28px; text-indent: 2em; font-family: tahoma, arial, 宋体;"><strong><img src="http://at.liuxinxiu.com/2017/05/image/a8.jpg" alt="http://at.liuxinxiu.com/2017/05/image/a8.jpg" width="800" height="474" /></strong></p>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/redis_bug/#blogcomment</link>
<title><![CDATA[[评论] Redis 未授权访问缺陷可轻易导致系统被黑]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/redis_bug/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>