8.7 11.28-11.31

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

某个目录允许上传图片,但可能会有用户利用一些方法,上传了一些php文件到该目录;

php中包含一些危险的函数,若开放php上传权限则有可能被上传一些恶意的×××文件;

这样有可能被恶意用户得到服务器的root权限,十分危险;

网站信息泄露:

网站的电话号码等信息被泄露

可能原因:

可能是×××者查询了服务器的数据库获取了电话号码

php程序存在漏洞或sql注入的漏洞

sql注入:用户会将sql查询语句通过特殊提交提交到服务器,服务器会将sql语句转换为正常的查询,然后获得数据

sql注入防范:在网站提交入口增加特殊符号过滤即可阻断sql注入漏洞

分析:

抓包分析->发现可疑sql查询->定位时间点->web服务器看时间段内的访问日志->发现可疑请求,对方向指定目录上传了一个php文件->服务器端对应目录没有禁止解析php->对方想办法上传了一个php×××文件,再通过浏览器访问该php文件->获得后门,得到了更高的权限->拿到数据库相关权限->获取数据

解决:

设置对应目录禁止解析php文件,上传的php×××文件不被解析则×××者无法得到更高的权限

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

   # </Directory>

     <Directory /data/wwwroot/111.com/nophp>

         php_admin_flag engine off nophp目录下禁止解析php

         <FilesMatch .*\.php(.*)> 禁止访问(.*)\.php.*)的文件

         Order allow,deny

         Deny from all 没有allow操作,匹配的项全部deny

         </FilesMatch>

     </Directory>

测试:

1 禁止访问(.*\.php(.*)+禁止解析php

blob.png

[root@hyc-01-01 nophp]# curl -x127.0.0.1:80 "http://111.com/nophp/2.php" -I

HTTP/1.1 403 Forbidden

Date: Wed, 08 Aug 2018 02:08:13 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1

2 禁止解析php

[root@hyc-01-01 nophp]# curl -x127.0.0.1:80 "http://111.com/nophp/2.php"

<?php

echo "error php success" 由于php无法被解析,所以直接显示了源代码

[root@hyc-01-01 nophp]# !curl

curl -x127.0.0.1:80 "http://111.com/nophp/2.php" -I

HTTP/1.1 200 OK

Date: Wed, 08 Aug 2018 02:11:37 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Last-Modified: Wed, 08 Aug 2018 01:47:44 GMT

ETag: "1f-572e2b2634cea"

Accept-Ranges: bytes

Content-Length: 31

Cache-Control: max-age=0

Expires: Wed, 08 Aug 2018 02:11:37 GMT

Content-Type: application/x-httpd-php

由于2.php无法被正常解析,所以直接被下载:

blob.png

一般存放静态文件的目录下不能存放php,这种目录下应该禁止解析php

 

11.29 限制user_agent

user_agent:浏览器标识

cc×××:

有时网站会受到cc×××,×××者通过软件或“肉鸡”,当要×××某网站时,将发动所有“肉鸡”同时访问某个站点,以至于站点无法承受这些访问;

通常cc×××的useer_agent是一致的,即使用的user_agent一样,并且访问的频率较快,通常1秒访问n次;

解决:

限制user_agent减轻服务器压力;

对方在访问时会收到状态码403,这样对方对服务器资源不会造成太大影响,仅仅是对方发送来了一个请求,带宽消耗也不会太大;

使用模块mod_rewrite

[root@hyc-01-01 nophp]# 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]

         定义user_agent的条件:

OR的意思是或者,即user_agent匹配第二行第三行的条件,不加OR则为并且,但无法同时匹配curlbaidu.com

NC忽略大小写,有时user_agent中会有部分大写字母(Mozilla/5.0

         RewriteRule .* - [F]

         FForbiden403)拒绝

     </IfModule>

测试:

配置生效前:

[root@hyc-01-01 logs]# curl -x127.0.0.1:80 "http://111.com/123.php"

hello world

生效后:

[root@hyc-01-01 logs]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@hyc-01-01 logs]# /usr/local/apache2.4/bin/apachectl graceful

[root@hyc-01-01 logs]# curl -x127.0.0.1:80 "http://111.com/123.php" -I

HTTP/1.1 403 Forbidden

Date: Wed, 08 Aug 2018 03:57:35 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1

blob.png

日志信息:

127.0.0.1 - - [08/Aug/2018:11:56:13 +0800] "GET http://111.com/123.php HTTP/1.1" 200 11 "-" "curl/7.29.0"

127.0.0.1 - - [08/Aug/2018:11:57:35 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - "-" "curl/7.29.0"

192.168.31.1 - - [08/Aug/2018:12:00:51 +0800] "GET /123.php HTTP/1.1" 200 11 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)"

使用-A参数指定user_agent

[root@hyc-01-01 logs]# curl -A "hyc hyc" -x127.0.0.1:80 "http://111.com/123.php"

hello world 指定user_agent后访问正常

[root@hyc-01-01 logs]# tail -20 /usr/local/apache2.4/logs/111.com-access_20180808.log

127.0.0.1 - - [08/Aug/2018:12:19:00 +0800] "GET http://111.com/123.php HTTP/1.1" 200 11 "-" "hyc hyc"

[root@hyc-01-01 logs]# curl -e "http://111.com" -A "hyc hyc" -x127.0.0.1:80 "http://111.com/123.php"

hello world[root@hyc-01tail -1 /usr/local/apache2.4/logs/111.com-access_20180808.log

127.0.0.1 - - [08/Aug/2018:12:23:09 +0800] "GET http://111.com/123.php HTTP/1.1" 200 11 "http://111.com" "hyc hyc"

[root@hyc-01-01 logs]#

-e指定referer信息,-A指定user_agent信息,-x省略hosts-I仅查看状态码,不显示具体信息

 

11.30 PHP相关配置(上)

PHP配置文件位置:

1 通过浏览器

查找网站使用php模块的php.ini配置文件:

在网站对应的目录下创建phpinfo的页面;

通过浏览器访问该页面找到配置文件;

操作:

[root@hyc-01-01 111.com]# touch phpinfo

[root@hyc-01-01 111.com]# vim phpinfo

<?php

phpinfo();

blob.png

2 执行php –i

部分情况下使用php –i查找的路径并不准确

Apache使用的是php的模块,而php –i查找的是一个php程序,该php程序与apache使用的php模块可能无关;

这种办法找到的路径通常不准确,有时这种办法找到的php.iniapache使用的php模块的php.ini文件不是一个;

由上图可知php模块配置文件路径,但配置文件没有加载

加载配置文件:

php源码包复制配置文件到配置文件路径下:

[root@hyc-01-01 php-7.1.6]# cp php.ini-development /usr/local/php7/etc/php.ini

刷新apache配置:

[root@hyc-01-01 php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful

blob.png

php模块配置文件加载成功

php相关配置:

[root@hyc-01-01 php-7.1.6]# cd /usr/local/php7/etc/

[root@hyc-01-01 etc]# vim php.ini

限定函数(禁用部分php中的函数):

312 ; It receives a comma-delimited list of function names.

 313 ; http://php.net/disable-functions

 314 disable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,pa

     ssthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,she

     ll_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,read

     link,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close,

     phpinfo

 315

 316 ; This directive allows you to disable certain classes for security reasons.

禁用的函数中也包括php.info

[root@hyc-01-01 etc]# /usr/local/apache2.4/bin/apachectl graceful

blob.png

访问提示phpinfo()已经被禁止

 

date.timezone(定义时区):

若不定义有时会出现告警信息

935

 936 [Date]

 937 ; Defines the default timezone used by the date functions

 938 ; http://php.net/date.timezone

 939 date.timezone =Asia/Shanghai 定义所在时区为上海

 940

display_errors(直接将错误信息显示在浏览器上):

475 ; Production Value: Off

 476 ; http://php.net/display-errors

 477 display_errors = Off on表示打开,off则错误信息不会输出到浏览器

 478

测试:

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/phpinfo.php"

[root@hyc-01-01 etc]# 无报错信息输出

blob.png

这样配置测试后发现网页上没有任何错误信息,并且curl测试也没有任何报错,这不合理,所以需要配置几个错误日志:

[root@hyc-01-01 etc]# vim /usr/local/php7/etc/php.ini

458 ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT

 459 ; http://php.net/error-reporting

 460 error_reporting =  E_ALL & ~E_NOTICE

用于定义错误日志级别,默认为E_ALL,会记录所有错误日志信息,最不严谨,以上的级别为生产环境中常用的级别;

生产环境中NOTICE出现几率很高,有时出现NOTICE并不代表出错;

 461

 462 ; This directive controls whether or not and where PHP will output errors,

 463 ; notices and warnings too. Error output is very useful during development, but

497 ; http://php.net/log-errors

 498 log_errors = On 开启错误日志

 499

 500 ; Set maximum length of log_errors. In error_log information about the source

582 ; Example:

 583 error_log = /tmp/php_errors.log 定义错误日志的保存路径

 584 ; Log errors to syslog (Event Log on Windows).

 585 ;error_log = syslog

测试:

[root@hyc-01-01 etc]# curl -x127.0.0.1:80 "http://111.com/phpinfo.php" -I

HTTP/1.1 403 Forbidden user_agentcurl)被禁止,所以被拒绝访问(403),属于httpd的报错信息

Date: Wed, 08 Aug 2018 12:37:04 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

Content-Type: text/html; charset=iso-8859-1

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/phpinfo.php" -I

HTTP/1.1 200 OK

Date: Wed, 08 Aug 2018 12:37:23 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Cache-Control: max-age=0

Expires: Wed, 08 Aug 2018 12:37:23 GMT

Content-Type: text/html; charset=UTF-8

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/phpinfo.php"

指定了user_agenta,但由于phpinfo()函数在php模块的配置文件php.ini中被禁止,所以无法得到信息;

并且由于php.ini文件中配置了display_errors = Off,导致没有报错信息;

 

php.ini中定义的error_log = /tmp/php_errors.log路径查看指定的php模块的错误日志文件:

[root@hyc-01-01 tmp]# ls -l php_errors.log

-rw-r--r-- 1 daemon daemon 882 8   8 20:37 php_errors.log

[root@hyc-01-01 tmp]# ps aux|grep httpd

root     27204  0.0  1.4 259560 14380 ?        Ss   807   0:10 /usr/local/apache2.4/bin/httpd -k start

daemon   42583  0.0  0.8 546388  8992 ?        Sl   20:21   0:00 /usr/local/apache2.4/bin/httpd -k start

daemon   42584  0.0  0.8 546388  8992 ?        Sl   20:21   0:00 /usr/local/apache2.4/bin/httpd -k start

daemon   42585  0.0  1.4 1017812 14256 ?       Sl   20:21   0:00 /usr/local/apache2.4/bin/httpd -k start

root     42695  0.0  0.0 112720   984 pts/0    S+   20:48   0:00 grep --color=auto httpd

生成错误日志文件的是httpd服务的启动用户daemon

可以发现php_errors.log 的属主为daemon,而daemon实际是httpd的属主;

当以上配置都完成但始终无法在对应路径生成错误日志文件时应该去检查生成文件的目录的权限信息(daemon是否对该目录有写权限);

或者可以在路径下手动创建php_errors.log,生成后再修改文件属主为daemon,权限改为777

[root@hyc-01-01 tmp]# cat php_errors.log

[08-Aug-2018 20:31:39 Asia/Shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/phpinfo.php on line 2

[08-Aug-2018 20:31:52 Asia/Shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/phpinfo.php on line 2

[08-Aug-2018 20:32:02 Asia/Shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/phpinfo.php on line 2

[08-Aug-2018 20:36:36 Asia/Shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/phpinfo.php on line 2

[08-Aug-2018 20:37:23 Asia/Shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/phpinfo.php on line 2

[08-Aug-2018 20:37:34 Asia/Shanghai] PHP Warning:  phpinfo() has been disabled for security reasons in /data/wwwroot/111.com/phpinfo.php on line 2

/data/wwwroot/111.com下编辑新文件:

[root@hyc-01-01 111.com]# vim 3.php

<?php

echo "hytjopfj"

jaeafdjhphngiqe

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/3.php" -I

HTTP/1.0 500 Internal Server Error 网页文件存在错误导致报错

Date: Wed, 08 Aug 2018 13:29:51 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Connection: close

Content-Type: text/html; charset=UTF-8

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/3.php"

[root@hyc-01-01 tmp]# cat php_errors.log

[08-Aug-2018 21:29:51 Asia/Shanghai] PHP Parse error:  syntax error, unexpected 'jaeafdjhphngiqe' (T_STRING), expecting ',' or ';' in /data/wwwroot/111.com/3.php on line 3

[08-Aug-2018 21:30:05 Asia/Shanghai] PHP Parse error:  syntax error, unexpected 'jaeafdjhphngiqe' (T_STRING), expecting ',' or ';' in /data/wwwroot/111.com/3.php on line 3

11.31 PHP相关配置(下)

安全选项open_basedir

一台服务器运行n个站点,也许部分站点代码有问题,此时这部分站点被×××黑了,×××黑了该站点后要继续×××以试图进入服务器上运行的其他站点,服务器上部分站点被黑后,其他原本没有被黑的站点也会面临被黑的风险

在一台服务器上将a网站的a目录与b网站的b目录隔离,×××黑了a目录后无法继续黑b网站,无权限进入b目录;

即使一台服务器仅跑了一个站点,该站点被黑后仍有必要将该站点的目录与其他目录隔离,避免整个服务器系统被×××***;

 

操作:

[root@hyc-01-01 etc]# vim php.ini

307 ; or per-virtualhost web server configuration file.

 308 ; http://php.net/open-basedir

 309 open_basedir = /data/wwwroot/1111.com:/tmp 故意将目录定义出错

 310

测试:

[root@hyc-01-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful

[root@hyc-01-01 111.com]# curl -A "a" -x127.0.0.1:80 "http://111.com/123.php"

[root@hyc-01-01 111.com]# curl -A "a" -x127.0.0.1:80 "http://111.com/123.php" -I

HTTP/1.0 500 Internal Server Error

Date: Wed, 08 Aug 2018 14:37:28 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Connection: close

Content-Type: text/html; charset=UTF-8

[root@hyc-01-01 111.com]# tail -5 /tmp/php_errors.log

[08-Aug-2018 22:30:48 Asia/Shanghai] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0

[08-Aug-2018 22:30:48 Asia/Shanghai] PHP Fatal error:  Unknown: Failed opening required '/data/wwwroot/111.com/123.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0

[08-Aug-2018 22:35:16 Asia/Shanghai] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/111.com/123.php) is not within the allowed path(s): (/data/wwwroot/1111.com:/tmp) in Unknown on line 0

[08-Aug-2018 22:35:16 Asia/Shanghai] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0

[08-Aug-2018 22:35:16 Asia/Shanghai] PHP Fatal error:  Unknown: Failed opening required '/data/wwwroot/111.com/123.php' (include_path='.:/usr/local/php7/lib/php') in Unknown on line 0

以上标红信息说明123.php不在open_basedir允许的目录下

 

php.ini配置文件中open_basedir1111.com目录改为111.com后再访问:

[root@hyc-01-01 etc]# /usr/local/apache2.4/bin/apachectl graceful

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/123.php" -I

HTTP/1.1 200 OK 访问正常

Date: Wed, 08 Aug 2018 14:43:15 GMT

Server: Apache/2.4.34 (Unix) PHP/7.1.6

X-Powered-By: PHP/7.1.6

Cache-Control: max-age=0

Expires: Wed, 08 Aug 2018 14:43:15 GMT

Content-Type: text/html; charset=UTF-8

[root@hyc-01-01 etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/123.php"

hello world[root@hyc-01-01 etc]#

php模块配置文件php.ini下设置的open_basedir是针对服务器上所有站点的,无法精确限制

 

httpd虚拟主机配置文件中配置open_basedir

根据不同的虚拟主机限制不同的open_basedir

[root@hyc-01-01 etc]# 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.comi

    ErrorLog "logs/abc.com-error_log"

    CustomLog "logs/abc.com-access_log" common

    php_admin_value open_basedir "/data/wwwroot/abc.com:/tmp/"

     #   AuthUserFile /data/.htpasswd

    #    require valid-user

   # </Directory>

     php_admin_value open_basedir "/data/wwwroot/111.com:/tmp/"

     <Directory /data/wwwroot/111.com/nophp>

         php_admin_flag engine off

        # <FilesMatch (.*)\.php(.*)>

php_admin_value:可以定义php.ini配置文件中的参数,如open_basedirerror_logerror_reporting

/data/wwwroot/111.com:/tmp/:open_basedir中允许/tmp是因为站点的临时文件会写在/tmp目录下;用户向站点上传一张图片,该图片会先被临时存放在/tmp目录下,然后再放到对应站点目录下,如果限制访问/tmp,那么该站点将无法上传图片


猜你喜欢

转载自blog.51cto.com/12216458/2156551
8.7