Forum Moderators: coopster
This is what I've currently doing,
ht tp://www.site.com/?p=includesname
$p = $incl
if (is_file("$incl")) {
include($incl);
}
else {
include('_pages/error.inc');
}
Would using this be better or even fix my problem with my includes being insecure?
$p = $_REQUEST["p"];
if (is_file("$p")) {
$incl = $p;
}
else {
$p = 'error.inc');
}
I dont really want to change all my pages with numeric names and check they're valid pages. I have a few pages that get varibles from the query strings & the checking could get somewhat complicated.
The problem is your only check of your incoming variable is if is_file($var); , whether you get this from $_REQUEST or not (meaning register_globals needs to be on).
I didn't even know about is_file so I looked it up, and it looks like it could be used as a sort of informal ad-hoc security check if you use php4, since the manual notes that only php5 supports url wrappers for it (meaning, in short, that hackers can use it to include off-site files - like nasty code on their own site, or even better, someone else's site they've compromised).
However, there isn't any mention of using this in conjunction with security, so a clever hacker could probably compromise it.
Have you been hit by a worm, btw?
Better would be to keep register globals off, and do something like:
$p = $_REQUEST['p'];
if(!preg_match('#^[a-zA-Z0-9_-]*\.[a-z]*$#', $p)) die('invalid characters in parameter');
Depending on your version of PHP and your setup this may not work. What I mean is that in later version is_file can accept url wrappers. Best thing to do is to match it off a list of known files (which could be automated).
For other tips see this link
[devx.com...]
Tim
It's better to 'whitelist' what you want than 'blacklist'. I.e., checking for slashes is good, but it's better to think, I only have letters, numbers, dashes and underlines in the filename, with one dot for the extension, followed by all lowercase letters - so when I include, let me check for that pattern. Don't think - 'throw it out if it's got this in it' - think : 'include it only if it's in the list of things that are ok'.
It's great you're just thinking about security to begin with, though - there are developers who don't consider themselves hobbyists who still don't get this far.