CentOS 7にApacheをインストールする方法
CentOS 7サーバーにApache 2.4をインストールする方法を説明します。安定したウェブサーバーを構築するための前提条件と手順を解説します。
ModSecurityは、さまざまなWebアプリケーションの潜在的な脆弱性を狙ったさまざまなサイバー攻撃からApache、Nginx、IISを保護するのに最適なオープンソースのWebアプリケーションファイアウォール(WAF)モジュールです。
この記事では、CentOS 7、Debian 8、Ubuntu 16.04にNginxのModSecurityをインストールして構成します。
root
ます。このガイドに従って、サーバーのカーネルとパッケージを利用可能な最新バージョンに更新します。
NginxとModSecurityを正常にコンパイルする前に、次のようにいくつかのソフトウェアパッケージをインストールする必要があります。
a)CentOS 7の場合:
yum groupinstall -y "Development Tools"
yum install -y httpd httpd-devel pcre pcre-devel libxml2 libxml2-devel curl curl-devel openssl openssl-devel
shutdown -r now
b)Debian 8またはUbuntu 16.04の場合:
apt-get install -y git build-essential libpcre3 libpcre3-dev libssl-dev libtool autoconf apache2-dev libxml2-dev libcurl4-openssl-dev automake pkgconf
NginxマスターブランチのModSecurityでいくつかの不安定性が報告されているため、現時点では、nginx_refactoring
可能な限り最新バージョンのブランチを使用することが公式に推奨されています。
nginx_refactoring
Nginx用ModSecurity のブランチをダウンロードします。
cd /usr/src
git clone -b nginx_refactoring https://github.com/SpiderLabs/ModSecurity.git
ModSecurityのコンパイル:
a)CentOS 7の場合:
cd ModSecurity
sed -i '/AC_PROG_CC/a\AM_PROG_CC_C_O' configure.ac
sed -i '1 i\AUTOMAKE_OPTIONS = subdir-objects' Makefile.am
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make
注:上記の2つのsed
コマンドは、新しいautomakeバージョンを使用する際の警告メッセージを防ぐために使用されます。
b)Debian 8またはUbuntu 16.04の場合:
cd ModSecurity
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
make
Nginx 1.10.3
執筆時点でのNginxの最新の安定版リリースをダウンロードして解凍します。
cd /usr/src
wget https://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz && rm -f nginx-1.10.3.tar.gz
a)CentOS 7の場合:
まず、Nginx 専用のユーザーnginx
と専用グループを作成する必要がありますnginx
。
groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx
次に、ModSecurityおよびSSLモジュールを有効にしてNginxをコンパイルします。
cd nginx-1.10.3/
./configure --user=nginx --group=nginx --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
make
make install
Nginxのデフォルトユーザーを変更します。
sed -i "s/#user nobody;/user nginx nginx;/" /usr/local/nginx/conf/nginx.conf
b)Debian 8またはUbuntu 16.04の場合:
まず、既存のユーザーwww-data
と既存のグループを使用する必要がありwww-data
ます。
次に、ModSecurityおよびSSLモジュールを有効にしてNginxをコンパイルします。
cd nginx-1.10.3/
./configure --user=www-data --group=www-data --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_module
make
make install
Nginxのデフォルトユーザーを変更します。
sed -i "s/#user nobody;/user www-data www-data;/" /usr/local/nginx/conf/nginx.conf
Nginxが正常にインストールされている場合、関連ファイルは次の場所にあります。
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
次の方法でインストールをテストできます。
/usr/local/nginx/sbin/nginx -t
何も問題がなければ、出力は次のようになります。
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
便宜上、Nginx用のsystemdユニットファイルを設定できます。
cat <<EOF>> /lib/systemd/system/nginx.service
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillStop=/usr/local/nginx/sbin/nginx -s stop
KillMode=process
Restart=on-failure
RestartSec=42s
PrivateTmp=true
LimitNOFILE=200000
[Install]
WantedBy=multi-user.target
EOF
次のように、Nginxを開始/停止/再起動できます。
systemctl start nginx.service
systemctl stop nginx.service
systemctl restart nginx.service
vi /usr/local/nginx/conf/nginx.conf
セグメント内で次のセグメントを見つけhttp {}
ます。
location / {
root html;
index index.html index.htm;
}
以下の行をlocation / {}
セグメントに挿入します。
ModSecurityEnabled on;
ModSecurityConfig modsec_includes.conf;
#proxy_pass http://localhost:8011;
#proxy_read_timeout 180s;
最終結果は次のようになります。
location / {
ModSecurityEnabled on;
ModSecurityConfig modsec_includes.conf;
#proxy_pass http://localhost:8011;
#proxy_read_timeout 180s;
root html;
index index.html index.htm;
}
保存して終了:
:wq!
注:上記のNginx構成は、リバースプロキシではなくWebサーバーとしてNginxを使用するためのサンプル構成にすぎません。Nginxをリバースプロキシとして使用している場合は#
、最後の2行の文字を削除し、適切に変更します。
/usr/local/nginx/conf/modsec_includes.conf
。
cat <<EOF>> /usr/local/nginx/conf/modsec_includes.conf
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/*.conf
EOF
注:上記の設定は、owasp-modsecurity-crs/rules/
ディレクトリ内のすべてのOWASP ModSecurityコアルールを適用します。選択的ルールのみを適用する場合は、include owasp-modsecurity-crs/rules/*.conf
行を削除して、ステップ4.5の後で必要な正確なルールを指定する必要があります。
cp /usr/src/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
cp /usr/src/ModSecurity/unicode.mapping /usr/local/nginx/conf/
/usr/local/nginx/conf/modsecurity.conf
ファイルを変更します。
sed -i "s/SecRuleEngine DetectionOnly/SecRuleEngine On/" /usr/local/nginx/conf/modsecurity.conf
cd /usr/local/nginx/conf
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
cd owasp-modsecurity-crs
mv crs-setup.conf.example crs-setup.conf
cd rules
mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
Nginxを起動します。
systemctl start nginx.service
外部アクセスを許可するためにポート80を開きます。
a)CentOS 7の場合:
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --reload
b)Debian 8の場合:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
touch /etc/iptables
iptables-save > /etc/iptables
c)Ubuntu 16.04の場合:
ufw allow OpenSSH
ufw allow 80
ufw default deny
ufw enable
Webブラウザで次のように指定します。
http://203.0.113.1/?param="><script>alert(1);</script>
grep
次のように使用して、エラーメッセージをフェッチします。
grep error /usr/local/nginx/logs/error.log
出力には、次のようないくつかのエラーメッセージが含まれます。
2017/02/15 14:07:54 [error] 10776#0: [client 104.20.23.240] ModSecurity: Warning. detected XSS using libinjection. [file "/usr/local/nginx/conf/owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf"] [line "56"] [id "941100"] [rev "2"] [msg "XSS Attack Detected via libinjection"] [data "Matched Data: found within ARGS:param: \x22><script>alert(1);</script>"] [severity "CRITICAL"] [ver "OWASP_CRS/3.0.0"] [maturity "1"] [accuracy "9"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-xss"] [tag "OWASP_CRS/WEB_ATTACK/XSS"] [tag "WASCTC/WASC-8"] [tag "WASCTC/WASC-22"] [tag "OWASP_TOP_10/A3"] [tag "OWASP_AppSensor/IE1"] [tag "CAPEC-242"] [hostname ""] [uri "/index.html"] [unique_id "ATAcAcAkucAchGAcPLAcAcAY"]
それでおしまい。ご覧のとおり、ModSecurityモジュールは、デフォルトのアクションポリシーに従って、この攻撃を正常にログに記録しています。さらにカスタム設定を行いたい場合は/usr/local/nginx/conf/modsecurity.conf
、/usr/local/nginx/conf/owasp-modsecurity-crs/crs-setup.conf
ファイルをよく確認して編集してください。
CentOS 7サーバーにApache 2.4をインストールする方法を説明します。安定したウェブサーバーを構築するための前提条件と手順を解説します。
CentOS 7にSeafileサーバーをインストールする方法。Seafile(コミュニティバージョン)は、ownCloudに似た無料のオープンソースファイル同期および共有ソリューションです。
CentOS 7にGraylogサーバーをインストールし、ログ管理を行う方法を学びます。
CentOS 7サーバーにApache 2.4をインストールする方法を説明します。安定したウェブサーバーを構築するための前提条件と手順を解説します。
FreeBSD 11.1におけるBlacklistdのインストール方法について詳しく解説します。この方法を通じて、強力なセキュリティ対策を実装できます。
サーバーマネージャーを使用して、Windows Serverの管理が向上します。セキュリティリスクを軽減し、効率的な管理を実現します。
CentOS 7にSeafileサーバーをインストールする方法。Seafile(コミュニティバージョン)は、ownCloudに似た無料のオープンソースファイル同期および共有ソリューションです。
Snortは無料のネットワーク侵入検知システムです。最新の方法で、SnortをDebianにインストールし、設定する手順を紹介します。ネットワークのセキュリティを強化しましょう。
CentOS 7にGraylogサーバーをインストールし、ログ管理を行う方法を学びます。
WindowsサーバーでWebサイトを実行している場合、電子メールも受信できるようにするためにhMailServerを使用する方法を解説します。
FiveMサーバーをUbuntu 19.04にインストールするための詳細なガイド。必要条件からインストール、起動、トラブルシューティングまで、すべてのステップを含みます。
Debian 10にWebDAVをデプロイする方法を学び、WsgiDAVとSSL証明書で安全な接続を実現しましょう。
ヘルスケアにおけるAIは、過去数十年から大きな飛躍を遂げました。したがって、ヘルスケアにおけるAIの未来は、日々成長を続けています。