Category: UC2006

SecurePHPandMySQLWebApplicationsTutorial

Contents

[edit] Secure Your PHP and MySQL Web Applications

Notes by Mike Hillyer

Session by Laura Thomson, OmniTI. Laura is author of a number of popular PHP and MySQL books.

Session will be on security at the developer level and is written from a programmer’s perspective.

Many developers plug along without any knowledge that they have security problems, security awareness is a relatively new thing. This session is not about guru-level knowledge, it is about developer-level practices.

[edit] MySQL Security Basics

[edit] Accounts and Privileges

[edit] Using Encryption

[edit] PHP Security Basics

[edit] External Data

[edit] Attacks

[edit] Attack #1 - Register Globals

Say code checks if ($valid_user) then show stuff…

http://www.foo.com/bar.php?valid_user=true

And you are seeing what you should not.

[edit] Attack #2 - XSS

An attempt to enter data into a web app with client-side code such as javascript. For example, malicious javascript in a guestbook. People can create annoying popups, do meta-refreshes, create dubious forms, steal cookies, and even create AJAX-based attacks.

Stolen cookies with XSS can lead to session hijacking.

You prevent this with output filtering to the browser using htmlentities().

Google for the XSS cheatsheet, you can copy and paste it into your forms as a testbed for XSS attacks.

[edit] Attack #3 - SQL Injection

SQL injection is adding your own queries to a string to be executed against MySQL/anyDB. By passing things like quotes and comment marks you can modify the query that the application tries to execute.

Another variation is command injection, if the PHP script tries to run system() or exec(), a user may be able to inject into it and get a command run on the server itself.

Prevent with mysql_real_escape_string, do not use magic quotes.

Alternatively prevent with prepared statements.

[edit] Attack #4 - Exposed Source Code

Fixes:

[edit] Attack #5 - Session Fixation

Session security works on the basis that the session ID is hard to guess. Not having to guess it makes thinsg easier.

PHP can accept session IDs through a cookie or URL.

Stealing sessions allows you to take over a logged-in user.

Session fixation means having a user follow a link with a provided session id in the URL. If they then login, you could connect with the session ID you provided and use their credentials.

Use session_regenerate_id() whenever a user logs in or changes their privilege level so an attacker cannot go back in and use the session id the provided to the target.

[edit] Attack #6 - Session hijacking

Similar to session fixation but we somehow obtain the user’s session ID.

Done with XSS and stealing cookies through javascript

Session IDs can be sniffed or obtained throug proxy servers.

Best to use SSL for anything that requires a logged-in user.

[edit] Attack #7 - CSRF

Cross Site Request Forgeries

A request for a page that looks as though it was initiated by a site’s trusted user, but was not (deliberately).

Example: <img src=”http://example.com/single_click_to_buy.php?item=12345″ />

In the example, a user browsing your page winds up buying an item without knowing it.

Do not use GET parameters to perform an action.

Make sure that users come through your forms, and each for submission matches a form you sent out.

Generate a one-time token and embed it in the form, save in the session, check on submission.

Not trivial to protect against this.

The famous MySpace attack used XSS and CSRF to determine who was looking at a user’s page, then used CSRF to send a friend request from the viewer to the page owner, thus building a huge friends list. As well, the friends of those targets also became his friends. The sheer size of the friends list crashed the system. CSRFs are even used to defeat captchas by taking a captcha and showing it to a user on another site, then pushing it back when a unwitting user reads the captcha.

[edit] Attack #8 - Code Injection

Similar to SQL injection and command injection. This involves executing arbitrary code on the target server, usually though file inclusion.

Example script parses user input to generate a filename in a theming system. The sample attack had the theme load an external file from across the net.

Code injection is prevented by filtering user input. Only allow expected values. Do not allow http:// in the filename argument.

In PHP.ini, allow_url_fopen should be disabled if possible. This blocks require/include/fopen on remote files.

[edit] Techniques for Prevention

Preventing SQL Injection and Designing for Security

Laura then provides some prepared statement examples, showing bindings for input and output.

By using prepared statements you do not need to filter data, the statement preparation does it for you.

After showing a mysqli example, Laura shows an example using PDO, a new common database abstraction library.

[edit] Designing for Security

Basic Principles

Dispatch architecture and MVC help improve security.

Use the error reporting, use E_ALL & E_STRICT to see all errors.

On aproduction server, set display_errors = off.

Attackers can use error messages to learn about your system and facilitate an attack.

[edit] Education and Code Review

[edit] Security Audits

[edit] Responsiveness

Look at

Overall a good overview for developer-level security consideration. I’ll be sure to hit Laura’s other session at the conference.

Retrieved from "http://forge.mysql.com/wiki/SecurePHPandMySQLWebApplicationsTutorial"

This page has been accessed 25,701 times. This page was last modified 13:54, 29 January 2008.

Find

Browse
MySQLForge
Main Page
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Special pages
New pages
File list
Statistics
Bug reports
More...