LAMP架构(8)限定目录禁止解析php,限制user_agent,php相关配置

限定目录禁止解析php
对于提供文件上传服务的网站,一定要禁止对程序类文件的解析,否则会有极大的安全隐患。此处以php为例,其他语言也一样。

禁止指定目录解析php:
[root@aliyun ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
虚拟主机1禁止指定目录解析php:
<VirtualHost *: 80 > 80是http的服务端口,不用改
ServerAdmin webmaster @ test.com 网站管理员邮箱,设置成自己的网站
DocumentRoot " /data/wwwroot/www.test.com " 虚拟主机根目录放网站程序
ServerName test.com 网站名,域名
ServerAlias www .test.com 网站别名,域名别名,可写多个要用空格隔开
(配置跳转跟这里的别名无关,有无别名这一项都会跳转成功,由rewrite跳转会有301状态码和说明)
<Directory /data/wwwroot/www.test.com> 需配置防盗的目录
SetEnvIfNoCase Referer "http://www.test.com" local_ref referer规则,变量local_ref
SetEnvIfNoCase Referer "http://test.com" local_ref referer规则,变量local_ref
SetEnvIfNoCase Referer "^$" local_ref referer规则,变量local_ref
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)"> 匹配referer的文件类型
Order Allow,Deny 命令,先允许后拒绝
Allow from env=local_ref 允许来自local_ref的变量
</filesmatch>
</Directory>
<Directory /data/wwwroot/www.test.com/upload> 指定需要禁止php解析的目录
php_admin_flag engine off php管理标记,关闭引擎(禁止解析php)
</Directory>
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^ www.test.com $ www.test.com的域名请求跳转至此
RewriteRule ^/(.*)$ http:// www.test.com /$1 [R= 301,L ] 状态码301永久跳转,L=last,跳一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$
</IfModule>
ErrorLog "logs/ test.com -error_log" 错误日志保存路径
SetEnvIf Request_URI ".*\.gif$" img 日志记录规则,变量表示.gif文件
SetEnvIf Request_URI ".*\.jpg$" img 日志记录规则,变量表示.jpg文件
SetEnvIf Request_URI ".*\.png$" img 日志记录规则,变量表示.png文件
SetEnvIf Request_URI ".*\.bmp$" img 日志记录规则,变量表示.bmp文件
SetEnvIf Request_URI ".*\.swf$" img 日志记录规则,变量表示.swf文件
SetEnvIf Request_URI ".*\.js$" img 日志记录规则,变量表示.js文件
SetEnvIf Request_URI ".*\.css$" img 日志记录规则,变量表示.cs文件
CustomLog "logs/test.com-access_log" combined env= ! img
</VirtualHost> 取反,不记录变量所表示的文件访问记录,访问日志保存路径
保存退出

测试语法并重新加载配置
[root@aliyun ~]# vim /data/wwwroot/www.test.com/upload/uptest.php
写入以下内容:
<?php
echo"我就看看,我不说话!":
?>
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aliyun ~]# curl -x127.0.0.1:80 www.test.com/upload/uptest.php
<?php
echo"我就看看,我不说话!";
?>
可以看到解析的是字符内容,而不是echo命令的运行结果,已经成功禁止upload目录解析php
[root@aliyun ~]#

限制user_agent
针对浏览器标识user_agent,可以用来限制一些频繁访问网站的爬虫程序或者阻挡恶意请求即cc攻击,

限制指定的user_agent访问:
[root@aliyun ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
虚拟主机1增加user_agent访问规则:
<VirtualHost *: 80 > 80是http的服务端口,不用改
ServerAdmin webmaster @ test.com 网站管理员邮箱,设置成自己的网站
DocumentRoot " /data/wwwroot/www.test.com " 虚拟主机根目录放网站程序
ServerName test.com 网站名,域名
ServerAlias www .test.com 网站别名,域名别名,可写多个要用空格隔开
(配置跳转跟这里的别名无关,有无别名这一项都会跳转成功,由rewrite跳转会有301状态码和说明)
<Directory /data/wwwroot/www.test.com> 需配置防盗的目录
SetEnvIfNoCase Referer "http://www.test.com" local_ref referer规则,变量local_ref
SetEnvIfNoCase Referer "http://test.com" local_ref referer规则,变量local_ref
SetEnvIfNoCase Referer "^$" local_ref referer规则,变量local_ref
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)"> 匹配referer的文件类型
Order Allow,Deny 命令,先允许后拒绝
Allow from env=local_ref 允许来自local_ref的变量
</filesmatch>
</Directory>
<Directory /data/wwwroot/www.test.com/upload> 指定需要禁止php解析的目录
php_admin_flag engine off php管理标记,关闭引擎(禁止解析php)
</Directory>
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^ www.test.com $ www.test.com的域名请求跳转至此
RewriteRule ^/(.*)$ http:// www.test.com /$1 [R= 301,L ] 状态码301永久跳转,L=last,跳一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$
</IfModule>
<IfModule mod_rewrite.c> 同上,限制useer_agent也需要rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] 不分大小写匹配包含curl.或
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] 包含baidu.com的user_agent
RewriteRule .* - [F] 规则-Forbidden,禁止访问
</IfModule>
ErrorLog "logs/ test.com -error_log" 错误日志保存路径
SetEnvIf Request_URI ".*\.gif$" img 日志记录规则,变量表示.gif文件
SetEnvIf Request_URI ".*\.jpg$" img 日志记录规则,变量表示.jpg文件
SetEnvIf Request_URI ".*\.png$" img 日志记录规则,变量表示.png文件
SetEnvIf Request_URI ".*\.bmp$" img 日志记录规则,变量表示.bmp文件
SetEnvIf Request_URI ".*\.swf$" img 日志记录规则,变量表示.swf文件
SetEnvIf Request_URI ".*\.js$" img 日志记录规则,变量表示.js文件
SetEnvIf Request_URI ".*\.css$" img 日志记录规则,变量表示.cs文件
CustomLog "logs/test.com-access_log" combined env= ! img
</VirtualHost> 取反,不记录变量所表示的文件访问记录,访问日志保存路径
保存退出

测试语法并重新加载配置
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful

用禁止的和未禁止的user_agent测试访问:
[root@aliyun ~]# curl -x 127.0.0.1:80 www.test.com/upload/uptest.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/uptest.php
on this server.<br />
</p>
</body></html>
[root@aliyun ~]# curl -x127.0.0.1:80 -A" baidu.com " www.test.com/upload/uptest.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/uptest.php
on this server.<br />
</p>
</body></html>
[root@aliyun ~]# curl -x127.0.0.1:80 -A" 123.com " www.test.com/upload/uptest.php
<?php
echo"我就看看,我不说话!";
?>

[root@aliyun ~]#

php相关配置

设置默认时区
查找php配置文件位置
[root@aliyun ~]# /usr/local/php/bin/ php -i|grep -i "loaded configuration file"
查看php信息且不分大小写过滤包含loaded configuration file的语句
PHP Warning : Unknown: It is not safe to rely on the system's timezone settings . You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
Loaded Configuration File => /usr/local/php/etc/php.ini 这是我们要找的配置文件位置
处理PHP Warning警告,设置默认时区
[root@aliyun ~]# vim /usr/local/php/etc/php.ini
搜索:date.timezone,编辑如下
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai 设置时区为亚洲/上海,并取消前面的注释符
[root@aliyun ~]# /usr/local/php/bin/php -i|grep -i "loaded configuration file" 再次运行已经没有警告
Loaded Configuration File => /usr/local/php/etc/php.ini
[root@aliyun ~]#

关闭php函数
[root@aliyun ~]# vim /usr/local/php/etc/php.ini
搜索: 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

配置错误日志
[root@aliyun ~]# vim /usr/local/php/etc/php.ini
分别搜索: error_log, log_errors, display_errors, error_reporting ,编辑如下
error_log = On
log_errors = /var/log/php/php_error.log
display_errors = Off
将error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT改为
error_reporting = E_ALL & ~E_NOTICE
保存退出
测试:
[root@aliyun ~]# mkdir -p /var/log/php
[root@aliyun ~]# chmod 777 /var/log/php
[root@aliyun ~]# vim /data/wwwroot/elon.org.cn/test2.php
<?php
echo 我们不一样!
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aliyun ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aliyun ~]# curl -x127.0.0.1:80 -i -A "123.com" www.test.com/test2.php
HTTP/1.0 500 Internal Server Error
Date: Sun, 03 Jun 2018 18:58:15 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.36
X-Powered-By: PHP/5.6.36
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8
[root@aliyun ~]# cat /var/log/php/php_errors.log
[04-Jun-2018 02:58:15 Asia/Shanghai] PHP Parse error: syntax error , unexpected end of file, expecting ',' or ';' in /data/wwwroot/elon.org.cn/test2.php on line 4
[root@aliyun ~]#

配置open_basedir,限定网站的活动目录
目前,一台服务器上跑多个网站的情况在企业中很普遍,如果其中一个网站被黑,很可能会连累到其他网站,需要通过open_basedir功能来限制一个网站的活动目录,

[root@aliyun ~]# vim /usr/local/php/etc/php.ini
搜索: open_basedir ,编辑如下
open_basedir = /tmp :/data/wwwroot/www.test.com 指定网站的活动目录,并取消前面的注释符
验证:

[root@aliyun ~]# touch /data/wwwroot/123.com/test.php
[root@aliyun ~]# curl -x127.0.0.1:80 -i 123.com/test.php
HTTP/1.0 500 Internal Server Error
Date: Sun, 03 Jun 2018 19:36:34 GMT
Server: Apache/2.4.33 (Unix) PHP/5.6.36
X-Powered-By: PHP/5.6.36
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

[root@aliyun ~]# tail -1 /var/log/php/php_errors.log
[04-Jun-2018 03:36:34 Asia/Shanghai] PHP Fatal error: Unknown: Failed opening required '/data/wwwroot/123.com/test.php' (include_path='.:/usr/local/php/lib/php') in Unknown on line 0
[root@aliyun ~]#


如果只运行了一台虚拟主机可以用php.ini里的方法来限定网站的活动目录,如果运行了多台虚拟机,就需要在httpd.conf里设置:

编辑httpd_vhosts.conf
[root@aliyun ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

在每一台虚拟主机设置的最后一行</VirtualHost>上方添加对应网站的活动目录,以实现逐站限定活动目录:
php_admin_value open_basedir "/data/wwwroot/www.test.com:/tmp/"
php_admin_value open_basedir 是固定格式

如本机虚拟主机1所示;
虚拟主机1
<VirtualHost *: 80 > 80是http的服务端口,不用改
ServerAdmin webmaster @ test.com 网站管理员邮箱,设置成自己的网站
DocumentRoot " /data/wwwroot/www.test.com " 虚拟主机根目录放网站程序
ServerName test.com 网站名,域名
ServerAlias www .test.com 网站别名,域名别名,可写多个要用空格隔开
(配置跳转跟这里的别名无关,有无别名这一项都会跳转成功,由rewrite跳转会有301状态码和说明)
<Directory /data/wwwroot/www.test.com> 需配置防盗的目录
SetEnvIfNoCase Referer "http://www.test.com" local_ref referer规则,变量local_ref
SetEnvIfNoCase Referer "http://test.com" local_ref referer规则,变量local_ref
SetEnvIfNoCase Referer "^$" local_ref referer规则,变量local_ref
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)"> 匹配referer的文件类型
Order Allow,Deny 命令,先允许后拒绝
Allow from env=local_ref 允许来自local_ref的变量
</filesmatch>
</Directory>
<Directory /data/wwwroot/www.test.com/upload> 指定需要禁止php解析的目录
php_admin_flag engine off php管理标记,关闭引擎(禁止解析php)
</Directory>
<IfModule mod_rewrite.c> 需要mod_rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_HOST} !^ www.test.com $ www.test.com的域名请求跳转至此
RewriteRule ^/(.*)$ http:// www.test.com /$1 [R= 301,L ] 状态码301永久跳转,L=last,跳一次
定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转, $1代表^/(.*)$
</IfModule>
<IfModule mod_rewrite.c> 同上,限制useer_agent也需要rewrite模块支持
RewriteEngine on 打开rewrite功能
RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR] 不分大小写匹配包含curl.或
RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC] 包含baidu.com的user_agent
RewriteRule .* - [F] 规则-Forbidden,禁止访问
</IfModule>
ErrorLog "logs/ test.com -error_log" 错误日志保存路径
SetEnvIf Request_URI ".*\.gif$" img 日志记录规则,变量表示.gif文件
SetEnvIf Request_URI ".*\.jpg$" img 日志记录规则,变量表示.jpg文件
SetEnvIf Request_URI ".*\.png$" img 日志记录规则,变量表示.png文件
SetEnvIf Request_URI ".*\.bmp$" img 日志记录规则,变量表示.bmp文件
SetEnvIf Request_URI ".*\.swf$" img 日志记录规则,变量表示.swf文件
SetEnvIf Request_URI ".*\.js$" img 日志记录规则,变量表示.js文件
SetEnvIf Request_URI ".*\.css$" img 日志记录规则,变量表示.cs文件
CustomLog "logs/test.com-access_log" combined env= ! img
php_admin_value open_basedir "/data/wwwroot/www.test.com:/tmp/" 限定网站活动目录
</VirtualHost> 取反,不记录变量所表示的文件访问记录,访问日志保存路径
保存退出




猜你喜欢

转载自blog.csdn.net/langyue919/article/details/80562213