My web site - 532Part IIIAdvanced Features and TechniquesPossible AttacksConnecting your server
Monday, July 23rd, 2007532Part IIIAdvanced Features and TechniquesPossible AttacksConnecting your server to the Internet is like setting up a storefront on a busy street. You relikely to have quite a few visitors, but if you re not careful, some less than desirable visitorsmay take advantage of you. Site defacementOften more embarrassing than harmful, site defacements are fairly common because thecracker has an opportunity to publicize his or her exploitation. Site defacements are some- times left as calling cards by a cracker who entered a system by more complicated means. It is possible to deface a badly designed Web site using only a Web browser. Take, for instance, the following program: $visitorn ); fclose($fp); } ?>
Visitors to this site:
This program implements a very rudimentary guest book. In reading this code, however, youshould feel a bit uneasy. Don t trust the network.This program accepts form data that weexpect to contain the visitor s name (in the variable $visitor) and stores it in a text file fordisplay to subsequent visitors. For the inputs we expect, there is no trouble. Now put on your script-kiddie hat for a moment and imagine what would happen if the inputcontained HTML tags. This simple program would blindly insert those tags into the pages itgenerates, and other visitors browsers would interpret them as usual. One particularly mali- cious tag is the 533Chapter 29SecurityWhen visitors load the guest book, their browsers receive this tag and immediately beginloading the hacked site. With a little ingenuity, the cracker could then take advantage of thevisitors trust of your site to extract personal information such as passwords or credit cardnumbers. The solution to this problem is to sanitize the input data. In this case, we want any charactersthat have special meaning to a browser to be translated into something harmless. Luckily, PHP provides a way to perform just such a translation. The function htmlspecialchars() converts the characters <, >, , and &to their representations as HTML entities (such as<). We change the first part of our program to use this new function as follows: $clean_visitorn ); fclose($fp); } ?> And we have patched a very significant security hole in our site. Accessing source codeEven if your PHP source code isn t a trade secret, you should still protect it from exposure tothe network. If an intruder can read your source code, then he or she need not experiment tofind a weakness. Instead, the intruder can simply analyze the code, looking for common mis- takes and other security holes. In general, the more helpful information you provide to poten- tial intruders, the more likely an intrusion. By hiding such tidbits as source code, directorynames, or usernames from the network, you can reduce the likelihood of an attack. One handy feature of PHP, error reporting to the browser, is great for development becauseit helps pinpoint problems but it can be bad for security, because it can also give directorypaths, filenames, usernames, and potentially database names on error. Minimize the risk byturning off error reporting to the browser in production systems, via the display_errorsdirective in php.ini. You can still use error reporting to the browser on development sys- tems if you wish, although it s safer to use the error_log()function to write error mes- sages to a log. CautionCrackers, script-kiddies, and other fiendsThe term hackeris commonly used to describe individuals more correctly labeled crackers. Within the computer community, crackersare those who, through luck or skill, break into com- puter systems and cause damage. Hackers are those who can hack read and write efficient(and often obscure) code in many languages. To a programmer, being labeled a hacker is anhonor, whereas being labeled a cracker probably means he or she should start reading the HelpWanted section. As if crackerwas not sufficiently derogatory, young crackers who use tools and scripts they findon the Web are called script-kiddies. These budding lawbreakers often have little understandingof what they are actually doing. They are usually the culprits behind low-tech attacks such as sitedefacement. A fairly good indicator of the work of a script-kiddie is the excessive use of mis- spelling and capitalization, as in W3RKOOLD00Dz.