Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

Can any one tell me if I stop quotes i.e ' in my application or website. then also my application can be sql injected or not

As I see you have to write quote and then semi colon to start your own line like

'; DELETE FROM Table;--


Plz give some relevant example in favor of your answer

Thanks In Advance
Posted

No, they still don't prevent Sql Injection, you should use SqlParameters to pass your values to the SQL Server. See MSDN documentation[^] for more on this.

The worst scenario in SQL Injection has been provided by you... To delete the table and then comment out the rest of the statement. Even if you allow quotes or not, it will exploit your database, you (however) are going to pass SQL command in a string format, and what do you think prevents user from adding a quote himself?

If I have to design a pattern, I would also not rely on SQL parameters. I would first try to validate the user's input, before even creating or constructing the SQL command. If the input is ok, then I would create the SQL command, also again... Passing the values using SQL parameters.
 
Share this answer
 
v4
Comments
binadi007 23-Mar-15 12:34pm    
Any example after stopping quote can able to sql inject?
Afzaal Ahmad Zeeshan 23-Mar-15 12:39pm    
I have added more details... Even if you try to remove quotes, user would still be able to add a few of his own. User can input anything, right? If you want to remove, remove quotes from the user input too; using input.Replace("'", "");. Then you can say, quotes have been removed. :)
binadi007 23-Mar-15 12:44pm    
What if I replace single quote to double quote i.e input.Replace("'","''")?
Afzaal Ahmad Zeeshan 23-Mar-15 12:47pm    
Wrong... When you will pass double string, the actual command string would break; it would be as the end of string has reached, other characters would be expected as variables or tokens and then a problem would generate again. Please see my answer again and try to understand the third-paragraph. You should validate the user input before creating the query. If the input is valid, then create and execute query, otherwise user is potential user; hacker or cracker, so you can tell him to fix his input.
binadi007 23-Mar-15 12:53pm    
Can you provide any substantial link to understand your third paragraph better
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Mar-15 14:39pm    
5ed; I added some more link to this, in particular, the Microsoft link addressing specifically SQL injection with ADO.NET.
Please see Solution 5.
—SA
Peter Leow 23-Mar-15 22:26pm    
Thank you, Sergey.
Just Google "SQL Injection without quotes" for examples of why this won't protect you from SQLi. For example:
No single quotes is allowed, Is this SQL Injection point still exploitable?[^]
SQL Smuggling, or The Attack That Wasn't There[^]

The "Unicode Smuggling" section from the second link is of particular interest - certain Unicode characters which your code sees as distinct from the single-quote may automatically be translated by the DBMS to a single quote, rendering your filter useless.

Rather than wasting your time trying to argue that a naive filter will be good enough, just use properly parameterized queries. ADO.NET makes it extremely easy to do!
 
Share this answer
 
Comments
Peter Leow 23-Mar-15 22:28pm    
+5.
In addition to Solutions 1 and 3:

This is the example of how SQL injection works: http://xkcd.com/327[^]. :-)
This is what you need to use: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

See also my past answers on the topic:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 23-Mar-15 16:23pm    
+5.
Sergey Alexandrovich Kryukov 23-Mar-15 16:46pm    
Thank you, Afzaal.
—SA
Peter Leow 23-Mar-15 22:26pm    
+5.
Sergey Alexandrovich Kryukov 23-Mar-15 22:32pm    
Thank you, Peter.
—SA
Santosh K. Tripathi 24-Mar-15 0:25am    
+5 from me :)
If you double-up any apostrophes in user input then that will protect against this attack

string sql = "select * from table where name = '" + userInput.Replace("'", "''") + "'";


However it is far better to use parameterised queries if using ado.net, or an ORM that automatically protects you against these things like Entity Framework.
 
Share this answer
 
Comments
binadi007 23-Mar-15 12:42pm    
Yeah I'm thinking the same that replace single quote to double quote can also protect the sql injection, and it is simple way too
Richard Deeming 23-Mar-15 14:36pm    
No, that won't protect against SQLi, except for the simplest attacks. A quick search for "SQL Injection without quotes" provides enough evidence that this type of filter doesn't work.
F-ES Sitecore 23-Mar-15 15:20pm    
I had a google and couldn't find anything relevant, maybe you could post an example of an attack that will still work?
Richard Deeming 23-Mar-15 15:21pm    
Have a look at the links in my answer (Solution #4), particularly the "Unicode Smuggling" section of the "SQL Smuggling" paper.
F-ES Sitecore 23-Mar-15 15:36pm    
Thanks, I looked at the PDF, albeit briefly, and those techniques seem to cover where validation is used as the SQL injection protection. The document actually states that OWASP recommends you defend against injection by escaping quotes (ie replace ' with '' in our instance). Again, if you could give me an actual code example or string that I could test I would be interested to see this working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900