|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
But imagine if, rather than entering only a name, the user types <HR><H1><P ALIGN=CENTER>Jane Smith</P></H1><HR>. The result would be Figure 35.4probably not what you wanted. Or imagine if a hacker entered <IMG SRC=/secret/project/funnyface.jpg>
when you requested the users name. Again, if the code from the preceding snippet were part of a CGI script with this HTML in the $user_Name variable, your Web server would happily show the hacker your secret adorable toddler picture! Figure 35.5 is an example.
Or what if The last signee!<FORM><SELECT> was entered as the users name in a guest book? The <SELECT> tag would cause the Web browser to ignore everything between it and a nonexistent </SELECT>, including any names that were added to the list later. Even though 10 people signed the guest book shown in Figure 35.6, only the first three appear because the third name contains a <FORM> and a <SELECT> tag.
But even more dangerous than entering simple HTML, a malicious hacker might enter a server-side include directive instead. If your Web server is configured to obey server-side includes, a user might type <!-- #include file=/secret/project/plan.txt --> instead of his name to see the complete text of your secret plans. Or he can enter <!---- #include file=/etc/passwd ----> to get your machines password file. And probably worst of all, a hacker might input <!---- #exec cmd=rm rf / ---->, and the innocent code in the snippet shown before would proceed to delete almost everything on your hard disk. This is a primary example of why Web servers should not run as root (the Super User). Running the Web server under a non-privileged user is the best way to reduce problems in case someone does find a hole in your script. See Common SSI Commands, p. 806.
Two solutions exist to the problem of the user entering HTML rather than flat text:
Handling External ProcessesAnother area where you must be careful is how your CGI script interfaces user input with any external processes. Because executing a program outside of your CGI script means that you have no control over what it does, you must do everything you can to validate the input you send to it before the execution begins. For instance, shell scripts often make the mistake of concatenating a command-line program with form input and then executing them together. This works fine if the user has entered what you expected, but additional commands may be sneaked in and unintentionally executed. The following fragment of shell script commits this error: FINGER_OUTPUT=finger $USER_INPUT echo $FINGER_OUTPUT If the user politely enters the email address of a person to finger, everything works as it should. But if he enters an email address followed by a semicolon and another command, that command will be executed as well. If the user enters webmaster@www.yourserver.com;rm -rf /, youre in considerable trouble.
Even if a hidden command isnt placed into user data, innocent input may give you something you dont expect. The following line, for instance, will give an unexpected resulta listing of all the files in the directoryif the user input is an asterisk: echo Your input: $USER_INPUT When sending user data through the shell, as both of these code snippets do, its a good idea to screen it for shell meta-characters. Such characters include the semicolon (which allows multiple commands on one line), the asterisk and the question mark (which perform file globbing), the exclamation point (which, under csh, references running jobs), the back quote (which executes an enclosed command), and so on. Like filtering filenames, maintaining a list of allowable characters is often easier than trying to catch each character that should be disallowed. The following Perl fragment crudely validates an email address: if ($email_Address ~= /[^a-zA-Z0-9_\-\+\@\.]) { # Illegal character! } else { system(finger $email_Address); }
|
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. |