Apache2.4セキュリティ「ディレクトリ一覧表示対策」
- 公開:
- 更新:
- カテゴリ: linux Apache
- タグ: Linux,CentOS,Apache,Security,2.4,7.2,DirectoryListing,.htaccess,httpd.conf
Apacheはデフォルトで「ディレクトリリスティング」という機能が有効になっています。
この機能が有効な場合、URLにディレクトリを指定したり、該当ページが無い場合に、そのディレクトリの一覧が表示されてしまいます。
こんな具合に
このままだと、このサイトのファイル一覧が容易に閲覧出来てしまい、攻撃のリスクが高まります。(フレームワークやそのバージョンなど、WEBアプリケーションを構成するものが見る人が見ればわかります。)
また、設定ファイルなどを読まれてしまうとさらに危険です。ですので、この機能を無効化してしまいましょう。
サーバ全体に設定する場合
- ファイル設置箇所
- /etc/httpd/conf
- ファイル名
- httpd.conf
# httpd.conf のあるディレクトリへ移動
cd /etc/httpd/conf
# vimコマンドで編集
vim httpd.conf
※vimコマンドを入れていない場合はviコマンドで編集出来ますが、テキストを色分けしてくれたりと便利で使いやすいので、vimコマンドのインストールをおすすめします。
ちなみに、いちいちファイルのある場所まで移動するのが面倒な場合は、フルパスで指定すればOKです。こんな具合に
vim /etc/httpd/conf/httpd.conf
エディターで開いたら、そこの131行目あたり
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
ここの
Options Indexes FollowSymLinks
を
Options FollowSymLinks
へ変更します。
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
#Options Indexes FollowSymLinks
Options FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
ここでは、最初にあった設定をコメントアウトし、新たにOptions FollowSymLinksを書き加えています。慣れないうちはこういった書き方をしておけば、設定(書式)に不備があった際にすぐに元に戻せて安心ですね。
Apacheを再起動する前に書式チェックを行います。
# httpd.confの文法チェック
apachectl configtest
↓「Syntax OK 」の表記が出たらOK
[root@localhost ~]# apachectl configtest
Syntax OK ←この表記が出たらOK
httpd.confを編集したら、再起動の前にかならず書式チェックを行う習慣をつけましょう。
書式チェックを行わずに再起動を行いエラーが出ると、わりとテンパります(本番サーバならなおさら)笑
それでは、Apacheを再起動します。
# Apacheを再起動
systemctl restart httpd.service
ちなみに、Apacheの再起動時のコマンドはいくつかありますが、下記でも行えます。
apachectl restart
再起動が完了したら、ブラウザからアクセスして画面を確認してみましょう。
ディレクトリの一覧が表示されなくなりました。
(ForbiddonになっていればOK)
特定のディレクトリ(範囲)のみ設定する場合
設定するなら全体でかけてしまうのが最良ですが、場合によっては環境の制限であったりでそれが難しい場合があります。その時は、.htaccessの設定からも、ディレクトリ一覧の表示を制御する事ができます。
ちなみに.htaccessを使うと、サイト全体でなくとも、設置した箇所で任意のディレクトリ以下の一覧表示を制御することもできます。
.htaccess の設定
# ファイル一覧表示を無効にする
Options -Indexes
ちなみに、.htaccessで設定を行う場合は、httpd.conf の AllowOverride設定を確認・修正の必要があります。
AllowOverride
AllowOverrideは、.htaccess による設定の上書きを許可するかを指定するディレクティブです。
- All
- 全ての上書きを許可する
- None
- 全ての上書きを禁止する
- AuthConfig
-
認証関連のディレクティブの使用を許可する。
(AuthUserFile・AuthName・AuthType)
※ベーシック認証はここ - FileInfo
-
ドキュメントタイプやファイルタイプ関連のディレクティブの使用を許可する。
(AddType・AddEncoding・AddLanguage)
※mod_rewrite はここ - Indexes
- ディレクトリインデックス関連のディレクティブの使用を許可する。 ※今回はここ
- Limit
-
アクセス制御関連のディレクティブの使用を許可する。
(Order・Allow・Deny)
※IP制限はここ - Options
- 特定のディレクトリにおける機能を指定するためのディレクティブの使用を許可する。