Given the frightening effects that unchecked user input can have on a Web site and its users, one would think that carrying out the necessary safeguards must be a particularly complex task. After all, the problem is so prevalent within Web applications of all types, prevention must be quite difficult, right? Ironically, preventing these types of attacks is really a trivial affair, accomplished by first passing the input through one of several functions before performing any subsequent task with it. Four standard functions are conveniently available for doing so: escapeshellarg() , escapeshellcmd() , htmlentities() , and strip_tags() . Note Keep in mind that the safeguards described in this section, and frankly throughout the chapter, while effective, offer only a few of the many possible solutions at your disposal. For instance, in addition to the four functions described in this section, you could also typecast incoming data to make sure it meets the requisite types as expected by the application. Therefore, although you should pay close attention to what’s discussed in this chapter, you should also be sure to read as many other security-minded resources as possible to obtain a comprehensive understanding of the topic. Escaping Shell Arguments The escapeshellarg() function delimits its arguments with single quotes and escapes quotes. Its prototype follows: string escapeshellarg(string arguments) The effect is such that when arguments is passed to a shell command, it will be considered a single argument. This is significant because it lessens the possibility that an attacker could masquerade additional commands as shell command arguments. Therefore, in the previously described file-deletion scenario, all of the user input would be enclosed in single quotes, like so: /opt/inventorymgr '50XCH67YU' '50; rm -rf *' Attempting to execute this would mean 50; rm -rf * would be treated by inventorymgr as the requested inventory count. Presuming inventorymgr is validating this value to ensure that it’s an integer, the call will fail and no real harm will be done. Escaping Shell Metacharacters The escapeshellcmd() function operates under the same premise as escapeshellarg() , but it sanitizes potentially dangerous input program names rather than program arguments. Its prototype follows: string escapeshellcmd(string command) This function operates by escaping any shell metacharacters found in the command. These metacharacters include # & ;`, |*?~< >^( ) [ ] { } $ \\ . You should use escapeshellcmd() in any case where the user’s input might determine the name of a command to execute. For instance, suppose the inventory-management application is modified to allow the user to call one of two available programs, foodinventorymgr or supplyinventorymgr , by passing along the string food or supply , respectively, together with the SKU and requested amount. The exec() command might look like this: exec("/opt/".$command."inventorymgr ".$sku." ".$inventory); Assuming the user plays by the rules, the task will work just fine. However, consider what would happen if the user were to pass along the following as the value to $command : blah; rm -rf *; This assumes the user also passes in 50XCH67YU and 50 as the SKU and inventory number, respectively. These values don’t matter anyway because the appropriate inventorymgr command will never be invoked since a bogus command was passed in to execute the nefarious rm command. However, if this material were to be filtered through escapeshellcmd() first, $command would look like this: blah\; rm -rf \*; This means exec() would attempt to execute the command /opt/blah rm -rf , which of course doesn’t exist. Please check back for the conclusion to this series.
blog comments powered by Disqus |
|
|
|
|
|
|
|