+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: SQL injection?

  1. #1
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    SQL injection?

    Hi,

    http://paperhub.x10hosting.com/links.php

    I have been told that it would be quite easy to do an SQL injection on this page.

    Does anyone know if this is true, and if it is how would I fix it?

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: SQL injection?

    I tried a couple of possible exploits, but I don't think any of them worked (however, I could be wrong too). What you should pay attention for sql injections to is every variable that goes in your query. For example with the order by: if $_GET["o"] is not equal to "asc" or "desc", don't use the value in your query. Pay special attention to strings that are actually dependant on user input, such as usernames. If such variables are used in your query, ALWAYS use mysql_escape_string on them, to make sure all special characters in sql are escaped.

    Example:
    PHP Code:
    $query "SELECT * FROM `users` WHERE `login`= '".mysql_escape_string($_GET["login"])."'"
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: SQL injection?

    Quote Originally Posted by Alex Mac View Post

    I have been told that it would be quite easy to do an SQL injection on this page.

    Does anyone know if this is true, and if it is how would I fix it?

    ~Callum
    The front end (ie the page with the fields) tells you nothing about the security of the script.

    As marshian points out, never trust input submitted over the web. Check it for unsafe characters. Check it for length. Check it for acceptable values.
    Nothing is always absolutely so.

  4. #4
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: SQL injection?

    (visited only http://paperhub.x10hosting.com/links.php )

    My suggestion would be to use POST instead of GET in every form, and to use pre-fabricated query's (if possible).

    eg.
    PHP Code:
    if ( POST_ob == name && POST_o == asc)
       
    // query, no variables in here
    else
    if ( 
    POST_ob == added && POST_o == desc)
       
    // query, no variables in here
    else
    ... 
    (ugly pseudo code)

    escape every user input (it couldn't hurt to escape your output, too..)


    easy and cheap solution :P
    Last edited by slacker3; 01-09-2010 at 04:51 PM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: SQL injection?

    Better yet, use a DB driver (such as PDO) that supports prepared statements, which aren't vulnerable to SQL injection. You still have to consider other types of injection attacks, such as XSS.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    vishal's Avatar
    vishal is offline -::-X10 Guru-::- vishal has a brilliant futurevishal has a brilliant future
    Join Date
    Nov 2009
    Location
    INDIA
    Posts
    5,254

    Re: SQL injection?

    My suggestion ,WE prevent SQL injections as opposed to binary, is to use URL Encoding or Hex Encoding.
    I haven't seen a complete example of stopping SQL Injections, most refer to use the mysql_real_escape_string function or param statements.
    Regards ~ Vishal
    Giving Reputation (at bottom of my post ) is the best way to encourage the person who helped you on forums.

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL injection?

    So is this better:

    Code:
    <?php
    if(isset($_GET['ob']) && isset($_GET['o']) && (($_GET['o'] == "asc") || ($_GET['o'] == "desc")) && (($_GET['ob'] == "name") || ($_GET['ob'] == "added") || ($_GET['ob'] == "date")))
    {
    $sql = "SELECT * FROM `links` ORDER BY `{$_GET['ob']}` {$_GET['o']}";
    }else{
    $sql = "SELECT * FROM `links` ORDER BY `Name` ASC";
    }
    $result = mysqli_query($cxn,$sql) or die("SQL failed");
    $num = 1;
    while ($row = mysqli_fetch_array($result))
    {
    extract($row);
    echo "<a href=\"$Link\"><b>$Name</b></a><br /><p>$Description</p><p style=\"font-size:7pt\">Added by $added at $Date</p><br />";
    $num++;
    } 
    ?>
    Thanks for helping

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: SQL injection?

    Yes, that code cannot create a query with any values different than legit ones. Very good
    Make sure your search function is as safe as this one and you'll be sql-injection-free.
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  9. #9
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: SQL injection?

    My search boxes are written by google and phpbb, so I'm kinda hoping they're secure

    Thanks again,
    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  10. #10
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Thumbs up Re: SQL injection?

    Quote Originally Posted by Alex Mac View Post
    My search boxes are written by google and phpbb, so I'm kinda hoping they're secure

    Thanks again,
    ~Callum

    They should be fine.

    You can test the most common SQL(or XSS)-injection attacks against your forms automatically
    with the firefox-extension "SQL Inject-Me" (or "XSS-me") which is part of the "Exploit-Me" Suite.

    List of useful Firefox Extensions on my site. (the last one in "Security auditing")
    Last edited by slacker3; 01-10-2010 at 09:46 AM.

+ Reply to Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. SQL Azure with ASP Dot Net
    By zegnhabi in forum Tutorials
    Replies: 0
    Last Post: 12-13-2009, 04:32 PM
  2. Against SQL injection (PHP-MySQL)
    By hipro1 in forum Programming Help
    Replies: 2
    Last Post: 08-08-2009, 08:50 PM
  3. SQL File injection.
    By Blazer9131 in forum Free Hosting
    Replies: 2
    Last Post: 10-20-2007, 11:32 PM
  4. [REQ] SQL Injection Prevention
    By Woolie in forum The Marketplace
    Replies: 8
    Last Post: 02-13-2006, 03:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers