| | #1 |
| Warrior Member Registered Member Join Date: 2013 Location: UK
Posts: 25
Thanks: 3
Thanked 4 Times in 4 Posts
|
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. |
| | |
| |
| | #2 | |
| I'm Batman War Room Member Join Date: 2013 Location: NJ - NH - TN
Posts: 1,267
Thanks: 278
Thanked 1,190 Times in 615 Posts
|
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. | |
| | ||
| |
| | #3 |
| Web Developer War Room Member Join Date: 2012 Location: Vancouver, BC
Posts: 593
Thanks: 149
Thanked 162 Times in 133 Posts
|
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. |
| |
| |
| The Following User Says Thank You to Andrew H For This Useful Post: |
| | #4 |
| Warrior Member Join Date: 2013
Posts: 16
Thanks: 0
Thanked 3 Times in 2 Posts
|
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. |
| |
| | #5 |
| digital carpenter War Room Member Join Date: 2013 Location: Pennsylvania
Posts: 1,306
Thanks: 480
Thanked 747 Times in 444 Posts
|
mysql_real_escape_string() is a good start |
| | |
| |
| The Following User Says Thank You to brutecky For This Useful Post: |
| | #6 |
| Warrior Member Join Date: 2014
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
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 \'. |
| | |
| |
| | #7 |
| Active Warrior Join Date: 2010
Posts: 31
Thanks: 0
Thanked 13 Times in 7 Posts
|
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 |
|
Scott Eichman http://landscapeplanning.org | |
| |
| | #8 |
| HyperActive Warrior Join Date: 2014
Posts: 136
Thanks: 6
Thanked 23 Times in 20 Posts
|
Read this discussion: php - SQL injection that gets around mysql_real_escape_string() - Stack Overflow |
| |
| The Following User Says Thank You to NobleSavage For This Useful Post: |
| | #9 |
| Warrior Member Join Date: 2014
Posts: 26
Thanks: 0
Thanked 2 Times in 2 Posts
|
which programming language do you use?
|
| |
| | #10 |
| Active Warrior Join Date: 2014 Location: Toronto, Canada
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
|
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 |
| | |
| |
| | #11 |
| HyperActive Warrior Join Date: 2014
Posts: 136
Thanks: 6
Thanked 23 Times in 20 Posts
| |
| |
| | #14 |
| HyperActive Warrior Join Date: 2008
Posts: 310
Thanks: 10
Thanked 39 Times in 35 Posts
|
//unsafe $users = $_POST["users_input"]; //Safe way to do it. $users = mysql_real_escape_string($_POST["users_input"]); |
| |
|
| Bookmarks |
| Tags |
| injection, sql |
| |