Cách cài đặt ModSecurity 3 với Nginx trên Ubuntu 22.04

theanh

Administrator
Nhân viên
Bảo mật trang web và ứng dụng web có thể là một nhiệm vụ đầy thách thức đối với bất kỳ quản trị viên hệ thống nào. Có rất nhiều công cụ mã nguồn mở có sẵn để bảo vệ trang web của bạn khỏi các cuộc tấn công DDoS. ModSecurity là Tường lửa ứng dụng web (WAF) miễn phí và mã nguồn mở giúp bảo vệ trang web của bạn khỏi nhiều loại tấn công, bao gồm tấn công tập lệnh chéo trang (XSS), tiêm SQL, chiếm quyền điều khiển phiên và nhiều loại khác nữa.

Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách cài đặt ModSecurity với Nginx trên Ubuntu 22.04.

Điều kiện tiên quyết​

  • Máy chủ chạy Ubuntu 22.04.
  • Mật khẩu gốc được cấu hình trên máy chủ.

Bắt đầu​

Trước tiên, bạn nên cập nhật và nâng cấp tất cả các gói phần mềm của mình lên phiên bản mới nhất. Bạn có thể cập nhật tất cả chúng bằng cách chạy lệnh sau:
Mã:
apt update -y
apt upgrade -y
Sau khi tất cả các gói được cập nhật, hãy cài đặt các gói cần thiết khác với các lệnh sau lệnh:
Mã:
apt install g++ flex bison curl apache2-dev doxygen libyajl-dev ssdeep liblua5.2-dev libgeoip-dev libtool dh-autoreconf libcurl4-gnutls-dev libxml2 libpcre++-dev libxml2-dev git liblmdb-dev libpkgconf3 lmdb-doc pkgconf zlib1g-dev libssl-dev -y
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.

Cài đặt ModSecurity trên Ubuntu 22.04​

Theo mặc định, gói ModSecurity không có trong kho lưu trữ mặc định của Ubuntu. Vì vậy, bạn sẽ cần biên dịch nó từ nguồn.

Đầu tiên, tải xuống phiên bản ModSecurity mới nhất bằng lệnh sau:
Mã:
wget https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.8/modsecurity-v3.0.8.tar.gz
Sau khi tải xuống hoàn tất, hãy giải nén tệp đã tải xuống bằng lệnh sau:
Mã:
tar -xvzf modsecurity-v3.0.8.tar.gz
Tiếp theo, điều hướng đến thư mục đã giải nén và định cấu hình thư mục đó bằng lệnh sau lệnh:
Mã:
cd modsecurity-v3.0.8
./build.sh
./configure
Tiếp theo, cài đặt bằng lệnh sau:
Mã:
make
make install

Cài đặt Nginx với hỗ trợ ModSecurity 3​

Tiếp theo, bạn sẽ cần cài đặt Nginx với hỗ trợ ModSecurity. Trước tiên, hãy tải xuống trình kết nối ModSecurity-nginx bằng lệnh sau:
Mã:
cd ~
git clone https://github.com/SpiderLabs/ModSecurity-nginx.git
Tiếp theo, hãy tải xuống nguồn Nginx bằng lệnh sau:
Mã:
wget https://nginx.org/download/nginx-1.20.2.tar.gz
Tiếp theo, hãy giải nén nguồn Nginx bằng lệnh sau lệnh:
Mã:
tar xzf nginx-1.20.2.tar.gz
Tiếp theo, tạo người dùng cho Nginx bằng lệnh sau:
Mã:
useradd -r -M -s /sbin/nologin -d /usr/local/nginx nginx
Tiếp theo, thay đổi thư mục thành nguồn Nginx và định cấu hình bằng lệnh sau lệnh:
Mã:
cd nginx-1.20.2
./configure --user=nginx --group=nginx --with-pcre-jit --with-debug --with-compat --with-http_ssl_module --with-http_realip_module --add-dynamic-module=/root/ModSecurity-nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
Tiếp theo, cài đặt bằng lệnh sau:
Mã:
make
make modules
make install
Tiếp theo, tạo liên kết tượng trưng của Nginx bằng lệnh sau lệnh:
Mã:
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
Tiếp theo, hãy xác minh phiên bản Nginx bằng lệnh sau:
Mã:
nginx -V
Bạn sẽ nhận được phiên bản Nginx trong lệnh sau đầu ra:
Mã:
nginx version: nginx/1.20.2
built by gcc 11.2.0 (Ubuntu 11.2.0-19ubuntu1)
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --with-pcre-jit --with-debug --with-compat --with-http_ssl_module --with-http_realip_module --add-dynamic-module=/root/ModSecurity-nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
Sau khi hoàn tất, bạn có thể tiến hành cấu hình Nginx với ModSecurity.

Cấu hình Nginx với ModSecurity​

Tiếp theo, sao chép các tệp cấu hình mẫu bằng lệnh sau:
Mã:
cp ~/modsecurity-v3.0.8/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
cp ~/modsecurity-v3.0.8/unicode.mapping /usr/local/nginx/conf/
Tiếp theo, sao lưu tệp cấu hình Nginx:
Mã:
cp /usr/local/nginx/conf/nginx.conf{,.bak}
Tiếp theo, chỉnh sửa tệp cấu hình Nginx:
Mã:
nano /usr/local/nginx/conf/nginx.conf
Xóa các dòng mặc định và thêm các dòng sau:
Mã:
load_module modules/ngx_http_modsecurity_module.so;
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_example.log; error_log /var/log/nginx/error_example.log; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
}
Lưu và đóng tệp, sau đó bật ModSecurity bằng lệnh sau:
Mã:
sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /usr/local/nginx/conf/modsecurity.conf
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.

Cài đặt ModSecurity Core Rule Bộ quy tắc cốt lõi OWASP ModSecurity cung cấp một bộ quy tắc để phát hiện và bảo vệ nhiều loại tấn công, bao gồm OWASP Top Ten, cảnh báo sai tối thiểu.​

Đầu tiên, hãy tải xuống bộ quy tắc OWASP bằng lệnh sau:
Mã:
cd
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /usr/local/nginx/conf/owasp-crs
Tiếp theo, đổi tên crs-setup.conf.example thành crs-setup.conf tệp:
Mã:
cp /usr/local/nginx/conf/owasp-crs/crs-setup.conf{.example,}
Tiếp theo, xác định các quy tắc bằng lệnh sau:
Mã:
echo -e "Include owasp-crs/crs-setup.conf
Include owasp-crs/rules/*.conf" >> /usr/local/nginx/conf/modsecurity.conf
Tiếp theo, xác minh Nginx xem có lỗi cấu hình nào không bằng lệnh sau lệnh:
Mã:
nginx -t
Nếu mọi thứ ổn, bạn sẽ nhận được kết quả sau:
Mã:
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
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.

Tạo tệp dịch vụ Systemd cho Nginx​

Tiếp theo, bạn sẽ cần tạo một tệp dịch vụ systemd để quản lý dịch vụ Nginx. Để bạn có thể bắt đầu và dừng dịch vụ Nginx thông qua hệ thống. Bạn có thể tạo tệp này bằng lệnh sau:
Mã:
nano /etc/systemd/system/nginx.service
Thêm các dòng sau:
Mã:
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
Lưu và đóng tệp, sau đó tải lại daemon systemd để áp dụng thay đổi:
Mã:
systemctl daemon-reload
Tiếp theo, khởi động và kích hoạt Nginx bằng lệnh sau:
Mã:
systemctl start nginx
systemctl enable nginx
Bạn có thể kiểm tra trạng thái Nginx bằng lệnh sau lệnh:
Mã:
systemctl status nginx
Bạn sẽ thấy kết quả sau:
Mã:
? nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/etc/systemd/system/nginx.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2022-10-11 15:40:39 UTC; 6s ago Docs: man:nginx(8) Process: 68438 ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 68439 ExecStart=/usr/local/nginx/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 68440 (nginx) Tasks: 2 (limit: 4579) Memory: 20.0M CPU: 293ms CGroup: /system.slice/nginx.service ??68440 "nginx: master process /usr/local/nginx/sbin/nginx -g daemon on; master_process on;" ??68441 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
Oct 11 15:40:38 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 11 15:40:39 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.

Xác minh ModSecurity​

Sau khi cài đặt và cấu hình Modsecurity bằng Nginx. Đã đến lúc kiểm tra nó. Chạy lệnh sau để kiểm tra Modsecurity chống lại lệnh tiêm:
Mã:
curl localhost?doc=/bin/ls
Nếu mọi thứ đều ổn, bạn sẽ nhận được thông báo "403 Forbidden".
Mã:
403 Forbidden

[HEADING=1]403 Forbidden[/HEADING]
nginx/1.20.2
Bạn cũng có thể kiểm tra nhật ký Modesecurity bằng lệnh sau:
Mã:
tail /var/log/modsec_audit.log
Bạn sẽ thấy nhật ký ModSecurity trong output:
Mã:
ModSecurity: Warning. Matched "Operator `PmFromFile' with parameter `unix-shell.data' against variable `ARGS:doc' (Value: `/bin/ls' ) [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/ls found within ARGS:doc: /bin/ls"] [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 "/"] [unique_id "166550286018.572845"] [ref "o1,6v10,7t: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 "/"] [unique_id "166550286018.572845"] [ref ""]
---IcTYGSZl---I--
---IcTYGSZl---J--
---IcTYGSZl---Z--

Kết luận​

Xin chúc mừng! bạn đã cài đặt thành công ModSecurity với Nginx trên Ubuntu 22.04. Bây giờ bạn có thể triển khai ModSecurity trong môi trường sản xuất của mình để chống lại các cuộc tấn công DDoS. Hãy thoải mái hỏi tôi nếu bạn có bất kỳ câu hỏi nào.
 
Back
Bên trên