What is the meaning of SQL injection? I am not able to understand the term. And what problems may be caused by SQL injection?
|
migrated from crypto.stackexchange.com Jan 24 '12 at 12:40
|
SQL injection most commonly happens when a programmer builds an SQL command by appending together (or interpolating) strings, using user-supplied input. e.g. Imagine this extract from a vulnerable piece of user authentication (login) pseudocode from a fictional web application.
At first glance you may think this looks sensible, but the problem is that it makes no distinction between the user-supplied data and the SQL code; data can be treated as code. This means that a malicous user can change the logic of the SQL statement. A malicous user could completely bypass the login protection if he could change the logic of the SQL command to produce an answer that guarantees at least one row is always returned. For example, if he entered a real username
Then this would grant him access to someone else's account. This is because the resultant SQL would look like this:
Note that the logical expression
which is logically the same as
which is logically the same as
Thus, all the users in the database will be returned, and he will be logged in as the first one in the list - which is usually the administrator. Also SQL injection can be used to read the all data out of the database. Entering the username as follows (SQL Server syntax) will list the user-defined table names
because this produces
because our injected data has an Some combinations of programming language database libraries and DBMSs also allow query-stacking. This is a technique where a whole new SQL command is appended on the end. The database will then execute both queries. Username:
produces
Now your application doesn't have any users (and requires SQL injection to log in). See mandatory XKCD comic. See this SQLi cheat sheet if you are interested in more techniques commonly used by attackers and vulnerability testers. So how do I avoid this? Actually, in many common scenarios, it is so easy. in pseudocode:
As always this isn't the whole story... Don't forget to defend in depth and always do input validation too, as you always would (should). WHERE clauses need extra attention as special characters such as |
||||
|
|
SQL Injection is a technique of using valid SQL commands to tamper with, delete or inject data into a back end database directly through weaknesses in the validation of code input to a web application which calls that database. It is one of the OWASP Top Ten most commonly used attack routes as it is very simple to exploit. It can be used to compromise entire databases of customer data, credit card data, financial records etc., or to get a copy of this data. Generally quite a major impact! The good thing is it is very easy to mitigate through Input Validation - most frameworks provide modules to do this. |
|||||||||||||
|
