Monday, March 31, 2014

Linux Bảo mật Nginx Web Server tốt nhất


Làm thế nào để tăng bảo mật cho nginx web server chạy hệ điều hành Linux hay UNIX
Mặc định Config Files và nginx port
  • /usr/local/nginx/conf/ : Thư mục cấu hình nginx server và /usr/local/nginx/conf/nginx.conf là file cấu hình chính.
  • /usr/local/nginx/html/ : mặc định document location
  • /usr/local/nginx/logs/ : mặc định file logs.
Nginx mặc định port HTTP : TCP 80
Nginx mặc định port HTTPS: : TCP 443

Test file cấu hình nginx khi mỗi lần thay đổi
#/usr/local/nginx/sbin/nginx -t
Mỗi lần thay đổi cấu hình ta load dòng lệnh dưới đây
#/usr/local/nginx/sbin/nginx -s reload

1.Turn on SELINUX
Chạy dòng lênh getsebool -a để xem hệ thống lockdown

#getsebool -a | less
#getsebool -a | grep off
#getsebool -a | grep o

ví dụ : dòng lệnh getsebool -a | grep off

[root@localhost ~]# getsebool -a | grep off
abrt_anon_write --> off
allow_cvs_read_shadow --> off
allow_daemons_use_tcp_wrapper --> off
Để bảo mật, ta thiết lập "on" và thay đổi "off" nếu không muốn áp dụng bằng dòng lệnh setsebool.

2.Cho phép tùy chọn Mount với quyền tối thiểu
Ví dụ: tạo một phân vùng ( partition ) /dev/sda4 và mount /nginx. chắc chắn /nginx mount với quyền ( permissions) noexec,nodev và nosetuid .file /etc/fstab cho mount /nginx như sau.

LABEL=/nginx /nginx ext3 defaults,nosuid,noexec,nodev 1 2
Note: Tạo phân vùng mới sử dụng dòng lệnh fdisk and mkfs.ext3

3.Cấu hình /etc/sysctl.conf
Thiết lập Linux kernel và networking qua /etc/sysctl.conf
# Avoid a smurf attack
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Turn on protection for bad icmp error messages
net.ipv4.icmp_ignore_bogus_error_responses = 1
# Turn on syncookies for SYN flood attack protection
net.ipv4.tcp_syncookies = 1
# Turn on and log spoofed, source routed, and redirect packets
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
# No source routed packets here
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
# Turn on reverse path filtering
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Make sure no one can alter the routing tables
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
# Don't act as a router
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
# Turn on execshild
kernel.exec-shield = 1
kernel.randomize_va_space = 1
# Tuen IPv6
net.ipv6.conf.default.router_solicitations = 0
net.ipv6.conf.default.accept_ra_rtr_pref = 0
net.ipv6.conf.default.accept_ra_pinfo = 0
net.ipv6.conf.default.accept_ra_defrtr = 0
net.ipv6.conf.default.autoconf = 0
net.ipv6.conf.default.dad_transmits = 0
net.ipv6.conf.default.max_addresses = 1
# Optimization for port usefor LBs
# Increase system file descriptor limit
fs.file-max = 65535
# Allow for more PIDs (to reduce rollover problems); may break some programs 32768
kernel.pid_max = 65536
# Increase system IP port limits
net.ipv4.ip_local_port_range = 2000 65000
# Increase TCP max buffer size setable using setsockopt()
net.ipv4.tcp_rmem = 4096 87380 8388608
net.ipv4.tcp_wmem = 4096 87380 8388608
# Increase Linux auto tuning TCP buffer limits
# min, default, and max number of bytes to use
# set max to at least 4MB, or higher if you use very high BDP paths
# Tcp Windows etc
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_window_scaling = 1

4.Hủy bỏ tất cả các Nginx modules không mong muốn

Gõ dòng lệnh dưới đây để xem modules bật hay tắt trong khi compiling nginx server
# ./configure --help | less
Ví dụ: disable SSI và autoindex modules
# ./configure --without-http_autoindex_module --without-http_ssi_module
# make
# make install

......continuous ..........

No comments:

Post a Comment