- Ngăn cản truy cập file .htaccess:
- Ngăn cản truy cập tới một file đặc biệt: Để chặn truy nhập vào một file đặc biệt nào đó giả sử là file secretfile.jpg12345
# prevent viewing of a specific file<filessecretfile.jpg>order allow,denydeny from all</files> - Chặn truy cập tới nhiều file:1234
<FilesMatch"\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">Order Allow,DenyDeny from all</FilesMatch> - Chống browse thư mục trái phép : Đảm bảo người dùng không có đủ quyền không thể xem toàn bộ trang web dưới dạng Directory listing.12
# disable directory browsingOptions All –Indexes
Ngược lại để cho phép người dùng có thể xem dưới dạng này, sử dụng:12# enable directory browsingOptions All +Indexes
Ngăn cản server listing directory:12# prevent folder listingIndexIgnore *
Ngăn cản truy nhập vào các file có định dạng nào đó, sử dụng IndexIgnore:12# prevent display of select file typesIndexIgnore *.wmv *.mp4 *.avi *.etc - Thay đổi trang index mặc định: thay vì sử dụng trang index một cách mặc định, ta cấu hình cho server nhận một file khác có chức năng tương tự như file index. Ví dụ:business.html12
# serve alternate default index pageDirectoryIndex business.html
Hoặc cho một loạt file đều có thể là file index, server sẽ tìm kiếm và đưa ra file đầu tiên tìm được là file index12# serve first available alternate default index page from seriesDirectoryIndex filename.html index.cgi index.pl default.htm - Giới hạn truy cập tới mạng LAN:123456
# limit access to local area network<LimitGET POST PUT>order deny,allowdeny from allallow from 192.168.0.0/33</Limit> - Bảo vệ thư mục bằng địa chỉ IP và/hoặc domain: Cấu hình cho phép truy cập ngoại trừ kết nối từ địa chỉ: x.y.z.v và từ domain.com1234567
# allow all except those indicated here<LimitGET POST PUT>order allow,denyallow from alldeny from x.y.z.vdeny from .*domain\.com.*</Limit>
Ngược lại với cấu hình bên trên, từ chối tất cả IP kết nối ngoại trừ x.y.z.v và domain.com1234567# deny all except those indicated here<LimitGET POST PUT>order deny,allowdeny from allallow from x.y.z.vallow from .*domain\.com.*</Limit>
Ngoài ra, cũng có thể tiết kiệm băng thông bằng cách block một số định dạng file đặc biệt như: .jpg, .zip, ,mp3, … từ các server ngoài (ở đây là abc và xyz)1234567# block visitors referred from indicated domains<IfModulemod_rewrite.c>RewriteEngine onRewriteCond %{HTTP_REFERER} abc\.com [NC,OR]RewriteCond %{HTTP_REFERER} xyz\.com [NC,OR]RewriteRule .* - [F]</ifModule> - Ngăn cản hoặc cho phép truy cập domain theo dảy địa chỉ IP: Có nhiều phương pháp để block một dảy địa chỉ IP bằng cách cấu hình .htaccess.
Cách thức đầu tiên có thể sử dụng CIDR (Classess Inter-Domain Routing) của dảy IP, cách này hiệu quả để block các mega-spammer như RIPE, Optinet, …1234567# block IP range by CIDR number<LimitGET POST PUT>order allow,denyallow from alldeny from 10.1.0.0/16deny from 80.0.0/8</Limit>
Để cho phép bởi CIDR:1234567# allow IP range by CIDR number<LimitGET POST PUT>order deny,allowdeny from allallow from 10.1.0.0/16allow from 80.0.0/8</Limit>
Một biện pháp khác chúng ta có thể block dảy IP đầu vào liên quan tới số truncating cho tới khi dảy mong muốn xuất hiện:123456789# block IP range by address truncation<LimitGET POST PUT>order allow,denyallow from alldeny from 99.88.77.66deny from 99.88.77.*deny from 99.88.*.*deny from 99.*.*.*</Limit>
Cho phép địa chỉ IP theo cách này:123456789# allow IP range by address truncation<LimitGET POST PUT>order deny,allowdeny from allallow from 99.88.77.66allow from 99.88.77.*allow from 99.88.*.*allow from 99.*.*.*</Limit>
Chặn hoặc cho phép nhiều địa chỉ trong cùng 1 dòng1234# block two unique IP addressesdeny from 99.88.77.66 11.22.33.44# block three ranges of IP addressesdeny from 99.88 99.88.77 11.22.33
Allow:1234# allow two unique IP addressesallow from 99.88.77.66 11.22.33.44# allow three ranges of IP addressesallow from 99.88 99.88.77 11.22.33
Có một số rule khác có thể sử dụng:12345678# block a partial domain via network/netmask valuesdeny from 99.1.0.0/255.255.0.0# block a single domaindeny from 99.88.77.66# block domain.com but allow sub.domain.comorder deny,allowdeny from domain.comallow from sub.domain.com - Ngừng các hotlinking, luân chuyển nội dung server: Mục đích nhằm giúp bạn ngăn cản các website khác sử dụng trực tiếp các hình ảnh, nội dung, link, … từ website của mình làm hao tốn băng thông. Tuy nhiên cấu hình này chỉ hoạt động khi kích hoạt mod_rewrite.1234567
# stop hotlinking and serve alternate content<IfModulemod_rewrite.c>RewriteEngine onRewriteCond %{HTTP_REFERER} !^$RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.com/.*$ [NC]RewriteRule .*\.(gif|jpg)$ http://www.domain.com/eatme.jpg (http://www.domain.com/wiki.jpg) [R,NC,L]</ifModule>
Để chuyển giao một page lỗi thay vì một số hình ảnh như wiki.jpg như bên trên, thay dòng RewriteRule bằng dòng:12# serve a standard 403 forbidden error pageRewriteRule .*\.(gif|jpg)$ - [F,L]
Để cho phép một domain ngoài có thể sử dụng hotlinking (goodsite chẳng hạn), thêm dòng cấu hình:12# allow linking from the following siteRewriteCond %{HTTP_REFERER} !^http://(www\.)?goodsite\.com/.*$ [NC]
- Password Protect: Đặt password bảo vệ là một biện pháp bảo vệ nội dung website và chỉ cho phép người dùng nội bộ có thể truy nhập nội dung.
Khi một thư mục được đặt password thì toàn bộ thư mục con và file trong thư mục sẽ được đặt password như vậy. Nội dung file .htaccess để thư mục chứa nó được bảo vệ bởi mật khẩu:1234567# password-protect single file<Filessecure.php>AuthType BasicAuthName "Prompt"AuthUserFile /home/path/.htpasswdRequire valid-user</Files>1234567# password-protect multiple files<FilesMatch"^(execute|index|secure|insanity|biscuit)*$">AuthType basicAuthName "Development"AuthUserFile /home/path/.htpasswdRequire valid-user</FilesMatch>123456# password-protect the directory in which this htaccess rule residesAuthType basicAuthName "This directory is protected"AuthUserFile /home/path/.htpasswdAuthGroupFile /dev/nullRequire valid-user12345678# password-protect directory for every IP except the one specified# place in htaccess file of a directory to protect that entire directoryAuthType BasicAuthName "Personal"AuthUserFile /home/path/.htpasswdRequire valid-userAllow from 99.88.77.66Satisfy Any
Bạn có thể tham khảo thêm tại Hướng dẫn tạo Password Protect thư mục trên Linux - Hạn chế tấn công từ chối dịch vụ bằng cách giới hạn kích thước file upload:12
# protect against DOS attacks by limiting file upload sizeLimitRequestBody 10240000 - Bảo vệ thư mục bằng cách disable việc thực thi các script:123
# secure directory by disabling script executionAddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgiOptions –ExecCGI - Tự động đặt CHMOD cho các loại file: Cách thức này đảm bảo đặt CHMOD cho các loại file xác định.1234567
# ensure CHMOD settings for specified file types# remember to never set CHMOD 777 unless you know what you are doing# files requiring write access should use CHMOD 766 rather than 777# keep specific file types private by setting their CHMOD to 400chmod .htpasswd files 640chmod .htaccess files 644chmod php files 600 - Ngụy trang tất cả các định dạng mở rộng : VD: tất cả các file và coi như file .php12
# diguise all file extensions as phpForceType application/x-httpd-php
Ngoài ra, có thể che giấu file php dưới các định dạng khác:123<FilesMatch"\.(htm|html|php)$">SetHandler application/x-httpd-php</FilesMatch> - Cấu hình SSL:12345
# require SSLSSLOptions +StrictRequireSSLRequireSSLSSLRequire %{HTTP_HOST} eq "domain.tld"ErrorDocument 403 https://domain.tld (https://domain.tld/)123# require SSL without mod_sslRewriteCond %{HTTPS} !=on [NC]RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
