Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. SQL Injection Prevention - How Good Are These Measures?

SQL Injection Prevention - How Good Are These Measures?

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasephplampmysqlsysadmin
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nalorin
    wrote on last edited by
    #1

    Some background: I have taken a few measures to prevent SQL injection on my PHP/MySQL setup (currently WAMP for development, but will be LAMP for production server): - In my users table, I have entered a "bad" user, with all the fields equal to 0.     - this is the first user in the table     - if a hacker tries to enter ' or ''=' in the uname field:         - (theoretically,) "bad" user will be first result, and         - (theoretically,) # of results will also be greater than 1 (more than 1 user)     - (theoretically,) the following code will prevent said hacker from gaining unauthorized access - mysql_real_escape_string() function will be used to escape input when site is launched, but right now it is not in use to allow testing of common SQL injection methods.     - I have read that mysql_real_escape_string() has some vulnerabilities.     - I know mysql_real_escape_string() is more secure than addslashes().

    /* // This block will be uncommented after development
    $u = mysql_real_escape_string($_POST[uname]);
    $p = mysql_real_escape_string($_POST[pass]); */

    // These lines will be replaced by commented block above after development
    $u = $_POST[uname];
    $p = $_POST[pass];

    // I could add " order by uid asc" to the following query, to ensure 'bad' user listed first if SQL injection occurs, and to prevent hacker from using order by
    $query = "select uid,uname,fname,lname,email,phone,other,pass from ads.users where uname = '$u'";
    $result = mysql_query($query);
    $rows = mysql_num_rows($result);

    // if more than 1 row, SQL injection attempted (uname has 'unique' flag in database, so only safe to return 1 row)
    if ($rows > 1) {
    /* <<Do Stuff - Security Measures (ban IP, etc)>> */
    die ("Error[20]: You have entered potentially harmful input. Security measures have

    B A 2 Replies Last reply
    0
    • N nalorin

      Some background: I have taken a few measures to prevent SQL injection on my PHP/MySQL setup (currently WAMP for development, but will be LAMP for production server): - In my users table, I have entered a "bad" user, with all the fields equal to 0.     - this is the first user in the table     - if a hacker tries to enter ' or ''=' in the uname field:         - (theoretically,) "bad" user will be first result, and         - (theoretically,) # of results will also be greater than 1 (more than 1 user)     - (theoretically,) the following code will prevent said hacker from gaining unauthorized access - mysql_real_escape_string() function will be used to escape input when site is launched, but right now it is not in use to allow testing of common SQL injection methods.     - I have read that mysql_real_escape_string() has some vulnerabilities.     - I know mysql_real_escape_string() is more secure than addslashes().

      /* // This block will be uncommented after development
      $u = mysql_real_escape_string($_POST[uname]);
      $p = mysql_real_escape_string($_POST[pass]); */

      // These lines will be replaced by commented block above after development
      $u = $_POST[uname];
      $p = $_POST[pass];

      // I could add " order by uid asc" to the following query, to ensure 'bad' user listed first if SQL injection occurs, and to prevent hacker from using order by
      $query = "select uid,uname,fname,lname,email,phone,other,pass from ads.users where uname = '$u'";
      $result = mysql_query($query);
      $rows = mysql_num_rows($result);

      // if more than 1 row, SQL injection attempted (uname has 'unique' flag in database, so only safe to return 1 row)
      if ($rows > 1) {
      /* <<Do Stuff - Security Measures (ban IP, etc)>> */
      die ("Error[20]: You have entered potentially harmful input. Security measures have

      B Offline
      B Offline
      Bradml
      wrote on last edited by
      #2

      To prevent SQL injection all you need to do is escape and use good validation. Other stuff can be a waste of time.


      Brad Australian The PHP MVP - Christian Graus on "Best books for VBscript" A big thick one, so you can whack yourself on the head with it.

      M C H 3 Replies Last reply
      0
      • B Bradml

        To prevent SQL injection all you need to do is escape and use good validation. Other stuff can be a waste of time.


        Brad Australian The PHP MVP - Christian Graus on "Best books for VBscript" A big thick one, so you can whack yourself on the head with it.

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        I usually use this function to prevent SQL injection, maybe it's useful for you :

        function quote_smart($value)
        {
        // Stripslashes
        if (get_magic_quotes_gpc())
        {
        $value = stripslashes($value);
        }
        // Quote if not a number or a numeric string
        if (!is_numeric($value))
        {
        $value = mysql_real_escape_string($value);
        }
        return $value;
        }

        Sorry for my English. I'm a freshman .

        1 Reply Last reply
        0
        • B Bradml

          To prevent SQL injection all you need to do is escape and use good validation. Other stuff can be a waste of time.


          Brad Australian The PHP MVP - Christian Graus on "Best books for VBscript" A big thick one, so you can whack yourself on the head with it.

          C Offline
          C Offline
          Chris Maunder
          wrote on last edited by
          #4

          Bradml wrote:

          Other stuff can be a waste of time

          Like using parameters instead of inlining?

          cheers, Chris Maunder

          CodeProject.com : C++ MVP

          B 1 Reply Last reply
          0
          • C Chris Maunder

            Bradml wrote:

            Other stuff can be a waste of time

            Like using parameters instead of inlining?

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            B Offline
            B Offline
            Bradml
            wrote on last edited by
            #5

            Exactly.


            Brad Australian The PHP MVP - Christian Graus on "Best books for VBscript" A big thick one, so you can whack yourself on the head with it.

            1 Reply Last reply
            0
            • B Bradml

              To prevent SQL injection all you need to do is escape and use good validation. Other stuff can be a waste of time.


              Brad Australian The PHP MVP - Christian Graus on "Best books for VBscript" A big thick one, so you can whack yourself on the head with it.

              H Offline
              H Offline
              Hesham Amin
              wrote on last edited by
              #6

              What level of validation is enough? I think that using prepared statements with parameters is the safest way, what do you think ?

              Hesham A. Amin My blog

              1 Reply Last reply
              0
              • N nalorin

                Some background: I have taken a few measures to prevent SQL injection on my PHP/MySQL setup (currently WAMP for development, but will be LAMP for production server): - In my users table, I have entered a "bad" user, with all the fields equal to 0.     - this is the first user in the table     - if a hacker tries to enter ' or ''=' in the uname field:         - (theoretically,) "bad" user will be first result, and         - (theoretically,) # of results will also be greater than 1 (more than 1 user)     - (theoretically,) the following code will prevent said hacker from gaining unauthorized access - mysql_real_escape_string() function will be used to escape input when site is launched, but right now it is not in use to allow testing of common SQL injection methods.     - I have read that mysql_real_escape_string() has some vulnerabilities.     - I know mysql_real_escape_string() is more secure than addslashes().

                /* // This block will be uncommented after development
                $u = mysql_real_escape_string($_POST[uname]);
                $p = mysql_real_escape_string($_POST[pass]); */

                // These lines will be replaced by commented block above after development
                $u = $_POST[uname];
                $p = $_POST[pass];

                // I could add " order by uid asc" to the following query, to ensure 'bad' user listed first if SQL injection occurs, and to prevent hacker from using order by
                $query = "select uid,uname,fname,lname,email,phone,other,pass from ads.users where uname = '$u'";
                $result = mysql_query($query);
                $rows = mysql_num_rows($result);

                // if more than 1 row, SQL injection attempted (uname has 'unique' flag in database, so only safe to return 1 row)
                if ($rows > 1) {
                /* <<Do Stuff - Security Measures (ban IP, etc)>> */
                die ("Error[20]: You have entered potentially harmful input. Security measures have

                A Offline
                A Offline
                alex barylski
                wrote on last edited by
                #7

                SQLi is best handled using the database's native escaping routines and not just relying on addslashes() -- there is actually a way to circumvent addslashes from what I remember. Filtering is probably a good practice as well. To avoid escaping, you could just use PDO and prepared statements which handles the escaping for you automagically as well.

                I'm finding the only constant in software development is change it self.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups