Wednesday, 4 September 2013

Hướng dẫn cài đặt và cấu hình CSF chống DoS, chống Hack (ConfigServer & Firewall) - P2 Cấu hình

Hướng dẫn cài đặt và cấu hình CSF chống DoS, chống Hack (ConfigServer & Firewall) - P2 Cấu hình
- File cấu hình nằm ở /etc/csf/csf.conf , các file còn lại được nhắc đến bên dưới nếu không nêu rõ đường dẫn thì đều nằm ở thư mục /etc/csf/
- Các tham số khi cấu hình có dạng ARGS = "VALUE" , trong đó
+ VALUE = "0" => Disable
+ VALUE = "1" => Enable
+ VALUE > 1 (VALUE = "20" , VALUE = "30" ... ) : giới hạn tối đa.
+ VALUE >1 (VALUE = "1800" , VALUE = "3600" ... ) : thời gian tối đa.

Bắt đầu:

Mã:

TESTING = "0"
Mặc định khi vừa cài TESTING = "1", với TESTING = "1" thì LFD daemon (Login Fail Detect daemon) sẽ không hoạt động, do đó nếu có gì sai sót thì server cũng sẽ không block IP của bạn. Khi cảm thấy cấu hình đã ổn thì tắt TESTING để LFD bắt đầu hoạt động và chặn các IP tấn công.

Mã:

TESTING_INTERVAL = "5"
Thời gian chạy cronjob để clear iptables nếu như TESTING=1 , tính bằng phút.

Mã:

AUTO_UPDATES = "0"
Disable auto update

Mã:

TCP_IN = "22,25,53,80,443"
Allow incoming TCP ports: cho ngừoi dùng kết nối đến các dịch vụ SSH, sendmail, DNS, Web trên server.

Mã:

TCP_OUT = "25,80"
Allow outgoing TCP port: cho phép server kết nối đến web server, sendmail server khác.

Mã:

UDP_IN = "53"
Allow incoming UDP ports: cho phép người dùng sử dụng dịch vụ DNS trên server.

Mã:

UDP_OUT = "53"
Allow outgoing UDP ports: cho phép server truy vấn DNS bên ngoài.

Mã:

ICMP_IN = "1"
Cho phép ping đến server.

Mã:

ICMP_IN_RATE = "1/s"
Giới hạn tần số ping đến server là 1/s. Nếu ping nhanh hơn tốc độ này sẽ nhận được "Request timeout" . Trong trường hợp nếu nhiều người cùng ping đến server cùng lúc , thì phần lớn sẽ nhận được các phản hồi "Request timeout" do server chỉ nhận 1 request/s , điều này làm chúng ta lầm tưởng kết nối mạng có vấn đề , mạng bị chập chờn nhưng thật ra không phải như vậy . Chỉ cần nâng thông số này lên cao một chút hoặc bỏ luôn ( set giá trị = 0 ) sẽ khắc phục được tình trạng trên.

Mã:

ETH_DEVICE = "eth0"
Mặc định csf sẽ cấu hình iptables để filter traffic trên toàn bộ các card mạng , ngoại trừ card loopback . Nếu như bạn muốn rules iptables chỉ applied vào card mạng "eth0" thì khai báo ở đây.

Mã:

ETH_DEVICE_SKIP = "eth1"
Nếu bạn không muốn rules iptables không applied vào card mạng nào thì khai báo ở đây. Ví dụ card "eth1" là card local , bạn không muốn filter trên card này thì cấu hình như trên.

Mã:

DENY_IP_LIMIT = "500
"
Giới hạn số lượng IP bị block "vĩnh viễn" bởi CSF (các IP này được lưu trong file /etc/csf/csf.deny). Con số này tùy thuộc vào resource của mỗi server, nếu dùng VPS thì con số này vào khoảng "200" là hợp lý , còn dedicated server thì khoảng "500". Khi số lượng IP bị block vượt qua con số này , csf sẽ tự động unblock IP cũ nhất (IP ở dòng 1 của file /etc/csf/csf.deny)

Mã:

LF_DAEMON = "1"
Enable tính năng Login fail detection.

Mã:

LF_CSF = "1"
Tự động restart CSF khi csf bị stop.

Mã:

PACKET_FILTER = "1"
Filter các gói tin TCP không hợp lệ (INVALID state như : sequence number không đúng , kết nối ko được thực hiện đủ qua 3 bước bắt tay ... )

Mã:

IPV6 = "0"
Disable IPV6 support

Mã:

SYNFLOOD = "1"SYNFLOOD_RATE = "30/s"SYNFLOOD_BURST = "40"
Enable synflood protection : Nếu 1 IP gửi 30 cú SYN trong vòng 1s và số lượng SYN connection tồn tại trên server đạt trên 40 thì block IP đó (temp block)

Mã:

CONNLIMIT = "80;20"
Giới hạn số lượng new concurrent connection đến server trên mỗi IP. Ví dụ trên có nghĩa : mỗi IP được phép mở 20 concurrent new connection đến port 80 trên server.

Mã:

PORTFLOOD = "80;tcp;20;5"
Giới hạn số lượng connection đến một port cụ thể trong một khoảng thời gian nhất định. Ví dụ như trên có nghĩa : nếu nhiều hơn 20 kết nối tcp đến port 80 trong vòng 5s thì block IP đó tối thiểu 5s tính từ packet cuối cùng của IP đó. Sau 5s IP đó sẽ tự động được unlock và truy cập bình thường.

Mã:

DROP_NOLOG = "10050,10051"
Danh sách các port khi bị drop sẽ không cần phải ghi vào log

Mã:

CONNLIMIT_LOGGING = "1"
Ghi log các IP vượt quá giới hạn CONNLIMIT cấu hình ở bước trên.

Mã:

LF_ALERT_TO = "your_email@your_domain.com"
Mặc định toàn bộ email thông báo sẽ được gửi về root của server . Nếu bạn muốn gửi đến địa chỉ email khác thì khai báo ở đây.

Mã:

LF_PERMBLOCK = "1"LF_PERMBLOCK_INTERVAL = "86400"LF_PERMBLOCK_COUNT = "6"LF_PERMBLOCK_ALERT = "1"
Enable tính năng block vĩnh viễn một IP . Nếu một IP bị temp ban (ban tạm) 6 lần khi vi phạm các rule sẽ block ip này 86400s ( 1 ngày) đồng thời gửi email về cho ngừoi quản trị biết.

Mã:

LF_TRIGGER = "1"
Enable tính năng Login Fail Detect cho từng dịch vụ cụ thể (được khai báo bên dưới)

Mã:

LF_TRIGGER_PERM = "1"
Khi LF_TRIGGER = "1" thì có thể enable LF_TRIGGER_PERM để kích hoạt block IP permanent
+ LF_TRIGGER_PERM = "1" => IP sẽ bị block permanent
+ LF_TRIGGER_PERM = "86400" => IP sẽ bị block 1 ngày

Mã:

LF_SELECT = "1"
Khi một IP vi phạm các rule của LFD thay vì block toàn bộ traffic từ IP này đến server thì chỉ block traffic đến dịch vụ mà IP này login fail (ví dụ login ftp sai nhiều lần thì block truy cập đến FTP nhưng vẫn cho phép truy cập vào website)

Mã:

LF_EMAIL_ALERT = "1"
Gửi email thông báo nếu một IP bị block bởi các trigger bên dưới

Mã:

LF_SSHD = "5"LF_SSHD_PERM = "1"
Nếu login SSH sai 5 lần thì sẽ bị block IP (temp block)
Nếu bị temp block lớn hơn số lần quy định ở LF_PERMBLOCK_COUNT (cấu hình bước trên ) thì sẽ block permanent.

Mã:

LF_FTPD = "0"LF_FTPD_PERM = "1"
Không kích hoạt login fail detect cho dịch vụ FTP.

Tương tự cho các dịch vụ còn lại bên dưới ( SMTP , POP3 , IMAP , .htpasswd , mod_security ... )

LF_SSH_EMAIL_ALERT = "0"
Không gửi email thông báo khi có một ai đó login thành công thông qua SSH

LF_SU_EMAIL_ALERT = "0"
Không gửi email thông báo khi có một người dùng "su" (switch user) qua người dùng khác. Không gửi email khi họ dùng lệnh "su" , bất kể "su" thành công hoặc thất bại.

Mã:

LF_DIRWATCH = "3600"
LFD sẽ check thư mục /tmp và /dev/shm định kỳ sau mỗi 3600s , nếu phát hiện ra các file nghi vấn là file độc hại sẽ gửi email thông báo đến cho chúng ta. Thường thì trên server thư mục, /temp và /dev/shm phân quyền cho phép mọi người dùng có quyền ghi trên thư mục này , do đó các attacker lợi dụng điều này để ghi mã độc vào đây (các file để back connect , local root exploit ... )

Mã:

LF_DIRWATCH_DISABLE = "1"
Khi phát hiện ra các file nghi vấn ở thư mục /tmp và /dev/shm sẽ mv chúng khỏi 2 thư mục trên và append vào file /etc/csf/suspicious.tar , thuận tiện cho chúng ta theo dõi , phân tích về sau và phần nào vô hiệu hóa cuộc tấn công của attacker.

Mã:

LF_DIRWATCH_FILE = "60"
Theo dõi sự thay đổi của các file và thư mục , nếu có thay đổi gửi email thông báo về cho chúng ta. Để theo dõi file/thư mục nào thì add chúng vào file csf.dirwatch. Cấu hình như trên thì 60s chạy 1 lần.

Mã:

LF_INTEGRITY = "0"
Kiểm tra tính toàn vẹn của hệ điều hành bằng cách so sánh MD5 của các file binary khi LFD start với MD5 của các file đó lúc kiểm tra. Nếu khác nhau thì sẽ gửi email thông báo. Tính năng này có thể sẽ hoạt động không chính xác khi hệ thống update và sẽ tăng I/O , load của server do phải tính toán MD5 rất nhiều lần. 

Mã:

LF_DISTATTACK = "0"
Phát hiện tấn công brute force từ mạng botnet. Nếu như một account bị login sai quá giới hạn cho phép từ nhiều IP khác nhau thì sẽ block toàn bộ IP đã login sai.

Mã:

LF_DISTATTACK_UNIQ = "2"
Số lượng IP tối thiểu để nhận biết đây là tấn công phân tán.

Mã:

LT_POP3D = "30"
Block login POP3 nếu một account được login nhiều hơn 30 lần trong 1 giờ từ 1 IP. Tương tự cho LT_IMAPD.

Mã:

LT_EMAIL_ALERT = "0"
Send email khi một account vượt quá giới hạn cho phép của LT_IMAPD và LT_POP3D

Mã:

LT_SKIPPERMBLOCK = "0"
Không áp dụng permanent block cho LT_POP3D/LT_IMAPD

Mã:

CT_LIMIT = "300"
Giới hạn số lượng connection từ một IP đến server . Nếu số lượng đó vượt quá 300 thì temp block IP đó.

Mã:

CT_INTERVAL = "30"
Các lần scan để kiểm tra cách nhau 30s.

Mã:

CT_EMAIL_ALERT = "1"
Gửi email thông báo nếu một IP bị block bởi connection tracking.

Mã:

CT_PERMANENT = "0"
Disable block permanent cho connectiong tracking.

Mã:

CT_BLOCK_TIME = "1800"
Thời gian block một IP nếu như vi phạm Connection tracking limit.

Mã:

CT_SKIP_TIME_WAIT = "0"
Khi đếm số lượng connection từ 1 IP đến server thì bỏ qua trạng thái TIME_WAIT của connection , không đếm trạng thái này.

Mã:

CT_STATES = "SYN_RECV"
Chỉ đếm các kết nối ở trạng thái SYN_RECV

Mã:

CT_PORTS = "80,443"
Chỉ áp dụng connection tracking cho các kết nối đến port 80 và 443.

Mã:

PS_INTERVAL = "300"PS_LIMIT = "15"
Trong 500s nếu kết nối đến nhiều hơn 15 port không có trên server sẽ block IP đó.

Mã:

PS_PORTS = "0:65535,ICMP"
Giới hạn range port sẽ được theo dõi.

Mã:

PS_PERMANENT = "0"
IP bị block bởi Port Scan Tracking sẽ là temp block hoặc là permanent : 
PS_PERMANENT = "0" : IP bị temp block
PS_PERMANENT = "1" : IP bị block permanent.

Mã:

PS_BLOCK_TIME = "3600"
Nếu PS_PERMANENT = "0" thì đây là thời gian temp block của một IP.

Mã:

PS_EMAIL_ALERT = "1"
Gửi email thông báo khi có một IP bị block.
Mặc định thì CSF gửi toàn bộ email về địa chỉ đã cấu hình ở phần "LF_ALERT_TO" , nếu trường hợp đặc biệt bạn muốn vài thông báo nào đó gửi về email khác thì có thể chạy một trong các lệnh sau: (nhớ chọn đúng phần alert muốn gửi để chạy lệnh)

perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/connectiontracking.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/exploitalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/filealert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/loadalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/logfloodalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/netblock.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/permblock.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/portknocking.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/portscan.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/resalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/scriptalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/tracking.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/usertracking.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/watchalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/x-arf.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/alert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/sshalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/processtracking.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/sualert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/integrityalert.txt
perl -i -p -e 's/To: root/To: your_email\@gmail.com/g;' /etc/csf/accounttracking.txt

Với :

/etc/csf/alert.txt - for port blocking emails
/etc/csf/tracking.txt - for POP3/IMAP blocking emails
/etc/csf/connectiontracking.txt - for connection tracking emails
/etc/csf/processtracking.txt - for process tracking alert emails
/etc/csf/usertracking.txt - for user process tracking alert emails
/etc/csf/sshalert.txt - for SSH login emails
/etc/csf/sualert.txt - for SU alert emails
/etc/csf/uialert.txt - for UI alert emails
/etc/csf/scriptalert.txt - for script alert emails
/etc/csf/filealert.txt - for suspicious file alert emails
/etc/csf/watchalert.txt - for watched file and directory change alert emails
/etc/csf/loadalert.txt - for high load average alert emails
/etc/csf/resalert.txt - for process resource alert emails
/etc/csf/exploitalert.txt - for system exploit alert emails
/etc/csf/integrityalert.txt - for system integrity alert emails
/etc/csf/relayalert.txt - for email relay alert emails
/etc/csf/portscan.txt - for port scan tracking alert emails
/etc/csf/permblock.txt - for temporary to permanent block alert emails
/etc/csf/netblock.txt - for netblock alert emails
/etc/csf/accounttracking.txt - for account tracking alert emails
/etc/csf/queuealert.txt - for email queue alert emails
/etc/csf/logfloodalert.txt - for log file flooding alert emails
/etc/csf/cpanelalert.txt - for WHM/cPanel account access emails
/etc/csf/portknocking.txt - for Port Knocking alert emails

Tuesday, 3 September 2013

APF and DOS-Deflate


What is APF (Advanced Policy Firewall)?
Advanced Policy Firewall (APF) is an iptables(netfilter) based firewall system designed around the essential needs of today’s Linux servers. The configuration is designed to be very informative and easy to follow. The management on a day-to-day basis is conducted from the command line with the ‘apf’ command, which includes detailed usage information on all the features.
Requirements:
- Root SSH access to your server
Install
Login to your server through SSH and su to the root user.
 
  1. cd /root/downloads or another temporary folder where you store your files.
     
  2. wget http://www.rfxnetworks.com/downloads/apf-current.tar.gz
     
  3. tar -xvzf apf-current.tar.gz
     
  4. cd apf-0.9.5-1/ or whatever the latest version is.
     
  5. Run the install file: ./install.sh
    You will receive a message saying it has been installed
    .: APF installed
    Install path: /etc/apf
    Config path: /etc/apf/conf.apf
    Executable path: /usr/local/sbin/apf
     
  6. Configure the firewall: (i mostly use nano editor) nano /etc/apf/conf.apf
    General configuration to get your firewall running. This isn't a complete detailed guide of every feature the firewall has. Look through the README and the configuration for an explanation of each feature.
    You may like to use DShield.org's "block" list of top networks that have exhibited suspicious activity.
    FIND: USE_DS="0"
    CHANGE TO: USE_DS="1"
     
  7. Configuring Firewall Ports:
    Cpanel Servers
    recommended settings for Cpanel Servers
     
    Common ingress (inbound) ports
    # Common ingress (inbound) TCP ports -3000_3500 = passive port range for Pure FTPD
    IG_TCP_CPORTS="21,22,25,53,80,110,143,443,2082,2083, 2086,2087, 2095, 2096,3000_3500"
    #
    # Common ingress (inbound) UDP ports
    IG_UDP_CPORTS="53"
    Common egress (outbound) ports
    # Common egress (outbound) TCP ports
    EG_TCP_CPORTS="21,25,80,443,43,2089"
    #
    # Common egress (outbound) UDP ports
    EG_UDP_CPORTS="20,21,53"
    Ensim Servers
    recommended settings for Ensim Servers
    Common ingress (inbound) ports
    # Common ingress (inbound) TCP ports
    IG_TCP_CPORTS="21,22,25,53,80,110,143,443,19638"
    #
    # Common ingress (inbound) UDP ports
    IG_UDP_CPORTS="53"
    Common egress (outbound) ports
    # Common egress (outbound) TCP ports
    EG_TCP_CPORTS="21,25,80,443,43"
    #
    # Common egress (outbound) UDP ports
    EG_UDP_CPORTS="20,21,53"
    Plesk Servers
    recommended settings for Plesk Servers
    # Common ingress (inbound) TCP ports
    IG_TCP_CPORTS="20,21,22,25,53,80,110,143,443,465,993,995,3306,4643,5224,8443,8447,10000,
    35000_35999"
    # Common ingress (inbound) UDP ports
    IG_UDP_CPORTS="20,21,53,1040"
    # Common ICMP (inbound) types
    # 'internals/icmp.types' for type definition; 'all' is wildcard for any
    IG_ICMP_TYPES="3,5,11,0,30,8"
    # Egress filtering [0 = Disabled / 1 = Enabled]
    EGF="0"
    # Common egress (outbound) TCP ports
    EG_TCP_CPORTS="20,21,22,25,53,80,110,143,443,465,993,995,3306,5224,8443,10000,35000_35999"
    # Common egress (outbound) UDP ports
    EG_UDP_CPORTS="20,21,37,53,123,1040"
    # Common ICMP egress (outbound) types
    # 'internals/icmp.types' for type definition; 'all' is wildcard for any
    EG_ICMP_TYPES="all"
    Save your changes! Ctrl + X then y (nano editor)
     
  8. Starting the firewall
    apf -s
    Other commands:
    usage /usr/local/sbin/apf [OPTION]
    -s|--start ............. load firewall policies
    -r|--restart ........... flush & load firewall
    -f|--flush|--stop ...... flush firewall
    -l|--list .............. list chain rules
    -st|--status ........... firewall status
    -a HOST|--allow HOST ... add host (IP/FQDN) to allow_hosts.rules and
    immediately load new rule into firewall
    -d HOST|--deny HOST .... add host (IP/FQDN) to deny_hosts.rules and
    immediately load new rule into firewall
     
  9. After everything is fine, change the DEV option
    nano /etc/apf/conf.apf
    # Set firewall cronjob (devel mode)
    # 1 = enabled / 0 = disabled
    DEVEL_MODE="0"
    It is recommended changing this back to "0" after you've had a chance to ensure everything is working well and tested the server out.
    Save your changes! Ctrl + X then y
    Restart the firewall: apf -r
     
  10.  Make APF Start automatically at boot time
    To autostart apf on reboot, run this:
    chkconfig --level 2345 apf on
    To remove it from autostart, run this:
    chkconfig --del apf

What is DOS-Deflate?
MediaLayer was in need of a script to automatically mitigate (D)DoS attacks. The necessity started when MediaLayer was the target of a rather large, consistent attack originating from multiple IP addresses. Each IP would have a large amount of connections to the server, as shown as by:
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
It became a general practice for us to be blocking IPs with a large amount of connections, but we wanted to get this automated. Zaf created a script mitigate this kind of attack. We kept improving it to meet our own needs and eventually posted it on Defender Hosting's Forum. (D)DoS-Deflate is now recognized as one of the best ways to block a (D)DoS attack at the software level.
Install
  1. Login to your server as root
     
  2. Download the install script
  3. Run the installer
    ssh install.sh
     
DOS-Deflate should now be installed.
Please note that DOS-Deflate uses APF to ban IPs so you must have it installed for DOS-Deflate to work properly.
Customizing DOS-Deflate is very easy. You have to edit /usr/local/ddos/ddos.conf with your favorite editor
e.g. nano /usr/local/ddos/ddos.conf
Every setting is explained in the configuration file.
e.g.
##### frequency in minutes for running the script
##### Caution: Every time this setting is changed, run the script with --cron
#####          option so that the new frequency takes effect
FREQ=1
##### How many connections define a bad IP? Indicate that below.
NO_OF_CONNECTIONS=150
##### APF_BAN=1 (Make sure your APF version is atleast 0.96)
##### APF_BAN=0 (Uses iptables for banning ips instead of APF)
APF_BAN=1
##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
##### KILL=1 (Recommended setting)
KILL=1
##### An email is sent to the following address when an IP is banned.
##### Blank would suppress sending of mails
EMAIL_TO="root"
##### Number of seconds the banned ip should remain in blacklist.
BAN_PERIOD=600
(You can experiment with the above settings. e.g. At a time period of frequent DOS attacks you can change no_of_connections to 50 and/or increase the ban_period)
Save your changes! Ctrl + X then y