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×××文件不被解析则×××者无法得到更高的权限

[[email protected] 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

[[email protected] 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

[[email protected] nophp]# curl -x127.0.0.1:80 "http://111.com/nophp/2.php"

<?php

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

[[email protected] 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无法被正常解析,所以直接被下载:

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

 

11.29 限制user_agent

user_agent:浏览器标识

cc×××:

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

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

解决:

限制user_agent减轻服务器压力;

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

使用模块mod_rewrite:

[[email protected] 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则为并且,但无法同时匹配curl和baidu.com;

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

RewriteRule .* - [F]

F即Forbiden(403)拒绝

</IfModule>

测试:

配置生效前:

[[email protected] logs]# curl -x127.0.0.1:80 "http://111.com/123.php"

hello world

生效后:

[[email protected] logs]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[[email protected] logs]# /usr/local/apache2.4/bin/apachectl graceful

[[email protected] 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

日志信息:

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

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

hello world 指定user_agent后访问正常

[[email protected] 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"

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

hello world[[email protected] -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"

[[email protected] logs]#

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

 

11.30 PHP相关配置(上)

PHP配置文件位置:

1 通过浏览器

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

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

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

操作:

[[email protected] 111.com]# touch phpinfo

[[email protected] 111.com]# vim phpinfo

<?php

phpinfo();

2 执行php –i

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

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

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

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

加载配置文件:

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

[[email protected] php-7.1.6]# cp php.ini-development /usr/local/php7/etc/php.ini

刷新apache配置:

[[email protected] php-7.1.6]# /usr/local/apache2.4/bin/apachectl graceful

php模块配置文件加载成功

php相关配置:

[[email protected] php-7.1.6]# cd /usr/local/php7/etc/

[[email protected] 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

[[email protected] etc]# /usr/local/apache2.4/bin/apachectl graceful

访问提示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

测试:

[[email protected] etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/phpinfo.php"

[[email protected] etc]# 无报错信息输出

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

[[email protected] 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

测试:

[[email protected] etc]# curl -x127.0.0.1:80 "http://111.com/phpinfo.php" -I

HTTP/1.1 403 Forbidden user_agent(curl)被禁止,所以被拒绝访问(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

[[email protected] 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

[[email protected] etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/phpinfo.php"

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

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

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

[[email protected] tmp]# ls -l php_errors.log

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

[[email protected] tmp]# ps aux|grep httpd

root     27204  0.0  1.4 259560 14380 ?        Ss   8月07   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;

[[email protected] 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下编辑新文件:

[[email protected] 111.com]# vim 3.php

<?php

echo "hytjopfj"

jaeafdjhphngiqe

[[email protected] 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

[[email protected] etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/3.php"

[[email protected] 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目录;

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

操作:

[[email protected] 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

测试:

[[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful

[[email protected] 111.com]# curl -A "a" -x127.0.0.1:80 "http://111.com/123.php"

[[email protected] 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

[[email protected] 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_basedir的1111.com目录改为111.com后再访问:

[[email protected] etc]# /usr/local/apache2.4/bin/apachectl graceful

[[email protected] 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

[[email protected] etc]# curl -A "a" -x127.0.0.1:80 "http://111.com/123.php"

hello world[[email protected] etc]#

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

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

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

[[email protected] 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_basedir、error_log、error_reporting等

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

原文地址:http://blog.51cto.com/12216458/2156551

时间: 2024-08-30 09:18:19

8.7 11.28-11.31的相关文章

11/28~11/29 学习内容

上一节课,我们通过SQL注入获取目标数据库中的数据,再通过获取帐户密码,进行爆破.后台路径成功地登录进入后台,再通过后台的文件上传漏洞,成功地获取网站的webshell,也就是获取了目标网站的权限. 今天我们讲XSS XSS利用方式   攻击客户端  窃取用户信息   管理员 普通用户 SQL注入利用数据   提权  写文件提权 XSS常用的几个payload <img/src=x> <svg/onload=alert(1)> <script>alert(1)<s

11.28 限定某个目录禁止解析php;11.29 限制user_agent;11.30,11.31 php相关配置(上下)

扩展: apache开启压缩  http://www.aminglinux.com/bbs/thread-5528-1-1.html apache2.2到2.4配置文件变更  http://www.aminglinux.com/bbs/thread-7292-1-1.html apache options参数  http://www.aminglinux.com/bbs/thread-1051-1-1.html apache禁止trace或track防止xss  http://www.aming

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

扩展 : apache开启压缩 : http://ask.apelearn.com/question/5528 apache2.2到2.4配置文件变更 : http://ask.apelearn.com/question/7292 apache options参数 : http://ask.apelearn.com/question/1051 apache禁止trace或track防止xss : http://ask.apelearn.com/question/1045 apache 配置htt

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

11.28 限定某个目录禁止解析php 核心配置文件内容<Directory /data/wwwroot/www.123.com/upload>php_admin_flag engine off</Directory>curl测试时直接返回了php源代码,并未解析 curl -x127.0.0.1:80 'http://123.com/upload/123.php' 11.29 限制user_agent user_agent可以理解为浏览器标识核心配置文件内容<IfModul

11.28限定某个目录禁止解析php11.29限制user_agent11.30-31php相关配置

11.28 限定某个目录禁止解析php例如一些目录允许上传图片,为防止有人上传带有病毒php文件,所以禁止php解析,一般存放静态的文件上的目录是不允许解析PHP文件的重新加载配置文件 创建upload目录,访问提示403状态码在浏览器打开是无法打开的,连访问的机会都没有将下图的注释掉再重新加载后测试,这时候不能解析了,显示它的源代码在浏览器打开提示下载11.29 限制user_agentvim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf重

计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

★计算1/1-1/2+1/3-1/4+1/5 -- + 1/99 - 1/100的值 #include <stdio.h> int main() { int i; double x = 1; double sum = 0; for (i = 1; i<101; i++) { sum = sum + x / i; x = x*(-1); } printf("1-1/2+1/3-1/4....-1/100=%f\n", sum); return 0; }

用c语言计算1/1-1/2+1/3-1/4+1/5-...+1/99-1/100

计算1/1-1/2+1/3-1/4+1/5-...+1/99-1/100. 方法一:计算1/1-1/2+1/3-1/4+1/5-...+1/99-1/100 #include <stdio.h>#include <math.h>main(){    float sum=0.0;     float n=0;     float a=0.0;     for (n=1;n<101;n++)     {          a=pow(-1,n+1);         sum=a/

【c语言】求1-1/2+1/3-1/4...+1/99-1/100

// 求1-1/2+1/3-1/4...+1/99-1/100 #include <stdio.h> int main() { double a = 1.0; int i; double sum = 1.0; for( i = 2; i <= 100; i++ ) { a = (-1) * a; sum = sum + a/i; } printf("1-1/2+1/3-1/4...+1/99-1/100=%f\n",sum); return 0; } <img

2018.11.28 Android踩坑(读写文件)

在学到使用Android Device Monitor 查看文件的时候,发现Android Device Monitor在Android Studio里面找不到了,网上查了原来是被官方弃用了,现在通过命令行的方式启动 1.打开Project Structure找到sdk安装路径 2.找到这个路径打开里面tools文件夹下面的monitor.bat双击运行即可打开Android Device Monitor 虽然说找到了Android Device Monitor,但是打开报了个Could not

11.10/11.11/11.12 安装PHP511.13安装PHP7

- 11.10/11.11/11.12 安装PHP5 - 11.13 安装PHP7 - 扩展 - php中mysql,mysqli,mysqlnd,pdo到底是什么 - http://blog.csdn.net/u013785951/article/details/60876816 - 查看编译参数 http://ask.apelearn.com/question/1295 # 11.10安装PHP5 上 -  PHP官网www.php.net -  当前主流版本为5.6/7.1  1.   c