-->

Apacheのセキュリティ設定

Apacheのセキュリティ設定

Ubuntu環境でApacheのセキュリティ設定。
security.confの上書き。

参考記事
Apacheでは概ねこれをやっておけば間違いないというセキュリティ設定があります。
ネットに転がってる記事だけだとUbuntu環境ではもうひと手間必要だったので、AIに聞きつつ情報をまとめました。

security.confを上書き

Ubuntu環境でaptを使いApacheをインストールした場合、他Linux環境のようなhttpdファイルやディレクトリが存在しません。
Ubuntuでのセキュリティの設定は
 /etc/apache2/conf-available/security.conf 
で設定します。
下はsecurity.confの初期状態です。
# Changing the following options will not really affect the security of the
# server, but might make attacks slightly more difficult in some cases.

#
# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of:  Full | OS | Minimal | Minor | Major | Prod
# where Full conveys the most information, and Prod the least.
#ServerTokens Minimal

ServerTokens OS
#ServerTokens Full


#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of:  On | Off | EMail
#ServerSignature Off

ServerSignature On

#
# Allow TRACE method
#
# Set to "extended" to also reflect the request body (only for testing and
# diagnostic purposes).
#
# Set to one of:  On | Off | extended

TraceEnable Off
#TraceEnable On
この初期状態では様々な既知の攻撃に弱いので、修正する必要があります。

セキュリティ設定の修正

初期状態の全ての設定を#でコメントアウトした上で、下のコード群を書き込みます。
# --- 情報漏洩対策 ---

# ヘッダー情報を最小限にする

ServerTokens Prod
# 画面上の署名を消す

ServerSignature Off
# 使用している言語を秘匿する

Header always unset "X-Powered-By"

# スニッフィング攻撃対策

# (ファイルの中身を勝手に推測して実行するのを禁止)

Header always set X-Content-Type-Options "nosniff"

# --- Denial of Service(DoS)攻撃対策 ---

# ブラウザ等からサーバーに送られてくるデータ容量を制限

# (巨大データ送信によるメモリ枯渇、ディスク容量枯渇の防止)

LimitRequestBody 10485760
# ブラウザ等からサーバーに送られてくるヘッダーの行数を制限

# (長いヘッダー情報送信によるメモリ枯渇、CPU枯渇の防止)

LimitRequestFields 50
# Slowloris攻撃対策

# (通信を意図的に遅延させたコネクション上限の枯渇を防止)

RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500

# Cross-Site Tracing (XST) 対策

# (TRACEリクエストを使ったcookie や認証ヘッダーの盗み取りを防止)

TraceEnable Off

# httpoxy脆弱性対策

# (HTTP_PROXYという環境変数を使った情報の盗み取りを防御)

RequestHeader unset Proxy

# 一度アクセスHTTPSにアクセスすると、その後6ヶ月間はHTTPSアクセスに強制

# (HTTPでアクセスされた瞬間に攻撃者が通信を傍受するのを阻止)

Header always set Strict-Transport-Security "max-age=15768000"
# 上記設定はHTTP通信でのテストが困難になるので、テスト段階ではこちら10分設定を推奨

#Header always set Strict-Transport-Security "max-age=600"


# --- Content-Security-Policy(CSP)設定 ---

# default-src設定 = 自分と同じドメインから配信されるファイルを許可

# https設定 = HTTPS通信から送られるデータは許可

# script-src設定 = 自サーバー内のJavaScript、またはHTML内のインラインと、テキストデータから変換されるJavaScriptを許可

# frame-ancestors設定 = 他人がiframeを使って自分のサイトを埋め込むのを禁止

Header always set Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'; frame-ancestors 'self';"

# --- ディレクトリ固有設定 ---

<Directory "
[アクセス制限したいディレクトリのpath]
">
    
# 意図しない設定上書きを防止。.htaccessを使ってる場合はこの設定はコメントアウトする必要がある。

    AllowOverride None
    
    
# ファイル一覧出力の禁止(ファイル情報の秘匿)

    
# シンボリックリンク禁止(非公開ファイルへの不正アクセスを禁止)

    Options -Indexes -FollowSymLinks
        
    
# GET, POST, HEAD 以外はすべて拒否

    <LimitExcept GET POST HEAD>
        Require all denied
    </LimitExcept>
</Directory>
 [アクセス制限したいディレクトリのpath] 
は、Apacheのみでサイトを運営してるなら多分
 /var/www/html 
辺りになると思います。
自分の場合 リバースプロキシ でTomcatと連携してるので、TomcatのwebappsへのPathを指定しています。

CSP設定に関しては、本当はもっと細かく設定した方が安全なのですが、あまりに長文になってしまうので諦めました。
凝りだすとここまで長文になってしまいます。しかも細かいメンテナンスが欠かせません。
Header always set Content-Security-Policy "default-src 'self' *.a8ad.jp *.a8.net *.googlesyndication.com *.doubleclick.net *.google.com *.gstatic.com *.google-analytics.com *.googletagmanager.com; script-src 'self' 'unsafe-inline' 'unsafe-eval' ajax.googleapis.com *.a8ad.jp *.a8.net *.googlesyndication.com *.doubleclick.net *.google.com *.gstatic.com *.google-analytics.com *.googletagmanager.com; img-src 'self' data: *.a8ad.jp *.a8.net *.googlesyndication.com *.doubleclick.net *.google.com *.gstatic.com *.google-analytics.com; frame-src 'self' *.a8ad.jp *.a8.net *.googlesyndication.com *.doubleclick.net *.google.com; connect-src 'self' *.googlesyndication.com *.doubleclick.net *.google.com *.adtrafficquality.google *.google-analytics.com; style-src 'self' 'unsafe-inline' *.googlesyndication.com *.google.com *.gstatic.com; font-src 'self' data: *.gstatic.com; frame-ancestors 'self';"

Headerコードを有効化

セキュリティヘッダーの設定で
 Header 
というコードを多用しましたが、あれは実はUbuntuの初期状態では使用できません。
変更したsecurity.confを保存し、下のコードでApacheの文法チェックをしてみます。
apache2ctl configtest
すると下のようなエラー文が出ます。
Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration

このエラーを回避するには、下のコードを打ち込んでHeaderのコードを有効化する必要があります。
sudo a2enmod headers
この後にApacheをリスタートすればセキュリティ設定はOK。
sudo service apache2 restart
0
0