ModSecurity kết hợp cảnh báo qua email


#####################################
Mục tiêu: thiết lập mod_security và kết hợp cảnh báo qua email. Bị khai thác, tấn công -> biết ngay.
Đối tượng: 1 VPS chạy LAMP nhiều host.
OS: CentOS 5-6
Công cụ: ModSecurity, Owasp Core Rule (hoặc manual rules), hết.
#####################################
ModSecurity được biết khá nhiều rồi, nhưng việc thiết lập để nó cảnh báo qua email thì ít Sysadmin quan tâm (có lẽ là nó hơi phiền phức), tuy nhiên với các server có số website ít ỏi, nội dung đơn giản, việc áp dụng cơ chế này tương đối hiệu quả.
Một số website dạng forum, blog nghiên cứu,… có các nội dung dễ vi phạm Rule, cần được test kỹ trước khi áp dụng các Rule của bộ Owasp-modsecurity core rule này vào, tránh trường hợp dùng Rule vô tội vạ, kết quả bị gửi cả chục ngàn email mỗi ngày.
B1. Cài mod_security bản mới nhất cho apache và bật SecRuleEngine On (bật mod_security)
B2. Trong thư mục: /etc/httpd/conf
– Download core rule cho mod_sec : https://github.com/SpiderLabs/owasp-modsecurity-crs
– Giải nén và đặt tên crs
  1. $ mv owasp-modsecurity* crs
– Kiểm tra đã include modsec2.user.conf trong modsec2.conf chưa? (hoặcmodsecurity.conf)
– Lưu ý: chỉ add rule thủ công thông qua file modsec2.user.conf.
B3. Tất cả rules hoạt động sẽ được nằm trong ./crs/activated_rules/, điều khiển qua./crs/modsecurity_crs_10_config.conf
  1. $ pwd
  2. /usr/local/apache/conf/crs
  3. $ ls
  4. CHANGELOG                               app_sensor                              modsecurity_crs_10_config.conf          slr_rules
  5. LICENSE                                 base_rules                              modsecurity_crs_10_config.conf.example  util
  6. README                                  experimental_rules                      modsecurity_crs_15_customrules.conf
  7. activated_rules                         lua                                     optional_rules
  8. $ ln -s /usr/local/apache/conf/crs/modsecurity_crs_10_config.conf activated_rules/modsecurity_crs_10_config.conf
  9. $ for f in `ls base_rules/` ; do ln -s /usr/local/apache/conf/crs/base_rules/$f activated_rules/$f ; done
  10. $ for f in `ls optional_rules/ | grep comment_spam` ; do ln -s /usr/local/apache/conf/crs/optional_rules/$f activated_rules/$f ; done
  11. $ ls -l activated_rules
  12. total 216
  13. lrwxr-xr-1 root  wheel  52 May 17 14:01 GsbMalware.dat -> /usr/local/apache/conf/crs/base_rules/GsbMalware.dat
  14. lrwxr-xr-1 root  wheel  68 May 17 14:01 modsecurity_35_bad_robots.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_35_bad_robots.data
  15. lrwxr-xr-1 root  wheel  66 May 17 14:01 modsecurity_35_scanners.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_35_scanners.data
  16. lrwxr-xr-1 root  wheel  73 May 17 14:01 modsecurity_40_generic_attacks.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_40_generic_attacks.data
  17. lrwxr-xr-1 root  wheel  79 May 17 14:01 modsecurity_41_sql_injection_attacks.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_41_sql_injection_attacks.data
  18. lrwxr-xr-1 root  wheel  74 May 17 14:14 modsecurity_42_comment_spam.data -> /usr/local/apache/conf/crs/optional_rules/modsecurity_42_comment_spam.data
  19. lrwxr-xr-1 root  wheel  66 May 17 14:01 modsecurity_50_outbound.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_50_outbound.data
  20. lrwxr-xr-1 root  wheel  74 May 17 14:01 modsecurity_50_outbound_malware.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_50_outbound_malware.data
  21. lrwxr-xr-1 root  wheel  73 May 17 14:01 modsecurity_crs_14_customrules.conf -> /usr/local/apache/conf/crs/base_rules/modsecurity_crs_14_customrules.conf
  22. lrwxr-xr-1 root  wheel  57 May 17 14:22 modsecurity_crs_10_config.conf -> /usr/local/apache/conf/crs/modsecurity_crs_10_config.conf
  23. lrwxr-xr-1 root  wheel  81 May 17 14:01 modsecurity_crs_20_protocol_violations.conf -> /usr/local/apache/conf/crs/base_rules/modsecurity_crs_20_protocol_violations.conf
  24. lrwxr-xr-1 root  wheel  80 May 17 14:01 modsecurity_crs_21_protocol_anomalies.conf -> /usr/local/apache/conf/crs/base_rules/modsecurity_crs_21_protocol_anomalies.conf
  25. lrwxr-xr-1 root  wheel  76 May 17 14:01 modsecurity_crs_23_request_limits.conf -> /usr/local/apache/conf/crs/base_rules/modsecurity_crs_23_request_limits.conf
  26. lrwxr-xr-1 root  wheel  73 May 17 14:01 modsecurity_crs_30_http_policy.conf -> /usr/local/apache/conf/crs/base_rules/modsecurity_crs_30_http_policy.conf
  27. .....
B4. Thêm vào vào modsec2.conf (hoặc modsecurity.conf)
  1. Include conf/crs/modsecurity_crs_10_config.conf
  2. Include conf/crs/activated_rules/*.conf
B5. Tạo thư mục modsec quản lý thông tin cảnh báo:
  1. $ mkdir /etc/httpd/modsec
– Các files:
  1. -rwxrwxrwx 1 root root  510 Dec  3 09:14 alert.all
  2. -rwxrwxrwx 1 root root    0 Dec  3 09:41 alert.log
  3. -rwxr-xr-x 1 root root  373 Dec  3 08:41 alert.sh
  4. -rw-r--r-- 1 root root 1274 Dec  3 08:42 sendmail.php
  5. -rw-r--r-- 1 root root  142 Dec  3 08:44 sendmail.sh
– Trong đó:
  1. [root@server2 modsec]#cat alert.sh
  2. #!/bin/sh
  3. # FILE: alert.sh
  4. # modsec sẽ trực tiếp gọi file này.
  5. # File này sẽ làm đổ log ra  alert.log
  6. # Các biến trong này được khởi tạo từ 1 rule trong modsec
  7. # VD:
  8. #SecRule ARGS "../../../" "deny,status:404,t:lowercase, setenv:HOSTNAME=%{SERVER_NAME},setenv:REMOTEIP=%{REMOTE_ADDR},setenv:REQUESTURI=%{REQUEST_URI},setenv:ARGS=%{ARGS}, setenv:UNIQUEID={%UNIQUE_ID},exec:/etc/httpd/modsec/alert.sh"
  9.  
  10. NOW="$(date +"%D %H:%M")"
  11. echo "Phat hien 1 cuoc tan cong vao $HOSTNAME<br/>
  12. Attacking IP: $REMOTEIP
  13. Request URI: $REQUESTURI
  14. Arguments: $ARGS
  15. Headers: $HEADER
  16. Time: $NOW
  17.  
  18. " >> /etc/httpd/modsec/alert.log
  19. #cat /etc/httpd/modsec/alert.log | mail –s 'ModSecurity Alert' <a href="mailto:thongngo@uns.vn">thongngo@uns.vn</a>
  20. echo Done.
  1. [root@server2 modsec]#cat sendmail.sh
  2. #!/bin/bash
  3. # File sẽ được chạy theo cron mỗi phút, chịu trách nhiệm:
  4. # 1. Gọi sendmail.php
  5. # 2. copy alert.log (lưu tạm) sang alert.all (lưu mãi)
  6. # 3. làm rỗng alert.log
  7. # Tạo cron: * * * * * sh /etc/httpd/modsec/sendmail.sh > /dev/null
  8. php /etc/httpd/modsec/sendmail.php
  9. cat /etc/httpd/modsec/alert.log &gt;&gt; /etc/httpd/modsec/alert.all
  10. :> /etc/httpd/modsec/alert.log
  1. [root@server1 modsec]#cat sendmail.php
  2. <?php
  3. ###########################################
  4. #  SENDMAIL TOOL
  5. #                     Copyright UNS Inc
  6. #                     Made by Thong Ngo
  7. #            FileName : sendmail.php
  8. #            Update 10/06/2012
  9. ###########################################
  10. # File này sẽ check xem, nếu log rỗng thì không alert mail.
  11. $headers  = "MIME-Version: 1.0\r\n";
  12. $headers .= "Content-type: text/html; charset=utf-8\r\n";
  13. $headers .= "From: UNS System";
  14. $headers .= "<noreply@uns.vn>";
  15. $email      = "thongngo@uns.vn";
  16. $result   = "";
  17. $file      = fopen("/etc/httpd/modsec/alert.log","r");
  18. while(!feof($file))
  19. {
  20. $result .= fgets($file,1024)."<br/>";
  21. }
  22. $tmp       = str_replace("<br/>","",$result);
  23. if ($tmp == "") { echo "Log empty !\r\n"; exit;}
  24. mail($email, "UNS - Modsecurity Alert",$result,$headers);
  25. ?&gt;
B6. Thiết lập cảnh bảo cho các Rule cần thiết trong các file Rule (mỗi file có nhiều Rule – lưu ý các file trong based_rules/):
Ví dụ: bổ sung cảnh báo “có khai thác file inclusion”:
  1. SecRule ARGS "../../../" "deny,status:404,t:lowercase, setenv:HOSTNAME=%{SERVER_NAME},setenv:REMOTEIP=%{REMOTE_ADDR},setenv:REQUESTURI=%{REQUEST_URI},setenv:ARGS=%{ARGS}, setenv:UNIQUEID={%UNIQUE_ID},exec:/etc/httpd/modsec/alert.sh"
Liên tục debug error_log và modsec_log để có kết quả tốt nhất.
B7. Restart httpd và kiểm tra kết quả.
Cơ chế này còn 1 số nhược điểm, đó là khi attacker cố tình flood các request khai thác (có|không tồn tại), thì hòm mail của thongngo@uns.vn sẽ lãnh đủ. Do đó, ta cần thiết lập thêm 1 phần là xác định IP, dạng tấn công, thời gian và gom nhóm để chỉ cảnh báo 1 hoặc vài lần nhất định cho IP với kiểu tấn công trong 1 khoảng thời gian.
Phần này xem như 1 kết thúc mở đầy hào hứng…. :D
Cảm ơn đã đọc đến đây.
Thong Ngo.

0 comments: