11

Possible Duplicate:
SQL injection that gets around mysql_real_escape_string()

I havent seen any valuabe or not outdated info on this. So, there is this question: Does mysql_real_escape_string() FULLY protect against SQL injection? Yet it is very outdated(its from '09), so as of php 5.3 and mysql 5.5 in '12, does it protect fully ?

Community
  • 1
  • 1
w8ph
  • 129
  • 1
  • 2
  • 8
  • 10
    Please for the love of Zaphod Beeblebrox upgrade yourself to prepared statements via PDO or the mysqli extension. `mysql*` is a dinosaur that should never be used. If so many "tutorial" sites didn't stubbornly continue to use it, the PHP dev team would've already deprecated it. New devotees to this backwards practice are minted daily because they don't know any better and after all, "the tutorial said I could do it this way." **Prepared statements are the correct way to prevent SQL injection**. –  Mar 22 '12 at 00:15
  • 1
    Source: http://news.php.net/php.internals/53799 –  Mar 22 '12 at 00:25
  • thank you @rdlowrey, my head was thinking "everyones using mysql, how bad can it be !?" apparently, you are right. – w8ph Mar 22 '12 at 00:57
  • I have researched a bit about mysqli, it seems like that I dont have to use "mysql_real_escape_string" at all. and, sql injection is impossible and everything I insert is actual text without me worring about anything. I just want to confirm this, is it true ? – w8ph Mar 22 '12 at 01:11
  • @w8ph `mysqli` does have an equivalent method: [mysqli_real_escape_string](http://php.net/manual/en/mysqli.real-escape-string.php). The thing is, the whole point of my comment is that you should use prepared statements, which negates the possibility of injection without the need for manual escaping. Take a look at the docs for [mysqli::prepare](http://www.php.net/manual/en/mysqli.prepare.php) for more info. –  Mar 22 '12 at 03:28
  • Possible duplicate of [SQL injection that gets around mysql\_real\_escape\_string()](https://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) – Cœur Jul 14 '18 at 12:41

3 Answers3

10

mysql_real_escape_string ALONE can prevent nothing.

Moreover, this function has nothing to do with injections at all.

Whenever you need escaping, you need it despite of "security", but just because it is required by SQL syntax. And where you don't need it, escaping won't help you even a bit.

The usage of this function is simple: when you have to use a quoted string in the query, you have to escape it's contents. Not because of some imaginary "malicious users", but merely to escape these quotes that were used to delimit a string. This is extremely simple rule, yet extremely mistaken by PHP folks.

This is just syntax related function, not security related.

Depending on this function in security matters, believing that it will "secure your database against malicious users" WILL lead you to injection.

A conclusion that you can make yourself:
No, this function is not enough.

Prepared statements is not a silver bullet too. It covers your back for only half of possible cases. See the important addition I made to the famous question for the details

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
4

long time since I read a blog post about this so it may no longer hold true BUT...

The posts stated that if you had unicode encoded characters in your string they would be missed by real escape string but would be evaluated by mysql engine - alluding to the idea that you could indeed still be open to a well placed injection.

I can't remember the blog post but this question on here is in the same ball-park.

Community
  • 1
  • 1
Ian Wood
  • 6,515
  • 5
  • 34
  • 73
-1

Yes. By properly escaping the string using the native mysql escape functions, it's not possible to "break out" and execute a query.

However, a better approach would be to use prepared statements. This will do a number of things. By using prepared statements you take advantage of even more optimization from the database and it will properly escape any data passed in. Take a look at: http://php.net/manual/en/mysqli.prepare.php

Taylor Dondich
  • 628
  • 4
  • 9