欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

十一周二次课(6月1日)

发布时间:2025/3/21 编程问答 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 十一周二次课(6月1日) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

11.28 限定某个目录禁止解析php

因为httpd开放了php模块,所以如果被人上传了文件(php类型),httpd就有可能会进行执行,一旦执行,就会让对方获得我们服务器的root权限,或者是被恶意删除或修改一些参数,导致服务器瘫痪或者是被

<Directory /home/wwwroot/111.com/img/icon> #禁止解析PHPphp_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,allowDeny from allAllow from 127.0.0.1</FilesMatch></Directory><Directory /data/wwwroot/111.com>SetEnvIfNoCase Referer "http://111.com" local_refSetEnvIfNoCase Referer "http://aaa.com" local_refSetEnvIfNoCase Referer "^$" local_ref<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">Order Allow,DenyAllow from env=local_ref</FilesMatch></Directory>

改为

#</FilesMatch> #</Directory><Directory /home/wwwroot/111.com/img/icon>php_admin_flag engine off<FilesMatch (.*)\.php(.*)>Order allow,denyDeny from all</FilesMatch></Directory><Directory /data/wwwroot/111.com><FilesMatch "admin.php(.*)">Order deny,allowDeny from allAllow from 127.0.0.1</FilesMatch></Directory><Directory /data/wwwroot/111.com>SetEnvIfNoCase Referer "http://111.com" local_refSetEnvIfNoCase Referer "http://aaa.com" local_refSetEnvIfNoCase 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,allowDeny from allAllow from 127.0.0.1</FilesMatch></Directory><Directory /data/wwwroot/111.com>SetEnvIfNoCase Referer "http://111.com" local_refSetEnvIfNoCase 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,denyDeny 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,denyDeny from all</FilesMatch></Directory><Directory /home/wwwroot/111.com><FilesMatch "admin.php(.*)">Order deny,allowDeny from allAllow 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 onRewriteCond %{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,denyDeny 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 = #0000BB314,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-zenith937,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.comServerAlias www.abc.com www.123.comphp_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.comServerAlias 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参数

  • 访问控制
    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
  • RewriteLogLevel 变为:logLevel
    如,LogLevel warn rewrite: warn

  • Namevirtualhost 被移除

  • 网站压缩,除了使用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

  • 安装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

  • 让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
  • 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 *:443DocumentRoot "/data/web/www"ServerName aaa.com:443ErrorLog "logs/error.log"CustomLog "logs/access.log" combinedSSLEngine onSSLCertificateFile /usr/local/apache/conf/ssl.crt/server.crtSSLCertificateKeyFile /usr/local/apache/conf/ssl.key/server.keySSLCertificateChainFile /usr/local/apache/conf/ssl.crt/intermediatebundle.crt

    转载于:https://blog.51cto.com/235571/2120647

    总结

    以上是生活随笔为你收集整理的十一周二次课(6月1日)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。