|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
If you decide that you must allow shell meta-characters in your input, ways exist to make their inclusion saferand other ways dont actually accomplish anything. Although you may be tempted to put quotation marks around user input that hasnt been validated to prevent the shell from acting on special characters, this almost never works. Look at the following: echo Finger information:<HR><PRE> finger $USER_INPUT echo </PRE> Although the quotation marks around $USER_INPUT will prevent the shell from interpreting, for example, an included semicolon that would enable a hacker to piggyback a command, this script still has several severe security holes. For instance, the input might be rm -rf /, with the back quotes causing the hackers command to be executed before finger is even considered. A better way to handle special characters is to escape them so that the shell takes their values without interpreting them. By escaping the user input, all shell meta-characters are ignored and treated instead as just more data to be passed to the program. The following line of Perl code does this for all nonalphanumeric characters: $user_Input =~ s/([^w])/\\\1/g; Now, if this user input were appended to a command, each charactereven the special characterswould be passed through the shell to finger. In general, validating user inputnot trusting anything sent to youwill make your code easier to read and safer to execute. Rather than trying to defeat a hacker after youre already running commands, give data the once-over at the door.
Guarding Against Loopholes when Executing External ProgramsWhen executing external programs, you must also be aware of how the user input you pass to those programs will affect them. You may guard your own CGI script against hacker tricks, but its all for naught if you blithely pass anything a hacker may have entered to external programs without understanding how those programs use that data. Many CGI scripts will send email to a particular person, for instance, containing data collected from the user by executing the mail program. This can be dangerous because mail has many internal commands, any of which can be invoked by user input. If you send text entered by the user to mail, for example, and that text has a line that starts with a tilde (~), mail will interpret the next character on the line as one of the many commands it can perform. ~r /etc/passwd, for example, will cause your machines password file to be read by mail and sent off to whomever the letter is addressed to, perhaps even the hacker. In an example such as this one, rather than using mail to send email from UNIX machines, you should use sendmail, the lower-level mail program that lacks many of mails features. But, of course, you should also be aware of sendmails commands so those cant be exploited. As a general rule, when executing external programs, you should use the one that fits your needs as closely as possible, without any frills. The less an external program can do, the less it can be tricked into doing.
Another example that demonstrates you must know your external programs well to use them effectively is grep. Most people will tell you that you cant get into much trouble with grep. However, grep can be fooled fairly easily, and how it fails is illustrative. The following code is an example: Its supposed to perform a case-sensitive search for a user-supplied term among many files: print(The following lines contain your term:<HR><PRE>); $search_Term =~ s/([^\w])/\\$1/g; system(grep $search_Term /public/files/*.txt); print(</PRE>); This all seems fine, unless you consider what happens if the user enters -i. Its not searched for, but functions as a switch to grep, as would any input starting with a dash. This will cause grep either to hang while waiting for the search term to be typed into standard input, or to error out when anything after the -i is interpreted as extra switch characters. This, undoubtedly, isnt what you wanted or planned. In this case, its not dangerous, but in other cases it might be. A harmless command doesnt exist, and each must be carefully considered from every angle. You should be as familiar as possible with every external program your CGI script executes. The more you know about the programs, the more you can do to protect them from bad data, both by screening that data and by disabling options or disallowing features.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |