+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Basic Protection from SQL Injection

  1. #1
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Basic Protection from SQL Injection

    SQL Injection is injecting SQL Through a Get or Post from a script into the SQL. for Example

    member.php?id=6

    The code for SQL may be

    Code:
    $id = $_GET['id']
    $row= mysql_query('select * from `members` where id=$id');
    This would allow the Attacker to Execute a Union Select statement.This would look like

    member.php?id=' UNION SELECT concat(username,char(58),password) FROM members

    A possible output would be

    Affix:d8b9bb5e644429268d274cf03c6d6e06

    All you would need to do is crack the hash

    So how exactly do you stop this attack?

    Its simple. There are many methods of protecting from SQL injection. I use 2. These are the ones Im going to teach you.

    If its a simple numerical ID such as the example above Just add a Value Check. In the above code it would look like below.

    Code:
    $id = $_GET['id'];
    if(!isnumeric($id)) { die("GTFO MY SERVER NOOB"); }
    [/size][/font]$row= mysql_query('select * from `members` where id=$id');
    Now if I tried to execute my Union Statement I would get an error

    GTFO MY SERVER NOOB

    Now what if you are using a string such as a search. a Union would be used the same way.

    This way I would use the 'mysql_real_escape_string'

    This would look like

    Code:
    $id = mysql_real_escape_string($_GET['id']);
    This string it now Properly escaped and will not allow Succesful Execution of SQL Injection.

    --------

    Any Questions E-Mail : Affix[@]FedoraProject.org

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Basic Protection from SQL Injection

    I like the error message
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Basic Protection from SQL Injection

    John has already written a SQL Injection tutorial: PHP: SQL Injections

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #4
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Basic Protection from SQL Injection

    This is just a basic one as you can see. It only shows the very basics, John's is an extensive version.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  6. #5
    Jordan Guest

    Re: Basic Protection from SQL Injection

    Not bad, +rep.

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Basic Protection from SQL Injection

    This is just a basic one as you can see. It only shows the very basics, John's is an extensive version.
    I can see that, but John's tutorial describes SQL injections in a more comprehensive way, I feel.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Basic Protection from SQL Injection

    It never hurts to see another example.

    Is the function isnumeric() your own or a typo for is_numeric()? As a side note, checking for type isn't necessarily a way to protect against SQL injections rather its a means to validate your data, and for table id's I find it better to rely on ctype_digit.

    Good info none-the-less.

  9. #8
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Re: Basic Protection from SQL Injection

    yeah sorry about the typo It was the first tutorial I ever wrote. I wrote this about 3-4 Years ago

  10. #9
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Basic Protection from SQL Injection

    That is a long time ago, why not improve it with your new knowledge?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  11. #10
    Affix is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2
    Rep Power
    12

    Re: Basic Protection from SQL Injection

    I will When I finnish Writing my paper on SQL Injection

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. SQL injection ?!
    By JackomoLight in forum Software Security
    Replies: 1
    Last Post: 02-23-2011, 04:54 PM
  2. GDB assembly injection?
    By Acedia in forum Software Security
    Replies: 1
    Last Post: 10-20-2010, 07:46 AM
  3. Javascript Injection
    By Ricardo-san in forum Security Tutorials
    Replies: 9
    Last Post: 01-07-2010, 10:02 AM
  4. PHP injection
    By zeroradius in forum PHP Development
    Replies: 5
    Last Post: 09-23-2009, 09:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts