 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
Advertisement
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
06-25-2008, 03:01 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
SQL Injection
Hey everyone, I am looking for some ideas or opinions on how well my secure my scripting is.
Should a query come from GET, POST, or any other means...
I always do this in the queries.
PHP Code:
$_GET['group_id'] = isset($_GET['group_id']) ? (int)$_GET['group_id'] : 0; $_POST['username'] = isset($_POST['username']) ? $_POST['username'] : ''; SELECT account_id, username, account_group FROM users WHERE belongs_to_group = '" . $_GET['group_id'] . "' AND username = '" . mysql_real_escape_string($_POST['username']) . "'
I know the query doesn't really make sense... it doesn't need too. Display purposes only.
As well, the username value would have validation from the form (overflow, and proper regex validation against the username).
I let the query fail should a user enter a string through proper handling though as it (atleast to my understanding) will be looking for '0' if anything but an integer is entered.
so, since no rows were found...
PHP Code:
if (mysql_num_rows($query) > 0) { // bla hbal hbalh do stuff } else { $this->throwError('Invalid account information supplied'); }
Also, my form class (everytime form input is sent in, all values are automatically trim'ed and strip_slashed for redisplay (if form error) / processing (if passed form validation).
Thoughts? Ideas? Suggestions to better protect myself? I am just getting ready to start another project and want to get even tighter security implemented if possible.
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
|
|
|
06-25-2008, 06:54 PM
|
#2 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
To clean things up even a bit more, I am now going to use sprintf to write queries. Is this really necessary? I see them doing this in the manual, and have never done it myself.
PHP Code:
// No need for casting as %d in sprintf will take care ofit. $_GET['group_id'] = isset($_GET['group_id']) ? $_GET['group_id'] : 0; $_POST['username'] = isset($_POST['username']) ? $_POST['username'] : ''; $query = sprintf("SELECT * FROM users WHERE user_name='%s' AND user_group='%d'", mysql_real_escape_string($_POST['user_name']), $_GET['user_group'] );
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
|
|
|
06-25-2008, 08:44 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,541
Thanks: 72
|
In the past, before I switched over to Zend Framework, I ran all my PHP variables, to be used in a MySQL statement, through the following function. I'm not sure if it's actually 100% secure, but it seemed to be. I think Salathe, a while ago, pointed out some problems regarding different file-types, such as boolean values, but I don't believe anybody actually discovered any such major flaws.
It'll serve as a good base nonetheless, for if you wish to add onto it, or switch some things around, or merely use it as a reference.
I did use it in conjunction with sprintf, as you mentioned. This function places in any quotes for you, and so you shouldn't actually put in any quotes into sprintf's first argument.
To give an example:
php Code:
$szMyVar1 = 'TalkPHP.com'; $szMyVar2 = 'TalkRSI.com'; $szSQL = sprintf( "SELECT * FROM myTable WHERE myColumn = %s OR myColumn = %s", mysql_parse_value ($szMyVar1), mysql_parse_value ($szMyVar2));
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-25-2008, 08:46 PM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Posts: 126
Thanks: 12
|
Just a little note: with mysql you don't have to put quotes around integers. So user_group = %d will do the job, no need to put quotes around it!
Futhermore, it looks quite safe to me.
Edit:
Wildhoney won the race to mention about the quotes. ;)
|
|
|
|
06-25-2008, 09:20 PM
|
#6 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
Yeah, I know you don't have to put quotes around integer values. For some reason, I always have. Not sure why. Just one of those things I started doing and formed a habit around it. Is there any actual benefit to not using quotes around numeric values?
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
|
|
|
06-25-2008, 11:06 PM
|
#7 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,541
Thanks: 72
|
None as far as I can see. I thought maybe MySQL wouldn't be able to perform mathematical equations on numbers which guise themselves as strings, but it appears I am wrong, as both of these work correctly:
sql Code:
UPDATE myTable SET myCount = myCount + "2" SELECT "1" + "10"
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-26-2008, 04:33 AM
|
#8 (permalink)
|
|
The Addict
Join Date: Sep 2007
Location: Denmark
Posts: 241
Thanks: 5
|
I don't even bother to make all the *_real_escape_string() functions, I've designed an input filtering class that does it all for me and I just call it like:
PHP Code:
$tuxxedo->input->doSqlSafe('p', Array('input1', 'input2', '...'));
The 'p' tells my class it has to clean post, it can also combine with more like 'pg' (POST + GET or simply r for REQUEST (POST / GET / COOKIE)) and then it modifies the global scope variables so I don't need to care about them when calling the query after.
Using something like this made my coding much easier ;)
__________________
|
|
|
06-26-2008, 09:59 AM
|
#9 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Posts: 126
Thanks: 12
|
Quote:
Originally Posted by drewbee
Yeah, I know you don't have to put quotes around integer values. For some reason, I always have. Not sure why. Just one of those things I started doing and formed a habit around it. Is there any actual benefit to not using quotes around numeric values?
|
For me it's just about "correct" coding, a string is a string, and a integer is an integer. I have no idea if there's any benefit if you're putting quotes around it or not. Though, logically it's more correct (to me).
|
|
|
|
06-26-2008, 02:32 PM
|
#10 (permalink)
|
|
The Acquainted
Join Date: May 2008
Posts: 175
Thanks: 9
|
Yeah, I have a database abstraction class too. I just happened to use mysql_real_escape_string in the example for better understanding;
usually, my queries look like:
$this->db->query(sprintf("SELECT blah blah blah x = '%s'", $this->db->safe($_POST['var'])));
I love it because if all is well with the query, it just returns and is ready for processing but if an error happens (crucial) if I have debug turned on it will output what i passed in as the query, error message etc. In production, it sends an email to me so I can catch SQL Injection attempts and the like.
Plus should I ever change databases i simply need to change the code in the query()method. Gotta love it:)
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
|
|
|
07-03-2008, 02:18 PM
|
#11 (permalink)
|
|
The Visitor
Join Date: Nov 2007
Posts: 3
Thanks: 1
|
I have been using my own escape functions on $_GET and $_POST before running them in queries but I am migrating over to the Zend Framework. Is this still necessary or does the Zend Framework take care of that for me?
|
|
|
|
07-03-2008, 03:20 PM
|
#12 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: the Netherlands
Posts: 224
Thanks: 2
|
@ Blakew: How about running a test-query via the Zend Framework? That way you'll have your awnser in a few minutes :)
(I don't know)
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|