Memo/Linux
Apache †
'Memo/Linux/Apache/' には、下位層のページがありません。
IPv6でしかListenしていない場合 †
環境
CentOS7.x 64bit
httpd-2.4.6-45.el7.centos.4.x86_64
Traceメソッドを無効化する †
Cross-Site Tracing(XST)攻撃対応
記事
sudo vi / etc/ httpd/ conf/ httpd.conf
----
TraceEnable off
----
sudo service httpd restart
バージョン番号等を隠す †
セキュリティ上、OSやアプリのバージョン番号が分かると攻撃しやすいので隠す
CentOS6.x, httpd-2.2
SSLv3.0を無効にする(POODLE脆弱性対応) †
vim / etc/ httpd/ conf.d/ ssl.conf
----
SSLEngine on
# SSLv3の無効化(POODLE脆弱性対応)
SSLProtocol all -SSLv2 -SSLv3
# ELB Security Policy-2015-05: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-security-policy-table.html
SSLCipherSuite 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DES-CBC3-SHA'
# 暗号化方式をサーバで指定 (Apache 2.1以上)
SSLHonorCipherOrder On
# SSL圧縮を無効(Apache 2.2.24以上)
SSLCompression off
----
service httpd configtest
service httpd restart
アクセスログにレスポンスタイムとロードバランサのアクセス元IPを追加 †
ロードバランサ配下にあるapacheの場合、アクセスログに残るIPはロードバランサのIPになる。アクセス元IPも残したい場合。
mod_rpaf を使う方法もある。
AWS ELBのバックエンドサーバにapacheが入っていると仮定して、アクセスログに以下の項目を追加したい場合
LDAP認証 †
CentOS6.3 x86_64 へインストールとNameVirtualHost? + リバースプロキシ(80 > 10080)の場合の設定
yum install httpd mod_ssl mod_authz_ldap
vim / etc/ httpd/ conf.d/ authz_ldap.conf
----
LoadModule authz_ldap_module modules/ mod_authz_ldap.so
# 証明書のチェック(SSL自己証明書の場合はOFF。ldapsを使う場合にも影響)
LDAPVerifyServerCert Off
# LDAP問い合わせキャッシュ秒
#LDAPOpCacheTTL 1
----
vim / etc/ httpd/ conf.d/ vhost_base.conf
----
NameVirtualHost * :80
< VirtualHost * :80 >
ServerAdmin root@ localhost
ServerName 127.0.0.1
DocumentRoot / var/ www/ html
# no_log: load balancer health check
SetEnvIf Request_URI "/check.html" no_log
ErrorLog logs/ error_log
CustomLog logs/ access_log combined env =! no_log
< Directory />
Options -Indexes FollowSymLinks
AllowOverride All
</ Directory>
</ VirtualHost>
----
vim / etc/ httpd/ conf.d/ example.com.conf
----
< VirtualHost * :80 >
ServerAdmin root@ localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot / var/ www/ vhost/ example.com/ public_html
# no_log: load balancer health check
SetEnvIf Request_URI "/check.html" no_log
ErrorLog logs/ example.com-error_log
CustomLog logs/ example.com-access_log combined env =! no_log
ProxyPass / http:// localhost:10080 /
ProxyPassReverse / http:// localhost:10080 /
< Proxy *>
AllowOverride None
AuthType Basic
AuthName "Auth LDAP"
AuthBasicProvider ldap
AuthLDAPUrl "ldaps://ldapserver:639/ou=users,dc=example,dc=com?uid"
AuthLDAPBindDN "cn=connector,ou=admin,dc=example,dc=com"
AuthLDAPBindPassword "********"
AuthzLDAPAuthoritative off
Require valid-user
</ Proxy>
< Directory />
Options -Indexes FollowSymLinks
AllowOverride All
</ Directory>
</ VirtualHost>
----
mod_rewriteとserver-statusの併用 †
RewriteCond % { REQUEST_URI} =/ server- status
RewriteRule ^.* $ / server- status [ L]
高速化 †
BASIC認証にワンタイムパスワードを使う †
一般ユーザ権限でapacheを起動 †
mod_rpaf でロードバランサ経由のアクセス元IPをログに残す †
CentOS5.8 x86_64 オリジナル+パッチでビルド
mod_tofu画像のリサイズ †
特定URLだけbasic認証を除外する †
.htaccessでmod_rewriteを使っている場合は、URLが書き換えられてしまうので、.htaccess側で指定する
sudoを使う †
Apache Killer脆弱性対策 †
CustomLog はサーバ設定ファイル, バーチャルホスト内でしか有効にならないので注意
Apache 2.2
cat >> / etc/ httpd/ conf.d/ range-CVE-2011 -3192 .conf << 'EOS'
# Apache 2.2
# Drop the Range header when more than 5 ranges.
# CVE-2011-3192
SetEnvIf Range (?:,.*?){5,5} bad-range=1
RequestHeader unset Range env=bad-range
# We always drop Request-Range; as this is a legacy
# dating back to MSIE3 and Netscape 2 and 3.
RequestHeader unset Request-Range
# optional logging.
CustomLog logs/range-CVE-2011-3192.log common env=bad-range
CustomLog logs/range-CVE-2011-3192.log common env=bad-req-range
EOS
service httpd graceful
Apache Pre 2.2 and 1.3
cat >> / etc/ httpd/ conf.d/ range-CVE-2011 -3192 .conf << 'EOS'
# Apache Pre 2.2 and 1.3
# Reject request when more than 5 ranges in the Range: header.
# CVE-2011-3192
#
RewriteEngine on
RewriteCond %{HTTP:range} !(bytes=[^,]+(,[^,]+){0,4}$|^$)
# RewriteCond %{HTTP:request-range} !(bytes=[^,]+(?:,[^,]+){0,4}$|^$)
RewriteRule .* - [F]
# We always drop Request-Range; as this is a legacy
# dating back to MSIE3 and Netscape 2 and 3.
RequestHeader unset Request-Range
EOS
service httpd graceful
テスト。不正なRangeリクエストを生成して、rangeが削除か「0-」に書き換えられているかをチェック
サーバ設定ファイル, バーチャルホスト内に記載した場合はログ(/var/log/httpd/range-CVE-2011-3192.log)もチェック
cat >> / var/ www/ html/ h.php << 'EOS'
<?php
$headers = apache_request_headers();
var_export($headers);
EOS
curl \
-H "HEAD / HTTP/1.1" \
-H "Range: bytes=0-,5-0,5-1,5-2,5-3,5-4,5-5,5-6,5-7,5-8,5-9,5-10,5-" \
-H "Request-Range: bytes=0-,5-0,5-1,5-2,5-3,5-4,5-5,5-6,5-7,5-8,5-9,5-10,5-" \
-H "Connection: close" \
http:// 127.0.0.1/ h.php
.htaccess tips †
ワイルドカードSSL自己証明書 †
*.example.com のワイルドカード自己証明書を作成する。Firefoxでは例外として追加すれば動作可能。他のバーチャルホストでも「<VirtualHost? *:443>」〜「</VirtualHost? >」をコピペすれば同じ。
本物は SSL証明書 RapidSSL 2600円 ワイルドカード 18000円 (RapidSSL Strategic Partner) が安い
Blog: 名前ベースのバーチャルホストに オレオレ ワイルドカード証明書でSSL対応してみた。 ? Neo Titans
openssl genrsa -aes256 2048 > server.key
パスワードは適当
openssl rsa -in server.key -out server-nopass.key
パスワード無しに変換
openssl req -new -x509 -days 3653 -text -key server-nopass.key > server.crt
Country Name ( 2 letter code) [ GB] :JP
State or Province Name ( full name) [ Berkshire] :
Locality Name ( eg, city) [ Newbury] :
Organization Name ( eg, company) [ My Company Ltd] :
Organizational Unit Name ( eg, section) [ ] :
Common Name ( eg, your name or your server's hostname) []:*.example.com
Email Address []:webmaster@example.com
mv server-nopass.key /etc/pki/tls/private/wild.example.com.key
mv server.crt /etc/pki/tls/certs/wild.example.com.crt
vim /etc/httpd/conf.d/sample.example.com.conf
----
<VirtualHost *:443>
ServerAdmin webmaster@example.com
ServerName sample.example.com
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/wild.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/wild.example.com.key
SetEnv HTTPS on
DocumentRoot /var/www/vhost/sample.example.com/public_html
ErrorLog logs/sample.example.com-ssl_error_log
CustomLog logs/sample.example.com-ssl_access_log combined env=!no_log
<Directory />
Options -Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
SSL証明書 †
基本的に1台のWEBサーバ(1GlobalIP)でNameVirtualHost? だと1つのSSL証明書しか使えない
*.example.com のようなワイルドカードSSL証明書を使う
CSRの作成。ワイルドカード証明書なら「Common Name: *.example.com」
openssl req -new -key www.example.com.key -out www.example.com.csr
Country Name ( 2 letter code) [ AU] :JP
State or Province Name ( full name) [ Some-State] :Tokyo
Locality Name ( eg, city) [ ] : Higashi Ikebukuro, Toshima-ku
Organization Name ( eg, company) [ Internet Widgits Pty Ltd] : Example Corp
Organizational Unit Name ( eg, section) [ ] : Network Administration
Common Name ( eg, YOUR name) [ ] : www.example.com
Email Address [ ] :
A challenge password [ ] :
An optional company name [ ] :
同時接続数の制限 †
ab(ApacheBench? ) †
Apache 2.2.11のabなら、そのままUser-agentを表示してくれるらしい
「Could not reliably determine the server's fully qualified domain name」ワーニング †
hostsにFQDNが設定されていないと、apache再起動や「httpd -S」等で表示されるワーニング
パフォーマンスチューニング †
RewriteRule? の使い方 †
/test/?var=foo のアクセスを /test.php?id=test&var=foo に渡したい場合
GETが欲しい時は以下を付ける
%{QUERY_STRING}
アクセスログの記録に特定条件を指定する †
SSL自己証明書 †
お試しでHTTPSサーバを立ててみたいときとか。実験用。
ブラウザによっては毎回認証するか聞いてくる(Opera9.02)
環境
CentOS release 6.2 (Final) x86_64
秘密鍵等の作成
# 秘密鍵の作成
openssl genrsa -aes128 1024 > server.key
# パスフレーズ無しの秘密鍵へ変換
openssl rsa -in server.key > server-nopass.key
# 公開鍵の作成
openssl req -new -key server-nopass.key > server.csr
-----
Country Name ( 2 letter code) [ XX] : JP
State or Province Name ( full name) [ ] : Tokyo
Locality Name ( eg, city) [ Default City] : Akasaka, Minato-ku
Organization Name ( eg, company) [ Default Company Ltd] : Example Inc.
Organizational Unit Name ( eg, section) [ ] : Example Section
Common Name ( eg, your name or your servers hostname ) [ ] : www.example.com
Email Address [ ] :
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password [ ] :
An optional company name [ ] :
----
# デジタル証明書の作成
openssl x509 -in server.csr -days 365 -req -signkey server-nopass.key > server.crt
# デフォルトパスへコピー
cp server.crt / etc/ pki/ tls/ certs/
cp server-nopass.key / etc/ pki/ tls/ private/
chmod 600 / etc/ pki/ tls/ private/ server-nopass.key / etc/ pki/ tls/ certs/ server.crt
SSL証明書を導入しているにもかかわらず、Operaで認証ダイアログが出る場合 †
サブドメインまで有効なSSL証明書(*.foo.com)をインストールしてあっても、Opera9.0では「SSL証明書とドメイン名が異なっています。」と毎回ダイアログが出る。
IE6.0やFirefox1.5では表示されない。
その場合は、apacheのServerName? が適切に設定するかを確認する。
緩やかな再起動 †
親プロセスを殺さずに、新しい子プロセスから新しい設定を適用する事ができます。
よってサービスを停止せずに新しい設定を適用できます。
Basic認証の.htpasswdを管理するcgi †
コンテンツタイプを設定する †
たとえば、*.php、*.inc、*.cls、*.classファイルをphpとして実行させたい場合は以下のように設定します。
編集
vi /etc/httpd/conf.d/php.conf
特定のファイルのみ設定
<Files ~ "\.(php|inc|class|cls)$">
</Files>
特定のディレクトリのみ設定
<Directory "パス">
AddType application/x-httpd-php .php
AddType application/x-httpd-php .inc
AddType application/x-httpd-php .cls
AddType application/x-httpd-php .class
</Directory>
htmlファイルで、charsetを指定しても文字化けする場合は? †
Apacheがデフォルトでhttp headerにcharsetを指定している場合があります。
以下のようにデフォルトでUTF-8が設定されていると文字化けしました。
AddDefaultCharset UTF-8
同時接続数を得る †
以下の結果が、httpd.confのMaxClients? を上回っているならば、MaxClients? を要調整。
$ netstat -a | grep http | wc -l
ログ解析ツール †
比較サイト
http://awstats.sourceforge.net/docs/awstats_compare.html
ファイル一覧を見えるようにする/ディレクトリインデックス †
ディレクトリ一覧を出したいディレクトリに以下の.htaccessファイルを作成する。
.htaccess
Options Indexes
その他 †