I’m usually pretty resourceful about finding information on my own, but when it comes to this subject, it’s really daunting the sheer amount of stuff there is out there. I’m getting a bit of information overload.
I’ve found dozens of articles on individual security topics, but I can’t get a sense of the bigger picture and how it all comes together in practice.
I need to see a bird’s-eye roadmap. Take this hypothetical example:
A Simple Hypothetical "Comments" Section:
Sign up: create a password/username combo that is to be stored safely in a MySQL table.
Log in.
Leave a comment.
What would be a "security roadmap" to follow on this most basic case?
It doesn’t help that every tutorial and PHP book on the planet uses the MySQL extensions, which, if I understand correctly, is a bad idea?
A. In general…
I’m assuming here that the programmer is not also the server administrator, and that the server admin more or less knows how to configure LAMP correctly and securely by default.
Of course, if necessary, a programmer can override most PHP settings in a custom
php.inifile located in the web root.Use an MVC framework.
I use CakePHP. The framework itself goes a long way to ensure fundamentally sound and secure coding practices.
B. Incoming data…
Sanitize and validate all data contained in
$_GET, $_POST, $_COOKIE,and$_REQUESTbefore programmatically manipulating the data.SQL Injection
Definition: Code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed.
Prevention: Parameterised queries using a library such as
mysqliorPDO. See the OWASP SQL Injection cheat sheet (string escaping functions likemysql_real_escape_stringare not recommended)Cross Site Scripting (XSS)
Definition: Security vulnerability typically found in web applications that allows code injection by malicious web users into the web pages viewed by other users. Examples of such code include client-side scripts (i.e., JavaScript).
Prevention: Context-dependent output escaping and encoding. See the OWASP XSS prevention cheat sheet.
C. Browser requests…
Cross Site Request Forgery (CSRF)
Definition: Type of malicious exploit of a website whereby unauthorized commands are transmitted from a user that the website trusts. Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user’s browser.
Prevention: Generate a unique “token”, typically when a browser session starts. Pass the token in all
POSTandGETrequests. Following thePOST/GETaction, check for the existence of the token in the session and then confirm the token sent byPOST/GETis identical to the token stored in the session. (An MVC framework like CakePHP makes this relatively easy to implement uniformly throughout your application.)D. Sessions…
Destroy session data when killing a session
After a session is complete (”logout”), destroy its data and don’t just clear the cookie (a malicious user could otherwise just re-instate the cookie and use the session again). Unset all indexes in
$_SESSIONby assigning it to an empty array.Store sessions as files above the web root or in a database
The default path for saving sessions on the server can be hijacked — especially in a shared hosting environment.
E. Passwords…
Enforce the selection of strong passwords
Require numbers, symbols, upper and lowercase letters in passwords
Password length should be around 12 to 14 characters
Hash and Salt all passwords
Do not use
sha1(),md5()orhash()to hash passwords. They’re not designed for this. You will want to use a function like bcrypt or PBDFK2. There’s some really good suggestions on this question. Your salt value should be completely random, and stored in the database (it’s not really a secret). An additional secret value (generally called “pepper”) can be stored in your application and prepended to passwords before using bcrypt, but it’s not clear how much security this really adds.F. In a custom
php.inilocated in web root…Disable register_globals
Prevention:
register_globals = OffDisable magic quotes
Prevention:
magic_quotes_gpc = OffDisable error reporting
Prevention:
display_errors = OffEnable error logging and save log file to a directory above web root
Prevention:
Store session data inside a directory above web root
Prevention:
session.save_path = /path/above/webroot/sessionsG. In a
.htaccessfile located in web root…Disable directory listings site-wide
Prevention:
Options -IndexesH. Valuable/Sensitive files…
Prevent unauthorized access/downloads by storing such files above the web root
This includes site administration/members-only sections and site/database configuration files!!
Use an intermediary script to serve the files inline or as an attachment
Keep your scripts(WordPress, PHPMyAdmin, etc.) updated.
Only allow access to PHPMyAdmin when you are using it. This prevents people from being able to use zero-day exploits on your install.
I. Uploaded files…
Validate file name stored in
$_FILESbefore using it for any kind of data manipulationBe aware that the provided mime type can be spoofed or otherwise wrong
Move all user-uploaded files to a directory above web root!!!
Don’t execute/serve uploaded files with include()
Try to not serve files with content types of “application/octet-stream,” “application/unknown,” or “plain/text”
J. Misc…
All “utility” files/programs in the web root created and used by the developer during the development of a site/application, that are not intended or required to be accessed by future site users, or otherwise pose some kind of security risk, should be removed when the site goes live.
For example, this includes a
phpinfo.phpfile (a files that prints the results ofphpinfo()), database utility scripts, etc.