Security Bootcamp 2012 - Bảo vệ Web App với Mod Security (Sử Hoàng Sơn)
1. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
1
2
Kiện toàn bảo mật Web với ModSecurity
Sử Hoàng Sơn | Email: shson.stttt@vinhlong.gov.vn
2. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Thực trạng
2
- Hầu hết các ứng dụng do nhiều đơn vị phát
triển với nhiều công nghệ khác nhau.
- Sử dụng các sản 2phẩm của bên thứ 3 mà
không biết chất lượng.
- Cần rất nhiều thời gian và nhân lực để kiểm
tra độ an toàn.
- Thói quen, tư duy người lập trình chưa quan
tâm nhiều đến an toàn sản phẩm, chưa có bộ
phận phụ trách an toàn sản phẩm.
ModSecurity Web Intrusion Detection and Prevention 2
3. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Vấn đề cần làm
3
- Công tác an toàn sản phẩm cần làm sớm
càng tốt.
2
- Bằng tất cả mọi cách cải tiến độ an toàn các
ứng dụng đang có (không dễ).
- Nhiều khả năng chúng ta phải tăng cường
an ninh từ bên ngoài.
- Đặt các ứng dụng không an toàn vào môi
trường an toàn.
ModSecurity Web Intrusion Detection and Prevention 3
4. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Yêu cầu
với công tác quản trị
4
Monitoring: biết chuyện gì đang xãy ra.
2
Detection: phát hiện khi bị tấn công.
Prevention: ngăn chặn được tấn công.
Assessment: phát hiện trước vấn đề trước
khi bị tấn công
ModSecurity Web Intrusion Detection and Prevention 4
5. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
5
2
Một số thiết bị bảo mật thường gặp
ModSecurity Web Intrusion Detection and Prevention 5
6. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Packet Filter Firewall
6
2
ModSecurity Web Intrusion Detection and Prevention 6
7. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Packet Filter Firewall
7
- Tạo ACL cơ bản dựa trên: Source IP,
Destination IP, Port, Service, Protocol.
2
- Nội dung và ngữ cảnh (mối liên hệ với các
gói khác) bị bỏ qua.
- Thông tin địa chỉ trong một gói có thể bị
xuyên tạc hoặc bị đánh lừa bởi người gửi.
- Packet chuyển qua có thể chứa thông tin
khai thác của Attacker.
ModSecurity Web Intrusion Detection and Prevention 7
8. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Network Intrusion Detect System (NIDS)
8
2
ModSecurity Web Intrusion Detection and Prevention 8
9. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Network Intrusion Detect System (NIDS)
9
- Chỉ xác định và cảnh báo không ngăn chặn
được tấn công.
2
- Không phân tích được nội dung đã mã hóa
(SSL).
- Không thể làm một Gateway Application.
- Dễ dẫn đến Fail Positive khi áp dụng để xử
lý data mức Application
ModSecurity Web Intrusion Detection and Prevention 9
11. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
GIỚI THIỆU
11
- ModSecurity là một Opensource web application firewall.
-Tác giả: Ivan Ristic.
2
- Phát triển dành cho Web Server Apache, Ngix, hiện
version 2.7 đã có cho IIS.
- Sử dụng giấy phép GPL,hoàn toàn miễn phí.
- Kết hợp Mod_proxy trở thành Reverse Proxy bảo
vệ cho nhiều máy chủ web.
- Hoạt động dựa trên Rule.
ModSecurity Web Intrusion Detection and Prevention 11
12. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Mô hình
12
2
ModSecurity Web Intrusion Detection and Prevention 12
13. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Mô hình kết hợp Reverse Proxy
13
2
Thuận lợi:
- Tạo một điểm duy nhất cho việc truy cập
- Che đậy mô hình mạng với bên ngoài
- Tập trung dễ giám sát và quản lý
ModSecurity Web Intrusion Detection and Prevention 13
14. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Chức năng
- Request filtering: phân tích và cản lọc (filter) các request gửi
14
đến Web Server trước khi chúng được đưa đến các modules
khác để xử lý.
- Understanding of the HTTP protocol: hiểu được giao thức
HTTP. Có khả năng cản lọc 2 dựa trên các thông tin ở HTTP
Header hay có thể xem xét đến từng thông số, cookies của các
request...
- POST payload analysis: phân tích nội dung (payload) của
POST requests.
- Audit logging: Khả năng ghi lại các Request (bao gồm cả
POST) để người quản trị có thể theo dõi nếu cần.
- HTTPS filtering: phân tích HTTPS.
ModSecurity Web Intrusion Detection and Prevention 14
15. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Mô hình xử lý Apache + ModSecurity
15
2
ModSecurity Web Intrusion Detection and Prevention 15
16. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Các giai đoạn xử lý
16
1. Phase Request Header
2. Phase Request Body
2
+ application/x-www-form-urlencoded
+ multipart/form-data
+ text/xml
3. Phase Response Header
4. Phase Response Body
5. Phase logging
ModSecurity Web Intrusion Detection and Prevention 16
17. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Cú pháp Rule
SecRule Target Operator [Actions]
17
- Target: Quy định cụ thể mục tiêu của request hoặc
response muốn kiểm tra. 2
- Operator: xác định phương pháp và so khớp dữ
liệu để kích hoạt Action. Thường dùng Regular
expressions để tạo Pattern.
- Actions: các hành động được thực hiện nếu
phù hợp (matching) rule. Action có thể là allow hoặc
deny các request; và quy định cụ thể các
status code khi response cho client...
ModSecurity Web Intrusion Detection and Prevention 17
18. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Biến và Collection
-Truy cập các thông số trong request, response
18
thông qua Biến hoặc Collection.
Collection:-
ENV, FILES, FILES_NAMES, FILES_SIZES,
2
FILES_TMPNAMES, GEO, IP,
REQUEST_COOKIES,
REQUEST_COOKIES_NAMES,
REQUEST_HEADERS,
REQUEST_HEADERS_NAMES,
RESPONSE_HEADERS,
RESPONSE_HEADERS_NAMES, SESSION, TX,
USER…
ModSecurity Web Intrusion Detection and Prevention 18
19. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
19
2
Chặn một số tấn công cơ bản
ModSecurity Web Intrusion Detection and Prevention
20. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Ngăn chặn HTTP Fingerprinting
Đánh lừa các20
công cụ HTTP Fingerprinting,
cung cấp các thông tin không chính xác cho Hacker
# Thay đổi chử ký Server
2
SecServerSignature “My-server/1.0“
# Từ chối request không chứa host header
SecRule &REQUEST_HEADERS:Host "@eq 0" "phase:1,deny"
# Từ chối request không chứa request header
SecRule &REQUEST_HEADERS:Accept "@eq 0" phase:1,deny"
# Chỉ cho phép GET HEAD va POST
SecRule REQUEST_METHOD !^(get|head|post)$ phase:1,t:lowerCase,deny"
#Chỉ cho phép HTTP version 1.0 và 1.1
SecRule REQUEST_PROTOCOL !^http/1.(0|1)$ “ phase:1,t:lowercase,deny"
ModSecurity Web Intrusion Detection and Prevention 20
21. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
CROSS-SITE SCRIPTING (XSS)
21
Cross-Site Scripting hay còn được gọi tắt là XSS
là một kĩ thuật tấn công bằng cách chèn vào các
2
website động (ASP, PHP, CGI, JSP ...) những thẻ
HTML hay những đoạn mã script nguy hiểm có thể
gây nguy hại cho những người sử dụng khác.
Các đoạn mã hầu hết được viết bằng Client Script
(Javascript, Jscript hoặc thậm chí DHML,HTML…)
ModSecurity Web Intrusion Detection and Prevention 21
22. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Chặn CROSS-SITE SCRIPTING (XSS)
22
Script fragment Regular expression
<script <script
eval( evals*(
2
onMouseOver onmouseover
onMouseOut onmouseout
onMouseDown onmousedown
onMouseMove onmousemove
onClick onclick
onDblClick ondblclick
onFocus onfocus
SecRule ARGS "<script" "deny,t:lowercase"
ModSecurity Web Intrusion Detection and Prevention 22
23. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
TẤN CÔNG DIRECTORY TRAVERSAL
23
-URL hợp lệ: http://example.com/getpage.php?page=1
-URL attack=http://example.com/getpage.php?page=../../../etc/passwd
Nếu không cấu hình để cản lọc hoặc cơ chế cấp quyền
Không đúng thì hacker sẽ lấy được thông tin từ file
2
/etc/passwd. Hacker có thể không chèn trực tiếp biến ../
mà thực hiện chèn các biến đã được mã hoá như
%2e%2e%2f, khi giải mã thì vẫn là ../
Danh sách các chuỗi chúng ta cần chặn:
../
..%2f
%2e%2e/
%2e%2e%2f
%2e./
ModSecurity Web Intrusion Detection and Prevention 23
24. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
CHẶN DIRECTORY TRAVERSAL
24
Chúng ta có thể sử dụng t:urlDecode của
ModSecurity để chuyển tất cả chuỗi được mã
2
hoá thành giá trị gốc của nó.
Rule để chặn hình thức tấn công này:
SecRule REQUEST_URI "../" "t:urlDecode,deny"
ModSecurity Web Intrusion Detection and Prevention 24
25. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
TẤN CÔNG SQL INJECTION
Hacker chèn các25 khóa SQL vào dữ liệu để làm sai lệch câu
từ
lệnh SQL, và nếu dữ liệu không được lọc thì khả năng bị tấn
công SQL Injection là rất cao
2
SELECT * FROM user WHERE username = '%s‘ AND password = '%s';
SELECT * FROM products WHERE id = 1; DROP TABLE products;
SELECT * FROM user WHERE username = '‘ OR ‘1’=‘1’ AND
password = '' OR '1'='1';
SELECT LOAD_FILE("/etc/passwd");
SELECT “Du lieu can ghi” INTO OUTFILE “test.txt";
ModSecurity Web Intrusion Detection and Prevention 25
26. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
CHẶN SQL INJECTION
26
SQL code Regular expression
UNION SELECT unions+select
2
UNION ALL SELECT unions+alls+select
INTO OUTFILE intos+outfile
DROP TABLE drops+table
ALTER TABLE alters+table
LOAD_FILE load_file
SELECT * selects+*
SecRule ARGS "intos+outfile" "t:lowercase,deny,msg: 'SQL Injection'"
ModSecurity Web Intrusion Detection and Prevention 26
27. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
CHỐNG THỰC THI LỆNH SHELL
27
Các Lệnh , chương trình,
đường dẫn thông thường:
- rm
- ls
2 - kill
- mail
- sendmail
- cat
- echo
- /bin/
- /etc/
- /tmp/
SecRule ARGS “rm|ls|kill(send)?mail|cat|echo|/bin/|/etc/|/tmp/)” “deny”
ModSecurity Web Intrusion Detection and Prevention 27
28. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
CHỐNG BRUTE FORCE
- Là hình thức tấn công Hacker liên tục đoán username,password,
28
email … và tiến hành đăng nhập thử cho đến khi thành công.
- Cách tốt nhất để ngăn tấn công dạng này là giới hạn số lần đăng
2
nhập thất bại
# Khóa 5 phút sau 3 lần thất bại
<LocationMatch ^/login>
# Khởi tạo collection với IP của User
SecAction "initcol:ip=%{REMOTE_ADDR},pass,nolog"
# Phát hiện đăng nhập sai với “Username does not exist”
SecRule RESPONSE_BODY "Username does not exist“
"phase:4,pass,setvar:ip.failed_logins=+1,expirevar:ip.failed_logins=300"
# khóa với số lần đăng nhập thất bại =3
SecRule IP:FAILED_LOGINS "@gt 3" deny
</Location>
ModSecurity Web Intrusion Detection and Prevention 28
29. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Thực hiện các shell scripts & Giởi email cảnh báo
SecRule ARGS 29
"drop table" "deny,t:lowercase,
setenv:HOSTNAME=%{SERVER_NAME},
setenv:REMOTEIP=%{REMOTE_ADDR},
setenv:REQUESTURI=%{REQUEST_URI},
2
exec:/usr/local/bin/email.sh"
#!/bin/sh
echo "Phat hien sql injection
Server: $HOSTNAME
Attacking IP: $REMOTEIP
Request URI: $REQUESTURI
Time: `date '+%D %H:%M'`
"|mail –s 'ModSecurity Alert' <a
href="mailto:suhoangson@gmail.com">suhoangson@gmail.co
m</a>
echo Done.
ModSecurity Web Intrusion Detection and Prevention 29
30. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Core Rule Set
30
Tập các Rule viết sẵn với nhiều dạng patterm:
- Real-time Blacklist Lookups IP từ các hãng danh tiếng
- Phát hiện Web-based Malsware dựa vào Google Safe Browsing
2
API
- Chống DoS HTTP Flooding
- Chống các dạng tấn công cơ bản
- Phát hiện : bots, crawlers, scanners độc hại khác
- Kết hợp Antivirut kiểm tra các File upload lên web application
- Chặn các thông báo sai từ ứng dụng
- Ẩn các thông báo lỗi từ Server
ModSecurity Web Intrusion Detection and Prevention 30
31. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
Thách thức
31
- Tỉ lệ Fail Positive vẫn có thể xãy ra.
- Viết Rule cần có nhiều kinh nghiệm về HTTP Protocol ,
Regular expressions… và2am hiểu các dạng tấn công.
- Hiệu xuất đáp ứng khi kết hợp Reverse Proxy bảo vệ
nhiều site.
-Khó khăn trong việc thống nhất các nhà phát triển web
tránh xung đột giửa Patterm với các giá trị đặc biệt do
người lập trình đưa vào. Hoặc thiết lập cách nhập liệu đặc
biệt cho các từ nhạy cảm.
ModSecurity Web Intrusion Detection and Prevention 31
32. SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
32 Question ?
Thank You!!!
2
Reference:
--ModSecurity Handbook: Ivan Ristic
- ModSecurity 2.5: Magus Mischel
- http://www.packtpub.com/article/
+ blocking-common-attacks-using-modsecurity-2.5-part1
+ blocking-common-attacks-using-modsecurity-2.5-part2
+ blocking-common-attacks-using-modsecurity-2.5-part3
- hvaonline.net
- Core Rule Set:
https://github.com/FireFart/owasp-modsecurity-crs
ModSecurity Web Intrusion Detection and Prevention 32