02.03.2010, 02:46
Hi
It seems there are a lot of people in this community who use MySQL, and are unaware of the dangers. MySQL is a fantastic data storage system, and it works very well, but if inputs are not sanitized correctly, your database can be easily attacked by anyone with simple knowledge of SQL language.
SQL injection can happen on any application which connects to an SQL database, including SA:MP, and it is up to you to make sure it doesn't happen to you.
For this article, we will assume that "text" is a string variable containing some user input, and "string" is a string which we will be using to store our query.
ALL user input must be filtered before using it in a MySQL query. Here is a query that is prone to SQL injection attacks:
This works fine with "safe" data. For example, if text = Blacklite , the query going to the database will look like this:
However, if the user enters '; DROP TABLE users;-- , then the query processed by MySQL will look like this:
The SQL above will actually delete the whole `users` table, if your MySQL user has the permission to do so!
To prevent this from happening, you need to use mysql_real_escape_string like so:
Now, safe queries will still work, but "dangerous" queries like the one above will be escaped, and will look like this:
Note the backslash (\) that the mysql_real_escape_string function has inserted before the single quote (') to stop MySQL from recognizing it as SQL code, and instead just treating it as a literal character.
And it's now all safe :)
One last thing: when using the mysql_real_escape_string() function, make sure you're using it properly with the correct syntax. If you use it incorrectly, it won't filter your strings!
It seems there are a lot of people in this community who use MySQL, and are unaware of the dangers. MySQL is a fantastic data storage system, and it works very well, but if inputs are not sanitized correctly, your database can be easily attacked by anyone with simple knowledge of SQL language.
SQL injection can happen on any application which connects to an SQL database, including SA:MP, and it is up to you to make sure it doesn't happen to you.
For this article, we will assume that "text" is a string variable containing some user input, and "string" is a string which we will be using to store our query.
ALL user input must be filtered before using it in a MySQL query. Here is a query that is prone to SQL injection attacks:
pawn Code:
format(string, sizeof(string), "SELECT * FROM users WHERE username='%s' LIMIT 1", text);
Code:
SELECT * FROM users WHERE username='Blacklite' LIMIT 1
Code:
SELECT * FROM users WHERE username=''; DROP TABLE users;
To prevent this from happening, you need to use mysql_real_escape_string like so:
pawn Code:
mysql_real_escape_string(text, string);
format(string, sizeof(string), "SELECT * FROM users WHERE username='%s' LIMIT 1", string);
Code:
SELECT * FROM users WHERE username='\'; DROP TABLE users;--' LIMIT 1
And it's now all safe :)
One last thing: when using the mysql_real_escape_string() function, make sure you're using it properly with the correct syntax. If you use it incorrectly, it won't filter your strings!