LoginSignup
0
1

More than 5 years have passed since last update.

CTFプラットフォームfbctfにphpを使った問題を追加する方法

Last updated at Posted at 2018-08-17

導入

セキュリティ業界では,クイズ大会などの競争を通して技術や知識を学ぶことがよく行われています.この手の分野では,DEFCONで行われているCTF(Capture The Flag)が有名で,「腕に覚えあり!」な猛者たちがハイレベルな競争を行なっています.

まあ,これをこのまま一般人が行うことは難しいのですが,やり方を取り入れることはとても有益と言われており,教育の一環として,様々な組織でCTFを行うことが増えてきています.幸運なことに,先人たちがそういったプラットフォームのソースコードを公開してくださっていますので,それを活用しない手はない私たちは,好きなプラットフォームをインストール&カスタマイズしながら進めるわけです.

今回,その中でもグラフィカルでかっこいいFacebookCTFのプラットフォームを使ってみましたが,一部でつまづいた点があったので,備忘のためにメモを残すことにしました.

image.png

環境

インストールの仕方は,githubを見ましょう.

事象

SQLインジェクションを試すWebアプリをphpで新たに書いて実行しようとしたところ,Errorが発生しました.

image.png

解決方法

お急ぎの方用

Facebookが開発したphp高速化の仕組みであるhhvmの仕様によるもので,phpを新たに作った場合,再コンパイルが必要ということらしい.

コマンドは以下の通り.

sudo rm /var/cache/hhvm/hhvm.hhbc
sudo hhvm-repo-mode enable "/var/www/fbctf"
sudo chown www-data:www-data /var/cache/hhvm/hhvm.hhbc
sudo service nginx restart
sudo service hhvm restart

こんな感じ

[ 18-08-17 12:28 ] /var/www/fbctf
$ sudo rm /var/cache/hhvm/hhvm.hhbc
[ 18-08-17 12:31 ] /var/www/fbctf
$ sudo hhvm-repo-mode enable "/var/www/fbctf"
running hphp...
parsing inputs...
Unable to stat file /var/www/fbctf/src/logincheck.php
Unable to stat file /var/www/fbctf/src/db.php
Unable to stat file /var/www/fbctf/vendor/google/apiclient-services/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/phpdocumentor/reflection-docblock/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/phpdocumentor/type-resolver/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/phpunit/php-token-stream/tests/_fixture/test1.php
Unable to stat file /var/www/fbctf/vendor/phpunit/php-token-stream/tests/_fixture/test2.php
Unable to stat file /var/www/fbctf/vendor/phpunit/php-token-stream/tests/_fixture/test3.php
Unable to stat file /var/www/fbctf/vendor/phpunit/php-token-stream/tests/_fixture/test4.php
Unable to stat file /var/www/fbctf/vendor/phpunit/php-token-stream/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/phpunit/phpunit-mock-objects/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/phpunit/phpunit/build/tools/phar:/composer.phar/bin/composer
Unable to stat file /var/www/fbctf/vendor/phpunit/phpunit/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/sebastian/comparator/vendor/autoload.php
Unable to stat file /var/www/fbctf/vendor/sebastian/resource-operations/build/arginfo.php
Unable to stat file /var/www/fbctf/vendor/guzzlehttp/promises/vendor/autoload.php
analyzeProgram...
analyzeProgram took 0'00" (839821 us) wall time
parsing inputs took 0'08" (8112099 us) wall time
pre-optimizing...
pre-optimizing took 0'00" (444307 us) wall time
creating binary HHBC files...
running HHBBC...
running HHBBC took 0'08" (8054159 us) wall time
creating binary HHBC files took 0'11" (11803217 us) wall time
all files saved in /var/cache/hhvm ...
running hphp took 0'21" (21363325 us) wall time

Success!
[ 18-08-17 12:31 ] /var/www/fbctf
$ sudo chown www-data:www-data /var/cache/hhvm/hhvm.hhbc
[ 18-08-17 12:31 ] /var/www/fbctf
$ sudo service nginx restart
[ 18-08-17 12:32 ] /var/www/fbctf
$ sudo service hhvm restart

たとえば,phpinfo()の場合,以下のように実行された.

image.png

よかったよかった.

ヒマな人用

どうやってたどり着いたかというと・・・,

1. 困ったらエラーログをみよう

nginxを使っていることは知っていたので,エラーログを見る...けど,何も出ていない...

/var/log/nginx/error.log

2. アクセスログを見てみよう

/var/log/nginx/error.log
(略)
xxx.xxx.xxx.xxx - - [17/Aug/2018:09:23:33 +0900] "GET /task/web.php HTTP/1.1" 404 634 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
xxx.xxx.xxx.xxx - - [17/Aug/2018:09:23:33 +0900] "GET /index.php?page=error HTTP/1.1" 200 758 "https://hogehoge.com/task/web.php" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

/task/web.phpが,今回新しく作成したファイルだけど,なぜか404で返されている.

3. ファイルが本当にあるか確認

[ 18-08-17 09:32 ] /var/www/fbctf/src/task
$ ls
web.php

ファイルは本当にある.

4. 設定ファイルは大丈夫か?

/etc/nginx/sites-enabled/fbctf.conf
(略)
  root /var/www/fbctf/src;
  index index.php;
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass  unix:/var/run/hhvm/sock;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
(略)

rootもあってるし,phpの処理もできてそう.

5. そもそもphpは動く?

コンソール上で”php -e "phpinfo();"と実行すると,きちんと帰ってくる.

そういえば,php.iniファイルが見当たらないけど,どこにあるんだろう? hhvmってなんだろう?

6. hhvmとは・・・(白目)

どうやら,Facebookがphp高速化のために作ったphp互換の新しい仕組みとのこと.
/var/logの下に,hhvmディレクトリを見つけたので,ログファイルを漁って見る.

(略)
[Fri Aug 17 09:23:33 2018] [hphp] [1280:7fad3c3ff700:5870:000001] [] \nNotice: File could not be loaded: /var/www/fbctf/src/task/web.php
[Fri Aug 17 09:30:38 2018] [hphp] [1280:7fad3cbff700:5917:000001] [] \nNotice: File could not be loaded: /var/www/fbctf/src/task/web.php
[Fri Aug 17 09:39:51 2018] [hphp] [1280:7fad3cbff700:5919:000001] [] \nNotice: File could not be loaded: /var/www/fbctf/src/web.php
[Fri Aug 17 09:40:06 2018] [hphp] [1280:7fad2c3ff700:5857:000001] [] \nNotice: File could not be loaded: /var/www/fbctf/src/task/web.php
(略)

お,なんかいっぱいある...「ロードされていない」とはどういう意味だろう?
もしかして,RailsやDjangoみたいに,routeを設定しないといけないとか??
ともかく,hhvmがどういう仕組みか知らないとどうしようもないか・・・.

hhvm公式Webサイト
https://hhvm.com/

image.png

なんか,バイトコードにコンパイルして動かすとか書いてある.
ということはコンパイルいる??

7. fbctfのサイトで再度マニュアルあさり

再度,fbctfのページをのぞいて,"comlile"と検索してみると.FAQに,コードを変更した時の対応について書いてあった!

image.png

というわけで,あとは「お急ぎの人用」のセクションに続く.

感想

マニュアルやFAQを読みましょう,ってことですね.それと,問題サーバと解答サーバは分離した方が,競技の幅が広がってよいので,あんまりこの手の操作は,お金をケチる時以外がいらないのかもしれませんね.

HHVMもnginxもほとんど扱っていなかったので,良い勉強になりました.
それにしても,Webの技術はついていくだけでも一つの才能なんじゃないかと思えるくらい,ひと昔前の知識って使えないもんですね.

このほかには,CTFdというプラットフォームが軽くて人気らしいので,今度試してみようかと思っています.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1