8.限定某个目录禁止解析php&限制user_agent

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8.限定某个目录禁止解析php&限制user_agent相关的知识,希望对你有一定的参考价值。

[toc]

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

有这样一种情况,有些站点和论坛是允许上传图片到服务器,但是这就给黑客留下了可进入服务器的大门,他们上传一些php或者js到服务器,然后被我们执行加载,有些函数可以让黑客获取最大的权限,从而对数据造成威胁!
为了避免这种事情的发生,我们需要限制上传类型。

1. 打开配置文件 httpd-vhosts.conf

在虚拟服务器中增加如下配置:

[[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 

2.凡是在upload目录中的php均不解析!并且匹配任意.php的文件,全部拒绝访问!

      <Directory /data/wwwroot/xavi.com/upload>
        php_admin_flag engine off //禁止php解析,所有访问都报403错误
       <FilesMatch (.*)\.php(.*)>//需要转义字符
       Order allow,deny //不加deny,它会访问源代码
       Deny from all
       </FilesMatch>

2.-t,-gracful,检查语法并开启httpd

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]#  /usr/local/apache2.4/bin/apachectl graceful
httpd not running, trying to start
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl start
httpd (pid 2838) already running
[[email protected] ~]#  /usr/local/apache2.4/bin/apachectl graceful

3.创建upload目录,以及在upload目录下创建123.php去测试。但未得到403结果

[[email protected] ~]# mkdir upload

[[email protected] ~]# ls
123.txt  anaconda-ks.cfg  httpd-2.4.29.tar.gz   rsync      test2
321.txt  awk              index.php             sed        upload
556.txt  grep             initial-setup-ks.cfg  split_dir  xaa
admin    httpd-2.4.29     [[email protected]       test1

[[email protected] ~]# cp index.php upload/
[[email protected] ~]# curl -x127.0.0.1:80 ‘http://xavi.cpm
[[email protected] ~]# curl -x127.0.0.1:80 ‘http://xavi.com/admin.php?adadede‘ -I
HTTP/1.1 404 Not Found
Date: Sun, 11 Mar 2018 03:33:57 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

4.找到犯错原因,得到验证结果

这里之所以没有得到403 fobiden的提示,是因为在练习测试过程我忽略了,指令执行的环境。[[email protected] xavi.com]上述操作正确的过程是应该在/xavi.comde 文件夹下操作,而不是在默认的文件路径下

以下是重新操作的过程

[[email protected] ~]# cd /data/wwwroot/xavi.com
[[email protected] xavi.com]# ls
123.php  admin  index.php  xavi.jpg  xavi.txt
[[email protected] xavi.com]# mkdir uplaod

[[email protected] xavi.com]# ls
123.php  admin  index.php  uplaod  xavi.jpg  xavi.txt
[[email protected] xavi.com]# mv uplaod upload
[[email protected] xavi.com]# ls
123.php  admin  index.php  upload  xavi.jpg  xavi.txt
[[email protected] xavi.com]# cp 123.php /upload
[[email protected] xavi.com]# !vim
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
[[email protected] xavi.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] xavi.com]#  /usr/local/apache2.4/bin/apachectl graceful
[[email protected] xavi.com]# !curl
curl -x127.0.0.1:80 ‘http://xavi.com/upload/123.php‘ -I
HTTP/1.1 403 Forbidden
Date: Sun, 11 Mar 2018 05:31:04 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

技术分享图片

5.测试下没有FilesMatch那段话的结果

技术分享图片

无法解析,直接显示内原代码

[[email protected] xavi.com]# !vim
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
[[email protected] xavi.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] xavi.com]#  /usr/local/apache2.4/bin/apachectl graceful
[[email protected] xavi.com]# curl -x127.0.0.1:80 ‘http://xavi.com/upload/123.php‘ 
<?php
echo "123.php";

技术分享图片

总结,如上当你访问.php文件时,直接拒绝,没有任何机会去访问,更别提执行了!如果程序员让upload可以允许解析,那只有说明他不合格!静态文件存储的地方是不允许放php的。没有考虑到任何数据安全!!!

11.29 访问控制,限制user_agent

1.什么是user_agent(浏览器标识)

User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。

2.cc攻击,肉鸡

cc攻击:是我们经常见到的最常见的一种攻击,几乎每天每个时段都会存在。CC攻击就是黑客利用所能利用的肉鸡(就是所谓的黑客利用技术手段攻击下来的他人服务器)去攻击(正常的访问)你的站点,导致你的站点不可以被正常的用户所浏览。但是也不是不可防止,在攻击的时候有一个规律的特征,user_agent是一致的(其referer和访问页面是一致的,并且在一秒内发动N次访问)!

3.核心配置文件

<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_USER_AGENT}  .*curl.* [NC,OR]
        RewriteCond %{HTTP_USER_AGENT}  .*baidu.com.* [NC]
        RewriteRule  .*  -  [F]
   </IfModule>

代码解析:

RewriteCond %{HTTP_USER_AGENT}  .*curl.* [NC,OR]   //匹配Curl的访问   [NC,OR] NC:忽略大小写。 OR:是或者的意思,要么这一条,要么下一条满足情况
RewriteCond %{HTTP_USER_AGENT}  .*baidu.com.* [NC]
RewriteRule  .*  -  [F]   // F:Forbidden 禁止

技术分享图片

4.测试,使用Curl方式访问直接被禁掉

[[email protected] xavi.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] xavi.com]#  /usr/local/apache2.4/bin/apachectl graceful
[[email protected] xavi.com]# curl -x127.0.0.1:80 ‘http://xavi.com/upload/123.php‘ -I
HTTP/1.1 403 Forbidden
Date: Sun, 11 Mar 2018 07:04:12 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

技术分享图片

5.使用curl -A :随意指定自己这次访问所宣称的自己的浏览器信息。

[[email protected] xavi.com]# curl -A "xavilinux xavilinux" -x127.0.0.1:80 ‘http://xavi.com/123.php‘ -I
HTTP/1.1 200 OK
Date: Sun, 11 Mar 2018 07:21:42 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
X-Powered-By: PHP/7.1.6
Content-Type: text/html; charset=UTF-8
  • 改变浏览方式即可访问。

6。查看日志文件: tail /usr/local/apache2.4/logs/xavi.com-access_20180311.log

[[email protected] xavi.com]# tail /usr/local/apache2.4/logs/xavi.com-access_20180311.log
192.168.72.1 - - [11/Mar/2018:14:02:02 +0800] "GET /upload/123.php HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
192.168.72.1 - - [11/Mar/2018:14:02:02 +0800] "GET /upload/123.php HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
127.0.0.1 - - [11/Mar/2018:15:04:12 +0800] "HEAD http://xavi.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [11/Mar/2018:15:04:12 +0800] "HEAD http://xavi.com/upload/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"
127.0.0.1 - - [11/Mar/2018:15:05:32 +0800] "GET http://xavi.com/upload/123.php HTTP/1.1" 403 223 "-" "curl/7.29.0"
127.0.0.1 - - [11/Mar/2018:15:05:32 +0800] "GET http://xavi.com/upload/123.php HTTP/1.1" 403 223 "-" "curl/7.29.0"
127.0.0.1 - - [11/Mar/2018:15:21:42 +0800] "HEAD http://xavi.com/123.php HTTP/1.1" 200 - "-" "xavilinux xavilinux"
127.0.0.1 - - [11/Mar/2018:15:21:42 +0800] "HEAD http://xavi.com/123.php HTTP/1.1" 200 - "-" "xavilinux xavilinux"
127.0.0.1 - - [11/Mar/2018:15:22:18 +0800] "GET http://xavi.com/123.php HTTP/1.1" 200 7 "-" "xavilinux xavilinux"
127.0.0.1 - - [11/Mar/2018:15:22:18 +0800] "GET http://xavi.com/123.php HTTP/1.1" 200 7 "-" "xavilinux xavilinux"

实用扩展:

apache 禁止trace或track防止xss攻击
http://ask.apelearn.com/question/1045

以上是关于8.限定某个目录禁止解析php&限制user_agent的主要内容,如果未能解决你的问题,请参考以下文章

限定某个目录禁止解析php限制user_agentphp相关配置

11.28 限定某个目录禁止解析php 11.29 限制user_agent 11.30/11.31

四十限定某个目录禁止解析php限制user_agentPHP相关配置

限定某个目录禁止解析php 限制user_agent php相关配置

LAMP(7限定某个目录禁止解析php 限制user_agent PHP相关配置PHP扩展模块

2018-3-7 11周2次课 限定某个目录禁止解析php限制user_agentphp相关配置