stig1 Posted December 3, 2008 Share Posted December 3, 2008 Hey Whats the best method to stop SQL Injection on all values entered into a database that the user gets to create? Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/ Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 mysql_real_escape_string() Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705437 Share on other sites More sharing options...
Jahren Posted December 3, 2008 Share Posted December 3, 2008 I personaly use function remove_html_tags($chaine) { return htmlentities($chaine, ENT_QUOTES); } and then I use function put_back_html_tags($chaine) { return html_entity_decode($chaine); } to read it back Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705441 Share on other sites More sharing options...
Xtremer360 Posted December 3, 2008 Share Posted December 3, 2008 I agree with gevans. I always use mysql_real_escape_string() Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705444 Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 Jahren, htmlentities wont escape single and double quotes which are the main issues in sql injection if you only use htmlentites I could probably hack any of your sites Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705507 Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Share Posted December 3, 2008 Jahren, htmlentities wont escape single and double quotes which are the main issues in sql injection if you only use htmlentites I could probably hack any of your sites he used htmlentities($chaine, ENT_QUOTES) Note ENT_QUOTES... Its an option to encode quotes or not basically, this is the same methord i use, and i havnt got an injection past it yet.. Though no method is 100% secure... And i cant speak for the other option, just my views. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705538 Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 Jahren, htmlentities wont escape single and double quotes which are the main issues in sql injection if you only use htmlentites I could probably hack any of your sites he used htmlentities($chaine, ENT_QUOTES) Note ENT_QUOTES... Its an option to encode quotes or not basically, this is the same methord i use, and i havnt got an injection past it yet.. Though no method is 100% secure... And i cant speak for the other option, just my views. That is true, but that function isn't built for the purpose of this thread. mysql_real_escape_string() from php.net; This function must always (with few exceptions) be used to make data safe before sending a query to MySQL. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705547 Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 Though I do use more than just that, I have a class set up to do a lot of different 'clensing' <?php var $old_string = array('\r\n', '\n', '\r'), $new_string = '<br />'; function input($input,$url = 1){ if('get_magic_quotes_gpc') $input = stripslashes($input); $input = mysql_real_escape_string($input); $input = strip_tags($input); $input = str_replace($this->old_string,$this->new_string,$input); $input = trim($input); if($url) $input = urlencode($input); $input = ($input == "") ? NULL : $input; return $input; } ?> Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705549 Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Share Posted December 3, 2008 Jahren, htmlentities wont escape single and double quotes which are the main issues in sql injection if you only use htmlentites I could probably hack any of your sites he used htmlentities($chaine, ENT_QUOTES) Note ENT_QUOTES... Its an option to encode quotes or not basically, this is the same methord i use, and i havnt got an injection past it yet.. Though no method is 100% secure... And i cant speak for the other option, just my views. That is true, but that function isn't built for the purpose of this thread. mysql_real_escape_string() from php.net; This function must always (with few exceptions) be used to make data safe before sending a query to MySQL. Or be extra safe and whack it through both ^^ EDIT Or what the post above me just said XD Use em all! Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705552 Share on other sites More sharing options...
gevans Posted December 3, 2008 Share Posted December 3, 2008 Haha, exactly. It's not needed but I always urlencode everything, then the little fu**ers are screwed Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705560 Share on other sites More sharing options...
corbin Posted December 4, 2008 Share Posted December 4, 2008 Y'all take a slightly different approach than I do. I only escape ' since it's the only thing (don't go and get all technical on me with this statement) that needs to be escaped (I use mysql_real_escape_string), and then if the HTML needs to be escaped, I escape it when it's echoed out to the user. Makes more sense to me. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705569 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Exactly, everyone has they're own methods, and as long as you're secure happy days! Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705573 Share on other sites More sharing options...
Jahren Posted December 4, 2008 Share Posted December 4, 2008 I use htmlencode also for portability. I encode html chars into the database for later use on windows or linux machines without problems ^^ Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705574 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 I've been using Rich Text Editors in some bespoke CMS's recently, they are a pain in the ass to work from browser to browser and have to go through a lot of filtering, kind of a similar idea Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705577 Share on other sites More sharing options...
ardyandkari Posted December 4, 2008 Share Posted December 4, 2008 i use this: (stripslashes(htmlentities(mysql_real_escape_string($whatever)))) maybe it is overboard, but i want all bases covered...twice Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705580 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 That can cause problems sometimes, imagine this from a form - ' OR 1=1 $var = $_POST['form the form']; $var = stripslashes(htmlentities(mysql_real_escape_string($var)))); $var is now \\' OR 1=1 so the first escape is now escaping the second SELECT * FROM database WHERE this='' OR 1=1 Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705584 Share on other sites More sharing options...
ardyandkari Posted December 4, 2008 Share Posted December 4, 2008 That can cause problems sometimes, imagine this from a form - ' OR 1=1 $var = $_POST['form the form']; $var = stripslashes(htmlentities(mysql_real_escape_string($var)))); $var is now \\' OR 1=1 so the first escape is now escaping the second SELECT * FROM database WHERE this='' OR 1=1 so you recommend just mysql_real_escape_string()? also, i did have it tested in the beta test area. darkwater (i think) used actunix or whatever and it didnt come up with any problems... one other question...if using just mysql_real_escape_string(), when you display the data, do you need to use stripslashes() then? Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705594 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 I just realised, you have stripslashes() and mysql_real_escape_string() (though you shouldn't, don't you mean addslashes()?) Using stripslashes(htmlentities(mysql_real_escape_string($var)))) will leave you with the original string, escaped and un-escaped if you meant addslashes(htmlentities(mysql_real_escape_string($var)))) you will have everything escaped twice; $var "'this is my variable'"; echo addslashes(htmlentities(mysql_real_escape_string($var))); //outputs \\\'this is my variable\\\' I'd recommend using just mysql_real_escape_string(); If you want to go further by all means use htmlentities() or urlencode() (as well as standard stuff, trim() for example) Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705603 Share on other sites More sharing options...
ardyandkari Posted December 4, 2008 Share Posted December 4, 2008 no...i dont use addslashes() mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a. it looks like it adds backslashes to those characters...when the entry is applied to the db, is it the actual entry or will it have extra slashes? or will it just do something like this: form entry-> ' OR 1=1 database entry -> \' OR 1=1 still confused about injection attacks...read up all over the net and they still go over my head. sorry. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705609 Share on other sites More sharing options...
corbin Posted December 4, 2008 Share Posted December 4, 2008 In MySQL (and most SQL dialects) '' denotes a string, much like it does in PHP. For example, in PHP, if you wanted to put This is Corbin's Post in a string with double quotes, you would have to do: $string = 'This is Corbin\'s Post'; Yes? Well, MySQL parses too. The MySQL server parses tokens out of the query. So, similar to in PHP, things need to be escaped. \ is the escape character in MySQL (and PHP). So, to send it a value of the mentioned string earlier, one would have to do: SELECT * FROM posts WHERE content = 'This is Corbin\'s post'; If it had been SELECT * FROM posts WHERE content = 'This is Corbin's post'; s post' would've been considered outside of the string. Now consider this example: SELECT 1 FROM users WHERE username = '$s1' AND password = '$2'; Now, let's pretend $s1 is "admin" What if someone submitted ' OR 1 = 1;-- as $s2? It would turn the query into: SELECT 1 FROM users WHERE username = 'admin' AND password = '' OR 1 = 1;--; Everything after and including -- is ignored, so that would in essence be: SELECT 1 FROM users WHERE username = 'admin' AND password = '' OR 1 = 1; Which would select 1 if the password were '' or if 1 = 1 (always true). Oh, to answer your real question, no the actual string won't have the slash. Does $string = 'This is Corbin\'s Post'; actually have the slash in it? Of course not. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705610 Share on other sites More sharing options...
ardyandkari Posted December 4, 2008 Share Posted December 4, 2008 thanks. both for the lengthy explanation and also the answer the idea of injection is still past me, but i didnt know that \ was the escape character for mysql. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705617 Share on other sites More sharing options...
corbin Posted December 4, 2008 Share Posted December 4, 2008 What do you not get about it? You're probably over complicating it. It's really not a difficult concept once you see it the first time. Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705628 Share on other sites More sharing options...
ardyandkari Posted December 4, 2008 Share Posted December 4, 2008 Now consider this example: SELECT 1 FROM users WHERE username = '$s1' AND password = '$2'; Now, let's pretend $s1 is "admin" What if someone submitted ' OR 1 = 1;-- as $s2? It would turn the query into: SELECT 1 FROM users WHERE username = 'admin' AND password = '' OR 1 = 1;--; Everything after and including -- is ignored, so that would in essence be: SELECT 1 FROM users WHERE username = 'admin' AND password = '' OR 1 = 1; Which would select 1 if the password were '' or if 1 = 1 (always true). basically i dont understand how the OR 1 = 1; portion works. this is assuming that this is a login, right? so what happens is in the user name text field you enter < admin > and in the password box you enter < ' OR 1 = 1; > (<>denotes the text box, you dont actually type it.) now this is assuming that you know what the user name is, but if one isnt sanitized, all probably aren't right? so you could use the same in both? i do understand that 1 = 1 is always true, but how does that affect the login? does that automatically log you in? Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705651 Share on other sites More sharing options...
ardyandkari Posted December 4, 2008 Share Posted December 4, 2008 btw... i am posting a link to a little program that i was working on... http://everkleen.biz/resort/literes/index.php i used the full <<stripslashes(htmlentities(mysql_real_escape_string($var))))>> sanitize string that i posted. the username is test the password is d if you want to look around, you can...not finished yet though. try injecting. like i said, it was tested at one point and it passed... Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705653 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 That page seems pretty safe after a little play, but I'm not sure why, lol stripslashes(htmlentities(mysql_real_escape_string($var))) is not a good thing to do, ur excaping and stripping slashes so you end up with the original string Link to comment https://forums.phpfreaks.com/topic/135427-sql-injection/#findComment-705907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.