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

配置防盜鏈及訪問控制介紹

Linux

配置防盜鏈

防盜鏈,就是不讓別人盜用你網站上的資源,這個資源,通常指的是圖片、視頻、歌曲、文檔等。


referer的概念

你通過A網站的一個頁面http://a.com/a.html 裏面的鏈接去訪問B網站的一個頁面http://b.com/b.html ,那麽這個B網站頁面的referer就是http://a.com/a.html。 也就是說,一個referer其實就是一個網址。


1.配置防盜鏈

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl graceful


參考配置文件內容如下:

<Directory /data/wwwroot/111.com>

SetEnvIfNoCase Referer "http://111.com" local_ref

SetEnvIfNoCase Referer "http://111.com" local_ref

SetEnvIfNoCase Referer "^$" local_ref

<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">

Order Allow,Deny

Allow from env=local_ref

</filesmatch>

</Directory>


解釋說明:

首先定義允許訪問鏈接的referer,其中^$為空referer,當直接在瀏覽器裏輸入圖片地址去訪問它時,它的referer就為空。然後又使用filesmatch來定義需要保護的文件類型,訪問txt、doc、mp3、zip、rar、jpg、gif、png格式的文件,當訪問這樣的類型文件時就會被限制。


修改後示例如下圖:

技術分享圖片



2.測試網頁訪問

瀏覽器訪問:http://111.com/qq.png

在其它網站上鏈接這個網址,還是打不開。

技術分享圖片技術分享圖片


然後在虛擬主機配置文件裏把第三方站點加入到白名單

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf


在下面圖片上把第三方站點網址加入到白名單,然後保存退出重新加載配置。

技術分享圖片


在點擊鏈接http://111.com/qq.png 訪問就可以了,這就是referer,如下圖

技術分享圖片


3.使用curl測試

[root@gary-tao 111.com]# curl -x127.0.0.1:80 111.com/qq.png -I

HTTP/1.1 200 OK

Date: Thu, 21 Dec 2017 13:41:38 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Last-Modified: Thu, 21 Dec 2017 06:18:31 GMT

ETag: "91ab-560d3ab3fbbc0"

Accept-Ranges: bytes

Content-Length: 37291

Cache-Control: max-age=86400

Expires: Fri, 22 Dec 2017 13:41:38 GMT

Content-Type: image/png


[root@gary-tao 111.com]# curl -e "http://wwww.qq.com/123.txt" -x127.0.0.1:80 111.com/qq.png -I //使用-e來定義referer,這個referer一定要以http://開頭,否則不管用。

HTTP/1.1 403 Forbidden

Date: Thu, 21 Dec 2017 13:42:17 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1


[root@gary-tao 111.com]# curl -e "http://111.com/123.txt" -x127.0.0.1:80 111.com/qq.png -I

HTTP/1.1 200 OK

Date: Thu, 21 Dec 2017 13:42:34 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Last-Modified: Thu, 21 Dec 2017 06:18:31 GMT

ETag: "91ab-560d3ab3fbbc0"

Accept-Ranges: bytes

Content-Length: 37291

Cache-Control: max-age=86400

Expires: Fri, 22 Dec 2017 13:42:34 GMT

Content-Type: image/png



訪問控制Directory

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


1.修改虛擬主機配置:

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf


核心配置文件內容如下:

<Directory /data/wwwroot/www.123.com/admin/>

Order deny,allow

Deny from all

Allow from 127.0.0.1

</Directory>


配置示例圖:

技術分享圖片


解釋說明:

使用<Directory>來指定要限制訪問的目錄,order定義控制順序,哪個在前面就先匹配哪個規則,在本例中deny在前面,所以要先匹配Deny from all,這樣所有的來源IP都會被限制,然後匹配Allow from 127.0.0.1,這樣又允許了127.0.0.1這個IP。最終的效果是,只允許來源IP為127.0.0.1的訪問。

驗證如下:


[root@gary-tao 111.com]# mkdir /data/wwwroot/111.com/admin/ //創建admin目錄,模擬網站後臺

[root@gary-tao 111.com]# ls

123.php admin index.php qq.png

[root@gary-tao 111.com]# touch /data/wwwroot/111.com/admin/index.php //在後臺目錄下面創建文件

[root@gary-tao 111.com]# ls admin/

index.php

[root@gary-tao 111.com]# vim admin/index.php //並寫入內容

[root@gary-tao 111.com]# cat admin/index.php

123456789

[root@gary-tao 111.com]# curl -x127.0.01:80 111.com/admin/index.php -I

HTTP/1.1 200 OK

Date: Mon, 25 Dec 2017 02:34:14 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Cache-Control: max-age=0

Expires: Mon, 25 Dec 2017 02:34:14 GMT

Content-Type: text/html; charset=UTF-8


[root@gary-tao 111.com]# curl -x127.0.01:80 111.com/admin/index.php

123456789

[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl graceful //加載配置

[root@gary-tao 111.com]# curl -x172.16.111.100:80 111.com/admin/index.php

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>403 Forbidden</title>

</head><body>

<h1>Forbidden</h1>

<p>You don't have permission to access /admin/index.php

on this server.<br />

</p>

</body></html>

[root@gary-tao 111.com]# curl -x127.0.01:80 111.com/admin/index.php

123456789

[root@gary-tao 111.com]# tail /usr/local/apache2.4/logs/111.com-access_20171225.log //查看日誌

127.0.0.1 - - [25/Dec/2017:10:34:14 +0800] "HEAD HTTP://111.com/admin/index.php HTTP/1.1" 200 - "-" "curl/7.29.0"

127.0.0.1 - - [25/Dec/2017:10:34:30 +0800] "GET HTTP://111.com/admin/index.php HTTP/1.1" 200 10 "-" "curl/7.29.0"

172.16.111.100 - - [25/Dec/2017:10:36:54 +0800] "GET HTTP://111.com/admin/index.php HTTP/1.1" 403 224 "-" "curl/7.29.0"

127.0.0.1 - - [25/Dec/2017:10:37:33 +0800] "GET HTTP://111.com/admin/index.php HTTP/1.1" 200 10 "-" "curl/7.29.0"

[root@gary-tao 111.com]# curl -x127.0.0.1:80 http://111.com/admin/adfafdafdas -I

HTTP/1.1 404 Not Found

Date: Mon, 25 Dec 2017 02:54:48 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1


[root@gary-tao 111.com]# curl -x172.16.111.100:80 http://111.com/admin/adfafdafdas -I

HTTP/1.1 403 Forbidden

Date: Mon, 25 Dec 2017 02:54:59 GMT

Server: Apache/2.4.29 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1


解釋說明:

本機有兩個IP,一個是172.16.111.100,一個是127.0.0.1,通過這兩個IP都可以訪問到站點.而來源分別為172.16.111.110和127.0.0.1,其實和本機IP是一樣的,curl測試狀態碼為403則被限制訪問了。


使用windowo瀏覽器訪問示例圖

技術分享圖片


[root@gary-tao 111.com]# tail /usr/local/apache2.4/logs/111.com-access_20171225.log //windows瀏覽器訪問日誌

172.16.111.1 - - [25/Dec/2017:11:00:37 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

172.16.111.1 - - [25/Dec/2017:11:00:39 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

172.16.111.1 - - [25/Dec/2017:11:00:39 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"

172.16.111.1 - - [25/Dec/2017:11:00:40 +0800] "GET /admin HTTP/1.1" 403 214 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"


解釋說明:

瀏覽器訪問提示Forbidden,其實就是403,再來看日誌,可以查看到對應的來源IP為172.16.111.1,希望不要把來源IP和本機IP搞混了,前面實驗中之所以本機IP和來源IP一樣,就是因為它相當於自己訪問自己,而後面用瀏覽器訪問,相當於拿windows



訪問控制FilesMatch

針對某個文件來做限制。

[root@gary-tao 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf


核心配置文件內容

<Directory /data/wwwroot/www.123.com>

<FilesMatch "admin.php(.*)">

Order deny,allow

Deny from all

Allow from 127.0.0.1

</FilesMatch>

</Directory>


[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl -t //檢測語法

Syntax OK

[root@gary-tao 111.com]# /usr/local/apache2.4/bin/apachectl graceful //加載配置


配置示例圖:

技術分享圖片


實驗結果如下:

技術分享圖片

配置防盜鏈及訪問控制介紹