Author Topic: SQL Injection  (Read 1177 times)

Offline chesney-93

  • Level 7
  • *
  • Posts: 30
  • Reputation: +0/-0
    • View Profile
SQL Injection
« on: June 30, 2011, 06:45:51 AM »
I've been looking at ways to prevent this and the most common I can see is the add slash function, what do you think and what do you use?


Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: SQL Injection
« Reply #1 on: June 30, 2011, 06:51:21 AM »
do not use addslashes function, sql injection is 'user input treated as sql content' which is solved by escaping

escaping means making sure the value is taken literally and not in a context of its surroundings

addslashes is not related to any context, therefore in the matter of escaping quite useless

for escaping values for mysql strings use mysql_real_escape_string or pdo's prepared statements (which are actually not exactly escaping themselves, but has this side effect)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: SQL Injection
« Reply #2 on: June 30, 2011, 07:05:49 AM »
I've been looking at ways to prevent this and the most common I can see is the add slash function, what do you think and what do you use?

Don't bother trying to escape user input, it is not a foolproof solution. You must use prepared statements if you wish to avoid sql injection. Its very easy to do. Check this link for a quick explanation.
http://www.ultramegatech.com/blog/2009/07/using-mysql-prepared-statements-in-php/

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: SQL Injection
« Reply #3 on: June 30, 2011, 07:11:51 AM »
Don't bother trying to escape user input
Only in context of the SQL if using prepared statements! and be careful about that

Generally never trust anything that's received from user (from forms, urls, http headers, anything)... actually, many people say never trust any input, thus not even your files or database - because for example you can escape input by prepared statements in SQL context, but if you print it out then, user can have included some JS script - so always escape/validate
« Last Edit: June 30, 2011, 07:14:11 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: SQL Injection
« Reply #4 on: June 30, 2011, 07:27:14 AM »
Don't bother trying to escape user input
Only in context of the SQL if using prepared statements! and be careful about that

Generally never trust anything that's received from user (from forms, urls, http headers, anything)... actually, many people say never trust any input, thus not even your files or database - because for example you can escape input by prepared statements in SQL context, but if you print it out then, user can sneak in some JS script - so always escape/validate
This is completely true. A user could embed some nasty js in there. Escaping will just not protect you from SQL injection, but escaping is certainly good to protect against other things.

--edited for clarity
« Last Edit: June 30, 2011, 07:41:39 AM by pirategaspard »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: SQL Injection
« Reply #5 on: June 30, 2011, 07:31:58 AM »
addslashes is fine, but it is recommended to use mysql_real_escape_string instead (prevents SQL injection for some exotic charsets).

Also very important thing, make sure integers are integers (or just make a dirty "a='var'" so everything is treates as a string). You need it to prevent integer injection which does not require slashes. Example "WHERE a=$var" hack: "$var=5 OR 0" if you do "WHERE a='$var'" it won't work; of course properly would be to make "$var=(int)$_GET['var']" instead.

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: SQL Injection
« Reply #6 on: June 30, 2011, 07:35:33 AM »
addslashes is fine, but it is recommended to use mysql_real_escape_string instead (prevents SQL injection for some exotic charsets).
How is it fine then if it prevents only something? :)

Also very important thing, make sure integers are integers (or just make a dirty "a='var'" so everything is treates as a string). You need it to prevent integer injection which does not require slashes. Example "WHERE a=$var" hack: "$var=5 OR 0" if you do "WHERE a='$var'" it won't work; of course properly would be to make "$var=(int)$_GET['var']" instead.
good point
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: SQL Injection
« Reply #7 on: June 30, 2011, 07:45:45 AM »
addslashes is fine, but it is recommended to use mysql_real_escape_string instead (prevents SQL injection for some exotic charsets).
How is it fine then if it prevents only something? :)
If you use latin or UTF-8 database charset you are fine with addslashes. Real_escape is require if you set it to some chinese charset or something :) So, for 99.999% of devs addslashes is technicly identical to real_escape.

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: SQL Injection
« Reply #8 on: June 30, 2011, 07:46:40 AM »
addslashes is fine, but it is recommended to use mysql_real_escape_string instead (prevents SQL injection for some exotic charsets).
None of the escaping techniques will be fool-proof for SQL injection. You're essentially creating a character black list and there will always be ways around that. Prepared statements are the ONLY way that will truly eliminate the possibility of SQL injection.  
As Nox said above, you will want to escape your input for other reasons, but not for protection against SQL injection

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: SQL Injection
« Reply #9 on: June 30, 2011, 11:03:38 AM »
Be aware that prepared statements require an additional trip to the sql server. So for every query you execute it must go to the sql server to validate then return and then you submit the prepared query and get your result. Also with prepared statements that means you are forced to use the mySQLi interface, which for version 5.x of PHP, does not allow persistent connections to the database.

Creating online addictions, one game at a time:

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: SQL Injection
« Reply #10 on: June 30, 2011, 11:44:04 AM »
So for every query you execute it must go to the sql server to validate then return and then you submit the prepared query and get your result.
Its my understanding that you should only get a double hit on the first request. Once to parse the SQL, and once to get the data. After that the parsed SQL is cached. Because of this prepared statements can be faster than a straight query.  Obviously there will be no speed benefit if you only use a query once however.

Edit: hadn't researched persistent connections before, but PHP manual says you can do them with mysqli as of 5.3
Quote
Unlike the mysql extension, mysqli does not provide a separate function for opening persistent connections. To open a persistent connection you must prepend p: to the hostname when connecting.
http://www.php.net/manual/en/mysqli.persistconns.php
« Last Edit: June 30, 2011, 11:51:39 AM by pirategaspard »

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: SQL Injection
« Reply #11 on: June 30, 2011, 02:02:46 PM »
Obviously there will be no speed benefit if you only use a query once however.
Which is why everyone should test for there own usage. I was simply pointing out the pitfalls of mySQLi extension which is not the panacea many point it out to be. For our games this simply is not the case as you have hundreds of players playing 4 different games and games unlike other site are equal to write operations as they are to read which really plays havoc with the query cache.

Quote
Edit: hadn't researched persistent connections before, but PHP manual says you can do them with mysqli as of 5.3

Unlike the mysql extension, mysqli does not provide a separate function for opening persistent connections. To open a persistent connection you must prepend p: to the hostname when connecting.
My mistake last time I looked into it, it did not support persistent connections and it was noted they would be supported when 6.x was released.






« Last Edit: June 30, 2011, 02:06:33 PM by codestryke »
Creating online addictions, one game at a time:

Offline Nox

  • Level 35
  • **
  • Posts: 768
  • Reputation: +12/-2
    • View Profile
Re: SQL Injection
« Reply #12 on: June 30, 2011, 02:22:39 PM »
Quote
Which is why everyone should test for there own usage. I was simply pointing out the pitfalls of mySQLi extension which is not the panacea many point it out to be. For our games this simply is not the case as you have hundreds of players playing 4 different games and games unlike other site are equal to write operations as they are to read which really plays havoc with the query cache.
Wouldn't it be benefitial so split it somehow?

As for 5.x ... today PHP 5.3 is already 2 years old! Anything starting with 5.2 and below is a relic and is unsupported and strongly unrecommended ... it's sad 5.3 have so small coverage still ... now when php5.4 is coming (alpha released... with really cool stuff and huge performance improvements) /flame

Of course you can have old projects, but I assume the OP doesn't work on 4 years old legacy code, but building something new
« Last Edit: June 30, 2011, 02:31:51 PM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline chesney-93

  • Level 7
  • *
  • Posts: 30
  • Reputation: +0/-0
    • View Profile
Re: SQL Injection
« Reply #13 on: July 02, 2011, 02:33:05 PM »
Loads of information here, well I'ved done a few bits of everything to just test it a little bit before I do everything.

Could someone give me an example of what to put to test against the injection

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: SQL Injection
« Reply #14 on: July 02, 2011, 02:47:35 PM »
Test yourself? What for? :D Put your website online, google for "hacking forum" and ask them to hack it. Simple, easy and the most accurate :D

Offline andrewjbaker

  • Level 17
  • *
  • Posts: 154
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: SQL Injection
« Reply #15 on: July 02, 2011, 02:53:07 PM »
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: SQL Injection
« Reply #16 on: July 02, 2011, 05:29:00 PM »
I'd suggest that you use Vega http://www.subgraph.com/products.html I've ran it against my own pages, and thus far it's shown nothing.(I've already previously made sure that there was no security flaws or issues before using this product and used it as a test to see if it was worth using.)

It's an automated tool, it requires java, but other than that problem, it's really awesome. It's also really fast. Plus it provides information about how to fix things(if you have an issue), I tried enabling directories(and it warned me of it and included more information about it). I suggest that _everyone_ here uses this tool to test their own code, even if they go over everything by hand and go line by line checking for all issues and attack vectors.

Offline Mutant

  • Level 10
  • *
  • Posts: 55
  • Reputation: +5/-0
    • View Profile
    • Kingdoms
Re: SQL Injection
« Reply #17 on: July 02, 2011, 06:13:43 PM »
SQL Injection is only one sort of attack you should be concerned about.

Check out https://www.owasp.org/ for others (including details on how to prevent them).

Offline StudioFortress

  • Level 3
  • *
  • Posts: 6
  • Reputation: +1/-0
    • View Profile
Re: SQL Injection
« Reply #18 on: July 20, 2011, 12:03:33 PM »
The best strategy I have to avoid security issues, is to just have a good architecture. I believe one reason we have all these security issues on various websites is because the insecure option is also the easiest. So by building a good library, that tackles the issue for me, it makes the secure option the easy option.

For example for SQL injection, I have my own Database class sitting between me and the DB, which automatically generates my SQL queries for me and escapes all inputs. That way 99% of my code, the common cases, prevent SQL by default. So now only the corner cases, the rare occasions I have to write SQL, are when I have to be careful. The other advantage is that if my security is flawed, then I only have to fix it in one place: the Database class.

I do this with all my libraries so I have multiple layers of security. For example with user input I state what I have to state what I think the input should be when it is retrieved (an integers, alphanumeric, a valid e-mail address, matched against a regular expression, and others). If the data fails to match, then 'null' is returned. For example you couldn't pass in a snippet of SQL as an id because it would fail to look like an integer.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal