PHP 安全根
我的朋友在我的脚本中发现了一个问题,它提供了对根文件的访问权限。
此网址提供密码文件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用 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.有几种不同的解决方案。
如果只能有一个文件名,则可以使用 basename() 解决方案。
但是,如果它可以是路径,则需要更复杂的解决方案
,还有一个有用的 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
there is also a useful PHP configuration option
open_basedir
您可以使用
realpath()
和dirname()
检查 URL$_SERVER['DOCUMENT_ROOT']
(或者任何可以“安全”下载的目录)。如果
realpath()
的结果指向安全目录之外,您可以拒绝下载请求。还有 open_basedir 安全指令(和运行时选项自 5.3 起)。
You can use
realpath()
anddirname()
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).
我想您有一个存储所有附件的目录。
只需测试文件是否位于您的目录中。
Starx 发布了一个看起来不错的解决方案。不过,这可以在没有数据库的情况下完成。如果有人上传文件,您可以将该文件存储为
md5($filename).$extension
并使用您的脚本。I suppose you have a directory where all attachments are stored.
Just test if file is located in your directory.
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.