PHP: Một số cách sử dụng .htaccess để cấu hình website - P2

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

 Tương tự nội dung mà các bạn đã tham khảo ở   phần 1, bài hướng dẫn này chúng ta tiếp tục bổ sung thêm 1 số cấu hình có liên quan đến bảo mật như sau:

Ngăn cản truy cập file .htaccess:

# secure htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

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.jpg.

# prevent viewing of a specific file
<files secretfile.jpg>
order allow,deny
deny from all
</files>

Chặn truy cập tới nhiều file:

<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny 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.

# disable directory browsing
Options 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:

# enable directory browsing
Options All +Indexes

Ngăn cản server listing directory:

# prevent folder listing
IndexIgnore *

Ngăn cản truy nhập vào các file có định dạng nào đó, sử dụng IndexIgnore:

# prevent display of select file types
IndexIgnore *.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.html

# serve alternate default index page
DirectoryIndex 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 index:

# serve first available alternate default index page from series
DirectoryIndex filename.html index.cgi index.pl default.htm

Giới hạn truy cập tới mạng LAN:

# limit access to local area network
<Limit GET POST PUT>
order deny,allow
deny from all
allow 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.com.

# allow all except those indicated here
<Limit GET POST PUT>
order allow,deny
allow from all
deny from x.y.z.v
deny 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.com.

# deny all except those indicated here
<Limit GET POST PUT>
order deny,allow
deny from all
allow from x.y.z.v
allow 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):

# block visitors referred from indicated domains
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{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, …

# block IP range by CIDR number
<Limit GET POST PUT>
order allow,deny
allow from all
deny from 10.1.0.0/16
deny from 80.0.0/8
</Limit>

Để cho phép bởi CIDR:

# allow IP range by CIDR number
<Limit GET POST PUT>
order deny,allow
deny from all
allow from 10.1.0.0/16
allow 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:

# block IP range by address truncation
<Limit GET POST PUT>
order allow,deny
allow from all
deny from 99.88.77.66
deny from 99.88.77.*
deny from 99.88.*.*
deny from 99.*.*.*
</Limit>

Cho phép địa chỉ IP theo cách này:

# allow IP range by address truncation
<Limit GET POST PUT>
order deny,allow
deny from all
allow from 99.88.77.66
allow 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òng:

# block two unique IP addresses
deny from 99.88.77.66 11.22.33.44
# block three ranges of IP addresses
deny from 99.88 99.88.77 11.22.33

Cho phép (allow):

# allow two unique IP addresses
allow from 99.88.77.66 11.22.33.44
# allow three ranges of IP addresses
allow from 99.88 99.88.77 11.22.33

Có một số rule khác có thể sử dụng:

# block a partial domain via network/netmask values
deny from 99.1.0.0/255.255.0.0
# block a single domain
deny from 99.88.77.66
# block domain.com but allow sub.domain.com
order deny,allow
deny from domain.com
allow 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:

# stop hotlinking and serve alternate content
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^

    http://(www\.)?domain\.com/.*$ [NC]

    RewriteRule .*\.(gif|jpg)$

    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:

# serve a standard 403 forbidden error page

RewriteRule .*\.(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:

# allow linking from the following site
RewriteCond %{HTTP_REFERER} !^
http://(www\.)?goodsite\.com/.*$ [NC]

Đặ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:

# password-protect single file
<Files secure.php>
AuthType Basic
AuthName "Prompt"
AuthUserFile /home/path/.htpasswd
Require valid-user
</Files> 
# password-protect multiple files
<FilesMatch "^(execute|index|secure|insanity|biscuit)*$">
AuthType basic
AuthName "Development"
AuthUserFile /home/path/.htpasswd
Require valid-user
</FilesMatch>
# password-protect the directory in which this htaccess rule resides
AuthType basic
AuthName "This directory is protected"
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user 
# password-protect directory for every IP except the one specified
# place in htaccess file of a directory to protect that entire directory
AuthType Basic
AuthName "Personal"
AuthUserFile /home/path/.htpasswd
Require valid-user
Allow from 99.88.77.66
Satisfy 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:

# protect against DOS attacks by limiting file upload size
LimitRequestBody 10240000

Bảo vệ thư mục bằng cách disable việc thực thi các script:

# secure directory by disabling script execution
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Options –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.

# 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 400
chmod .htpasswd files 640
chmod .htaccess files 644
chmod php files 600

Ngụy trang tất cả các định dạng mở rộng:

Ví dụ ngụy trang tất cả các file và coi như file .php:

# diguise all file extensions as php
ForceType application/x-httpd-php

Ngoài ra, có thể che giấu file php dưới các định dạng khác:

<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

Cấu hình SSL:

# require SSL
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "domain.tld"
ErrorDocument 403

https://domain.tld (https://domain.tld/)

# require SSL without mod_ssl
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^.*$

​https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Nguồn: http://wiki.matbao.net/Mot-so-cach-su-dung-htaccess-de-cau-hinh-website-P2.ashx

» Tiếp: Một số cách sử dụng .htaccess để cấu hình website - P3
« Trước: Một số cách sử dụng .htaccess để cấu hình website - P1
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!