1. 程式人生 > >配置防盜鏈,訪問控制

配置防盜鏈,訪問控制

[toc]

配置防盜鏈,訪問控制

11.25 配置防盜鏈

1.防盜鏈和referer概念

防盜鏈,通俗講就是不讓別人盜用你網站上的資源,這個資源指的是圖片、視訊、歌曲、文件等,在這之前需要理解一下referer的概念,如果你通過A網站的一個頁面http://a.com/a.html裡面的連結去訪問B網站的一個頁面http://b.com/b.html,那麼這個B網站頁面的referer就是http://a.com/a.html。也就是說,一個referer就是一個網址。

2.編輯虛擬主機配置檔案httpd-vhosts

[[email protected]
~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwroot/xavi.com" ServerName xavi.com ServerAlias www.example.com <Directory /data/wwwroot/xavi.com> //把xavi.com設為白名單,對應規則Allow SetEnvIfNoCase Referer "http://xavi.com" local_ref // 定義允許訪問連結的referer SetEnvIfNoCase Referer "http://aaa.com" local_ref SetEnvIfNoCase Referer "^$" local_ref //把空referer設為白名單,對應規則Allow;其中^$為空referer,即直接訪問的地址,當直接再瀏覽器裡輸入圖片地址去訪問它時,它的referer就為空。 <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)"> // 對txt、doc等格式的檔案執行訪問控制,訪問這樣型別的檔案時就會被限制. Order Allow,Deny //白名單地址allow,其他deny,執行順序依次為allow、deny,反過來將導致都被禁止訪問 Allow from env=local_ref // 只有符合白名單上的referer才能訪問xavi.com目錄。 </filesmatch>

3.開啟httpd服務,-t檢查語法,graceful重新載入

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful

4. 測試

建立測試需要條件:

[[email protected] ~]# touch /data/wwwroot/xavi.com/xavi.jpg
[[email protected] ~]# touch /data/wwwroot/xavi.com/xavi.txt
[
[email protected]
~]# curl -x127.0.0.1:80 -I xavi.com/xavi.jpg //圖片和JPEG都在空refer裡 HTTP/1.1 200 OK Date: Thu, 08 Mar 2018 14:11:57 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Last-Modified: Thu, 08 Mar 2018 14:11:27 GMT ETag: "0-566e7406be177" Accept-Ranges: bytes Content-Type: image/jpeg [[email protected] ~]# curl -x127.0.0.1:80 -I xavi.com/xavi.txt HTTP/1.1 200 OK Date: Thu, 08 Mar 2018 14:12:13 GMT Server: Apache/2.4.29 (Unix) PHP/7.1.6 Last-Modified: Thu, 08 Mar 2018 14:11:34 GMT ETag: "0-566e740d104e7" Accept-Ranges: bytes Content-Type: text/plain

這裡使用-e來定義referer,且這個referer一定要以http://開頭.

[[email protected] ~]# curl -x192.168.72.130:80 -I -e "http://xavi.com/xavi.txt" http://xavi.com/xavi.jpg
HTTP/1.1 200 OK
Date: Thu, 08 Mar 2018 14:13:17 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Last-Modified: Thu, 08 Mar 2018 14:11:27 GMT
ETag: "0-566e7406be177"
Accept-Ranges: bytes
Content-Type: image/jpeg

使用非允許的referre會返回403狀態碼

[[email protected] ~]# curl -x192.168.72.130:80 -I -e "http://xavix.com/xavi.txt" http://xavi.com/xavi.jpg  //xavix.com不在refer白名單中
HTTP/1.1 403 Forbidden
Date: Thu, 08 Mar 2018 14:20:18 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

11.26 訪問控制Directory

對於一些比較重要的網站內容,除了可以使用使用者認證限制訪問之外,還可以通過其他一些方法做到限制,比如限制IP,也可以限制user_agent。限制IP指的是限制訪問網址的來源IP,而限制user_agent,通常用來限制惡意或者不正常的請求.

1. 限制IP訪問,編輯配置檔案

<Directory /data/wwwroot/xavi.com/admin/>  // 指定需要訪問控制的網站的admin目錄
Order deny,allow   //訪問控制的順序,先所有都拒絕,然後再允許指定的ip。和iptables不同,可以全部規則都執行下去。
Deny from all   // 拒絕所有的來源ip
Allow from 127.0.0.1 //指定允許訪問的來源ip(指定網段也可以192.168.0.0/24)
    </Directory>

mark

2.驗證過程

匹配127.0.0.1IP訪問

[[email protected] ~]# mkdir /data/wwwroot/xavi.com/admin/
[[email protected] ~]# echo "admin" > /data/wwwroot/xavi.com/admin/index.html
[[email protected] ~]#  /usr/local/apache2.4/bin/apachectl graceful
[[email protected] ~]# curl -x127.0.0.1:80 -I xavi.com/admin/index.html
HTTP/1.1 200 OK
Date: Thu, 08 Mar 2018 14:49:45 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Last-Modified: Thu, 08 Mar 2018 14:45:59 GMT
ETag: "6-566e7bbe73f30"

用本機ip訪問,被阻止訪問

[[email protected] ~]# curl -x192.168.72.130:80 -I xavi.com/admin/index.html //不是執行的ip
HTTP/1.1 403 Forbidden
Date: Thu, 08 Mar 2018 14:52:21 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

檢視錯誤日誌cat /usr/local/apache2.4/logs/xavi-access_log

[[email protected] ~]# cat /usr/local/apache2.4/logs/xavi-access_log
127.0.0.1 - - [06/Mar/2018:22:48:23 +0800] "GET HTTP://xavi.com/ HTTP/1.1" 401 381
127.0.0.1 - - [06/Mar/2018:22:50:18 +0800] "HEAD HTTP://xavi.com/ HTTP/1.1" 401 -
192.168.72.1 - - [06/Mar/2018:22:58:52 +0800] "GET / HTTP/1.1" 401 381
192.168.72.1 - - [06/Mar/2018:22:59:22 +0800] "GET /favicon.ico HTTP/1.1" 401 381
127.0.0.1 - xavi [06/Mar/2018:23:03:45 +0800] "GET HTTP://xavi.com/ HTTP/1.1" 401 381

11.27 訪問控制FilesMatch

編輯虛擬主機配置檔案,進行FilesMatch配置;既要匹配檔案,又要限制IP

1.編輯配置filesmatch

[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<Directory /data/wwwroot/xavi.com/admin/>
<FilesMatch "admin.php(.*)">   //檔案匹配admin.php後面跟任意的字元
Order deny,allow     //訪問控制的順序,先所有都拒絕,然後再允許指定的ip
Deny from all          // 拒絕所有的來源ip
Allow from 127.0.0.1  //指定允許訪問的來源ip(指定網段也可以192.168.0.0/24)
</FilesMatch>

檢查語法錯誤,在載入

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]#  /usr/local/apache2.4/bin/apachectl graceful

測試結果:

[[email protected] ~]# curl -x192.168.72.130:80 http://xavix.com/admin/alsdedadsdk -I //這裡只允許127.0.0.1訪問

HTTP/1.1 404 Not Found
Date: Thu, 08 Mar 2018 15:08:06 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1
[[email protected] ~]# vim /data/wwwroot/xavi.com/admin/index.php
[[email protected] ~]# curl -x127.0.0.1:80 'http://xavi.com/admin/index.php?alsdedadsd' -I
HTTP/1.1 200 OK //這裡只允許127.0.0.1訪問
Date: Thu, 08 Mar 2018 15:19:19 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8

[[email protected] ~]# curl -x127.0.0.1:80 'http://xavi.com/admin/.php?alsdedadsd' -I //能夠連線,但是無此頁面

HTTP/1.1 404 Not Found 
Date: Thu, 08 Mar 2018 15:19:57 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

試驗結果:只有127.0.0.1能夠訪問 admin.php(.*)的網頁。其餘IP,無此許可權;

files和filesmatch等的區別

mark