#49670 [Bgs]: prefix bypass in file_get_contents
| From: | david at majorsecurity dot info | Date: | Sun, 27 Sep 2009 20:47:09 +0000 |
| Subject: | #49670 [Bgs]: prefix bypass in file_get_contents | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-142962@lists.php.net to get a copy of this message | ||
ID: 49670
User updated by: david at majorsecurity dot info
Reported By: david at majorsecurity dot info
Status: Bogus
Bug Type: Filesystem function related
Operating System: Unix
PHP Version: 5.3.0
New Comment:
Hello. In my oppinion this is an issue that should at least be
mentioned into the documentation of the this function. Reason? Because
unfortunelly there are a lot of so called "security guys" that says you
should safe your page by setting a prefix like "site_" and define an
extension like ".txt". That would end up in having a request like
target.com/page=news which than is passed as i.e. "site_news.txt". So as
php developer you may think that you're on the safe site because it is
not able to open any file that does not have the defined prefix or file
extention -> which is definetly wrong!
So what I'm saying is that is would make sense to inform the developers
and users directly on the documenation/manual to this function that it
is insecure if you trust in any prefix or suffix and mention that you
should do following stept when using file_get_contents:
1.) filter out the poison null byte ( %00 )
2.) filter ".." and "/" to ensure that no directory traversal is
possible any more while using file_get_contents
3.) Put all trusted parameter values into a whitelist
4.) Meticulously go through the code forcing PHP to cast the data to
the desired type, in this case the (string) casts
And it is NOT a solution just to say "NEVER trust the user input".
There should be some infos like that on the documentation site.
That's my 2 cent ... and I am working as professional it security
consultant and doing penetration tests since years ... so I know what
I'm talking about.
Previous Comments:
------------------------------------------------------------------------
[2009-09-25 21:59:00] jani@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
You must be kidding? This is not a file_get_contents() issue at all,
you
just failed the 1st rule of coding: NEVER trust the input.
------------------------------------------------------------------------
[2009-09-25 18:42:08] david at majorsecurity dot info
Description:
------------
If you use a prefix (i.e myownprefix_ ) in your source code to make
sure that only files containing this prefix can be opened, this can be
bypassed within file_get_contents().
Let's say that you have placed 2 files in your htdocs folder:
1. /htdocs/david/myownprefix_home.php
2. /htdocs/david/home.php
3. /htdocs/bug.php
Reproduce code:
---------------
---
From manual page: function.file-get-contents
---
Example code (bug.php):
<?php
if(isset($_GET['seite'])) {
$seite = $_GET['seite'];
$datei =
nl2br(file_get_contents("./david/myownprefix_$seite.php"));
echo $datei;
}
?>
Proof of concept:
http://localhost/bug.php?seite=/../home
Expected result:
----------------
Now this would open the file /htdocs/home.php and our defined prefix
will be bypassed.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49670&edit=1