2010-07-31 18:20Checking PHP security with PHPOne interesting thing about software is that because it is written in a language that a computer can understand, it is also possible, in theory, to get the computer to tell you things about that software. There are various classes of utilities from static analysers to code style checkers, but I have recently been considering how possible it is to check for uses of specific deprecated functions and classes in a PHP project. The actual checker I was thinking of creating would use regular expressions and be written in PHP itself, but because the process would be a static analysis one, which I hoped didn’t have to resort to tokenising the PHP source code it was examining, this checker could in fact be written in any language just as (if not more) easily. As is often the case in my blog posts about PHP recently, it turns out that various “features” of PHP make things more difficult (regardless of the language that the checker is written in), although trying to solve a problem the wrong way doesn’t help either. The checkerThere are many potential properties of a PHP project that one might want to verify, but the particular case which motivated this blog post was a switch from PHP’s mysql and mysqli raw query functions to its prepared statement functions, in a project I was working on. The first approach one might try is to grep the source code for mentions of mysql_query or mysqli_query, but in this project (most of) the actual uses of those functions were nicely encapsulated in a DB class. So, grepping could at least find instances where the DB class was not used, and they could be ported over to using it, but then the task becomes one of detecting instances of where the DB class is being used. This is not simply a matter of removing the deprecated methods on the old DB class and seeing what breaks, however, as unfortunately the old functionality had to be kept around for use by some legacy code (which is not exposed to users). The idea for the improved checker, therefore, was to also look for instances of loading the DB class, so that files which were not entitled to use it could be transitioned to the new preparedStatementDB class. This meant looking for occurrences of One final class of security assurances we would want is that no one had used prepared statements in an unsafe way, such as using string manipulation to build the queries that are then prepared. This could in theory be done by checking that the The attacksOf course, if one is trying to write software to check for deliberate security vulnerabilities introduced by rogue contributors, perhaps one has a bigger problem than the choice of programming language, but I like to think that if you aim for stopping deliberate errors, you end up catching a lot of accidental ones you otherwise would have missed. The sort of accidental error in the code someone might make, which would thwart the automated checker, is inclusion of excessive whitespace (possibly even new lines) between the “ Where it gets really tricky is when people start doing Unfortunately there is a wider class of run-time generated code that PHP will execute, which means casting the net even wider to catch the most sneaky coder. For example, PHP has an ConclusionSo, it seems almost possible to write a regular expression based checker of PHP source files that checks if the code conforms to some enhanced-security subset of PHP. Most PHP coders probably don’t understand the factory pattern anyway, and if they did you could possibly wrap up the dangerous But would using a proper language like Java with its Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
QuicksearchCategoriesSyndicate This BlogBlog Administration |