Download Zend Enterprise PHP Patterns
Transcript
C H A P T E R 6 N S E C U R I N G Y O U R P H P A P P LI C A T I O N S To prevent this sort of attack vector from being successful your application should always be very specific about where it expects its input to come from. For example, instead of using the [NAMQAOP superglobal (that could mean it was a GET, POST, or cookie value), use the specific data source superglobals of [CAP, [LKOP, or [?KKGEA as needed for the logic of your application. Another common source of attack vectors comes from an unhealthy trust of the [OANRAN superglobal within PHP applications. While there are certain data elements of this array that you could argue are indeed worthy of trust, many of the more commonly used values are provided by the client during the request and are just as dangerous as any data found in [CAP. For example, while the [OANRANW#NAIKPA[=@@N#Y field is a safe value (the IP address that originated the request) the [OANRANW#DPPL[T[BKNS=N@A@[BKN#Y variable (used if the requesting client is behind a proxy) is not and must be validated to ensure that data was not interjected. Likewise, the [OANRANW#DPPL[NABANAN#Y variable (the URL of the previously requested page) is entirely determined by an HTTP header sent by the client and must be scrutinized before usage. Of all of the [OANRAN variables, however, the ones that are the most dangerous are also the ones that are used by far the most in PHP applications: the [OANRANW#LDL[OAHB#Y, [OANRANW#L=PD[EJBK#Y, [OANRANW#DPPL[DKOP#Y, and [OANRANW#L=PDPN=JOH=PA@#Y variables. These variables are dangerous because their value is actually determined in significant degree by the URL requested by the client to the web server and therefore are potential attack vectors. For example, the [OANRANW#LDL[OAHB#Y variable is widely used as the action attribute of an HTML form but is calculated based on the URL used to request the current page. Since a request to a URL such as dppl6++sss*at]ilha*_ki+ej`at*ldl+bkk+^]n is a completely valid request (causing the ej`at*ldl script to be requested with +bkk+^]n as the [OANRANW#L=PD[EJBK#Y), using this variable blindly allows an attacker to encode JavaScript into the path information and ultimately cause it to be executed in the application when the output is returned. That said, the bottom line is clear: just because it is data you are receiving from the underlying web server doesn’t mean it is secure. There are a number of times the web server simply passes the data it receives from the requesting client along to PHP, and therefore all of it should be filtered and validated as any other data is. Data Filtering and Validation Once you have limited your input data sources to the specific ones expected by your application (and identified the untrustworthy data), next it is important that you filter the data from these sources and then validate that data to ensure that it meets your assumed criteria. For example, if you expect the variable [CAPW#j]ia#Y to be an alphabetical string between 2 and 20 characters, you would first filter that variable to remove any non-alphabetical characters that may exist and then validate that the resulting string is between 2 and 20 characters in length. This process of input filtering and validation can be a time-consuming process and often is a place where corners are cut in order to meet deadlines. Thankfully however, Download at WoWeBook.Com 127