#####################################
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.
#####################################
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)
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
– Download core rule cho mod_sec : https://github.com/SpiderLabs/owasp-modsecurity-crs
– Giải nén và đặt tên crs
- $ 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.
– 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
- $ pwd
- /usr/local/apache/conf/crs
- $ ls
- CHANGELOG app_sensor modsecurity_crs_10_config.conf slr_rules
- LICENSE base_rules modsecurity_crs_10_config.conf.example util
- README experimental_rules modsecurity_crs_15_customrules.conf
- activated_rules lua optional_rules
- $ ln -s /usr/local/apache/conf/crs/modsecurity_crs_10_config.conf activated_rules/modsecurity_crs_10_config.conf
- $ for f in `ls base_rules/` ; do ln -s /usr/local/apache/conf/crs/base_rules/$f activated_rules/$f ; done
- $ for f in `ls optional_rules/ | grep comment_spam` ; do ln -s /usr/local/apache/conf/crs/optional_rules/$f activated_rules/$f ; done
- $ ls -l activated_rules
- total 216
- lrwxr-xr-x 1 root wheel 52 May 17 14:01 GsbMalware.dat -> /usr/local/apache/conf/crs/base_rules/GsbMalware.dat
- lrwxr-xr-x 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
- lrwxr-xr-x 1 root wheel 66 May 17 14:01 modsecurity_35_scanners.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_35_scanners.data
- lrwxr-xr-x 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
- lrwxr-xr-x 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
- lrwxr-xr-x 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
- lrwxr-xr-x 1 root wheel 66 May 17 14:01 modsecurity_50_outbound.data -> /usr/local/apache/conf/crs/base_rules/modsecurity_50_outbound.data
- lrwxr-xr-x 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
- lrwxr-xr-x 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
- lrwxr-xr-x 1 root wheel 57 May 17 14:22 modsecurity_crs_10_config.conf -> /usr/local/apache/conf/crs/modsecurity_crs_10_config.conf
- lrwxr-xr-x 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
- lrwxr-xr-x 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
- lrwxr-xr-x 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
- lrwxr-xr-x 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
- .....
B4. Thêm vào vào modsec2.conf (hoặc modsecurity.conf)
- Include conf/crs/modsecurity_crs_10_config.conf
- Include conf/crs/activated_rules/*.conf
B5. Tạo thư mục modsec quản lý thông tin cảnh báo:
- $ mkdir /etc/httpd/modsec
– Các files:
- -rwxrwxrwx 1 root root 510 Dec 3 09:14 alert.all
- -rwxrwxrwx 1 root root 0 Dec 3 09:41 alert.log
- -rwxr-xr-x 1 root root 373 Dec 3 08:41 alert.sh
- -rw-r--r-- 1 root root 1274 Dec 3 08:42 sendmail.php
- -rw-r--r-- 1 root root 142 Dec 3 08:44 sendmail.sh
– Trong đó:
- [root@server2 modsec]#cat alert.sh
- #!/bin/sh
- # FILE: alert.sh
- # modsec sẽ trực tiếp gọi file này.
- # File này sẽ làm đổ log ra alert.log
- # Các biến trong này được khởi tạo từ 1 rule trong modsec
- # VD:
- #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"
- NOW="$(date +"%D %H:%M")"
- echo "Phat hien 1 cuoc tan cong vao $HOSTNAME<br/>
- Attacking IP: $REMOTEIP
- Request URI: $REQUESTURI
- Arguments: $ARGS
- Headers: $HEADER
- Time: $NOW
- " >> /etc/httpd/modsec/alert.log
- #cat /etc/httpd/modsec/alert.log | mail –s 'ModSecurity Alert' <a href="mailto:thongngo@uns.vn">thongngo@uns.vn</a>
- echo Done.
- [root@server2 modsec]#cat sendmail.sh
- #!/bin/bash
- # File sẽ được chạy theo cron mỗi phút, chịu trách nhiệm:
- # 1. Gọi sendmail.php
- # 2. copy alert.log (lưu tạm) sang alert.all (lưu mãi)
- # 3. làm rỗng alert.log
- # Tạo cron: * * * * * sh /etc/httpd/modsec/sendmail.sh > /dev/null
- php /etc/httpd/modsec/sendmail.php
- cat /etc/httpd/modsec/alert.log >> /etc/httpd/modsec/alert.all
- :> /etc/httpd/modsec/alert.log
- [root@server1 modsec]#cat sendmail.php
- <?php
- ###########################################
- # SENDMAIL TOOL
- # Copyright UNS Inc
- # Made by Thong Ngo
- # FileName : sendmail.php
- # Update 10/06/2012
- ###########################################
- # File này sẽ check xem, nếu log rỗng thì không alert mail.
- $headers = "MIME-Version: 1.0\r\n";
- $headers .= "Content-type: text/html; charset=utf-8\r\n";
- $headers .= "From: UNS System";
- $headers .= "<noreply@uns.vn>";
- $email = "thongngo@uns.vn";
- $result = "";
- $file = fopen("/etc/httpd/modsec/alert.log","r");
- while(!feof($file))
- {
- $result .= fgets($file,1024)."<br/>";
- }
- $tmp = str_replace("<br/>","",$result);
- if ($tmp == "") { echo "Log empty !\r\n"; exit;}
- mail($email, "UNS - Modsecurity Alert",$result,$headers);
- ?>
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”:
Ví dụ: bổ sung cảnh báo “có khai thác file inclusion”:
- 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…. 
Cảm ơn đã đọc đến đây.
Thong Ngo.

0 comments: