第十九課時預習筆記
配置防盜鏈
防盜鏈,就是不讓別人盜用你網站上的資源,這個資源,通常指的是圖片、視訊、歌曲、文件等。
referer的概念
你通過A網站的一個頁面http://a.com/a.html 裡面的連結去訪問B網站的一個頁面http://b.com/b.html ,那麼這個B網站頁面的referer就是http://a.com/a.html。 也就是說,一個referer其實就是一個網址。
1.配置防盜鏈
[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
Syntax OK
[[email protected] 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.測試網頁訪問
在其它網站上鍊接這個網址,還是打不開。
然後在虛擬主機配置檔案裡把第三方站點加入到白名單
[[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在下面圖片上把第三方站點網址加入到白名單,然後儲存退出重新載入配置。
在點選連結http://111.com/qq.png 訪問就可以了,這就是referer,如下圖
3.使用curl測試
[[email protected] 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
[[email protected] 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
[[email protected] 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.修改虛擬主機配置:
[[email protected] 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的訪問。
驗證如下:
[[email protected] 111.com]# mkdir /data/wwwroot/111.com/admin/ //建立admin目錄,模擬網站後臺
[[email protected] 111.com]# ls
123.php admin index.php qq.png
[[email protected] 111.com]# touch /data/wwwroot/111.com/admin/index.php //在後臺目錄下面建立檔案
[[email protected] 111.com]# ls admin/
index.php
[[email protected] 111.com]# vim admin/index.php //並寫入內容
[[email protected] 111.com]# cat admin/index.php
123456789
[[email protected] 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
[[email protected] 111.com]# curl -x127.0.01:80 111.com/admin/index.php
123456789
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful //載入配置
[[email protected] 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>
[[email protected] 111.com]# curl -x127.0.01:80 111.com/admin/index.php
123456789
[[email protected] 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"
[[email protected] 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
[[email protected] 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瀏覽器訪問示例圖
[[email protected] 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
針對某個檔案來做限制。
[[email protected] 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>
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t //檢測語法
Syntax OK
[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful //載入配置
配置示例圖:
限制user_agent
user_agent可以理解為瀏覽器標識 user_agent為什麼可以做訪問控制呢? 背景:比如網站受到cc***。***的人通過軟體或者肉機,想***某個網站的時候,把所有的肉機發動起來,讓它們同時訪問一個站點。但是cc***往往有一個特徵,就是user_agent一致的,訪問地址一致。訪問速度快,每秒N次
核心配置檔案內容 <IfModule mod_rewrite.c> RewriteEngine on
php相關配置
檢視php配置檔案位置 /usr/local/php/bin/php -i|grep -i "loaded configuration file" 或者phpinfo(); 但是有些情況下使用-i是不準的。 如果想找的更準。比如找11.com,那麼可以在11.com的根目錄下建立phpinfo()檔案。瀏覽器訪問它。檢視配置檔案
cp -r /usr/local/src/php-5.6.30/php.ini-production /usr/local/php/etc/php.ini vi /usr/local/php/etc/php.ini //搜尋disable_functions
date.timezone //定義時區 date.timezone =Asia/shanghai
disable_functions //安全函式(禁止掉這些比較危險的函式) eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close 在生產環境中會把phpinfo禁掉。因為有時候不小心寫了一個phpinfo檔案,上傳上去被***發現。能夠看到系統的目錄。不安全 eval:一句話***使用了該函式如果把這個函式禁了,即使上傳了***也沒辦法解析
日誌相關: error_log, log_errors, display_errors, error_reporting
display_errors = On //會把錯誤資訊直接顯示在瀏覽器上
(生產環境該引數改成Off,為了避免暴露資訊)但是有一個問題,就是頁面什麼都不顯示(白頁)。那麼還需要配置錯誤日誌。 log_errors = On //表示錯誤日誌開啟狀態 error_log = /tmp/php_errors.log //定義錯誤日誌所在位置 還需要定義error_log的級別。如果級別很高的話,很嚴謹的話,只會記錄一些比較嚴峻的錯誤,不太嚴峻的就不記錄,比如警告等、所以放鬆些 error_reporting //定義日誌的級別 生產環境使用 E_ALL & ~E_NOTICE (Show all errors, except for notices)
模擬:把phpinfo函式禁掉。 1.phpinfo放入disable_functions裡 2.display_errors = Off 3.log_errors = On 4.error_log = /tmp/php_errors.log 5./usr/local/apache2.4/bin/apachectl -t && graceful 6.ll /tmp/ srwxrwxrwx. 1 mysql mysql 0 12月 11 18:39 mysql.sock -rw-r--r--. 1 daemon daemon 290 12月 11 21:23 php_errors.log 7.cat /tmp/php_errors.log [11-Dec-2017 21:23:49 Asia/shanghai] PHP Warning: phpinfo() has been disabled for security reasons in /data/wwwroot/discuz2/lsx/1.php on line 2
php_errors.log的屬主實際是httpd的屬主daemon。因為這個日誌是以daemon這個程序的身份建立的。當你定義了一個日誌,但是日誌始終沒有生成,就要看一下定義的目錄有沒寫許可權,而且寫檔案的程序是這個程序。
為了保守起見,可以把這個檔案建立好/tmp/php_errors.log,在授權777許可權 grep error_log /usr/local/php/etc/php.ini error_log = /tmp/php_errors.log ;error_log = syslog