php query security

Discussion in 'PHP' started by Silver89, Jul 27, 2008.

  1. #1
    Hi,

    When inserting a comment into a database is it safe to insert as the following:

    mysql_real_escape_string($comment);
    PHP:
    Then when retrieving it too display on a web page use:

    stripslashes($comment);
    PHP:
    So the slashes only appear in the database?

    Thanks
     
    Silver89, Jul 27, 2008 IP
  2. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If the comment is going to be submitted by a member of the public, then no, not quite. Unless you want people to be able to post html tags, you'll want to use
    htmlentities($comment);
    Code (markup):
    when it is fetched from the database for display, as well as stripslashes. (For more info on this, search for XSS on google).

    If you do want people to be able to use html tags, but not execute XSS 'attacks' then someone else'll have to post some code to filter out XSS while allowing some html (haven't got any code for it ready right now).
     
    Pos1tron, Jul 27, 2008 IP
    Silver89 likes this.
  3. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #3
    Using stripslashes after data fetch is wrong, you don't need it.
     
    wmtips, Jul 28, 2008 IP
    Silver89 likes this.
  4. slaydragon

    slaydragon Banned

    Messages:
    1,403
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi, why is that wrong? if we do not strip the slashes, and just output it, results will be with "slashes"?
     
    slaydragon, Jul 28, 2008 IP
  5. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #5
    Where do you see "extra slashes" here? mysql_real_escape_string used only for database writing, and string in DB will be "as is".
     
    wmtips, Jul 28, 2008 IP
  6. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #6
    Could someone put some sample xss attack code so I can input and see if anything happens or is it too malicious?

    I'm using Before input:

    
    function secureinput($comment)
    {
    	$comment = mysql_real_escape_string($comment);
    	$replace = array('<', '>');
    	$comment = str_replace($replace,'',$comment);
    	return $comment;
    }
    
    PHP:
    then from database:

    $comment = striplashes($row);
    PHP:
    This allows the bbcode and smilies etc to all work and as far as i'm aware keeps everything secure.
     
    Silver89, Jul 29, 2008 IP
  7. hanz

    hanz Peon

    Messages:
    241
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    http://ha.ckers.org/xss.html

    and you should run striplashes() before mysql_real_escape_string(), Magic Quotes is the keyword. Currently you are turning
    '
    Code (markup):
    into
    \\\'
    Code (markup):
    but you only need
    \'
    Code (markup):
     
    hanz, Jul 29, 2008 IP
  8. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #8
    When i said i run striplashes, I meant i run it in the script that displays the code on another page, is this wrong?

    What should the function above look like?
     
    Silver89, Jul 29, 2008 IP