Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About
知乎专栏多维度架构 微信号 netkiller-ebook | QQ群:128659835 请注明“读者”

7.2. php.ini

7.2.1. Magic quotes

限于5.2。x 版本

magic_quotes_gpc = On
magic_quotes_runtime = On
			

测试程序

			
<form action="" method="post" >
STR:<input type="text" name="str">
<input type="submit">
</form>
<?php

if (get_magic_quotes_gpc()) {
	$str = $_POST['str'];
	echo '这里是get_magic_quotes_gpc()转义过后的:' ,$str, '<hr />';
} else {
	$str = addslashes($_POST['str']);
	echo '现在通过addslashes传递过来的值是:' ,$_POST['str'], '<br>';
}


function stringFilter($str)
{
	if (ini_get('magic_quotes_gpc)') {
		return $str;
	} else {
		return addslashes($str);
	}
}
			
			

7.2.2. 危险PHP函数

这些函数应该尽量避免使用它们

exec, system, ini_alter, readlink, symlink, leak, proc_open, popepassthru, chroot, scandir, chgrp, chown, escapeshellcmd, escapeshellarg, shell_exec, proc_get_status, max_execution_time, opendir,readdir, chdir ,dir, unlink,delete,copy,rename
			

对于后门植入主要是用下面几个方法

eval, gzinflate, str_rot13, base64_decode
			

针对目录与文件的函数

disable_functions=chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,copy,mkdir,rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown
			

针对 php.ini 操作的函数

ini_set,
			

7.2.2.1. chdir()函数安全演示

				
$ cat chdir.php
<pre>
<?php
echo "current:".getcwd();
echo '<br />';
chdir('/');
echo "chdir:".getcwd();
echo '<br />';
$lines = file('etc/passwd');

foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
?>
</pre>

				
				

运行结果

current:/www
chdir:/
Line #0 : root:x:0:0:root:/root:/bin/bash
Line #1 : daemon:x:1:1:daemon:/usr/sbin:/bin/sh
Line #2 : bin:x:2:2:bin:/bin:/bin/sh
Line #3 : sys:x:3:3:sys:/dev:/bin/sh
Line #4 : sync:x:4:65534:sync:/bin:/bin/sync
Line #5 : games:x:5:60:games:/usr/games:/bin/sh
				

7.2.3. 隐藏PHP版本信息

expose_php Off
			

7.2.4. session名字可以泄露你的服务器采用php技术

session.name = PHPSESSID
			

伪装成Tomcat

session.name = JSESSIONID
			

7.2.5. 隐藏PHP出错信息

display_errors = Off
			
同时开启error_log日志
error_log = php_errors.log
			

7.2.6. open_basedir 防止操作web环境意外文件目录

			
open_basedir = /www/:/tmp/
			
			

测试脚本

			
<?php
chdir('/etc');

printf(file('/etc/fstab'));
			
			

实际效果

			
Warning: chdir(): open_basedir restriction in effect. File(/etc) is not within the allowed path(s): (/www/:/tmp/) in /www/index.php on line 2

Warning: file(): open_basedir restriction in effect. File(/etc/fstab) is not within the allowed path(s): (/www/:/tmp/) in /www/index.php on line 2

Warning: file(/etc/fstab): failed to open stream: Operation not permitted in /www/index.php on line 2