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.
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:
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.
Đầu tiên, tải xuống phiên bản ModSecurity mới nhất bằng lệnh sau:
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:
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:
Tiếp theo, cài đặt bằng lệnh sau:
Tiếp theo, hãy tải xuống nguồn Nginx bằng lệnh sau:
Tiếp theo, hãy giải nén nguồn Nginx bằng lệnh sau lệnh:
Tiếp theo, tạo người dùng cho Nginx bằng lệnh sau:
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:
Tiếp theo, cài đặt bằng lệnh sau:
Tiếp theo, tạo liên kết tượng trưng của Nginx bằng lệnh sau lệnh:
Tiếp theo, hãy xác minh phiên bản Nginx bằng lệnh sau:
Bạn sẽ nhận được phiên bản Nginx trong lệnh sau đầu ra:
Sau khi hoàn tất, bạn có thể tiến hành cấu hình Nginx với ModSecurity.
Tiếp theo, sao lưu tệp cấu hình Nginx:
Tiếp theo, chỉnh sửa tệp cấu hình Nginx:
Xóa các dòng mặc định và thêm các dòng sau:
Lưu và đóng tệp, sau đó bật ModSecurity bằng lệnh sau:
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.
Tiếp theo, đổi tên crs-setup.conf.example thành crs-setup.conf tệp:
Tiếp theo, xác định các quy tắc bằng lệnh sau:
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:
Nếu mọi thứ ổn, bạn sẽ nhận được kết quả sau:
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.
Thêm các dòng sau:
Lưu và đóng tệp, sau đó tải lại daemon systemd để áp dụng thay đổi:
Tiếp theo, khởi động và kích hoạt Nginx bằng lệnh sau:
Bạn có thể kiểm tra trạng thái Nginx bằng lệnh sau lệnh:
Bạn sẽ thấy kết quả sau:
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.
Nếu mọi thứ đều ổn, bạn sẽ nhận được thông báo "403 Forbidden".
Bạn cũng có thể kiểm tra nhật ký Modesecurity bằng lệnh sau:
Bạn sẽ thấy nhật ký ModSecurity trong output:
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
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
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
Mã:
tar -xvzf modsecurity-v3.0.8.tar.gz
Mã:
cd modsecurity-v3.0.8
./build.sh
./configure
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
Mã:
wget https://nginx.org/download/nginx-1.20.2.tar.gz
Mã:
tar xzf nginx-1.20.2.tar.gz
Mã:
useradd -r -M -s /sbin/nologin -d /usr/local/nginx nginx
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
Mã:
make
make modules
make install
Mã:
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
Mã:
nginx -V
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
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/
Mã:
cp /usr/local/nginx/conf/nginx.conf{,.bak}
Mã:
nano /usr/local/nginx/conf/nginx.conf
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; } }
}
Mã:
sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /usr/local/nginx/conf/modsecurity.conf
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
Mã:
cp /usr/local/nginx/conf/owasp-crs/crs-setup.conf{.example,}
Mã:
echo -e "Include owasp-crs/crs-setup.conf
Include owasp-crs/rules/*.conf" >> /usr/local/nginx/conf/modsecurity.conf
Mã:
nginx -t
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
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
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
Mã:
systemctl daemon-reload
Mã:
systemctl start nginx
systemctl enable nginx
Mã:
systemctl status nginx
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.
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
Mã:
403 Forbidden
[HEADING=1]403 Forbidden[/HEADING]
nginx/1.20.2
Mã:
tail /var/log/modsec_audit.log
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--