EDIT FOR MORE INFO I have a page in an admin area where users can write their own PHP/HTML files for adding extra functionality etc they want to use in their website. I have an array of "dangerous" words to filter out any messing with core database tables, such as being able to write a malicious code to drop the members table (for example).
The textarea/code area takes the content and uses fwrite
to put it all into a PHP file which can then be included via template tags on a website.
When the file is saved, it reads all the text and checks it against the "dangerous array" of words that can't be used (eg: exec, system, delete from, drop database etc).
But I've recently found a flaw where a variable like below can be put in the file, and then a mysql
command could be used like so:
$var = "DR" . "OP ". "TA" . "BL" . "E";
$sql = mysql_query($var tablename);
Checking all the file contents through a array match of words doesn't work in this scenario.
$dangerous = "/\b(exec|system|delete from|drop database)\b/i";
if (preg_match($dangerous, $code)) {
$tag_code = preg_replace($dangerous, '', $tag_code);
}
------- end edit
I have a function which checks an array of "bad words" and filters them out when saving a new custom user generated file (similar to what this question says), but I've discovered that it can easily be circumvented by splitting the invalid words with a variable.
For example, "DELETE FROM" is an invalid phrase, but if a file is made with this:
$var = "DE" . "LET". "E " . "FR" . "OM";
The array filter obviously can't detect this in the usual way. The problem is that these variable "hacks" can be written in any combination, so I'm not sure how - or if - there's a way to detect things like this in a file.
Can this be done?