ModSecurityは、SQLインジェクション、クロスサイトスクリプティング、ローカルファイルインクルードなどのいくつかのタイプの攻撃からWebアプリケーションを保護するために使用される、人気のある無料のオープンソースWebアプリケーションファイアウォールです。これは、Webサイト、cPanel、およびその他のホスティングコントロールパネルを保護するために頻繁に使用されます。 ModSecurityは主にApacheWebサーバー用に設計されていますが、NginxWebサーバーでも機能します。
この投稿では、RockyLinux8にNginxを使用してModSecurityをインストールする方法を紹介します。
前提条件
- Atlantic.NetクラウドプラットフォームでRockyLinux8を実行しているサーバー
- サーバーで構成されているrootパスワード
ステップ1-Atlantic.Netクラウドサーバーを作成する
まず、Atlantic.Netクラウドサーバーにログインします。 2GB以上のRAMを搭載したオペレーティングシステムとしてRockyLinux8を選択して、新しいサーバーを作成します。 SSH経由でクラウドサーバーに接続し、ページの上部で強調表示されているクレデンシャルを使用してログインします。
サーバーにログインしたら、次のコマンドを実行して、ベースシステムを最新の利用可能なパッケージで更新します。
dnf update -y
ステップ2–必要な依存関係をインストールする
まず、必要なすべての依存関係をサーバーにインストールする必要があります。次のコマンドですべてをインストールできます:
dnf install gcc-c++ flex bison yajl curl-devel curl zlib-devel pcre-devel autoconf automake git curl make libxml2-devel pkgconfig libtool httpd-devel redhat-rpm-config git wget openssl openssl-devel vim
dnf --enablerepo=powertools install doxygen yajl-devel -y
次に、次のコマンドを使用してEPELおよびRemiリポジトリをインストールします。
dnf install epel-release https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
次に、次のコマンドを使用してGeoIPをインストールします。
dnf --enablerepo=remi install GeoIP-devel -y
ステップ3–ModSecurityをインストールする
まず、次のコマンドを使用してModSecurityの最新バージョンをダウンロードします。
git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity
次に、ディレクトリをModSecurityに変更し、次のコマンドを使用して他のモジュールをインストールします。
cd ModSecurity git submodule init git submodule update
次に、次のコマンドを使用してModSecurityをコンパイルしてインストールします。
./build.sh ./configure make make install
ステップ4–LibModsecurityサポートを使用してNginxをインストールする
NginxでLibModsecurityのサポートを有効にするには、LibModsecurityをサポートするNginxをコンパイルする必要があります。
まず、次のコマンドを使用してModSecurity-nginxコネクタをダウンロードします。
cd ../ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git
次に、次のコマンドを使用してNginxの最新の安定バージョンをダウンロードします。
wget http://nginx.org/download/nginx-1.19.10.tar.gz
次に、次のコマンドを使用して、ダウンロードしたファイルを抽出します。
tar xzf nginx-1.19.10.tar.gz
次に、次のコマンドを使用してNginxのユーザーを作成します。
useradd -r -M -s /sbin/nologin -d /usr/local/nginx nginx
次に、ディレクトリをNginxソースに変更し、次のコマンドを使用してコンパイルします。
cd nginx-1.19.10 ./configure --user=nginx --group=nginx --with-pcre-jit --with-debug --with-http_ssl_module --with-http_realip_module --add-module=/root/ModSecurity-nginx
次に、次のコマンドを使用してインストールします。
make make install
次に、次のコマンドを使用して、サンプルのModSecurity構成ファイルとUnicodeマッピングファイルをコピーします。
cp /root/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf cp /root/ModSecurity/unicode.mapping /usr/local/nginx/conf/
次に、Nginx構成ファイルをバックアップします:
cp /usr/local/nginx/conf/nginx.conf{,.bak}
次に、次のコマンドを使用してNginx構成ファイルを編集します。
nano /usr/local/nginx/conf/nginx.conf
すべての行を削除し、次の行を追加します:
user nginx; worker_processes 1; pid /run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name nginx.example.com; modsecurity on; modsecurity_rules_file /usr/local/nginx/conf/modsecurity.conf; access_log /var/log/nginx/access_kifarunix-demo.log; error_log /var/log/nginx/error_kifarunix-demo.log; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
ファイルを保存して閉じてから、Nginxログディレクトリを作成します:
mkdir /var/log/nginx
ステップ5–NginxのSystemdサービスファイルを作成する
次に、Nginxサービスを管理するためのsystemdサービスファイルを作成する必要があります。次のコマンドを使用して作成できます:
nano /etc/systemd/system/nginx.service
次の行を追加します:
[Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=mixed PrivateTmp=true [Install] WantedBy=multi-user.target
ファイルを保存して閉じ、次のコマンドを使用してNginxバイナリのシンボリックリンクを作成します。
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
次に、systemdデーモンをリロードして、変更を適用します。
systemctl daemon-reload
次に、Nginxサービスを開始し、システムの再起動時に開始できるようにします。
systemctl enable --now nginx
次のコマンドでNginxのステータスを確認できます:
systemctl status nginx
次の出力が得られます:
● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2021-11-08 09:59:48 UTC; 5s ago Process: 73046 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 73044 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 73043 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 73048 (nginx) Tasks: 2 (limit: 11411) Memory: 3.2M CGroup: /system.slice/nginx.service ├─73048 nginx: master process /usr/sbin/nginx └─73049 nginx: worker process Nov 08 09:59:48 rockylinux systemd[1]: Starting The nginx HTTP and reverse proxy server... Nov 08 09:59:48 rockylinux nginx[73044]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok Nov 08 09:59:48 rockylinux nginx[73044]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful Nov 08 09:59:48 rockylinux systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument Nov 08 09:59:48 rockylinux systemd[1]: Started The nginx HTTP and reverse proxy server.
ステップ6–ModSecurityルールを有効にする
次のコマンドで有効にできます:
sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /usr/local/nginx/conf/modsecurity.conf
また、次のコマンドを使用して監査ログを有効にします。
sed -i 's#/var/log/modsec_audit.log#/var/log/nginx/modsec_audit.log#' /usr/local/nginx/conf/modsecurity.conf
ステップ7–OWASPModSecurityコアルールセットをインストールする
OWASPは、ModSecurityの一般的な攻撃検出ルールを提供します。次のコマンドでダウンロードできます:
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /usr/local/nginx/conf/owasp-crs
次に、次のコマンドを使用して、OWASPルール構成ファイルの名前を変更します。
cp /usr/local/nginx/conf/owasp-crs/crs-setup.conf{.example,}
次に、ModSecurity構成ファイルでOWASPルールを定義します。
echo -e "Include owasp-crs/crs-setup.conf\nInclude owasp-crs/rules/*.conf" >> /usr/local/nginx/conf/modsecurity.conf
次に、Nginxサービスを再起動して、変更を適用します。
systemctl restart nginx
ステップ8–ModSecurityを確認する
この時点で、Nginxがインストールされ、ModSecurityサポートが設定されています。それでは、テストしてみましょう。
curlコマンドを使用して次のコマンドインジェクションを実行します。
curl localhost/index.html?exec=/bin/bash
すべてが正常であれば、以下に示すように「403Forbidden」エラーが発生するはずです。
<html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.19.10</center> </body> </html>
詳細については、ModSecurityログを確認することもできます:
tail -100 /var/log/nginx/modsec_audit.log
次の出力が表示されます。
---imefFQJy---D-- ---imefFQJy---E-- <html>\x0d\x0a<head><title>403 Forbidden</title></head>\x0d\x0a<body>\x0d\x0a<center><h1>403 Forbidden</h1></center>\x0d\x0a<hr><center>nginx/1.19.10</center>\x0d\x0a</body>\x0d\x0a</html>\x0d\x0a ---imefFQJy---F-- HTTP/1.1 403 Server: nginx/1.19.10 Date: Mon, 08 Nov 2021 10:00:55 GMT Content-Length: 154 Content-Type: text/html Connection: keep-alive ---imefFQJy---H-- ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:exec' (Value: `/bin/bash' ) [file "/usr/local/nginx/conf/owasp-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf"] [line "496"] [id "932160"] [rev ""] [msg "Remote Command Execution: Unix Shell Code Found"] [data "Matched Data: bin/bash found within ARGS:exec: /bin/bash"] [severity "2"] [ver "OWASP_CRS/3.2.0"] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-shell"] [tag "platform-unix"] [tag "attack-rce"] [tag "paranoia-level/1"] [tag "OWASP_CRS"] [tag "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION"] [tag "WASCTC/WASC-31"] [tag "OWASP_TOP_10/A1"] [tag "PCI/6.5.2"] [hostname "127.0.0.1"] [uri "/index.html"] [unique_id "1636365655"] [ref "o1,8v21,9t:urlDecodeUni,t:cmdLine,t:normalizePath,t:lowercase"] ModSecurity: Access denied with code 403 (phase 2). Matched "Operator `Ge' with parameter `5' against variable `TX:ANOMALY_SCORE' (Value: `5' ) [file "/usr/local/nginx/conf/owasp-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "80"] [id "949110"] [rev ""] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [data ""] [severity "2"] [ver "OWASP_CRS/3.2.0"] [maturity "0"] [accuracy "0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "127.0.0.1"] [uri "/index.html"] [unique_id "1636365655"] [ref ""]
結論
上記のガイドでは、RockyLinux8にNginxを使用してModSecurityをインストールする方法を説明しました。これでNginxWebサーバーがModSecurityWAFで保護されました。 ModSecurityは、さまざまな攻撃からサーバーを保護できます。今すぐAtlantic.NetからVPSホスティングを始めましょう!