Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.
Naturally we'd want some kind of validation to make sure that $_GET['page'] isn't anything deadly. However I began wondering about cross server includes a while back and a huge problem they could cause. Last night I tried it out, I'll give an example of what I did on my server. (domain.com being a different site)
I expected the database info for the site but no dice. Is it because I am including a file from a different server? Then why all the problems with the above template system?
Okay, here's the dice. If the server's PHP parser is on, the files are safe, because when you try to include the file, the server wil parse it and you'll simply have a blank page.
The big problem is when another person tricks YOU into including the wrong file.
if ( !isset( $_GET['p'] ) )
$_GET['p'] = "Main";
else if ( is_file("/absolutepath/Pages/LoggedIn/{$_GET['p']}.php") ) {
if ( isset($_SESSION['id']) )
$_GET['p'] = "/LoggedIn/{$_GET['p']}";
else {
errorBox( "You must be logged in to view this page." );
unset( $_GET['p'] );
}
} else if ( is_file("/absopath/Pages/Landing/{$_GET['p']}.php") )
$_GET['p'] = "/Landing/{$_GET['p']}";
else if ( !is_file("/absopath/Pages/{$_GET['p']}.php") )
$_GET['p'] = "Error";
if ( $_GET['p'] ) include "/absopath/Pages/{$_GET['p']}.php";
Looks a little messy, but it works really well, never had any problems. If the $_GET['p'] page isn't in the folders you specify, it will never get included. Hackers got nothin on me.
Last edited by Todd_Z on Sat Aug 13, 2005 11:18 am, edited 1 time in total.
Well you aren't stripping out "../"s so its possible that a file could escape the sandbox. It is forced to be a php file so they couldn't show the passwd file. However I would suggest some for of cleaning on the filename to remove ../s
and test.php is at /home/Roja/public_html/test, it will display true to both.
Which means you can then include a php file from anywhere in the path - not just the subdirectory you defined..
Todd_Z wrote:Looks a little messy, but it works really well, never had any problems. Hackers got nothin on me.
Thats why statements like "I've never had any problems" have no place in a discussion about security. Just because you haven't had a problem, doesn't mean there isn't one.
And yes, hackers do have something on you - some tricks and knowledge.
explain to me how if I am including "/home/blah/public_html/{$_GET['p']}/" how a hacker could view files above the public_html folder? If you tried to include /home/blah/public_html/../../index.php", you would get an error for file not being found.
would probably be the "easiest" way to do this, I personally never include anything from user input, I opt to store my content in the database.
Yup, that's what I was trying to get at. Unless you match with a regexp to "cleanse" the data to ensure it has no ../../s or use an explicit basename, there is a chance they can break out.
Its a very small chance, in this cause because his example is forced to be a php file, but attackers could try some common "cruft" like index-old, or index-bak or debug, etc all of which might be present, etc. A lot of people might have a "testing.php" in their webroot for phpinfo.... so they might be able to find something like that, etc