Seven habits for writing secure PHP applications


SUBMITTED BY: Guest

DATE: April 30, 2013, 11:37 p.m.

FORMAT: PHP

SIZE: 23.9 kB

HITS: 1293

  1. ------------------------------------------------------------------------
  2. - Validate input
  3. - Guard your file system
  4. - Guard your database
  5. - Guard your session data
  6. - Guard against Cross-Site Scripting (XSS) vulnerabilities
  7. - Verify form posts
  8. - Protect against Cross-Site Request Forgeries (CSRF)
  9. ------------------------------------------------------------------------
  10. - Validate input
  11. Validating data is the most important habit you can possibly adopt when it comes to security. And when it comes to input, it's simple: Don't trust users. Your users are probably good people, and most are likely to use your application exactly as you intended. However, whenever there is chance for input, there is also chance for really, really bad input. As an application developer, you must guard your application against bad input. Carefully considering where your user input is going and what it should be will allow you to build a robust, secure application.
  12. Although file system and database interaction are covered later, there are general validation tips that cover every sort of validation:
  13. Use white-listed values
  14. Always revalidate limited selections
  15. Use built-in escape functions
  16. Validate for correct data types, like numbers
  17. White-listed values are values that are valid, as opposed to black-listed values that are invalid. The distinction is that often when doing validation, the list or range of possible values is smaller than the list of invalid values, many of which can be unknown or unexpected.
  18. When you're doing validation, remember that it's often easier to conceptualize and validate what the application allows instead of trying to guard against all the unknown values. For instance, to limit values in a field to all numbers, write a routine that makes sure the input is all numbers. Don't write the routine to search for non-numerical values and mark it as invalid if any are found.
  19. - Guard your file system
  20. In July 2000, a Web site leaked customer data that was found in files on a Web server. A visitor to the Web site manipulated the URL to view files containing the data. Although the files were erroneously placed, this example underscores the importance of guarding your file system against attackers.
  21. If your PHP application does anything with files and has variable data that a user can enter, be careful that you scrub the user input to make sure users can't do anything with the file system that you don't want them to do. Listing 1 shows an example of a PHP site that downloads an image given a name.
  22. Listing 1. Downloading a file
  23. <?php
  24. if ($_POST['submit'] == 'Download') {
  25. $file = $_POST['fileName'];
  26. header("Content-Type: application/x-octet-stream");
  27. header("Content-Transfer-Encoding: binary");
  28. header("Content-Disposition: attachment; filename=\"" . $file . "\";" );
  29. $fh = fopen($file, 'r');
  30. while (! feof($fh))
  31. {
  32. echo(fread($fh, 1024));
  33. }
  34. fclose($fh);
  35. } else {
  36. echo("<html><head><");
  37. echo("title>Guard your filesystem</title></head>");
  38. echo("<body><form id=\"myFrom\" action=\"" . $_SERVER['PHP_SELF'] .
  39. "\" method=\"post\">");
  40. echo("<div><input type=\"text\" name=\"fileName\" value=\"");
  41. echo(isset($_REQUEST['fileName']) ? $_REQUEST['fileName'] : '');
  42. echo("\" />");
  43. echo("<input type=\"submit\" value=\"Download\" name=\"submit\" /></div>");
  44. echo("</form></body></html>");
  45. }
  46. As you can see, the relatively dangerous script in Listing 1 serves any file that the Web server has read access to, including files in the session directory (see "Guard your session data") and even some system files such as /etc/passwd. This example has a text box in which the user can type the file name for example purposes, but the file name could just as easily be supplied in the query string.
  47. Configuring file system access along with user input is dangerous, so it's best to avoid it altogether by designing your application to use a database and hidden, generated file names. However, that's not always possible. Listing 2 provides an example of a routine that validates file names. It uses regular expressions to make sure that only valid characters are used in the file name and checks specifically for the dot-dot characters: ...
  48. Listing 2. Checking for valid file name characters
  49. function isValidFileName($file) {
  50. /* don't allow .. and allow any "word" character \ / */
  51. return preg_match('/^(((?:\.)(?!\.))|\w)+$/', $file);
  52. }
  53. - Guard your database
  54. In April 2008, a U.S. state's Department of Corrections leaked sensitive data as a result of the SQL column names being used in the query string. This leak allowed malicious users to select which columns they wanted to display, submit the page, and get the data. This leak shows how users can figure out ways to make their input do things that the application developers definitely didn't foresee and underscores the need for careful defense against SQL injection attacks.
  55. Listing 3 shows an example of a script that runs an SQL statement. In this example, the SQL statement is a dynamic statement that would allow the same attack. Owners of this form may be tempted to think they're safe because they've limited the column names to select lists. However, the code neglects attention to the last habit regarding form spoofing just because the code limits the selection to drop-down boxes doesn't mean that someone can't post a form with whatever they want in it (including an asterisk [*]).
  56. Listing 3. Executing an SQL statement
  57. <html>
  58. <head>
  59. <title>SQL Injection Example</title>
  60. </head>
  61. <body>
  62. <form id="myFrom" action="<?php echo $_SERVER['PHP_SELF']; ?>"
  63. method="post">
  64. <div><input type="text" name="account_number"
  65. value="<?php echo(isset($_POST['account_number']) ?
  66. $_POST['account_number'] : ''); ?>" />
  67. <select name="col">
  68. <option value="account_number">Account Number</option>
  69. <option value="name">Name</option>
  70. <option value="address">Address</option>
  71. </select>
  72. <input type="submit" value="Save" name="submit" /></div>
  73. </form>
  74. <?php
  75. if ($_POST['submit'] == 'Save') {
  76. /* do the form processing */
  77. $link = mysql_connect('hostname', 'user', 'password') or
  78. die ('Could not connect' . mysql_error());
  79. mysql_select_db('test', $link);
  80. $col = $_POST['col'];
  81. $select = "SELECT " . $col . " FROM account_data WHERE account_number = "
  82. . $_POST['account_number'] . ";" ;
  83. echo '<p>' . $select . '</p>';
  84. $result = mysql_query($select) or die('<p>' . mysql_error() . '</p>');
  85. echo '<table>';
  86. while ($row = mysql_fetch_assoc($result)) {
  87. echo '<tr>';
  88. echo '<td>' . $row[$col] . '</td>';
  89. echo '</tr>';
  90. }
  91. echo '</table>';
  92. mysql_close($link);
  93. }
  94. ?>
  95. </body>
  96. </html>
  97. So, to form the habit of guarding your database, avoid dynamic SQL code as much as possible. If you can't avoid dynamic SQL code, don't use input directly for columns. Listing 4 shows an example of the power of adding a simple validation routine to the account number field to make sure it cannot be a non-number in addition to using static columns.
  98. Listing 4. Guarding with validation and mysql_real_escape_string()
  99. <html>
  100. <head>
  101. <title>SQL Injection Example</title>
  102. </head>
  103. <body>
  104. <form id="myFrom" action="<?php echo $_SERVER['PHP_SELF']; ?>"
  105. method="post">
  106. <div><input type="text" name="account_number"
  107. value="<?php echo(isset($_POST['account_number']) ?
  108. $_POST['account_number'] : ''); ?>" /> <input type="submit"
  109. value="Save" name="submit" /></div>
  110. </form>
  111. <?php
  112. function isValidAccountNumber($number)
  113. {
  114. return is_numeric($number);
  115. }
  116. if ($_POST['submit'] == 'Save') {
  117. /* Remember habit #1--validate your data! */
  118. if (isset($_POST['account_number']) &&
  119. isValidAccountNumber($_POST['account_number'])) {
  120. /* do the form processing */
  121. $link = mysql_connect('hostname', 'user', 'password') or
  122. die ('Could not connect' . mysql_error());
  123. mysql_select_db('test', $link);
  124. $select = sprintf("SELECT account_number, name, address " .
  125. " FROM account_data WHERE account_number = %s;",
  126. mysql_real_escape_string($_POST['account_number']));
  127. echo '<p>' . $select . '</p>';
  128. $result = mysql_query($select) or die('<p>' . mysql_error() . '</p>');
  129. echo '<table>';
  130. while ($row = mysql_fetch_assoc($result)) {
  131. echo '<tr>';
  132. echo '<td>' . $row['account_number'] . '</td>';
  133. echo '<td>' . $row['name'] . '</td>';
  134. echo '<td>' . $row['address'] . '</td>';
  135. echo '</tr>';
  136. }
  137. echo '</table>';
  138. mysql_close($link);
  139. } else {
  140. echo "<span style=\"font-color:red\">" .
  141. "Please supply a valid account number!</span>";
  142. }
  143. }
  144. ?>
  145. </body>
  146. </html>
  147. This example also shows the use of the mysql_real_escape_string() function. This function properly scrubs your input so it doesn't include invalid characters. If you've been relying on magic_quotes_gpc, be forewarned that it is deprecated and will be removed in PHP V6. Avoid relying on it now and write your PHP applications to be secure without it. Also, remember that if you're using an ISP, there's a chance that your it doesn't have magic_quotes_gpc enabled.
  148. Finally, in the improved example, you can see that the SQL statement and output do not include a dynamic column selection. This way, if you add columns to the table later that have different information, you can print them. If you're using a framework to work with your database, there is a chance that your framework does the SQL validation for you already. Make sure to check with the documentation for your framework to be sure; if you're still unsure, do the validation to err on the safe side. Even if you're using a framework for database interaction, you still need to perform the other verification.
  149. - Guard your session
  150. By default, session information in PHP is written to a temporary directory. Consider the form in Listing 5, which shows how to store a user's ID and account number in a session.
  151. Listing 5. Storing data in session
  152. <?php
  153. session_start();
  154. ?>
  155. <html>
  156. <head>
  157. <title>Storing session information</title>
  158. </head>
  159. <body>
  160. <?php
  161. if ($_POST['submit'] == 'Save') {
  162. $_SESSION['userName'] = $_POST['userName'];
  163. $_SESSION['accountNumber'] = $_POST['accountNumber'];
  164. }
  165. ?>
  166. <form id="myFrom" action="<?php echo $_SERVER['PHP_SELF']; ?>"
  167. method="post">
  168. <div><input type="hidden" name="token" value="<?php echo $token; ?>" />
  169. <input type="text" name="userName"
  170. value="<?php echo(isset($_POST['userName']) ? $_POST['userName'] : ''); ?>" />
  171. <br />
  172. <input type="text" name="accountNumber"
  173. value="<?php echo(isset($_POST['accountNumber']) ?
  174. $_POST['accountNumber'] : ''); ?>" />
  175. <br />
  176. <input type="submit" value="Save" name="submit" /></div>
  177. </form>
  178. </body>
  179. </html>
  180. Listing 6 shows the contents of the /tmp directory.
  181. Listing 6. The session files in the /tmp directory
  182. -rw------- 1 _www wheel 97 Aug 18 20:00 sess_9e4233f2cd7cae35866cd8b61d9fa42b
  183. As you can see, the session file, when printed (see Listing 7), contains the information in a fairly readable format. Because the file has to be readable and writable by the Web server user, the session files can create a major problem for anyone on a shared server. Someone other than you can write a script that reads these files so they can try to get values out of the session.
  184. Listing 7. The contents of a session file
  185. userName|s:5:"ngood";accountNumber|s:9:"123456789";
  186. Storing passwords
  187. Passwords should never, ever, ever be stored in plain text anywhere — not in a database, session, file system, or any other form. The best way to handle passwords is to store them encrypted and compare the encrypted passwords with one another. Although this may seem obvious, storing them in plain text seems to be done quite a bit in practice. Any time you use a Web site that can send you your password instead of resetting it means that either the password is stored in plain text or there is code available for decrypting the password if it's encrypted. Even if it's the latter, the code for decryption can be found and exploited.
  188. You can do two things to guard your session data. The first is to encrypt anything that you put into session. But just because you've encrypted the data doesn't mean it's completely safe, so be careful with relying on this as your sole means of guarding your session. The alternative is to store your session data in a different place, like a database. You still have to make sure you're locking down your database, but this approach solves two problems: First, it puts your data into a more secure place than a shared file system; second, it enables your application to scale across multiple Web servers more easily with shared sessions across multiple hosts.
  189. To implement your own session persistence, see the session_set_save_handler() function in PHP. With it, you can store session information in a database or implement a handler for encrypting and decrypting all of your data. Listing 8 provides an example of the function use and skeleton functions for implementation. You can also check out examples of how to use a database in the Resources section.
  190. Listing 8. session_set_save_handler() function example
  191. function open($save_path, $session_name)
  192. {
  193. /* custom code */
  194. return (true);
  195. }
  196. function close()
  197. {
  198. /* custom code */
  199. return (true);
  200. }
  201. function read($id)
  202. {
  203. /* custom code */
  204. return (true);
  205. }
  206. function write($id, $sess_data)
  207. {
  208. /* custom code */
  209. return (true);
  210. }
  211. function destroy($id)
  212. {
  213. /* custom code */
  214. return (true);
  215. }
  216. function gc($maxlifetime)
  217. {
  218. /* custom code */
  219. return (true);
  220. }
  221. session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
  222. - Guard against XSS vulnerabilities
  223. XSS vulnerabilities represent a large portion of all the documented Web-site vulnerabilities in 2007 (see Resources). An XSS vulnerability occurs when a user has the ability to inject HTML code into your Web pages. The HTML code can carry JavaScript code inside script tags, thus allowing JavaScript to run whenever a page is drawn. The form in Listing 9 could represent a forum, wiki, social networking, or any other site where it's common to enter text.
  224. Listing 9. Form for inputting text
  225. <html>
  226. <head>
  227. <title>Your chance to input XSS</title>
  228. </head>
  229. <body>
  230. <form id="myFrom" action="showResults.php" method="post">
  231. <div><textarea name="myText" rows="4" cols="30"></textarea><br />
  232. <input type="submit" value="Delete" name="submit" /></div>
  233. </form>
  234. </body>
  235. </html>
  236. Listing 10 demonstrates how the form could print the results, allowing an XSS attack.
  237. Listing 10. showResults.php
  238. <html>
  239. <head>
  240. <title>Results demonstrating XSS</title>
  241. </head>
  242. <body>
  243. <?php
  244. echo("<p>You typed this:</p>");
  245. echo("<p>");
  246. echo($_POST['myText']);
  247. echo("</p>");
  248. ?>
  249. </body>
  250. </html>
  251. Listing 11 provides a basic example in which a new window pops open to Google's home page. If your Web application does not guard against XSS attacks, the only limit to the harm done is the imagination of the attacker. For instance, someone could add a link that mimics the style of the site for phishing purposes (see Resources).
  252. Listing 11. Malicious input text sample
  253. <script type="text/javascript">myRef = window.open('http://www.google.com','mywin',
  254. 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');</script>
  255. To guard yourself against XSS attacks, filter your input through the htmlentities() function whenever the value of a variable is printed to the output. Remember to follow the first habit of validating input data with white-listed values in your Web application's input for names, e-mail addresses, phone numbers, and billing information.
  256. A much safer version of the page that shows the text input is shown below.
  257. Listing 12. A more secure form
  258. <html>
  259. <head>
  260. <title>Results demonstrating XSS</title>
  261. </head>
  262. <body>
  263. <?php
  264. echo("<p>You typed this:</p>");
  265. echo("<p>");
  266. echo(htmlentities($_POST['myText']));
  267. echo("</p>");
  268. ?>
  269. </body>
  270. </html>
  271. - Guard against invalid posts
  272. Form spoofing is when someone makes a post to one of your forms from somewhere you didn't expect. The easiest way to spoof a form is simply to create a Web page that submits to a form, passing all the values. Because Web applications are stateless, there's no way of being absolutely certain that the posted data is coming from where you want it to come from. Everything from IP addresses to hostnames, at the end of the day, can be spoofed. Listing 13 shows a typical form that allows you to enter information.
  273. Listing 13. A form for processing text
  274. <html>
  275. <head>
  276. <title>Form spoofing example</title>
  277. </head>
  278. <body>
  279. <?php
  280. if ($_POST['submit'] == 'Save') {
  281. echo("<p>I am processing your text: ");
  282. echo($_POST['myText']);
  283. echo("</p>");
  284. }
  285. ?>
  286. </body>
  287. </html>
  288. Listing 14 shows a form that will be post to the form in Listing 13. To try this, you can put the form on a Web site, then save the code in Listing 14 as an HTML document on your desktop. When you have saved the form, open it in a browser. Then you can fill in the data and submit the form, observing while the data is processed.
  289. Listing 14. A form for collecting your data
  290. <html>
  291. <head>
  292. <title>Collecting your data</title>
  293. </head>
  294. <body>
  295. <form action="processStuff.php" method="post">
  296. <select name="answer">
  297. <option value="Yes">Yes</option>
  298. <option value="No">No</option>
  299. </select>
  300. <input type="submit" value="Save" name="submit" />
  301. </form>
  302. </body>
  303. </html>
  304. The potential impact of form spoofing, really, is that if you have a form that has drop-down boxes, radio buttons, checkboxes, or other limited input, those limits mean nothing if the form is spoofed. Consider the code in Listing 15, which contains a form with invalid data.
  305. Listing 15. A form with invalid data
  306. <html>
  307. <head>
  308. <title>Collecting your data</title>
  309. </head>
  310. <body>
  311. <form action="http://path.example.com/processStuff.php"
  312. method="post"><input type="text" name="answer"
  313. value="There is no way this is a valid response to a yes/no answer..." />
  314. <input type="submit" value="Save" name="submit" />
  315. </form>
  316. </body>
  317. </html>
  318. Think about it: If you have a drop-down box or a radio button that limits the user to a certain amount of input, you may be tempted not to worry about validating the input. After all, your input form ensures that users can only enter certain data, right? To limit form spoofing, build measures to ensure that posters are likely to be who they say they are. One technique you can use is a single-use token, which does not make it impossible to spoof your forms but does make it a tremendous hassle. Because the token is changed each time the form is drawn, a would-be attacker would have to get an instance of the sending form, strip out the token, and put it in their spoofing version of the form. This technique makes it highly unlikely that someone can build a permanent Web form to post unwanted requests to your application. Listing 16 provides an example of a one-type form token.
  319. Listing 16. Using a one-time form token
  320. <?php
  321. session_start();
  322. ?>
  323. <html>
  324. <head>
  325. <title>SQL Injection Test</title>
  326. </head>
  327. <body>
  328. <?php
  329. echo 'Session token=' . $_SESSION['token'];
  330. echo '<br />';
  331. echo 'Token from form=' . $_POST['token'];
  332. echo '<br />';
  333. if ($_SESSION['token'] == $_POST['token']) {
  334. /* cool, it's all good... create another one */
  335. } else {
  336. echo '<h1>Go away!</h1>';
  337. }
  338. $token = md5(uniqid(rand(), true));
  339. $_SESSION['token'] = $token;
  340. ?>
  341. <form id="myFrom" action="<?php echo $_SERVER['PHP_SELF']; ?>"
  342. method="post">
  343. <div><input type="hidden" name="token" value="<?php echo $token; ?>" />
  344. <input type="text" name="myText"
  345. value="<?php echo(isset($_POST['myText']) ? $_POST['myText'] : ''); ?>" />
  346. <input type="submit" value="Save" name="submit" /></div>
  347. </form>
  348. </body>
  349. </html>
  350. - Protect against CSRF
  351. Cross-Site Request Forgeries (CSRF attacks) are exploits that take advantage of user privileges to carry out an attack. In a CSRF attack, your users can easily become unsuspecting accomplices. Listing 17 provides an example of a page that carries out a certain action. This page looks up user login information from a cookie. As long as the cookie is valid, the Web page processes the request.
  352. Listing 17. A CSRF example
  353. <img src="http://www.example.com/processSomething?id=123456789" />
  354. CSRF attacks are often in the form of <img> tags because the browser unwittingly calls the URL to get the image. However, the image source could just as easily be the URL of a page on the same site that does some processing based on the parameters passed into it. When this <img> tag is placed with an XSS attack — which are the most common of the documented attacks — users can easily do something with their credentials without knowing it — thus, the forgery.
  355. To guard yourself against CSRF, use the one-use token approach you use in your habit of verifying form posts. Also, use the explicit $_POST variable instead of $_REQUEST. Listing 18 demonstrates a poor example of a Web page that processes identically — whether the page is called by a GET request or by having a form posted to it.
  356. Listing 18. Getting the data from $_REQUEST
  357. <html>
  358. <head>
  359. <title>Processes both posts AND gets</title>
  360. </head>
  361. <body>
  362. <?php
  363. if ($_REQUEST['submit'] == 'Save') {
  364. echo("<p>I am processing your text: ");
  365. echo(htmlentities($_REQUEST['text']));
  366. echo("</p>");
  367. }
  368. ?>
  369. </body>
  370. </html>
  371. Listing 19 shows a cleaned-up version of this page that only works with a form POST.
  372. Listing 19. Getting the data only from $_POST
  373. <html>
  374. <head>
  375. <title>Processes both posts AND gets</title>
  376. </head>
  377. <body>
  378. <?php
  379. if ($_POST['submit'] == 'Save') {
  380. echo("<p>I am processing your text: ");
  381. echo(htmlentities($_POST['text']));
  382. echo("</p>");
  383. }
  384. ?>
  385. </body>
  386. </html>

comments powered by Disqus