|
SQL InjectionMany web developers are unaware of how SQL queries can be tampered with, and assume that an SQL query is a trusted command. It means that SQL queries are able to circumvent access controls, thereby bypassing standard authentication and authorization checks, and sometimes SQL queries even may allow access to host operating system level commands. Direct SQL Command Injection is a technique where an attacker creates or alters existing SQL commands to expose hidden data, or to override valuable ones, or even to execute dangerous system level commands on the database host. This is accomplished by the application taking user input and combining it with static parameters to build a SQL query. The following examples are based on true stories, unfortunately. Owing to the lack of input validation and connecting to the database on behalf of a superuser or the one who can create users, the attacker may create a superuser in your database. Example 6.2. Splitting the result set into pages ... and making superusers (PostgreSQL)<?php
0;
If it happened, then the script would present a superuser access to him.
Note that
Note:
It is common technique to force the SQL parser to ignore the rest of the
query written by the developer with
A feasible way to gain passwords is to circumvent your search result pages.
The only thing the attacker needs to do is to see if there are any submitted variables
used in SQL statements which are not handled properly. These filters can be set
commonly in a preceding form to customize Example 6.3. Listing out articles ... and some passwords (any database server)<?php
'
If this query (playing with the
SQL UPDATE's are also susceptible to attack. These queries are
also threatened by chopping and appending an entirely new query to it. But
the attacker might fiddle with the Example 6.4. From resetting a password ... to gaining more privileges (any database server)<?php
<?php A frightening example how operating system level commands can be accessed on some database hosts. Example 6.5. Attacking the database hosts operating system (MSSQL Server)<?php
<?php
MSSQL Server executes the SQL statements in the batch including a command
to add a new user to the local accounts database. If this application
were running as
Note:
Some of the examples above is tied to a specific database server. This does not mean that a similar attack is impossible against other products. Your database server may be similarly vulnerable in another manner. You may plead that the attacker must possess a piece of information about the database schema in most examples. You are right, but you never know when and how it can be taken out, and if it happens, your database may be exposed. If you are using an open source, or publicly available database handling package, which may belong to a content management system or forum, the intruders easily produce a copy of a piece of your code. It may be also a security risk if it is a poorly designed one. These attacks are mainly based on exploiting the code not being written with security in mind. Never trust any kind of input, especially that which comes from the client side, even though it comes from a select box, a hidden input field or a cookie. The first example shows that such a blameless query can cause disasters.
Besides these, you benefit from logging queries either within your script or by the database itself, if it supports logging. Obviously, the logging is unable to prevent any harmful attempt, but it can be helpful to trace back which application has been circumvented. The log is not useful by itself, but through the information it contains. More detail is generally better than less. Code Examples / Notes » security.database.sql_injectionctm
This is a very helpful document from the MySQL site (in .pdf format) : http://dev.mysql.com/tech-resources/articles/ guide-to-php-security-ch3.pdf some
mysql_escape_string() has been deprecated (see docs), use mysql_real_escape_string()
17-mar-2006 05:48
If you use the PEAR package and prepare() / execute() your queries, you will hardly have to worry about any of this. Of course, it's still a good idea to make sure you're putting valid data in your database... anonymous
Here is a useful class that deals with SQL injection: http://www.phpinsider.com/php/code/SafeSQL/ |