Re: file_exists
| From: | Stut | Date: | Thu, 15 Nov 2007 22:54:08 +0000 |
| Subject: | Re: file_exists | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-264651@lists.php.net to get a copy of this message | ||
Instruct ICC wrote:
You clearly don't know what the realpath function does. Look it up. -Stut -- http://stut.net/Something like the following would be much better (untested)... $page = realpath(dirname(__FILE__).'/inc/'.$_GET['page'].'.php'); $expecteddir = realpath(dirname(__FILE__).'/inc'); if (substr($page, 0, strlen($expecteddir)) != $expecteddir) {Good points about (.php, evil-payload, and evil-payload.php?). Although I'll defer to a security expert, your modification looks good to not include a remote site's code. But on a shared host, what about this?: index.php?page=../../../../../../../../../../../../home/evil-user-home-dir/evil-payload.php If that gives something like: $expecteddir === "/home/stut/phpstuff/inc/../../../../../../../../../../../../home/evil-user-home-dir/evil-payload.php" maybe it will include "/home/evil-user-home-dir/evil-payload.php"// Ideally return a 403 status here die('Access denied');} // Now we know it's a file in the right directory if (file_exists($page)) {include($page);} else {// Return a 404 status here die('Resource not found');} That should lock the requested page to the given directory. If anyone can see any way around that I'd be interested in hearing about it. -Stut -- http://stut.net/No, you've missed the point. $expecteddir is a fixed variable that you, the script author, specify. It does not contain anything coming from external veriables. You then compare the full path you build from the external variables to $expecteddir to verify that the file is in the right directory. I suggest you read the code I posted again. -StutI meant if $page evaluates to "/home/stut/phpstuff/inc/../../../../../../../../../../../../home/evil-user-home-dir/evil-payload.php" which it does not. However I don't think your if (substr($page, 0, strlen($expecteddir)) != $expecteddir) ever evaluates to TRUE. So you'll never get Access denied. So how you set $page saved your ass. Good job.