Notes on PHP Security "literature search" by Mary Ann - Fall 2012



Legend:

"Development and Implementation of Secure Web Applications"

This is the most recent and most definitive offering from Ron.  It certainly gives us more than enough to get going with.
It is very complete - no need to read others if you've digested this one.
It discusses "Network-level filtering" (pg. 42) - support for the .htaccess restrictions.
Very generic - doesn't focus on any one platform or language.  Contains site-wide recommendations as well as code-specific.
CPNI = Centre for the Protection of National Infrastructure - gov.uk

"PHP and OWASP Top Ten Security Vulnerabilites"

This document is written by one of the author's of the O-Reilly "PHP Cookbook" book. 
  1. Unvalidated Parameters
  2. Broken Access Control
  3. Broken Account and Session Management
  4. Cross-Site Scripting (XSS) Flaws
  5. Buffer Overflows
  6. Command Injection Flaws
  7. Error Handling Problems
  8. Insecure Use of Cryptography
  9. Remote Administration Flaws
  10. Web and Application Server Misconfiguration


"PHP Security Guilde 1.0"

This document has some very detailed examples about how various exploits work.  I have highlighted only new information/suggestions.

"Seven habits for writing secure PHP applications"

Most of this is a repeat of previous things, but the article has some nice detailed examples.
  1. Validate input
  2. Guard your file system
  3. Guard your database
  4. Guard your session data
  5. Guard against Cross-Site Scripting (XSS) vulnerabilities
  6. Verify form posts
  7. Protect against Cross-Site Request Forgeries (CSRF)

"Top 10 PHP Security Vulnerabilities

Entertaining - includes the following 4 links to other phpmaster.com articles.

"Migrate from the MySQL Extension to PDO"

Discussion of transitioning from mysql -> mysqli(mproved) -> PhpDataObjects.

Stuck with mysql on PHP4.3 (gamma).  Here's a good example of using it "securely":

01    <?php
02    // Step 1: Establish a connection
03    $db = mysql_connect("localhost", "testusr", "secretpass");
04    mysql_select_db("testdb", $db);
05     
06    // Step 2: Construct a query
07    $query = "SELECT * FROM foo WHERE bar = '" . mysql_real_escape_string($zip) . "'";
08     
09    // Step 3: Send the query
10    $result = mysql_query($query, $db);
11     
12    // Step 4: Iterate over the results
13    while($row = myql_fetch_assoc($result)) {
14        print_r($row);
15    }
16     
17    // Step 5: Free used resources
18    mysql_free_result($result);
19    mysql_close($db);

"Cross-Site Scripting Attacks (XSS)" 

Note: following sentence outlines 3 separate issues:
"In order to implement solid security measures which prevents XSS attacks, we should be mindful of data validation, data sanitization, and output escaping."
  1. Data Validation - whitelisting allowed input (eg. preg_match)
  2. Data Sanitization - removing suspicious buts (eg. strip_tags, preg_replace)
  3. Output Escaping - before presenting to user (eg. htmlspecialchars)
Eg.
<?php
// validate comment
$comment = trim($_POST["comment"]);
if (empty($comment)) {
    exit("must provide a comment");
}

// sanitize comment
$comment = strip_tags($comment);

// comment is now safe for storage
file_put_contents("comments.txt", $comment, FILE_APPEND);

// escape comments before display
$comments = file_get_contents("comments.txt");
echo htmlspecialchars($comments);

"Input Validation Using Filter Functions"

Talks about 2 functions available as of PHP 5.2:


"Preventing Cross-Site Request Forgeries (CSRF)"

Actually I don't understand this one.  He seems to be suggesting that a security code should be included on all forms to verify that the request isn't coming from a bad hat site.
(explained in more detail in CPNI doc)