PDA

View Full Version : SQL Injections



Hitman
24-11-2007, 04:18 PM
Hey!

I'm not good with security (lol), what do I need to do to secure my forums that put data into sql databases?

To stop sql injections, html etc?

Can somebody find me a tut or write the code with where it goes? I've used google, no use. I had it before, but the link is dead. :(

MrCraig
24-11-2007, 04:24 PM
Hey!

I'm not good with security (lol), what do I need to do to secure my forums that put data into sql databases?

To stop sql injections, html etc?

Can somebody find me a tut or write the code with where it goes? I've used google, no use. I had it before, but the link is dead. :(

Use a cleaning function :)



<?
function clean($str)
{
$cl = strip_tags(addslashes(stripslashes(htmlspecialchar s($str))));
return $cl;
}


Then use the cleaning string when declaring vars.

eg

$v1 = clean($_GET[v1]);

Hitman
24-11-2007, 04:43 PM
Thanks! :)

lolwut
24-11-2007, 04:53 PM
mysql_real_escape_string() is the best thing to help you with this, but make sure you're connected to a database first or it won't work.
I.e:

include something.php;
$var = $_POST['var'];
$newvar = mysql_real_escape_string($var);

MrCraig
24-11-2007, 05:03 PM
Dont mean to sound stupid, but what does mysql_real_escape_string do anyways ?

Like does it filter out commands or something?

Florx
24-11-2007, 06:20 PM
http://us.php.net/mysql_real_escape_string

Beau
24-11-2007, 09:37 PM
I'd suggest using sprintf as well:



$query = sprintf("SELECT * FROM users WHERE username='%s'", mysql_real_escape_string($_POST['username']);
$query = mysql_query($query);


That tells PHP beforehand what the string should look like, thus preventing injections somewhat.

Also, don't throw strip_tags, htmlspecialchars and mysql_real_escape_string into the one clean function. Make two; one for sanitizing and one for returning. Sanitizing will apply to all data going into the database (just need mysql_real_escape_string), and returning for data being echoed back to the user (htmlspecialchars imo, no need to strip the tags, just display everything back). You'll find yourself using a load of server resources if you echo everything back using mysql_real_escape_string.

Hitman
24-11-2007, 11:13 PM
I'd suggest using sprintf as well:



$query = sprintf("SELECT * FROM users WHERE username='%s'", mysql_real_escape_string($_POST['username']);
$query = mysql_query($query);
That tells PHP beforehand what the string should look like, thus preventing injections somewhat.

Also, don't throw strip_tags, htmlspecialchars and mysql_real_escape_string into the one clean function. Make two; one for sanitizing and one for returning. Sanitizing will apply to all data going into the database (just need mysql_real_escape_string), and returning for data being echoed back to the user (htmlspecialchars imo, no need to strip the tags, just display everything back). You'll find yourself using a load of server resources if you echo everything back using mysql_real_escape_string.

OK, so for everything put the mysql_real_escape_string, for things like signature etc put the htmlspecialchars (so no redirections etc).

Beau
24-11-2007, 11:26 PM
OK, so for everything put the mysql_real_escape_string, for things like signature etc put the htmlspecialchars (so no redirections etc).

Pretty much. Thing is, you mightn't want to remove HTML from certain things, like news posts and stuff. That's why you don't want it all in the same function ;)

Hitman
24-11-2007, 11:32 PM
Pretty much. Thing is, you mightn't want to remove HTML from certain things, like news posts and stuff. That's why you don't want it all in the same function ;)
You're 2 steps ahead of me. :P

Hitman
25-11-2007, 03:56 PM
I can't edit but I have a question.

Would I be best to use this if I didn't want html, sql injections, javascript etc? I'll be making BBcode for html, so no redirects etc...



<?
function clean($str)
{
$cl = strip_tags(addslashes(stripslashes(htmlspecialchar s($str))));
return $cl;
?>

Beau
26-11-2007, 05:29 AM
Don't bother with addslashes, stripslashes will work by itself fine. But apart from that, looks good.

Mentor
26-11-2007, 03:32 PM
A relvent comic:

http://imgs.xkcd.com/comics/exploits_of_a_mom.png

MrCraig
26-11-2007, 04:04 PM
ROFL. @ above.

Hitman
26-11-2007, 04:10 PM
A relvent comic:

http://imgs.xkcd.com/comics/exploits_of_a_mom.png
Aha, thanks for posting that. +rep :D

zeJosh
26-11-2007, 04:21 PM
Hahah, very good!

Beau
27-11-2007, 05:13 AM
I saw this during SOSE in the computer lab. I started laughing, and everyone looked at me weirdly :P

Want to hide these adverts? Register an account for free!