Closed Thread
Thread Tools Search this Thread
Unread 15th Nov 2013, 08:36 AM   #1
Warrior Member
Registered Member
 
Join Date: 2013
Location: UK
Posts: 25
Thanks: 3
Thanked 4 Times in 4 Posts
Default
SQL Injection?
Share on: 
fb share twitter share gplus share more share

How can I block SQL Injection into my MySQL queries?

I am new to this, and was showing a web developer pal, who just blew my mind when he used an injection.

Any help welcomed.


forgottenlife is offline  
Unread 15th Nov 2013, 08:44 AM   #2
I'm Batman
War Room Member
 
ronrule's Avatar
 
Join Date: 2013
Location: NJ - NH - TN
Posts: 1,267
Thanks: 278
Thanked 1,190 Times in 615 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

Originally Posted by forgottenlife View Post

How can I block SQL Injection into my MySQL queries?

I am new to this, and was showing a web developer pal, who just blew my mind when he used an injection.

Any help welcomed.

Stripping out apostrophes from strings before allowing them to be used in a SQL statement would be a good start.

Personally, in my programming days I used stored procedures for everything - put all of the T-SQL code into stored procedures and then deny the web user access to INSERT/UPDATE/DELETE database tables, but GRANT access to execute the stored procedures. That way even if someone was able to hack the site and get the database username/password all they could do is execute the stored procedures.

-
Ron Rule
http://ronrule.com
ronrule is offline  
Unread 15th Nov 2013, 12:17 PM   #3
Web Developer
War Room Member
 
Andrew H's Avatar
 
Join Date: 2012
Location: Vancouver, BC
Posts: 593
Thanks: 149
Thanked 162 Times in 133 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

Yeah for the sake of developers that come after you please don't use stored procedures.

SQL injection is basically allowing a person to execute arbitrary code by not sanitizing their input. So, all that needs to be done is to sanitize the input (simple eh?)

So some of the old dogs, or amateurs, will quickly jump in and go 'just use mysql_real_escape_string' - don't listen to them.

So I present you the solution: PDO and prepared statements. Check out this tutorial, if you are serious about programming it will likely be the most important 30 minutes you use this year: Why you Should be using PHP’s PDO for Database Access | Nettuts+

And don't feel bad, when I first found out about SQL injection my mind was blown and i was in your exact same situation.

"You shouldn't come here and set yourself up as the resident wizard of oz."
Andrew H is offline  
The Following User Says Thank You to Andrew H For This Useful Post:
Unread 18th Nov 2013, 03:05 PM   #4
Warrior Member
 
Join Date: 2013
Posts: 16
Thanks: 0
Thanked 3 Times in 2 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

This is from bobby-tables.com

"Use parameterized SQL calls.
That's it.

Don't try to escape invalid characters.

Don't try to do it yourself. Learn how to use parameterized statements.
Always, every single time.
The strip gets one thing crucially wrong.

The answer is not to "sanitize your database inputs" yourself. It is prone to error."


I always use this website because I change languages so often. It is a very good reference.
BrandByApi is offline  
Unread 18th Nov 2013, 08:03 PM   #5
digital carpenter
War Room Member
 
brutecky's Avatar
 
Join Date: 2013
Location: Pennsylvania
Posts: 1,306
Thanks: 480
Thanked 747 Times in 444 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

mysql_real_escape_string()

is a good start

brutecky is offline  
The Following User Says Thank You to brutecky For This Useful Post:
Unread 24th Aug 2014, 11:29 PM   #6
Warrior Member
 
Join Date: 2014
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

SQL injection refers to the act of someone inserting a MySQL statement to be run on your database without your knowledge. Injection usually occurs when you ask a user for input, like their name, and instead of a name they give you a MySQL statement that you will unknowingly run on your database.
This problem has been known for a while and PHP has a specially-made function to prevent these attacks. All you need to do is use the mouthful of a function mysql_real_escape_string.
What mysql_real_escape_string does is take a string that is going to be used in a MySQL query and return the same string with all SQL Injection attempts safely escaped. Basically, it will replace those troublesome quotes(') a user might enter with a MySQL-safe substitute, an escaped quote \'.

wilsonmarcial is offline  
Unread 21st Sep 2014, 11:39 PM   #7
Active Warrior
 
sjeichman's Avatar
 
Join Date: 2010
Posts: 31
Thanks: 0
Thanked 13 Times in 7 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

I'm not sure why Andrew H has issues with stored procedures. Perhaps it's because MySQL is a clunky open source product and doesn't handle them correctly. You get what you pay for!

Ronrule has the right idea. Stored procedures and functions in a real database allow you to limit the input by limiting the length of input parameters, In the stored procedure, you can validate that the input is what you expect for the particular query and build the Select statement. As Ronrule also said, you grant access to execute the procedure but allow no direct access to the tables themselves.

Some folks mentioned a function that escapes certain characters. How does the query work if you are querying on name and the name is something like O'Donnell?

Another advantages to using stored procedures is that the table and column names in your database do not appear in your web code. Some "security by obscurity"! But every little bit helps, If people don't know the table names, SQL injection of a "Delete From" command becomes very difficult.

I have been DBA-ing, programming in SQL and interfacing a large identity management database with websites for the past 12 years, and with over 2.5 million queries a year into the database, we have not had any SQL injection!

My database administration, development, anddata security consulting services are available.

Scott

sjeichman is offline  
Unread 22nd Sep 2014, 12:53 AM   #8
HyperActive Warrior
 
Join Date: 2014
Posts: 136
Thanks: 6
Thanked 23 Times in 20 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

Read this discussion:

php - SQL injection that gets around mysql_real_escape_string() - Stack Overflow
NobleSavage is offline  
The Following User Says Thank You to NobleSavage For This Useful Post:
Unread 23rd Sep 2014, 05:16 PM   #9
Warrior Member
 
Join Date: 2014
Posts: 26
Thanks: 0
Thanked 2 Times in 2 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

which programming language do you use?
MRgauss is offline  
Unread 24th Sep 2014, 11:33 AM   #10
Active Warrior
 
Join Date: 2014
Location: Toronto, Canada
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

Are you using any platform such as WordPress or any framework for coding? Or is is raw php code.

Essentially all it is: You have to ensure any values that are passed to mysql from user input are sanitized. It varies slightly from query to query but the idea is the same.

I am assuming your application is rather small so if you would like to provide some references I am sure we could help you out.

Mr Alexander

Mr Alexander

Web Developer & Consultant with extensive experience, since 1998

Websites.Consulting
mralexanderca is offline  
Unread 24th Sep 2014, 04:42 PM   #11
HyperActive Warrior
 
Join Date: 2014
Posts: 136
Thanks: 6
Thanked 23 Times in 20 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

Originally Posted by MRgauss View Post

which programming language do you use?
Python whenever possible. I tend to get drug into PHP.
NobleSavage is offline  
Unread 26th Sep 2014, 01:47 PM   #12
I Code Stuff
War Room Member
 
dad2four's Avatar
 
Join Date: 2011
Location: Kirkwood, MO USA
Posts: 302
Thanks: 210
Thanked 80 Times in 52 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

PDO or MySQLi

Good luck if you are using codeigniter since MySQLi is not implemented.....

dad2four is offline  
Unread 26th Sep 2014, 01:48 PM   #13
I Code Stuff
War Room Member
 
dad2four's Avatar
 
Join Date: 2011
Location: Kirkwood, MO USA
Posts: 302
Thanks: 210
Thanked 80 Times in 52 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

Wow this post was from November of last year? Who dug this thing up anyway? hehe

dad2four is offline  
Unread 29th Sep 2014, 08:45 PM   #14
HyperActive Warrior
 
Member8200's Avatar
 
Join Date: 2008
Posts: 310
Thanks: 10
Thanked 39 Times in 35 Posts
Default
Re: SQL Injection?
Share on: 
fb share twitter share gplus share more share

//unsafe
$users = $_POST["users_input"];



//Safe way to do it.
$users = mysql_real_escape_string($_POST["users_input"]);
Member8200 is offline  
Closed Thread


Bookmarks

Tags
injection, sql

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 11:36 AM.