|
|
|
|
![]() ![]() |
Oct 9 2005, 03:46 AM
Post
#1
|
|
|
Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 1,020 Joined: 25-September 05 From: L.A. Member No.: 12,251 |
Does anyone know how to keep people from using SQL injection on my site, like stop them from commenting everything beyond the text box out?
|
|
|
|
Oct 9 2005, 10:01 AM
Post
#2
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 326 Joined: 7-October 05 Member No.: 12,650 |
Yep there is.
Okay, i don't quite know how to explain, but these are the basics. Sql injections involve performing unauthorized sql commands such as 'delete `tablename`'; Well, in an attempt to stop these, what you need to do is 2 main things, there are others i believe although can't remember them. 1) firstly make sure that you haven't got majorly changable sql like "truncate `$table`"; 2)use two quotes instead of one for forms which the user can type on, this helps greatly. I learnt php at school ages ago, so am sure this is right. Sorry if it's not. Hope this helps. |
|
|
|
Oct 9 2005, 10:46 AM
Post
#3
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 874 Joined: 30-July 04 Member No.: 246 |
QUOTE 2)use two quotes instead of one for forms which the user can type on, this helps greatly. How does that help? Not that I'm saying you're wrong, but double quotes are just as easily broken out of as single quotes. qwertyiscool, in order for it to be effective, you need to thouroughly 'sanitize' the user's input. One way of doing so may be to check for any characters which you know should not appear in the string - or, as is sometimes easier, making sure there are only characters that can appear in the string. For example, a person's name is only going to contain letters, with the possibility of one or more spaces or full stops (if the name is abreviated) - so something like this could be used: CODE // Will match if the string contains any characters that are NOT alphabeticall, a period, or a space. if( preg_match('/[^a-zA-Z\. ]/', $_POST['name']) ) { // Contains invalid characters. } There isn't really a single universal solution for weeding out SQL injection - it really depends on the situation, and what sorts of data the user is going to input. |
|
|
|
Oct 9 2005, 03:53 PM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 123 Joined: 5-September 05 Member No.: 11,522 |
I personally try to structure my SQL statments so that the absolute minimum changes are able to occur. So, like sportytalk said, add detail to your SQL statements so that no one can add information to your statement. I also often use parentheses, which make it really hard to change the original statement. If you want the best documentation about how you can structure your statements, I would suggest exploring the SQL documentation at http://www.mysql.com.
Good Luck! And good coding! |
|
|
|
Oct 10 2005, 04:19 AM
Post
#5
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 874 Joined: 30-July 04 Member No.: 246 |
Again, using parentheses will do little to provide any sort of protection.
Take this, for example: CODE mysql_query('SELECT * FROM members WHERE user_name = ("' . $_POST['user_name'] . '")'); // The query would become: mysql_query('SELECT * FROM members WHERE user_name = ("Spectre")'); This could easily have values injected into it as follows: CODE // In the user_name field, simply place '") OR user_name = ("admin"'. // The query would become: mysql_query('SELECT * FROM members WHERE user_name = ("Spectre") OR user_name = ("admin")'); One of the reasons that some scripts are so 'easy' to exploit is because they display MySQL errors. Take, for example, IPB (I don't know about this version, but previous versions are guilty as charged) - when a MySQL error occurs, it displays the exact error returned from MySQL, which gives away database structure information and displays which values are being added where. If the attacker doesn't know what MySQL queries are taking place, and isn't aware of the field names and structure of the database, it makes it more difficult for him to effectively inject any SQL statements or modify any conditionals etc. (certainly not impossible, just more difficult). Extracting information can also be tricky if no returned results are sent as output. |
|
|
|
Oct 10 2005, 10:19 AM
Post
#6
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
hi qwertyiscool,
you can use regular expressions to match invalid input and filter out what can be special characters in a statement like the * or ? etc.. Actually list out a set of inputs that would result in injecting SQL in ur query. Then write statements to filter out such inputs. In Java you can have a filter class which would filter the input based on regular expression and then can be safely used. I am not sure of the exact solution in PHP but something would surely be possible along similar lines. Cheers |
|
|
|
Oct 10 2005, 10:41 AM
Post
#7
|
|
|
Administrator ![]() Group: Admin Posts: 1,459 Joined: 11-June 04 From: Somewhere in Time & Space. Member No.: 1 |
The most recommended way is using the PHP inbuilt function.
CODE string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] ) Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection. (PHP 4 >= 4.3.0) QUOTE This function will escape special characters in the unescaped_string, taking into account the current charset of the connection so that it is safe to place it in a mysql_query(). Example : CODE <?php $item = "Zak's and Derick's Laptop"; $escaped_item = mysql_real_escape_string($item); printf ("Escaped string: %s\n", $escaped_item); ?> |
|
|
|
Nov 5 2005, 05:24 AM
Post
#8
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 15 Joined: 4-November 05 Member No.: 13,784 |
Filtering out the"bad stuff" is essentially what you want to do, but how can you know what all the bad stuff is? A better way to approach this, imho, is to only permit the "safe stuff" and therefore by default you omit anything unsafe.
Regular expressions are really useful for this...allow only a-zA-Z0-9 as legal input. Any other character gets rejected. Well, ok, in practce you might have to permit other characters like the single quote (for names like O'Hare) and the hyphen, maybe one ot two others depending on the app. I have to admit I often also strip out certain character sequences like "exe", "select", "where", etc. If it messes up a user's input to the point that it's unusable, then they might have to use another channel of communication. I think that's a fair price to ask for security. What I do is strip out any offending character and then send the input through, *maybe* giving the user a peek at it with a short explanation as to why it's different from the original input. Frankly, if they try to slip a pipe into a query, they're lucky if I don't add their IP to a banned list ;-) |
|
|
|
Nov 10 2005, 09:07 PM
Post
#9
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 73 Joined: 10-November 05 Member No.: 14,067 |
Hmm i find by using ( $_REQUEST ) is much safer then ( $_POST ) also like some people have stated here, using statements in your scripts such as
CODE if (!ctype_alnum($variable)) { die ('Illegal Characters'); } , thus would prevent anyone from using anything besides alpha numeric characters ( letters & numbers ) . Now this is basically just preventing people from attacking you with your form , there's lot's of other thing's they can inject through . All depends on what your running.
|