11.28-31 限定某個目錄禁止解析php, 限制user_agent, php相關配置

訪問控制 – 禁止php解析.

note某個目錄下禁止解析PHP,這個頗有做用,咱們作網站安全的時候,這個用的不少,好比某些目錄能夠上傳文件,爲了不上傳的文件有木馬,因此咱們禁止這個目錄下面的訪問解析PHP。php

1.先禁止解析PHP
<Directory /data/wwwroot/111.com/upload>
        php_admin_flag engine off
  </Directory>

 [root@linux-129 111.com]# curl -x127.0.0.1:80 '111.com/upload/admin.php'
<?php
echo "FilesMatch控制";
?>
2.禁止解析php而且拒絕任何人的訪問
<Directory /data/wwwroot/111.com/upload>
        php_admin_flag engine off
 <filesmatch "(.*)php">
        	Order deny,allow
        	Deny from all 
   	 </filesmatch>
    </Directory>
[root@linux-129 111.com]# curl -x127.0.0.1:80 '111.com/upload/admin.php' -I
HTTP/1.1 403 Forbidden
Date: Mon, 09 Apr 2018 09:27:33 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

訪問控制 – user_agent

有時候咱們的網站會受到攻擊,好比:CC攻擊
大概原理,攻擊的人他經過一些手段,軟件或者肉雞
什麼是肉雞
把別人的服務器黑了,拿到權限去控制它,若是想攻擊一個網站的時候,他能夠取控制肉雞去訪問這個網站,若是10000個肉雞去訪問網站,一半網站服務器併發不會很高,10000臺去訪問,他們的帶寬,數據庫都受不了。
CC攻擊有一個很是規矩的特色,他的referer是同樣的,user_agent也是同樣html

這樣咱們能夠經過限制user_agent來減輕服務器壓力linux

核心配置文件內容   
   <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_USER_AGENT}  .*curl.* [NC,OR]
        RewriteCond %{HTTP_USER_AGENT}  .*baidu.com.* [NC]
        RewriteRule  .*  -  [F]
    </IfModule>

curl -A "123123" 指定user_agent
解釋:shell

OR 或者的意思
NC表示忽略大小寫
F forbidden的意思 禁止訪問

curl的用法
-x  指定目標IP:目標端口
-u 後面是指定用戶和密碼
-I  不顯示內容,只顯示狀態碼
-e 指定 referer  -e  」http://www.123.com「 必須是http://開頭
-A 指定user_agent(瀏覽器的標示)curl -A 「123123」

PHP相關配置

查看php配置文件位置數據庫

/usr/local/php/bin/php -i|grep -i "loaded configuration file"
• 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   \\禁掉一些危險的函數,你如一句話木馬用到的函數eval,

• display_errors  on   \\修改爲off,防止錯誤信息顯示在頁面上。

• log_errors on   \\打開錯誤日誌功能

• error_log=/tmp/php_errors.log    \\定義錯誤日誌的路徑

• error_reporting  \\定義錯誤日誌級別,默認是all ,生成環境用第二個

• open_basedir  \\安全相關參數,用來限定全部網站的目錄,可是一臺服務器有不少站點,而它們的目錄是同一個,若是用open_dasedir限定在一個目錄下面,那這個目錄下面的網站均可以來去自如的運行,這個就和咱們要的效果不同了,因此咱們能夠在虛擬主機配置文件中去配置

• php_admin_value open_basedir "/data/wwwroot/111.com:/tmp/"   \\針對不一樣的虛擬主機限制不一樣的open_basedir在每個虛擬主機配置中能夠加入這個配置,將它們的站點目錄都限制在指定的目錄下面;
[root@linux-129 111.com]# ls /tmp/php_errors.log
/tmp/php_errors.log
[root@linux-129 111.com]# cat !$
cat /tmp/php_errors.log
[10-Apr-2018 12:23:08 Asia/shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/index.php on line 2

[root@linux-129 111.com]# ls -l !$
ls -l /tmp/php_errors.log
-rw-r--r-- 1 daemon daemon 145 4月  10 12:23 /tmp/php_errors.log

它的屬主和屬組都是daemon
發現這個daemon有apache進程身份去生成的
apache

有時候咱們發現定義了錯誤日誌,結果這個錯誤日誌始終沒有生成,咱們就要去檢查這個定義錯誤日誌的目錄到底有沒有寫權限,這個寫文件的是apache的啓動用戶daemon
爲了保險起見,咱們定了一個errors_log
咱們徹底能夠將這個文件建立好,而後作一個777權限vim

[root@linux-129 111.com]# vim /data/wwwroot/111.com/2.php  \\編輯一個錯誤的php
[root@linux-129 111.com]# curl -x127.0.0.1:80 111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Tue, 10 Apr 2018 04:35:17 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Connection: close
Content-Type: text/html; charset=UTF-8
[root@linux-129 111.com]# cat /tmp/php_errors.log
[10-Apr-2018 12:23:08 Asia/shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/index.php on line 2
[10-Apr-2018 12:35:17 Asia/shanghai] PHP Parse error:  syntax error, unexpected end of file in /data/wwwroot/111.com/2.php on line 4
syntax error:語法錯誤
[root@linux-129 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.

php_admin_value open_basedir "/data/wwwroot/111.com:/tmp/" \在每個虛擬主機配置中能夠加入這個配置,將它們的站點目錄都限制在指定的目錄下面瀏覽器

php_admin_value 這個參數能夠在虛擬主機配置文件中添加php.ini裏面的一些參數,好比error_log,open_base.dir,error_reporting等等。安全

這裏爲何要設置一個/tmp 呢?
由於默認的臨時文件是在/tmp下,若是/tmp被限制了,它連他的臨時文件都不能寫入進去,它上傳的圖片會先臨時放到/tmp目錄下面,而後慢慢放到要放的目錄下面去。服務器

相關文章
相關標籤/搜索