Difference between revisions of "SQL Injection"

From Hostek.com Wiki
Jump to: navigation, search
(Created page with "==Tips on Preventing SQL Injection Attacks== To sanitize form input for sending to a database, always be sure to escape the single quote by searching and replacing it with tw...")
 
Line 7: Line 7:
 
<ul><li><b>Escape single quotation marks</b>. Include code within your Web applications that replaces single apostrophes with double apostrophes. This will force the database server to recognize the apostrophe as a literal character rather than a string delimiter.</li><li><b>Filter user input. </b>Filtering user input before it is passed into a SQL query will prevent the possibility of executing SQL commands. (ex. Strip input strings of all characters that are not alphanumeric (a-z, A-Z or 0-9))</li><li><b>Limit the privileges available to the account that executes Web application code</b>. For example, if an account only had permission to perform the intended action (retrieving records from a table), then delete, insert or create queries would not be possible.</li><li><b>Reduce or eliminate debugging information</b>. When an error condition occurs on your server, the Web user should not see technical details of the error. This type of information could aid an intruder seeking to explore the structure of your database.</li></ul>
 
<ul><li><b>Escape single quotation marks</b>. Include code within your Web applications that replaces single apostrophes with double apostrophes. This will force the database server to recognize the apostrophe as a literal character rather than a string delimiter.</li><li><b>Filter user input. </b>Filtering user input before it is passed into a SQL query will prevent the possibility of executing SQL commands. (ex. Strip input strings of all characters that are not alphanumeric (a-z, A-Z or 0-9))</li><li><b>Limit the privileges available to the account that executes Web application code</b>. For example, if an account only had permission to perform the intended action (retrieving records from a table), then delete, insert or create queries would not be possible.</li><li><b>Reduce or eliminate debugging information</b>. When an error condition occurs on your server, the Web user should not see technical details of the error. This type of information could aid an intruder seeking to explore the structure of your database.</li></ul>
  
**Note to PHP users connecting to MySQL databases
+
*Note to PHP users connecting to MySQL databases
  
 
There are at least two ways to make MySQL queries safe using PHP. The first, and often simplest, method is to use mysql_real_escape_string on each variable used in a MySQL query. Please see the <a href="http://php.net/mysql_real_escape_string">PHP documentation</a> for more information.
 
There are at least two ways to make MySQL queries safe using PHP. The first, and often simplest, method is to use mysql_real_escape_string on each variable used in a MySQL query. Please see the <a href="http://php.net/mysql_real_escape_string">PHP documentation</a> for more information.
  
 
The second method is to use prepared statements. You can do this using the MySQL Improved Extension in PHP 5. For more information, please refer to the <a href="http://php.net/manual/en/book.mysqli.php">PHP documentation</a> for more information.
 
The second method is to use prepared statements. You can do this using the MySQL Improved Extension in PHP 5. For more information, please refer to the <a href="http://php.net/manual/en/book.mysqli.php">PHP documentation</a> for more information.
 +
 +
[[category:Databases-MySQL]]

Revision as of 04:03, 22 November 2012

Tips on Preventing SQL Injection Attacks

To sanitize form input for sending to a database, always be sure to escape the single quote by searching and replacing it with two single quotes. This will cause the database to send the quote string as a literal character rather than interpreting it as the closing of a string. Be aware, however, that since numeric input does not require quotes, this technique will not be effective. In the case of numeric input, simply check that the form input is indeed numeric.

One method of preventing SQL injection is to avoid the use of dynamically generated SQL in your code. By using parameterized queries and stored procedures, you then make it impossible for SQL injection to occur against your application.

  • Escape single quotation marks. Include code within your Web applications that replaces single apostrophes with double apostrophes. This will force the database server to recognize the apostrophe as a literal character rather than a string delimiter.
  • Filter user input. Filtering user input before it is passed into a SQL query will prevent the possibility of executing SQL commands. (ex. Strip input strings of all characters that are not alphanumeric (a-z, A-Z or 0-9))
  • Limit the privileges available to the account that executes Web application code. For example, if an account only had permission to perform the intended action (retrieving records from a table), then delete, insert or create queries would not be possible.
  • Reduce or eliminate debugging information. When an error condition occurs on your server, the Web user should not see technical details of the error. This type of information could aid an intruder seeking to explore the structure of your database.
  • Note to PHP users connecting to MySQL databases

There are at least two ways to make MySQL queries safe using PHP. The first, and often simplest, method is to use mysql_real_escape_string on each variable used in a MySQL query. Please see the <a href="http://php.net/mysql_real_escape_string">PHP documentation</a> for more information.

The second method is to use prepared statements. You can do this using the MySQL Improved Extension in PHP 5. For more information, please refer to the <a href="http://php.net/manual/en/book.mysqli.php">PHP documentation</a> for more information.