security bootcamp 2012 - bảo vệ web app với mod security (sử hoàng sơn)
TRANSCRIPT
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
1
2
Sử Hoàng Sơn | Email: [email protected]
Kiện toàn bảo mật Web với ModSecurity
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
2
2
ModSecurity Web Intrusion Detection and Prevention 2
- Hầu hết các ứng dụng do nhiều đơn vị phát - 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.triển với nhiều công nghệ khác nhau.- Sử dụng các sản phẩm của bên thứ 3 mà - Sử dụng các sản phẩm của bên thứ 3 mà không biết chất lượng.không biết chất lượng.- Cần rất nhiều thời gian và nhân lực để kiểm - Cần rất nhiều thời gian và nhân lực để kiểm tra độ an toàn. tra độ an toàn. - Thói quen, tư duy người lập trình chưa quan - 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ộ 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.phận phụ trách an toàn sản phẩm.
Thực trạng
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
3
2
ModSecurity Web Intrusion Detection and Prevention 3
- Công tác an toàn sản phẩm cần làm sớm - Công tác an toàn sản phẩm cần làm sớm càng tốt. càng tốt.
- Bằng tất cả mọi cách cải tiến độ an toàn các - 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ễ).ứng dụng đang có (không dễ).
- Nhiều khả năng chúng ta phải tăng cường - Nhiều khả năng chúng ta phải tăng cường an ninh từ bên ngoài. an ninh từ bên ngoài.
- Đặt các ứng dụng không an toàn vào môi - Đặt các ứng dụng không an toàn vào môi trường an toàn.trường an toàn.
Vấn đề cần làm
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
4
2
ModSecurity Web Intrusion Detection and Prevention 4
Yêu cầu với công tác quản trị
MonitoringMonitoring: biết chuyện gì đang xãy ra. : biết chuyện gì đang xãy ra.
DetectionDetection: phát hiện khi bị tấn công.: phát hiện khi bị tấn công.
PreventionPrevention: ngăn chặn được tấn công.: ngăn chặn được tấn công.
AssessmentAssessment: phát hiện trước vấn đề trước: phát hiện trước vấn đề trước khi bị tấn côngkhi bị tấn công
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
5
2
ModSecurity Web Intrusion Detection and Prevention 5
Một số thiết bị bảo mật thường gặp
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
6
2
ModSecurity Web Intrusion Detection and Prevention 6
Packet Filter Firewall
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
7
2
ModSecurity Web Intrusion Detection and Prevention 7
Packet Filter Firewall
- Tạo ACL cơ bản dựa trên: Source IP, - Tạo ACL cơ bản dựa trên: Source IP,
Destination IP, Port, Service, Protocol.Destination IP, Port, Service, Protocol.
- Nội dung và ngữ cảnh (mối liên hệ với các - Nội dung và ngữ cảnh (mối liên hệ với các
gói khác) bị bỏ qua.gói khác) bị bỏ qua.
- Thông tin địa chỉ trong một gói có thể bị - 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. 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 - Packet chuyển qua có thể chứa thông tin
khai thác của Attacker.khai thác của Attacker.
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
8
2
ModSecurity Web Intrusion Detection and Prevention 8
Network Intrusion Detect System (NIDS)
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
9
2
ModSecurity Web Intrusion Detection and Prevention 9
Network Intrusion Detect System (NIDS)
- Chỉ xác định và cảnh báo không ngăn chặn - Chỉ xác định và cảnh báo không ngăn chặn
được tấn công.được tấn công.
- Không phân tích được nội dung đã mã hóa - Không phân tích được nội dung đã mã hóa
(SSL).(SSL).
- Không thể làm một Gateway Application.- Không thể làm một Gateway Application.
- Dễ dẫn đến Fail Positive khi áp dụng để xử - Dễ dẫn đến Fail Positive khi áp dụng để xử
lý data mức Applicationlý data mức Application
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
10
2
ModSecurity
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
11
2
ModSecurity Web Intrusion Detection and Prevention 11
- ModSecurity là một Opensource web application firewall.-Tác giả: Ivan Ristic.
- 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.
GIỚI THIỆU
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
12
2
ModSecurity Web Intrusion Detection and Prevention 12
Mô hình
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
13
2
ModSecurity Web Intrusion Detection and Prevention 13
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ý
Mô hình kết hợp Reverse Proxy
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
14
2
ModSecurity Web Intrusion Detection and Prevention 14
Chức năng- Request filtering: phân tích và cản lọc (filter) các request gửi đế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 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.
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
15
2
ModSecurity Web Intrusion Detection and Prevention 15
Mô hình xử lý Apache + ModSecurity
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
16
2
ModSecurity Web Intrusion Detection and Prevention 16
Các giai đoạn xử lý
1. Phase Request Header
2. Phase Request Body
+ application/x-www-form-urlencoded
+ multipart/form-data
+ text/xml
3. Phase Response Header
4. Phase Response Body
5. Phase logging
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
17
2
ModSecurity Web Intrusion Detection and Prevention 17
Cú pháp Rule
SecRule Target Operator [Actions]
- Target: Quy định cụ thể mục tiêu của request hoặc response muốn kiểm tra. - 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ácstatus code khi response cho client...
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
18
2
ModSecurity Web Intrusion Detection and Prevention 18
Biến và Collection-Truy cập các thông số trong request, response thông qua Biến hoặc Collection.Collection:- ENV, FILES, FILES_NAMES, FILES_SIZES, FILES_TMPNAMES, GEO, IP, REQUEST_COOKIES, REQUEST_COOKIES_NAMES, REQUEST_HEADERS, REQUEST_HEADERS_NAMES, RESPONSE_HEADERS, RESPONSE_HEADERS_NAMES, SESSION, TX, USER…
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
19
2
ModSecurity Web Intrusion Detection and Prevention
Chặn một số tấn công cơ bản
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
20
2
ModSecurity Web Intrusion Detection and Prevention 20
Ngăn chặn HTTP Fingerprinting
# Thay đổi chử ký ServerSecServerSignature “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.1SecRule REQUEST_PROTOCOL !^http/1\.(0|1)$ “ phase:1,t:lowercase,deny"
Đánh lừa các công cụ HTTP Fingerprinting,cung cấp các thông tin không chính xác cho Hacker
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
21
2
ModSecurity Web Intrusion Detection and Prevention 21
CROSS-SITE SCRIPTING (XSS)
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
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…)
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
22
2
ModSecurity Web Intrusion Detection and Prevention 22
Chặn CROSS-SITE SCRIPTING (XSS)
Script fragment Regular expression
<script <script
eval( evals*(
onMouseOver onmouseover
onMouseOut onmouseout
onMouseDown onmousedown
onMouseMove onmousemove
onClick onclick
onDblClick ondblclick
onFocus onfocus
SecRule ARGS "<script" "deny,t:lowercase"
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
23
2
ModSecurity Web Intrusion Detection and Prevention 23
TẤN CÔNG DIRECTORY TRAVERSAL -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ềnKhông đúng thì hacker sẽ lấy được thông tin từ file /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./
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
24
2
ModSecurity Web Intrusion Detection and Prevention 24
CHẶN DIRECTORY TRAVERSAL
Chúng ta có thể sử dụng t:urlDecode của
ModSecurity để chuyển tất cả chuỗi được mã
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"
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
25
2
ModSecurity Web Intrusion Detection and Prevention 25
TẤN CÔNG SQL INJECTION
SELECT * FROM user WHERE username = '%s‘ AND password = '%s';
Hacker chèn các từ khóa SQL vào dữ liệu để làm sai lệch câu
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
SELECT * FROM user WHERE username = '‘ OR ‘1’=‘1’ AND password = '' OR '1'='1';
SELECT * FROM products WHERE id = 1; DROP TABLE products;
SELECT LOAD_FILE("/etc/passwd");
SELECT “Du lieu can ghi” INTO OUTFILE “test.txt";
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
26
2
ModSecurity Web Intrusion Detection and Prevention 26
SQL code Regular expression
UNION SELECT unions+select
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'"
CHẶN SQL INJECTION
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
27
2
ModSecurity Web Intrusion Detection and Prevention 27
CHỐNG THỰC THI LỆNH SHELL Các Lệnh , chương trình,đường dẫn thông thường:- rm - ls - kill - mail - sendmail - cat - echo - /bin/ - /etc/ - /tmp/
SecRule ARGS “rm|ls|kill(send)?mail|cat|echo|/bin/|/etc/|/tmp/)” “deny”
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
28
2
ModSecurity Web Intrusion Detection and Prevention 28
CHỐNG BRUTE FORCE - Là hình thức tấn công Hacker liên tục đoán username,password,
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
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 UserSecAction "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 =3SecRule IP:FAILED_LOGINS "@gt 3" deny</Location>
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
29
2
ModSecurity Web Intrusion Detection and Prevention 29
Thực hiện các shell scripts & Giởi email cảnh báoSecRule ARGS "drop table" "deny,t:lowercase, setenv:HOSTNAME=%{SERVER_NAME}, setenv:REMOTEIP=%{REMOTE_ADDR}, setenv:REQUESTURI=%{REQUEST_URI}, exec:/usr/local/bin/email.sh"
#!/bin/shecho "Phat hien sql injectionServer: $HOSTNAMEAttacking IP: $REMOTEIPRequest URI: $REQUESTURITime: `date '+%D %H:%M'`"|mail –s 'ModSecurity Alert' <a href="mailto:[email protected]">[email protected]</a>echo Done.
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
30
2
ModSecurity Web Intrusion Detection and Prevention 30
Core Rule Set
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
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
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
31
2
ModSecurity Web Intrusion Detection and Prevention 31
Thách thức
- 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à am 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.
SECURITY BOOTCAMP 2012 | Make yourself to be an expert!
32
2
ModSecurity Web Intrusion Detection and Prevention 32
Thank You!!!
Question ?
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