LAMP環境-限定PHP解析、useragent、PHP相關配置、Apache相關配置

11.28 限定某個目錄禁止php解析

本節內容應用於對靜態文件目錄或可寫的目錄進行優化設置,經過限制解析/訪問權限來避免別惡意攻擊,提升安全性。php

編輯虛擬主機配置文件:html

[root@1 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
    <Directory /data/wwwroot/111.com/upload>
        php_admin_flag engine off
    </Directory>

建立相應的目錄:linux

[root@1 111.com]# mkdir upload
……
[root@1 111.com]# ls upload/
123.php  abc.jpg  baidu.png

測試:

[root@1 111.com]# curl -x192.168.8.131:80 'http://111.com/upload/123.php' 
<?php
echo "welcom to 123file";
?>

[root@1 111.com]# curl -x192.168.8.131:80 'http://111.com/upload/baidu.png' -I
HTTP/1.1 200 OK
Date: Thu, 03 Aug 2017 04:47:16 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Thu, 03 Aug 2017 04:25:26 GMT
ETag: "e7a-555d1c5172a6c"
Accept-Ranges: bytes
Content-Length: 3706
Content-Type: image/png

說明: 在此訪問123.php文件時直接顯示源代碼,即沒法進行PHP解析,訪問其餘類型的文件沒問題。web

添加PHP訪問限制

添加參數「< FilesMatch (.*)\ .php(. *) > 」:shell

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

    <Directory /data/wwwroot/111.com/upload>
        php_admin_flag engine off
        <FilesMatch (.*)\.php(.*)>
        Order Allow,Deny
        Deny from all
        </FilesMatch>
    </Directory>

說明: 若是隻設置禁止PHP解析,用戶訪問PHP文件時會顯示源代碼,添加該參數能夠避免用戶看到服務器PHP源碼,進一步提高安全性。apache

測試:

[root@1 111.com]# curl -x127.0.0.1:80 111.com/upload/123.php -I
HTTP/1.1 403 Forbidden
Date: Thu, 03 Aug 2017 04:28:49 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

[root@1 111.com]# curl -x127.0.0.1:80 111.com/upload/baidu.png -I
HTTP/1.1 200 OK
Date: Thu, 03 Aug 2017 04:29:25 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Thu, 03 Aug 2017 04:25:26 GMT
ETag: "e7a-555d1c5172a6c"
Accept-Ranges: bytes
Content-Length: 3706
Content-Type: image/png

說明: 此時訪問123.php的狀態碼爲403,即沒法訪問!vim

11.29 限制user_agent

user_agent(用戶代理):是指瀏覽器(搜索引擎)的信息包括硬件平臺、系統軟件、應用軟件和用戶我的偏好。瀏覽器

需求背景:
有時候網站受到CC攻擊,其原理是:攻擊者藉助代理服務器(肉機)生成指向受害主機的合法請求,實現DDOS和假裝。CC攻擊的一個特色就是其useragent是一致的,因此,能夠經過限制攻擊者useragent的方法來阻斷其攻擊。安全

編輯虛擬主機配置文件:服務器

[root@1 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
……
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_USER_AGENT}  .*curl.* [NC,OR]
        RewriteCond %{HTTP_USER_AGENT}  .*baidu.com.* [NC]
        RewriteRule  .*  -  [F]
    </IfModule>
……

說明: NC表示忽略大小寫,OR選項表示或者(不加任何選項表而且)鏈接下一個條件,F=forbidden禁止。

檢測:

[root@1 111.com]# curl -x192.168.8.131:80 'http://111.com/123.php' -I
HTTP/1.1 403 Forbidden
Date: Thu, 03 Aug 2017 06:59:14 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

[root@1 111.com]# curl -A "aminglinux aminglinux" -x192.168.8.131:80 'http://111.com/123.php' -I
HTTP/1.1 200 OK
Date: Thu, 03 Aug 2017 07:01:01 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8
[root@1 111.com]# curl -A "aminglinux aminglinux" -x192.168.8.131:80 'http://111.com/123.php' 
welcom to 123file

說明: curl -A 指定useragent。

11.30 PHP相關配置

查看PHP配置文件:

/usr/local/php/bin/php -i|grep -i "loaded configuration file"

PHP參數

  1. 設定時區
date.timezone
  1. 一些功能選項:

「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 」
以上功能選項能夠經過「disable_function」來限制,以達到提升網站安全性的目的:

disable_function=
  1. 日誌相關
  • display_errors=On/Off :設定是否顯示錯誤緣由,須要注意的是,此處設置爲off(防止用戶看到)後必須設置錯誤日誌,設定保存路徑,和錯誤日誌級別,不然將沒法查找錯誤緣由 。

  • log_errors=On/Off 開啓/關閉錯誤日誌

  • 「error_log=/tmp/」 設定錯誤日誌的保存路徑。若是定義好路徑後沒法生產日誌,此時須要檢查日誌文件所在目錄是否有寫(w)權限

  • 「error_reporting =」 設定錯誤日誌級別,級別有:E_ ALL 、~E_ NOTICE 、~E_ STRICT 、~E_DEPRECATED(能夠自由組合)。生產環境使用:E_ ALL & ~E_ NOTICE就能夠。

官方說明:

E_ALL (Show all errors, warnings and notices including coding standards.)
E_ALL & ~E_NOTICE  (Show all errors, except for notices)
E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
  1. 安全參數「open_basedir」
open_basedir, if set, limits all file operations to the defined directory
; and below.  This directive makes most sense if used in a per-directory
; or per-virtualhost web server configuration file.  
譯:若是設置了open_basedir選項,將會把全部關於文件的操做限制在指定目錄及其子目錄。
將該指令設定在每一個目錄或者虛擬主機web服務器配置文件中很是重要。

說明: php.ini文件中的內容是針對全部虛擬主機進行的配置。

問題: 一臺服務器運行着不止一臺虛擬主機,因此在該文件下設置該選項並不合適。那麼,該如何設定該配置呢?

辦法: 分別在每一個虛擬主機的配置文件進行相關設置。

[root@1 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
php_admin_value open_basedir "/data/wwwroot/111.com:/tmp/"

說明: 「php_admin_value」能夠定義php.ini中的參數。使用該辦法分別在每一個虛擬主機設定相關的「open_basedir」便可! 在此開放「/tmp/」目錄是爲了使臨時文件能正常寫入。

相關文章
相關標籤/搜索