Apache2.4セキュリティ「ディレクトリ一覧表示対策」
- 公開日
- 更新日
- カテゴリ: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
- アクセス制御関連のディレクティブの使用を許可する
- (PostsListOrder ・ Allow ・ Deny)
- ※ IP 制限はここ
- Options
- 特定のディレクトリにおける機能を指定するためのディレクティブの使用を許可する。