Tối ưu, kiện toàn và bảo mật Apache

0
283

Bài này mình cóp nhặt từ Báo cáo “Tối ưu, kiện toàn và bảo mật Apache” của thầy Lê Thanh Thương, Lớp Linux System Admin. Trong đó, mính có lược bớt phần nói về bảo mật PHP vì mình chưa quan tâm lắm.

Nguyên tắc quan trọng để giảm thiểu các đe dọa về bảo mật

  • Tạo User riêng / Group riêng để chạy Apache (tương tự cho Nginx), tránh dùng nobody:nogroup, càng nên tránh chạy Process Apache bằng user root vì nếu tin tắc đột nhập thành công nhờ lỗ hổng của một Process, Họ có quyền truy dụng đến các procecss khác có cùng UID/GID.
  • Khi tạo User cho Apache, cần lưu ý không cho User Apache có thư mục $HOME riêng như các User thường khác: -d /dev/null, không cho User Apache sử dụng Shell: -s /sbin/nologin để hạn chế tin tắc có cơ hội tiếp cận với hệ thống.
  • Kiểm soát File Permissions của hệ thống thư mục của ứng dụng Web. Nếu ứng dụng Web không cần chức năng Upload cho người dùng thì tốt hơn là nên -w cho tất cả mọi người (Loại quyền ghi cho owner,group-owner,others). Nếu cho phép upload thì chỉ chmod quyền +w cho đúng thư mục chứa file upload mà thôi và nhớ là phải chown thư mục đó với account chạy Apache.
  • Disable các Module không cần thiết, không dùng đến.

Phân biệt Apache mpm worker và Apache mpm prefork

MPM Worker (Multi-Processing Module) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads. The most important directives used to control this MPM are ThreadsPerChild, which controls the number of threads deployed by each child process and MaxClients, which controls the maximum total number of threads that may be launched. Đọc thêm về mpm worker: http://httpd.apache.org/docs/2.2/mod/worker.html.

MPM Prefork implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other. This MPM is very self-regulating, so it is rarely necessary to adjust its configuration directives. Most important is that MaxClients be big enough to handle as many simultaneous requests as you expect to receive, but small enough to assure that there is enough physical RAM for all processes. Đọc thêm về mpm prefork: http://httpd.apache.org/docs/2.2/mod/prefork.html.

Sự khác biệt, ưu và nhược điểm của từng loại? http://codebucket.co.in/apache-prefork-or-worker/

PHP chỉ phù hợp với mpm prefork? Bạn chỉ nên sử dụng mpm prefork khi dùng Apache + mod_php. Xem thêm phần Why shouldn’t I use Apache2 with a threaded MPM in a production environment?

Cài đặt Apache bằng cách biên dịch để tắt các Module không cần thiết

Các Module cần thiết trong Apache:

  • httpd_core: Module cốt lõi của Apache, required.
  • mod_access: Enables allow/deny type access controls.
  • mod_auth: Enables user authentication.
  • mod_dir: Controls formatting of directory listings.
  • mod_log_config: Enables use of custom log formats.
  • mod_mime: Provides support for MIME files.
  • mod_so: Provides runtime support for dynamic shared objects (DSOs).
  • mod_unique_id: Generates a unique request identifier for each request.
  • mod_security: Đây là một module quan trọng trong việc bảo mật, đóng vai trò Reverse Proxy và Firewall cho Apache.
  • mod_rewrite: Enables URI-to-filename mapping.

http://docstore.mik.ua/orelly/networking_2ndEd/tcp/ch11_03.htm

Các Module còn lại của Apache nên được tắt nếu không có nhu cầu phải sử dụng đến. Đặc biệt là hai module nguy hiểm như mod_autoindex (Enables automatic index generation) và mod_info (Enables use of the server-info handler)

Lệnh cấu hình biên dịch Apache

./configure --prefix=/usr/local/apache \
--with-mpm=prefork \
--enable-auth \
--disable-cgi \
--disable-cgid \
--enable-dir \
--enable-log-config \
--enable-mime \
--enable-rewrite \
--disable-ssl \
--enable-unique-id \
--disable-suexec --enable-so \
--disable-auth-dbm \
--disable-auth-digest \
--disable-autoindex \
--disable-cern-meta \
--disable-dav \
--disable-dav-fs \
--disable-env \
--disable-expires \
--disable-file-cache \
--disable-headers \
--disable-include \
--disable-mime-magic \
--disable-negotiation \
--disable-setenvif \
--disable-speling \
--disable-usertrack \
--disable-vhost-alias \
--disable-auth-anon \
--disable-charset-lite \
--disable-disk-cache \
--disable-mem-cache \
--disable-cache \
--disable-deflate \
--disable-filter --disable-version \
--disable-cgid \
--disable-cgi \
--disable-ext-filter \
--disable-case-filter \
--disable-case-filter-in \
--disable-proxy \
--disable-proxy-connect \
--disable-proxy-ftp \
--disable-proxy-http \
--disable-status \
--disable-asis \
--disable-info \
--disable-imap \--disable-actions \
--disable-userdir

Cấu hình và Tối ưu Apache

Hiểu từng dòng cấu hình của Apache và ảnh hưởng của nó đến hệ thống ra sao? Trong từng trường hợp cụ thể thì nên cấu hình như thế nào là tối ưu?

http://httpd.apache.org/docs/2.4/configuring.html

Dưới đây là bảng cấu hình chuẩn của Apache httpd.conf trong trường hợp một Average Server và lượng truy cập bình thường (Không có dấu hiệu của một cuộc tấn công DDOS).

# Wait up to 300 seconds for slow clients
TimeOut 300
# Trong trường hợp bị DDOS thì giảm TimeOut xuống dưới 60s
# Allow connections to be reused between requests
KeepAlive On
# allow a maximum of 100 requests per connection
MaxKeepAliveRequests 100
# wait up to 15 seconds for the next request on an open connection
KeepAliveTimeout 15

# Số lượng Request đồng thời tối đa, vượt quá con số này thì các request sẽ đưa vào hàng đợi hoặc bị từ chối.
MaxClients 150

# 0 là Unlimited, mặc định nên để 0, khi bị DDOS thì sửa giá trị này thành một con số khác 0, ví dụ 20000, để Apache hủy Child Process này sau khi phục vụ xong số request trên và dựng lại một Child Process khác.
MaxRequestsPerChild 0
d
# keep 5 servers (Child Process) ready to handle requests
MinSpareServers 5
# do not keep more than 10 servers idle
MaxSpareServers 10
# Start with 5 servers
StartServers 5

Tìm hiểu về mod_security và chroot

ModSecurity: https://www.modsecurity.org/

ChrootWhat is a Chroot Environment? A chroot environment is an operating system call that will change the root location temporarily to a new folder. Typically, the operating system’s conception of the root directory is the actual root located at “/”. However, with chroot, you can specify another directory to serve as the top-level directory for the duration of a chroot. Any applications that are run from within the chroot will be unable to see the rest of the operating system in principle. Similarly, a non-root user who is confined to a chroot environment will not be able to move further up the directory hierarchy.

Đọc thêm tại: https://www.digitalocean.com/community/tutorials/how-to-configure-chroot-environments-for-testing-on-an-ubuntu-12-04-vps

Dùng chroot chỉ gây khó khăn thêm cho những kẻ có ý định tấn công Hệ thống của bạn, Bạn không nên xem chroot là biện pháp bảo mật duy nhất. Có thể dùng Docker để thay thế chroot, Docker hỗ trợ nhiều chức năng hơn.

Nếu sử dụng chroot, cần có thêm vài bước cấu hình để các dịch vụ sử dụng chroot như Apache, PHP, MySQL có thể nhìn thấy nhau và liên lạc với nhau.

==Cần quan tâm đến Số lượng kết nối tối đa và thời gian cho mỗi kết nối của MySQL (hay Postgresql) để có cách tối ưu phù hợp với tình hình, nếu User đông và xảy ra tình trạng người này kết nối được, người kia không thì ta nên tăng số lượng kết tối đa và giảm thời lượng cho mỗi kết nối xuống một giá trị phù hợp==

Know your Enemy

Ứng dụng Web có thể phải đối mặt với các kiểu tấn công khác nhau. Trong đó, phổ biến là:

  • XSS – Cross-site scripting
  • SQL Injection
  • Spoofing File Upload
  • File Inclusion Vulnerability
  • Eval() Vulnerability & Exploitation
  • Sea-surf Attack (Cross-site yêu cầu giả mạo – CSRF)

ĐỂ LẠI BÌNH LUẬN