1. 程式人生 > >十一周二次課

十一周二次課

php解析 user_agent php curl

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

因為httpd開放了php模塊,所以如果被人上傳了木馬文件(php類型),httpd就有可能會進行執行,一旦執行,就會讓對方獲得我們服務器的root權限,或者是被惡意刪除或修改一些參數,導致服務器癱瘓或者是被攻擊

    <Directory /home/wwwroot/111.com/img/icon> #禁止解析PHP
        php_admin_flag engine off
    </Directory>   

那麽怎麽配置設置禁止php 解析
核心配置文件內容
<Directory /data/wwwroot/www.123.com/upload>

php_admin_flag engine off
</Directory>
curl測試時直接返回了php源代碼,並未解析
首先編輯虛擬主機配置文件

    #</FilesMatch> 
    #</Directory>

    <Directory /data/wwwroot/111.com>
        <FilesMatch  "admin.php(.*)">
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </FilesMatch>
    </Directory>

    <Directory /data/wwwroot/111.com>
        SetEnvIfNoCase Referer "http://111.com" local_ref
        SetEnvIfNoCase Referer "http://aaa.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>

改為

      #</FilesMatch> 
    #</Directory>
    <Directory /home/wwwroot/111.com/img/icon>
        php_admin_flag engine off
        <FilesMatch (.*)\.php(.*)>
        Order allow,deny
        Deny from all
        </FilesMatch>
    </Directory>
    <Directory /data/wwwroot/111.com>
        <FilesMatch  "admin.php(.*)">
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </FilesMatch>
    </Directory>

    <Directory /data/wwwroot/111.com>
        SetEnvIfNoCase Referer "http://111.com" local_ref
        SetEnvIfNoCase Referer "http://aaa.com" local_ref
        SetEnvIfNoCase Referer "^$" local_ref
:wq       

檢查語法,重新加載配置

[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost ~]# 

[root@localhost ~]# cd /data/wwwroot/111.com
[root@localhost 111.com]# ls
123.php  admin  index.php  qq.png
[root@localhost 111.com]# mkdir upload
[root@localhost 111.com]# ls
123.php  admin  index.php  qq.png  upload
[root@localhost 111.com]# cp 123.php upload/

[root@localhost 111.com]# !curl
curl -x127.0.0.1:80 ‘http://111.com/admin.php?/alsjdf‘ -I
HTTP/1.1 404 Not Found
Date: Thu, 12 Oct 2017 12:41:28 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

再來訪問下

[root@localhost 111.com]# curl -x127.0.0.1:80 ‘http://111.com/img/icon/123.php‘ -I
HTTP/1.1 403 Forbidden
Date: Thu, 12 Oct 2017 12:42:49 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[root@localhost 111.com]# curl -x127.0.0.1:80 ‘http://111.com/img/icon/123.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 /upload/123.php
on this server.<br />
</p>
</body></html>
[root@localhost 111.com]# 

先把filesmatch 註釋掉

      #</FilesMatch> 
    #</Directory>
    <Directory /home/wwwroot/111.com/img/icon>
        php_admin_flag engine off
        #<FilesMatch (.*)\.php(.*)>
        #Order allow,deny
        #Deny from all
        #</FilesMatch>
    </Directory>
    <Directory /data/wwwroot/111.com>
        <FilesMatch  "admin.php(.*)">
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </FilesMatch>
    </Directory>

    <Directory /data/wwwroot/111.com>
        SetEnvIfNoCase Referer "http://111.com" local_ref
        SetEnvIfNoCase Referer "http://aaa.com" local_ref
:wq  

[root@localhost 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost 111.com]# 

再來訪問

[root@localhost 111.com]# !curl
curl -x127.0.0.1:80 ‘http://111.com/img/icon/123.php‘
<?
echo "123.php";
[root@localhost 111.com]# 

這個時候進一步限制它 連讓它訪問的機會都沒有,更別說去解析php了
再次打開配置文件 把剛剛註釋的取消,

   #</Directory>
    <Directory /home/wwwroot/111.com/img/icon>
        php_admin_flag engine off
        <FilesMatch (.*)\.php(.*)>
        Order allow,deny
        Deny from all
        </FilesMatch>

[root@localhost 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost 111.com]# /usr/local/apache2.4/bin/apachectl graceful

再來訪問
直接提示無法訪問403
禁止php解析,是為讓服務器更加安全,尤其是針對可以寫的目錄;可以寫的目錄,一般是不需要解析php,這個需要牢記,一般靜態文件存放的目錄是不允許解析php 的

11.29 限制user_agent

首先打開虛擬主機配置文件

   #<Directory /home/wwwroot/111.com>
    # <FilesMatch 123.php>    
    #   AllowOverride AuthConfig 
    #   AuthName "111.com user auth" 
    #   AuthType Basic 
    #   AuthUserFile /data/.htpasswd 
    #   require valid-user
    #</FilesMatch> 
    #</Directory>

    <Directory /home/wwwroot/111.com/img/icon>
        php_admin_flag engine off
        <FilesMatch (.*)\.php(.*)>
        Order allow,deny
        Deny from all
        </FilesMatch>
    </Directory>
    <Directory /home/wwwroot/111.com>
        <FilesMatch  "admin.php(.*)">
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </FilesMatch>
    </Directory>

 插入 --                                                             44,5          61%

添加配置文件後,然後 檢查配置文件,重新加載配置文件

    #<Directory /home/wwwroot/111.com>
    # <FilesMatch 123.php>    
    #   AllowOverride AuthConfig 
    #   AuthName "111.com user auth" 
    #   AuthType Basic 
    #   AuthUserFile /data/.htpasswd 
    #   require valid-user
    #</FilesMatch> 
    #</Directory>
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_USER_AGENT}  .*curl.* [NC,OR]
        RewriteCond %{HTTP_USER_AGENT}  .*baidu.com.* [NC]
        RewriteRule  .*  -  [F]
    </IfModule>
    <Directory /home/wwwroot/111.com/img/icon>
        php_admin_flag engine off
        <FilesMatch (.*)\.php(.*)>
        Order allow,deny
        Deny from all
        </FilesMatch>
    </Directory>
    <Directory /home/wwwroot/111.com>
        <FilesMatch  "admin.php(.*)">
        Order deny,allow
:wq 

[root@localhost 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@localhost 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost 111.com]# 

再來訪問下

[root@localhost 111.com]# !curl
curl -x127.0.0.1:80 ‘http://111.com/img/icon/123.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 /upload/123.php
on this server.<br />
</p>
</body></html>
[root@localhost 111.com]# curl -x127.0.0.1:80 ‘http://111.com/img/icon/123.php‘ -I
HTTP/1.1 403 Forbidden
Date: Thu, 12 Oct 2017 13:41:04 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[root@localhost 111.com]# 

[root@localhost 111.com]# curl -x127.0.0.1:80 ‘http://111.com/123.php‘ -I
HTTP/1.1 403 Forbidden
Date: Thu, 12 Oct 2017 13:41:49 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[root@localhost 111.com]# 

查看下日誌文件

[root@localhost 111.com]# tail /usr/local/apache2.4/logs/123.com-access_20171012.log
192.168.0.190 - - [12/Oct/2017:20:51:50 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://111.com/123.php" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:20:54:14 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:20:54:16 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:20:54:29 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:21:22:31 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:21:22:32 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:21:22:34 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
127.0.0.1 - - [12/Oct/2017:21:40:54 +0800] "GET http://111.com/img/icon/123.php HTTP/1.1" 403 223 "-" "curl/7.29.0"
127.0.0.1 - - [12/Oct/2017:21:41:04 +0800] "HEAD http://111.com/img/icon/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [12/Oct/2017:21:41:49 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
[root@localhost 111.com]# 

再來試下
curl -A "aiker aiker" -x127.0.0.1:80 ‘http://111.com/123.php‘ -I 可以crul -A 可以指定user_agent
curl -e "http://" 也可以指定Referer
curl -x指定,
crul -I 僅僅是查看它的狀態碼

[root@localhost 111.com]# curl -A "aiker aiker" -x127.0.0.1:80 ‘http://111.com/123.php‘ -I
HTTP/1.1 200 OK
Date: Thu, 12 Oct 2017 13:47:03 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8

[root@localhost 111.com]# curl -A "aiker aiker" -x127.0.0.1:80 ‘http://111.com/123.php‘
123.php
[root@localhost 111.com]# 
[root@localhost 111.com]# 
[root@localhost 111.com]# 

來看看訪問日誌 user_agent 是"aiker aiker"

[root@localhost 111.com]# tail /usr/local/apache2.4/logs/123.com-access_20171012.log
192.168.0.190 - - [12/Oct/2017:20:54:16 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:20:54:29 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:21:22:31 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:21:22:32 +0800] "GET /upload/123.php HTTP/1.1" 403 223 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
192.168.0.190 - - [12/Oct/2017:21:22:34 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
127.0.0.1 - - [12/Oct/2017:21:40:54 +0800] "GET http://111.com/img/icon/123.php HTTP/1.1" 403 223 "-" "curl/7.29.0"
127.0.0.1 - - [12/Oct/2017:21:41:04 +0800] "HEAD http://111.com/img/icon/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [12/Oct/2017:21:41:49 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [12/Oct/2017:21:47:03 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "aiker aiker"
127.0.0.1 - - [12/Oct/2017:21:47:19 +0800] "GET http://111.com/123.php HTTP/1.1" 200 7 "-" "aiker aiker"
[root@localhost 111.com]# 

11.30/11.31 php相關配置

11.30 PHP相關配置(上)

查看php配置文件位置

/usr/local/php/bin/php -i|grep -i "loaded configuration file"
date.timezone
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
error_log, log_errors, display_errors, error_reporting
open_basedir
php_admin_value open_basedir "/home/wwwroot/111.com:/tmp/"

列出111.com 目錄下文件目錄 修改inidex.php內容

[root@localhost 111.com]# ls
123.php  admin  index.php  qq.png  upload
[root@localhost 111.com]# vi index.php

<?php
echo "111.com";
~                                                                                        
~                                                                                        

~                                                                                        
~                                                                                        
"index.php" 2L, 22C

修改為

[root@localhost 111.com]# vi index.php

<?php
phpinfo();
~                                                                                        
~                                                                                        
~                                                                                        

:wq

去php包下面拷貝一個文件php.ini-development 到/usr/local/php7/etc/php.ini

[root@localhost 111.com]# cd /usr/local/src/php-7.2.1/
[root@localhost php-7.2.1]# cp php.ini-
php.ini-development  php.ini-production   
[root@localhost php-7.2.1]# cp php.ini-development /usr/local/php7/etc/php.ini
[root@localhost php-7.2.1]# 

重新加載下配置,再去windows瀏覽器裏刷新下看下

[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful

打開配置文件vim /usr/local/php7/etc/php.ini 搜索disable_functions

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini

[PHP]

;;;;;;;;;;;;;;;;;;;
; About php.ini   ;
;;;;;;;;;;;;;;;;;;;
; PHP‘s initialization file, generally called php.ini, is responsible for
; configuring many of the aspects of PHP‘s behavior.

; PHP attempts to find and load this configuration from a number of locations.
; The following is a summary of its search order:
; 1. SAPI module specific location.
; 2. The PHPRC environment variable. (As of PHP 5.2.0)
; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0)
; 4. Current working directory (except CLI)
; 5. The web server‘s directory (for SAPI modules), or directory of PHP
; (otherwise in Windows)
; 6. The directory from the --with-config-file-path compile time option, or the
; Windows directory (C:\windows or C:\winnt)
; See the PHP docs for more specific information.
; http://php.net/configuration.file

; The syntax of the file is extremely simple.  Whitespace and lines
; beginning with a semicolon are silently ignored (as you probably guessed).
; Section headers (e.g. [Foo]) are also silently ignored, even though
; they might mean something in the future.

; Directives following the section heading [PATH=/www/mysite] only
; apply to PHP files in the /www/mysite directory.  Directives
; following the section heading [HOST=www.example.com] only apply to
; PHP files served from www.example.com.  Directives set in these
; If -1 is used, then dtoa mode 0 is used which automatically select the best
; precision.
serialize_precision = -1

; 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.
; http://php.net/open-basedir
;open_basedir =

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names.
; http://php.net/disable-functions
disable_functions =

; This directive allows you to disable certain classes for security reasons.
; It receives a comma-delimited list of class names.
; http://php.net/disable-classes
disable_classes =

; Colors for Syntax Highlighting mode.  Anything that‘s acceptable in
; <span style="color: ???????"> would work.
; http://php.net/syntax-highlighting
;highlight.string  = #DD0000
;highlight.comment = #FF9900
;highlight.keyword = #007700
;highlight.default = #0000BB
                                                                       314,1         15%

默認這個是空的disable_functions =
我們把所有的函數都禁掉

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names.
; http://php.net/disable-functions
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

; This directive allows you to disable certain classes for security reasons.
; It receives a comma-delimited list of class names.
; http://php.net/disable-classes
disable_classes =

; Colors for Syntax Highlighting mode.  Anything that‘s acceptable in
; <span style="color: ???????"> would work.
; http://php.net/syntax-highlighting
;highlight.string  = #DD0000
:wq      

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost php-7.2.1]# 

當然我們會使用它這個phpinfo,打開配置文件把phpinfo 去掉

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names.
; http://php.net/disable-functions
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

; This directive allows you to disable certain classes for security reasons.
; It receives a comma-delimited list of class names.
; http://php.net/disable-classes
disable_classes =

; Colors for Syntax Highlighting mode.  Anything that‘s acceptable in
; <span style="color: ???????"> would work.
; http://php.net/syntax-highlighting
;highlight.string  = #DD0000
;highlight.comment = #FF9900
;highlight.keyword = #007700
:wq     

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost php-7.2.1]# 

第二個date.timezone,打開php配置文件 搜素timezone

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini

;extension=php_tidy.dll
;extension=php_xmlrpc.dll
;extension=php_xsl.dll

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;

[CLI Server]
; Whether the CLI web server uses ANSI color coding in its terminal output.
cli_server.color = On

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

; http://php.net/date.default-latitude
;date.default_latitude = 31.7667

; http://php.net/date.default-longitude
;date.default_longitude = 35.2333

; http://php.net/date.sunrise-zenith
;date.sunrise_zenith = 90.583333

; http://php.net/date.sunset-zenith
                                                                       937,23        48%

定義;date.timezone = Asia/Chongqing
再把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
搜索display 把display_errors = On 改成Off 也就是說 我不需要把這些錯誤信息輸出到瀏覽器裏

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost php-7.2.1]# 

使用curl

[root@localhost php-7.2.1]# curl -x127.0.0.1:80 http://111.com/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 /index.php
on this server.<br />
</p>
</body></html>

還是403,是因為設了user_agent

[root@localhost php-7.2.1]# curl -A "a" -x127.0.0.1:80 http://111.com/index.php -I
HTTP/1.1 200 OK
Date: Thu, 12 Oct 2017 14:31:51 GMT
Server: Apache/2.4.29 (Unix) PHP/7.2.1
X-Powered-By: PHP/7.2.1
Content-Type: text/html; charset=UTF-8

[root@localhost php-7.2.1]# 

這樣是可以了,只不過他沒有任何的輸出,這個就不正常了,不是我們想要的,我們不知道它哪裏有問題,一切都是未知的,這個時候需要配置一個錯誤日誌
打開配置文件 搜索error_log

; Log errors to specified file. PHP‘s default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog

定義error_log 的日誌路徑 ,還要配置 它的級別,如果你定義的級別很高的話,它僅僅會記錄一些比較嚴峻的錯誤,一些不太嚴峻的錯誤,他就不計,像警告的不計,不計我也不知道錯誤在哪,所以可以把它搞得稍微放松一些,不要那麽嚴謹

error_log = /tmp/php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog

搜索error_reporting
error_reporting = E_ALL這個是最不嚴謹的,在生產環境當中,我們用E_ALL & ~E_NOTICE (Show all errors, except for notices) 因為在生產環境當中這個notice出現頻率很高的

; Common Values:
;   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)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL

再來用curl訪問下 ,生成了php_errors.log

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful

[root@localhost php-7.2.1]# curl -A "a" -x127.0.0.1:80 http://111.com/index.php
[root@localhost php-7.2.1]# ls /tmp/
ks-script-sk5n23
mysql.sock
pear
php_errors.log
systemd-private-40d73240fa4b483bb2b7ae3d299e980d-vmtoolsd.service-w87bfr
yum.log
[root@localhost php-7.2.1]# 

可以看下它的屬主屬組是誰,是daemon,daemon是httpd 的屬主
這個日誌實際上是以這個進程的身份去生成的

[root@localhost php-7.2.1]# ls -l /tmp/php_errors.log 
-rw-r--r--. 1 daemon daemon 135 10月 12 22:44 /tmp/php_errors.log
[root@localhost php-7.2.1]# 

[root@localhost php-7.2.1]# ps aux |grep httpd
root       2335  0.0  1.3 258884 13600 ?        Ss   20:36   0:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon     3636  0.0  1.4 678896 14644 ?        Sl   22:43   0:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon     3637  0.0  1.0 545712 10400 ?        Sl   22:43   0:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon     3638  0.0  1.0 545712 10400 ?        Sl   22:43   0:00 /usr/local/apache2.4/bin/httpd -k graceful
root       3727  0.0  0.0 112680   976 pts/0    S+   22:46   0:00 grep --color=auto http
[root@localhost php-7.2.1]# 

[root@localhost php-7.2.1]# grep error_log /usr/local/php7/etc/php.ini
; server-specific log, STDERR, or a location specified by the error_log
; Set maximum length of log_errors. In error_log information about the source is
error_log = /tmp/php_errors.log
;error_log = syslog
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=
[root@localhost php-7.2.1]# 

[root@localhost php-7.2.1]# touch /tmp/php_errors.log ; chmod 777 /tmp/php_errors.log ^C
[root@localhost php-7.2.1]# cat /tmp/php_errors.log 
[12-Oct-2017 14:44:09 UTC] PHP Warning:  phpinfo() has been disabled for security reasons in /home/wwwroot/111.com/index.php on line 2
[root@localhost php-7.2.1]# 

phpinfo() has been disabled for security reasons 處於安全的原因把這個phpinfo 函數禁掉了

來模擬一個錯誤

[root@localhost php-7.2.1]# vim /home/wwwroot/111.com/2.php

<?php
echo 123;
alksdkdkdlldldldd
~                                                                                        
~                                                                                        

~                                                                                        
:wq    

[root@localhost php-7.2.1]# vim /home/wwwroot/111.com/2.php
[root@localhost php-7.2.1]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Thu, 12 Oct 2017 14:54:10 GMT
Server: Apache/2.4.29 (Unix) PHP/7.2.1
X-Powered-By: PHP/7.2.1
Connection: close
Content-Type: text/html; charset=UTF-8

[root@localhost php-7.2.1]# 

可以看看它的錯誤日誌 結果是 syntax error
這個日誌級別就比上面的高級了 一個是Warning ,一個是error,error 肯定比較嚴謹,很嚴重

[root@localhost php-7.2.1]# !cat
cat /tmp/php_errors.log 
[12-Oct-2017 14:44:09 UTC] PHP Warning:  phpinfo() has been disabled for security reasons in /home/wwwroot/111.com/index.php on line 2
[12-Oct-2017 14:54:10 UTC] PHP Parse error:  syntax error, unexpected end of file in /home/wwwroot/111.com/2.php on line 4
[root@localhost php-7.2.1]# 

有時候,定義了一個錯誤日誌,但是這個錯誤日誌始終沒有生成,那麽就需要檢查一下定義錯誤日誌所在的目錄,到底httpd有沒有寫權限,
最保險的辦法,就是在所在目錄創建一個錯誤日誌的文件,然後賦予它777的權限,這樣就不需要擔心這個文件httpd是否有寫權限了

前面是一些安全相關的函數,下面一個是怎麽樣去打開 調試 錯誤日誌的,因為排查一個問題沒有錯誤日誌是不行的

11.31 PHP相關配置(下)

下面來介紹一個安全相關的參數
open_basedir
php_admin_value open_basedir "/home/wwwroot/111.com:/tmp/"
安全相關的參數
一臺服務器上,運行了多個站點,有一臺服務器假如代碼有問題,結果這個站點被黑客攻擊了,被黑客拿到了權限,黑客拿了權限肯定會繼續往裏滲透,繼續往裏滲透,就會有可能滲透到其他的站點,同時導致其他的站點被黑
open_basedir 限制不能串崗
open_basedir = /data/wwwroot/1111.com:/tmp
這裏配置 /tmp的目的是因為,打開任何文件的時候都會產生一個緩存文件,如果不允許/tmp的話會導致任何站點都沒有辦法訪問

打開php配置文件,搜索open_basedir

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini

; 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.
; http://php.net/open-basedir
;open_basedir =

定義 open_basedir = /home/wwwroot/111.com:/tmp
假如故意寫錯,現在 open_basedir = /data/wwwroot/1111.com:/tmp

open_basedir = /data/wwwroot/1111.com:/tmp

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names.
; http://php.net/disable-functions
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

:wq  

訪問下

[root@localhost php-7.2.1]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Thu, 12 Oct 2017 15:10:58 GMT
Server: Apache/2.4.29 (Unix) PHP/7.2.1
X-Powered-By: PHP/7.2.1
Connection: close
Content-Type: text/html; charset=UTF-8

把2.php改正,同樣還是錯誤500

[root@localhost php-7.2.1]# vi /home/wwwroot/111.com/2.php

<?php
echo 123;
alksdkdkdlldldldd
~                                                                                        
~                                                                                        
~          
[root@localhost php-7.2.1]# vi /home/wwwroot/111.com/2.php

改正了
<?php
echo 123;
~                                                                                        
~                                                                                        

~                                                                                        
~                                                                                        
:wq

[root@localhost php-7.2.1]# vi /home/wwwroot/111.com/2.php
[root@localhost php-7.2.1]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php -I
HTTP/1.0 500 Internal Server Error
Date: Thu, 12 Oct 2017 15:13:37 GMT
Server: Apache/2.4.29 (Unix) PHP/7.2.1
X-Powered-By: PHP/7.2.1
Connection: close
Content-Type: text/html; charset=UTF-8

[root@localhost php-7.2.1]# 

看看它的錯誤輸出 /home/wwwroot/111.com/2.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0 2.php並沒有在運行的目錄下,所以它才是把報錯500

[root@localhost php-7.2.1]# !cat
cat /tmp/php_errors.log 
[12-Oct-2017 14:44:09 UTC] PHP Warning:  phpinfo() has been disabled for security reasons in /home/wwwroot/111.com/index.php on line 2
[12-Oct-2017 14:54:10 UTC] PHP Parse error:  syntax error, unexpected end of file in /home/wwwroot/111.com/2.php on line 4
[12-Oct-2017 15:10:58 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/home/wwwroot/111.com/2.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[12-Oct-2017 15:10:58 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[12-Oct-2017 15:10:58 UTC] PHP Fatal error:  Unknown: Failed opening required ‘/home/wwwroot/111.com/2.php‘ (include_path=‘.:/usr/local/php7/lib/php‘) in Unknown on line 0
[12-Oct-2017 15:13:37 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/home/wwwroot/111.com/2.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0
[12-Oct-2017 15:13:37 UTC] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[12-Oct-2017 15:13:37 UTC] PHP Fatal error:  Unknown: Failed opening required ‘/home/wwwroot/111.com/2.php‘ (include_path=‘.:/usr/local/php7/lib/php‘) in Unknown on line 0
[root@localhost php-7.2.1]# 

現在進入php配置文件 把它改成 改到我們這個目錄下

open_basedir = /home/wwwroot/111.com:/tmp

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names.
; http://php.net/disable-functions
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

; This directive allows you to disable certain classes for security reasons.
; It receives a comma-delimited list of class names.
; http://php.net/disable-classes
disable_classes =
:wq

[root@localhost php-7.2.1]# vim /usr/local/php7/etc/php.ini
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost php-7.2.1]# curl -A "a" -x127.0.0.1:80 http://111.com/2.php
123[root@localhost php-7.2.1]# 

這個時候就不會報錯,就可以訪問

但是改php.ini呢,有點問題,如果這個服務器上跑了N多個站點,怎麽去做限制呢?你的網站全部再/wwwroot/目錄下 ,限定在這個級別下,這又有何用呢?這個目錄下所有的網站,他都可以來去自如,不合適,那怎麽樣才合適,你應該針對這些站點,針對這些網站 針對他們去做open_basedir,咱們php.ini是做不到的,因為php.ini 是針對所有站點的,
但是還有一個方法,去apache虛擬主機配置文件裏去做
進入配置文件,改回來

; http://php.net/open-basedir
open_basedir = 

; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names.
; http://php.net/disable-functions
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

; This directive allows you to disable certain classes for security reasons.
; It receives a comma-delimited list of class names.
; http://php.net/disable-classes
disable_classes =
:wq       

進入apache 虛擬主機配置文件

[root@localhost php-7.2.1]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com www.123.com
    php_admin_value open_basedir "/data/wwwroot/abc.com:/tmp/"
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/home/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.example.com 2111.com.cn
    #<Directory /home/wwwroot/111.com>
    # <FilesMatch 123.php>    
    #   AllowOverride AuthConfig 
    #   AuthName "111.com user auth" 
    #   AuthType Basic 
    #   AuthUserFile /data/.htpasswd 
    #   require valid-user
    #</FilesMatch> 
    #</Directory>
    php_admin_value open_basedir "/home/wwwroot/111.com:/tmp/

[root@localhost php-7.2.1]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@localhost php-7.2.1]# /usr/local/apache2.4/bin/apachectl graceful
[root@localhost php-7.2.1]# !curl
curl -A "a" -x127.0.0.1:80 http://111.com/2.php
123[root@localhost php-7.2.1]# 

這樣就可以了,針對不同的虛擬主機 限制不同的open_basedir

擴展

apache開啟壓縮

這裏的壓縮並不是對網站的圖片壓縮,而是對普通的靜態文件,諸如html, js, css 等元素壓縮。不要小看這個壓縮功能,如果一個網站的請求量很大的話,這樣可以節省海量帶寬,在我國帶寬資源非常昂貴,所以小小的一個壓縮功能可以為企業節省不少的成本呢!下面就來看看如何配置它?

首先,需要看一下我們的apache是否支持壓縮功能。
/usr/local/apache2/bin/apachectl -l
看看是否有mod_deflate
如果這裏沒有,那繼續看一下
ls /usr/local/apache2/modules/
下面有沒有 mod_deflate.so 這個文件

如果這裏也沒有,那說明你的apache不支持壓縮,需要重編譯一下,或者擴展形式安裝,或者重新編譯apache, 需要在編譯的時候,加上 --enable-deflate=shared

好,如果你的apache有了deflate這個模塊支持,也就支持了壓縮功能。

下面該配置httpd.conf 了。
在httpd.conf 中增加 :

LoadModule deflate_module modules/mod_deflate.so

然後再增加如下配置:

DeflateCompressionLevel 5
AddOutputFilterByType DEFLATE text/html text/plain text/xml 
AddOutputFilter DEFLATE js css

其中DeflateCompressionLevel 是指壓縮程度的等級,從1到9,9是最高等級。

apache2.2到2.4配置文件變更

指令控制了在特定目錄中將使用哪些服務器特性。Options屬性有一個非常特別的功能: 如果你沒有用“+”或者“-”來增加或者減少一個功能的時候,每個之前定義的Options的所有功能都會被取消, 直到你又為它指定一些功能。所以options屬性在整體設置和虛擬主機設置的是不相關的, 互相不起作用,因為他們在特定的範圍內被重載了。 如果要在虛擬主機裏面使用在整體設置中的Options的設置, 那麽就不要在虛擬主機設置中指定Options屬性。如果要增加或者減少功能, 那麽用“+”或者“-”符號來實 Options 指令控制了在特定目錄中將使用哪些服務器特性。 可選項能設置為 None ,在這種情況下,將不啟用任何額外特性。或設置為以下選項中的一個或多個:
All 除MultiViews之外的所有特性。這是默認設置。
ExecCGI 允許執行CGI腳本.
FollowSymLinks 服務器會在此目錄中使用符號連接。 註意:即便服務器會使用符號連接,但它不會改變用於匹配配置段的路徑名。 如果此配置位於配置段中,則此設置會被忽略。
Includes 允許服務器端包含。
IncludesNOEXEC 允許服務器端包含,但禁用#exec命令和#exec CGI。但仍可以從ScriptAliase目錄使用#include 虛擬CGI腳本。
Indexes 如果一個映射到目錄的URL被請求,而此目錄中又沒有DirectoryIndex(例如:index.html)那麽服務器會返回一個格式化後的目錄 列表。
MultiViews 允許內容協商的多重視圖。
SymLinksIfOwnerMatch 服務器僅在符號連接與其目的目錄或文件擁有者具有同樣的用戶id時才使用它。 註意:如果此配置出現在配置段中,此選項將被忽略。 一般來說,如果一個目錄被多次設置了 Options ,則最特殊的一個會被完全接受,而各個可選項的設定彼此並不融合。然而,如果所有施用於 Options 指令的可選項前都加有+或-符號,此可選項將被合並。所有前面加有+號的可選項將強制覆蓋當前可選項設置,而所有前面有-號的可選項將強制從當前可選項設置中去除。
比如說,沒有任何+和-符號:

Options Indexes FollowSymLinks
Options Includes  

則只有 Includes 設置到/web/docs/spec目錄上。
然而如果第二個 Options 指令使用了+和-符號:

Options Indexes FollowSymLinks
Options +Includes -Indexes

那麽就會有 FollowSymLinks 和 Includes 設置到/web/docs/spec目錄上。

apache options參數

  1. 訪問控制
    2.2 的時候
    Order deny,allow
    Deny from all

    在 2.4 需要改成

    Require all denied

常用的配置有:

Require all denied   
Require all granted   
Require host xxx.com   
Require ip 192.168.1 192.168.2   
Require local
  1. RewriteLogLevel 變為:logLevel
    如,LogLevel warn rewrite: warn

  2. Namevirtualhost 被移除

  3. 網站壓縮,除了使用mod_deflate,還要mod_filter
    使用ssl,除了使用mod_ssl,還需要mod_socache_shmcb

    apache禁止trace或track防止xss

    TRACE和TRACK是用來調試web服務器連接的HTTP方式。
    支持該方式的服務器存在跨站腳本漏洞,通常在描述各種瀏覽器缺陷的時候,把"Cross-Site-Tracing"簡稱為XST。
    攻擊者可以利用此漏洞欺騙合法用戶並得到他們的私人信息。

禁用trace可以使用rewrite功能來實現

RewriteEngine On
RewriteCondi %{REQUEST_METHOD} ^TRACE
RewriteRule .* - [F]

或者還可以直接在apache的配置文件中配置相應參數

TraceEnable off

apache 配置https 支持ssl

  1. 安裝openssl
    apache2.0 建議安裝0.9版本,我曾經試過2.0.59 對openssl-1.0編譯不過去
    下載Openssl:http://www.openssl.org/source/

       tar -zxf openssl-0.9.8k.tar.gz    //解壓安裝包   
       cd openssl-0.9.8k                 //進入已經解壓的安裝包   
       ./config                          //配置安裝。推薦使用默認配置   
       make && make install              //編譯及安裝   

    openssl默認將被安裝到/usr/local/ssl

  2. 讓apache支持ssl,編譯的時候,要指定ssl支持。
    靜態或者動態
    靜態方法即 --enable-ssl=static --with-ssl=/usr/local/ssl
    動態方法 --enable-ssl=shared --with-ssl=/usr/local/ssl
    其中第二種方法會在module/ 目錄下生成 mod_ssl.so 模塊,而靜態不會有,當然第二種方法也需要在httpd.conf 中加入

    LoadModule ssl_module modules/mod_ssl.so   
  3. 1 創建私鑰
    在創建證書請求之前,您需要首先生成服務器證書私鑰文件。
    cd /usr/local/ssl/bin //進入openssl安裝目錄
    openssl genrsa -out server.key 2048 //運行openssl命令,生成2048位長的私鑰server.key文件。如果您需要對 server.key 添加保護密碼,請使用 -des3 擴展命令。Windows環境下不支持加密格式私鑰,Linux環境下使用加密格式私鑰時,每次重啟Apache都需要您輸入該私鑰密碼(例:openssl genrsa -
    des3 -out server.key 2048)。 
    cp server.key   /usr/local/apache/conf/ssl.key/

    3.2 生成證書請求(CSR)文件

    openssl req -new -key server.key -out certreq.csr   
    Country Name:                           //您所在國家的ISO標準代號,中國為CN   
    State or Province Name:                 //您單位所在地省/自治區/直轄市   
    Locality Name:                          //您單位所在地的市/縣/區   
    Organization Name:                      //您單位/機構/企業合法的名稱   
    Organizational Unit Name:               //部門名稱   
    Common Name:                            //通用名,例如:www.itrus.com.cn。此項必須與您訪問提供SSL服務的服務器時所應用的域名完全匹配。   
    Email Address:                          //您的郵件地址,不必輸入,直接回車跳過   
    "extra"attributes                        //以下信息不必輸入,回車跳過直到命令執行完畢。 

3.3 備份私鑰並提交證書請求
請將證書請求文件certreq.csr提交給天威誠信,並備份保存證書私鑰文件server.key,等待證書的簽發。服務器證書密鑰對必須配對使用,私鑰文件丟失將導致證書不可用。

4.安裝證書
4.1 獲取服務器證書中級CA證書
為保障服務器證書在客戶端的兼容性,服務器證書需要安裝兩張中級CA證書(不同品牌證書,可能只有一張中級證書)。
從郵件中獲取中級CA證書:
將證書簽發郵件中的從BEGIN到 END結束的兩張中級CA證書內容(包括“-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”)粘貼到同一個記事本等文本編輯器中,中間用回車換行分隔。修改文件擴展名,保存為conf/ssl.crt/intermediatebundle.crt文件(如果只有一張中級證書,則只需要保存並安裝一張中級證書)。
4.2 獲取EV服務器證書
將證書簽發郵件中的從BEGIN到 END結束的服務器證書內容(包括“-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”) 粘貼到記事本等文本編輯器中,保存為ssl.crt/server.crt文件

4.3 apache的配置 2.0的配置
httpd.conf 中增加

Listen  443
NameVirtualHost *:443

    DocumentRoot "/data/web/www"
    ServerName aaa.com:443
    ErrorLog "logs/error.log"
    CustomLog "logs/access.log" combined

     SSLEngine on
     SSLCertificateFile /usr/local/apache/conf/ssl.crt/server.crt
     SSLCertificateKeyFile /usr/local/apache/conf/ssl.key/server.key
     SSLCertificateChainFile /usr/local/apache/conf/ssl.crt/intermediatebundle.crt

十一周二次課