|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: kansas
Posts: 673
|
addslashes() vs mysql_real_escape_string()...the final debate
addslashes() and mysql_real_escape_string() are an equal solution to cleansing data before it's inserted in a database.
There, I've said it. I've done quite a bit of web searching to prove this point to myself, but I'm very curious about why people swear by mysql_real_escape_string(). The only things people seem to say about mysql_real_escape_string() is that it's safer...and somehow they always fail to mention why it's safer. Character encoding.....ok--I don't quite grasp it all. Just show me any kind of sql injection that gets by addslashes() but not mysql_real_escape_string(). |
|
|
|
|
|
#2 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Canada
Posts: 2,564
|
aw crap this bugs me. haha i remember seeing an example of just that, which is why i switched to the *escape_string()
and now, i can't remember where I saw it and I can't seem to find it ![]() |
|
|
|
|
|
#3 |
|
SitePoint Enthusiast
![]() Join Date: Dec 2004
Location: Oklahoma City, OK, USA
Posts: 94
|
Well, mysql_real_escape_string doesn't protect against sql injections more than addslashes, but that's not the reason you use it. addslashes() was from the developers of PHP whereas mysql_real_escape_string uses the underlying MySQL C++ API (i.e. from the developers of MySQL). mysql_real_escape_string escapes EOF chars, quotes, backslashes, carriage returns, nulls, and line feeds. There is also the charset aspect.
However, it is a common thought among a lot of PHP programmers (beginning and even more advanced) that SQL injections are the only thing to guard against with sanitizing user input using it in a query. That, actually, is incorrect. If you only rely on *_escape_string and addslashes because you are only thinking about injections, you leave yourself vulnerable to attacks from users. MySQL has some good tips to PHP programmers in their documentation that is, sadly, no where to be found in PHP's documentation (that I know of, as I've read almost, if not all, and PHP's mysql documentation). http://dev.mysql.com/tech-resources/...curity-ch3.pdf . It's a nice read, especially if you like reading articles about PHP programming (*guilty*). Scroll down to page 78 where they talk about LIKE attacks. If you aren't one for reading, they use the following illustration of something that neither mysql_real_escape_string or addslashes protects against: PHP Code:
PHP Code:
Hope that helps some, Jonathan |
|
|
|
|
|
#4 | ||
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: kansas
Posts: 673
|
Thanks for the response, MrOrange,
a couple thoughts: Quote:
Quote:
Any thoughts? and thanks for the link--there's some very interesting material in there ![]() |
||
|
|
|
|
|
#5 | |||
|
SitePoint Enthusiast
![]() Join Date: Dec 2004
Location: Oklahoma City, OK, USA
Posts: 94
|
I guess I didn't quite articulate my thought in my original post.
addslashes() and mysql_real_escape_string() are both as effective at preventing most SQL injections -- although, not completely effective as one would have to count the % and _ in LIKE clauses as injections, which neither function protects against. They both protect to the same degree. The mysql_real_escape_string function does not protect against SQL injections anymore than addslashes(). Quote:
PHP Code:
addslashes escapes: ', ", \, and NUL mysql_real_escape_string escapes, in addition: \x00, \n, \r, and \x1a. Quote:
Quote:
I hope that clears up any confusion. |
|||
|
|
|
|
|
#6 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: kansas
Posts: 673
|
Quote:
![]() |
|
|
|
|
|
|
#7 | |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Oct 2004
Location: New York
Posts: 359
|
Quote:
Note: I assume you mean escaping, not cleansing. Cleansing is an informal alias of filtering. The important issue is that mysql_real_escape_string() is more appropriate for escaping data used in a MySQL query. Although I've explained this several times here, it hasn't helped. I decided to blog about it in a bit more detail and provide a specific example (e.g., code) that demonstrates an SQL injection attack that is immune to addslashes(): http://shiflett.org/archive/184 Please link to this anytime someone tries to debate this again. :-) |
|
|
|
|
|
|
#8 |
|
SitePoint Member
Join Date: Mar 2005
Posts: 1
|
shiftlett,
Thanks for your blog post. I read it 5 mins ago. So, you say that mysql_real_escape_string is more efficient by SQL injections? ...and is it still good to use addslashes()? just to clear my mind ![]() thank you |
|
|
|
|
|
#9 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Oct 2004
Location: New York
Posts: 359
|
I've never benchmarked addslashes() and mysql_real_escape_string() to see which is fastest. The point is that addslashes() can be insufficient for protecting against SQL injection when you're using MySQL.
As Andi mentions in the comments, using bound parameters still offers the strongest protection against SQL injection, but I wanted to focus on this particular debate. Hope that helps. :-) |
|
|
|
|
|
#10 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: kansas
Posts: 673
|
Hey Chris,
thanks for the information--really interesting post. My question is: Is the exploit you mentioned only an issue if you're using the GBK character set in mysql? That was my understanding, but just wanted to clarify. Thanks, aamonkey |
|
|
|
|
|
#11 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Oct 2004
Location: New York
Posts: 359
|
It's an issue for many multi-byte character sets, specifically those with a valid character ending in 0x5c (a backslash in ISO-8859-1), because addslashes() can be tricked into adding that byte, creating a valid multi-byte character instead of escaping the single quote.
UTF-8 is immune to this attack, because all of its multi-byte characters specifically avoid bytes with a value less than 128 - this is because the first two bits are 10 in every byte except the first. Hope that helps. |
|
|
|
|
|
#12 |
|
SitePoint Member
Join Date: Aug 2006
Location: Los Angeles, CA
Posts: 1
|
Question about the level of escaping w/magic_quotes_gpc turned on.
Does anyone know if if magic_quotes_gpc = 1 accomplishes the same escaping as addslashes(), or is it more encompassing like mysql_real_escape_string() ? Been curious about this one for a while but could not get the answer through net search or anything. |
|
|
|
|
|
#13 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2006
Posts: 6,132
|
Quote:
|
|
|
|
|
|
|
#14 | |
|
SitePoint Member
Join Date: Nov 2006
Location: China
Posts: 7
|
Quote:
|
|
|
|
|
|
|
#15 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Canada
Posts: 2,564
|
Quote:
the title says it all another title says it all ...and more... Did you happen to search google before (or even after) posting your question? I can only assume you didn't because then your question would have been answered? |
|
|
|
|
|
|
#16 |
|
SitePoint Enthusiast
![]() Join Date: Jul 2006
Posts: 83
|
strip_tags() is the best stripper for html related tags.
|
|
|
|
|
|
#17 | |
|
SitePoint Member
Join Date: Nov 2006
Location: China
Posts: 7
|
Quote:
|
|
|
|
|
|
|
#18 |
|
SitePoint Member
Join Date: Sep 2007
Posts: 13
|
I would recommend using the escape_string() much simpler.
|
|
|
|
|
|
#19 |
|
SitePoint Mentor
![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,818
|
|
|
|
|
|
|
#20 |
|
SitePoint Mentor
![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 9,610
|
|
|
|
|
|
|
#21 |
|
SitePoint Member
Join Date: Nov 2007
Posts: 7
|
thanks for the tip!
|
|
|
|
|
|
#22 |
|
SitePoint Member
Join Date: Nov 2007
Posts: 1
|
felgall absolutly right, it's just reference
|
|
|
|
|
|
#23 |
|
SitePoint Member
Join Date: Jan 2008
Posts: 1
|
I think mysql_real_escape_string() is safe enough.
|
|
|
|
|
|
#24 |
|
SitePoint Zealot
![]() ![]() Join Date: Jan 2004
Location: Cape Cod
Posts: 156
|
Thanks for the link MrOrange that is a great read and I wish I had read it years ago.
|
|
|
|
|
|
#25 |
|
SitePoint Member
Join Date: Feb 2008
Posts: 2
|
Thanks for the EXTREMELY useful link! I have been searching for it for a long time
![]() |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 21:23.














Linear Mode
