PHP 安全根

发布于 2024-09-15 07:02:59 字数 186 浏览 4 评论 0原文

我的朋友在我的脚本中发现了一个问题,它提供了对根文件的访问权限。

此网址提供密码文件:

http://site.com/attachment.php?file=../../../../../../etc/passwd

如何逃脱此安全漏洞?

My friend found a problem in my script, it gives acces to root files.

This url gives passwd file:

http://site.com/attachment.php?file=../../../../../../etc/passwd

How to escape this security hole?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-09-22 07:02:59

不要使用 URL 字符串下载文件....定义唯一 ID 来表示文件,而不是路径。

您可能见过这样的下载 http://www.mysite.com/download.php?id=23423 他们的作用是,使用此 id 从数据库中取出文件名和路径然后下载它。

Dont download the files using URL String.... Define unique IDs to denote a file, rather than paths.

You might have seen downloads like this http://www.mysite.com/download.php?id=23423 what they do, use this id, to take out the file name and path from the db and then download it.

半暖夏伤 2024-09-22 07:02:59

有几种不同的解决方案。
如果只能有一个文件名,则可以使用 basename() 解决方案。

但是,如果它可以是路径,则需要更复杂的解决方案

//assume current directory, but can be set anything. Absolute path of course
$basedir   = dirname(__FILE__);
//assume our files are below document root. 
//Otherwise use it's root dir instead of DOCUMENT_ROOT
$filename  = realpath($_SERVER['DOCUMENT_ROOT'].$_GET['file']);
if (substr($filename,0,strlen($basedir)) !== $basedir) {
  header ("HTTP/1.0 403 Forbidden"); 
  exit; 
}

,还有一个有用的 PHP 配置选项 open_basedir

There are several different solutions.
If there can be only a filename, a basename() solution would work.

However, if it can be path, a more complex solution is needed

//assume current directory, but can be set anything. Absolute path of course
$basedir   = dirname(__FILE__);
//assume our files are below document root. 
//Otherwise use it's root dir instead of DOCUMENT_ROOT
$filename  = realpath($_SERVER['DOCUMENT_ROOT'].$_GET['file']);
if (substr($filename,0,strlen($basedir)) !== $basedir) {
  header ("HTTP/1.0 403 Forbidden"); 
  exit; 
}

there is also a useful PHP configuration option open_basedir

吻安 2024-09-22 07:02:59

您可以使用 realpath()dirname() 检查 URL $_SERVER['DOCUMENT_ROOT'] (或者任何可以“安全”下载的目录)。

如果realpath()的结果指向安全目录之外,您可以拒绝下载请求。

还有 open_basedir 安全指令(和运行时选项自 5.3 起)。

You can use realpath() and dirname() to check URLs against $_SERVER['DOCUMENT_ROOT'] (or whatever directory is "safe" for downloading).

If the result of realpath() points outside the safe directory, you can deny the download request.

There's also the open_basedir security directive (and runtime option as of 5.3).

海风掠过北极光 2024-09-22 07:02:59

我想您有一个存储所有附件的目录。

只需测试文件是否位于您的目录中。

 // http://www.php.net/manual/en/function.basename.php
 // http://cz.php.net/manual/en/function.file-exists.php 
 if (file_exists($attachments_path . "/" . basename($_GET['file'])) {
  // do work
 }

Starx 发布了一个看起来不错的解决方案。不过,这可以在没有数据库的情况下完成。如果有人上传文件,您可以将该文件存储为 md5($filename).$extension 并使用您的脚本。

I suppose you have a directory where all attachments are stored.

Just test if file is located in your directory.

 // http://www.php.net/manual/en/function.basename.php
 // http://cz.php.net/manual/en/function.file-exists.php 
 if (file_exists($attachments_path . "/" . basename($_GET['file'])) {
  // do work
 }

Starx posted a solution which seems fine. It can be done without a database, though. If somebody uploads a file you can store the file as md5($filename).$extension and use your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文