<p>If I remove all the ' characters from a SQL query, is there some other way to do a SQL injection attack on the database?</p>
<p>How can it be done? Can anyone give me examples?</p>

[{'answer_id': 71769, 'author': 'Raithlin', 'author_id': 6528, 'author_profile': 'https://Stackoverflow.com/users/6528', 'pm_score': 4, 'selected': False, 'text': '<p>Yes, depending on the statement you are using. You are better off protecting yourself either by using Stored Procedures, or at least parameterised queries.</p>\n<p>See <a href="http://en.wikipedia.org/wiki/SQL_injection#Preventing_SQL_Injection" rel="nofollow noreferrer">Wikipedia</a> for prevention samples.</p>\n'}, {'answer_id': 71780, 'author': 'Bob Dizzle', 'author_id': 9581, 'author_profile': 'https://Stackoverflow.com/users/9581', 'pm_score': 2, 'selected': False, 'text': '<p>. . . uh about 50000000 other ways</p>\n\n<p>maybe somthing like <code>5;  drop table employees;  --</code></p>\n\n<p>resulting sql may be something like:\n<code>select * from somewhere where number = 5; drop table employees;  -- and sadfsf</code></p>\n\n<p>(<code>--</code> starts a comment)</p>\n'}, {'answer_id': 71782, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 0, 'selected': False, 'text': '<p>It depends on how you put together the query, but in essence yes.</p>\n\n<p>For example, in Java if you were to do this (deliberately egregious example):</p>\n\n<pre><code> String query = "SELECT name_ from Customer WHERE ID = " + request.getParameter("id");\n</code></pre>\n\n<p>then there\'s a good chance you are opening yourself up to an injection attack.</p>\n\n<p>Java has some useful tools to protect against these, such as PreparedStatements (where you pass in a string like "SELECT name_ from Customer WHERE ID = ?" and the JDBC layer handles escapes while replacing the ? tokens for you), but some other languages are not so helpful for this.</p>\n'}, {'answer_id': 71783, 'author': 'mdb', 'author_id': 8562, 'author_profile': 'https://Stackoverflow.com/users/8562', 'pm_score': 1, 'selected': False, 'text': '<p>Yes, absolutely: depending on your SQL dialect and such, there are many ways to achieve injection that do not use the apostrophe.</p>\n\n<p>The only reliable defense against SQL injection attacks is using the parameterized SQL statement support offered by your database interface.</p>\n'}, {'answer_id': 71784, 'author': 'Johan', 'author_id': 11347, 'author_profile': 'https://Stackoverflow.com/users/11347', 'pm_score': 6, 'selected': True, 'text': '<p>Yes, there is.  An excerpt from <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="noreferrer">Wikipedia</a></p>\n\n<p><code>"SELECT * FROM data WHERE id = " + a_variable + ";"</code></p>\n\n<p>It is clear from this statement that the author intended a_variable to be a number correlating to the "id" field. However, if it is in fact a string then the end user may manipulate the statement as they choose, thereby bypassing the need for escape characters. For example, setting a_variable to</p>\n\n<p><code>1;DROP TABLE users</code></p>\n\n<p>will drop (delete) the "users" table from the database, since the SQL would be rendered as follows:</p>\n\n<p><code>SELECT * FROM DATA WHERE id=1;DROP TABLE users;</code></p>\n\n<p>SQL injection is <em>not</em> a simple attack to fight.  I would do very careful research if I were you.</p>\n'}, {'answer_id': 71830, 'author': 'hollystyles', 'author_id': 2083160, 'author_profile': 'https://Stackoverflow.com/users/2083160', 'pm_score': 0, 'selected': False, 'text': "<p>Thing is apostrophe's maybe genuine input and you have to escape them by doubling them up when you are using inline SQL in your code. What you are looking for is a regex pattern like:</p>\n\n<pre><code>\\;.*--\\\n</code></pre>\n\n<p>A semi colon used to prematurely end the genuine statement, some injected SQL followed by a double hyphen to comment out the trailing SQL from the original genuine statement. The hyphens may be omitted in the attack.</p>\n\n<p>Therefore the answer is: No, simply removing apostrophes does not gaurantee you safety from SQL Injection.</p>\n"}, {'answer_id': 71904, 'author': 'CJM', 'author_id': 6898, 'author_profile': 'https://Stackoverflow.com/users/6898', 'pm_score': 1, 'selected': False, 'text': "<p>Rather that trying to figure out which characters to filter out, I'd stick to parametrized queries instead, and remove the problem entirely.</p>\n"}, {'answer_id': 71943, 'author': 'Veynom', 'author_id': 11670, 'author_profile': 'https://Stackoverflow.com/users/11670', 'pm_score': 3, 'selected': False, 'text': '<p>Yes, it is definitely possible.</p>\n<p>If you have a form where you expect an integer to make your next SELECT statement, then you can enter anything similar:</p>\n<p>SELECT * FROM <code>thingy</code> WHERE attributeID=</p>\n<ul>\n<li>5 (good answer, no problem)</li>\n<li>5; DROP table <code>users</code>; (bad, bad, bad...)</li>\n</ul>\n<p>The following website details further classical SQL injection technics: <a href="http://www.0x000000.com/index.php?i=14" rel="nofollow noreferrer">SQL Injection cheat sheet</a>.</p>\n<p>Using parametrized queries or stored procedures is not any better. These are just pre-made queries using the passed parameters, which can be source of injection just as well. It is also described on this page: <a href="http://www.0x000000.com/?i=148&amp;bin=10010100" rel="nofollow noreferrer">Attacking Stored Procedures in SQL</a>.</p>\n<p>Now, if you supress the simple quote, you prevent only a given set of attack. But not all of them.</p>\n<p>As always, do not trust data coming from the outside. Filter them at these 3 levels:</p>\n<ul>\n<li>Interface level for obvious stuff (a drop down select list is better than a free text field)</li>\n<li>Logical level for checks related to data nature (int, string, length), permissions (can this type of data be used by this user at this page)...</li>\n<li>Database access level (escape simple quote...).</li>\n</ul>\n<p>Have fun and don\'t forget to check <a href="http://en.wikipedia.org/wiki/SQL_injection#Preventing_SQL_Injection" rel="nofollow noreferrer">Wikipedia</a> for answers.</p>\n'}, {'answer_id': 71992, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>Also- even if you do just look for the apostrophe, you don't want to remove it.  You want to <em>escape</em> it.  You do that by replacing every apostrophe with two apostrophes.  </p>\n\n<p>But parameterized queries/stored procedures are so much better.</p>\n"}, {'answer_id': 72398, 'author': 'Johnny Bravado', 'author_id': 12222, 'author_profile': 'https://Stackoverflow.com/users/12222', 'pm_score': 2, 'selected': False, 'text': '<p>Parameterized inline SQL or parameterized stored procedures is the best way to protect yourself. As others have pointed out, simply stripping/escaping the single quote character is not enough.</p>\n\n<p>You will notice that I specifically talk about "parameterized" stored procedures. Simply using a stored procedure is not enough either if you revert to concatenating the procedure\'s passed parameters together. In other words, wrapping the exact same vulnerable SQL statement in a stored procedure does not make it any safer. You need to use parameters in your stored procedure just like you would with inline SQL.</p>\n'}, {'answer_id': 72452, 'author': 'Ken Ray', 'author_id': 12253, 'author_profile': 'https://Stackoverflow.com/users/12253', 'pm_score': 0, 'selected': False, 'text': "<p>I can only repeat what others have said.  Parametrized SQL is the way to go.  Sure, it is a bit of a pain in the butt coding it - but once you have done it once, then it isn't difficult to cut and paste that code, and making the modifications you need.  We have a lot of .Net applications that allow web site visitors specify a whole range of search criteria, and the code builds the SQL Select statement on the fly - but everything that could have been entered by a user goes into a parameter. </p>\n"}, {'answer_id': 72476, 'author': 'GvS', 'author_id': 11492, 'author_profile': 'https://Stackoverflow.com/users/11492', 'pm_score': 3, 'selected': False, 'text': '<p>I suggest you pass the variables as parameters, and not build your own SQL. Otherwise there will allways be a way to do a SQL injection, in manners that we currently are unaware off.</p>\n\n<p>The code you create is then something like:</p>\n\n<pre><code>\' Not Tested\nvar sql = "SELECT * FROM data WHERE id = @id";\nvar cmd = new SqlCommand(sql, myConnection);\ncmd.Parameters.AddWithValue("@id", request.getParameter("id"));\n</code></pre>\n\n<p>If you have a name like mine with an \' in it. It is very annoying that all \'-characters are removed or marked as invalid.</p>\n\n<p>You also might want to look at this <a href="https://stackoverflow.com/questions/1284/catching-sql-injection-and-other-malicious-web-requests">Stackoverflow question about SQL Injections</a>.</p>\n'}, {'answer_id': 74918, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>When you are expecting a numeric parameter, you should always be validating the input to make sure it\'s numeric. Beyond helping to protect against injection, the validation step will make the app more user friendly. </p>\n\n<p>If you ever receive id = "hello" when you expected id = 1044, it\'s always better to return a useful error to the user instead of letting the database return an error.</p>\n'}, {'answer_id': 188863, 'author': 'AviD', 'author_id': 10080, 'author_profile': 'https://Stackoverflow.com/users/10080', 'pm_score': 2, 'selected': False, 'text': '<p>Since this a relatively older question, I wont bother writing up a complete and comprehensive answer, since most aspects of that answer have been mentioned here by one poster or another.<br>\nI do find it necessary, however, to bring up another issue that was not touched on by anyone here - SQL Smuggling. In certain situations, it is possible to "smuggle" the quote character \' into your query <em>even if you tried to remove it</em>. In fact, this may be possible even if you used proper commands, parameters, Stored Procedures, etc. </p>\n\n<p>Check out the full research paper at <a href="http://www.comsecglobal.com/FrameWork/Upload/SQL_Smuggling.pdf" rel="nofollow noreferrer">http://www.comsecglobal.com/FrameWork/Upload/SQL_Smuggling.pdf</a> (disclosure, I was the primary researcher on this) or just google "SQL Smuggling".</p>\n'}]
['https://Stackoverflow.com/questions/71756', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']