Can I get SQL injection attack from SELECT statement?

允我心安 提交于 2020-01-01 04:56:48

问题


2 Questions actually:

I know i must use Stored Procedures as much as Possible, but i would like to know the following please.

A: Can i get a SQL Injection attack from a SELECT statement such as (Select * from MyTable) ?

B: Also, can i get a SQL Injection attack when I use the SQLDataSource in ASP.NET?


回答1:


To answer your questions.

A: Yes, you can get an SQL Injection attack from any query that takes parameters (even calling stored procedures if you are not using the provided methods by your platform and doing it via SQL calls).

I was asked to provide an example of how an injection can be made even by using stored procedure. I've seen applications developed that do use stored procedures, but in this way:

// C# - DON'T DO THIS!
String regionName = assignedSomewhereElse();
SQLCommand sqlCmd = DatabaseConnection.CreateCommand();
SQLCommand sqlCmd.CommandText =
    String.Format("EXECUTE sp_InsertNewRegion '{0}'", regionName);

sqlCmd.ExecuteNonQuery();

Obviously, this is not the way to call a stored procedure. You should use your platform's abstractions or parametrized queries.


B: SQLDataSource is an abstraction layer for your database. It will create the SQL queries for you and automatically sanitize them in order to prevent injection.

In order to avoid injection, either:

  • Sanitize your inputs
  • Use the abstraction layer provided by your platform.
  • Use parametrized queries.



回答2:


You can get an SQL injection attack anytime that you are not using parameterized queries, for the most part.

If your example,

 SELECT * from MyTable

there isn't any user-inputted data, so that should be fine. However, something like:

 SELECT * from MyTable WHERE name='x'

(x being a parameter) then there's a chance that someone injects some SQL into their name.

B: ASP.NET uses parameterized queries because it builds the query based on the parameters that you provide programmatically.




回答3:


I suggest to enjoy this xkcd comic, a good lesson about sql injection




回答4:


Injection hacks occur when you give the user the ability to manipulate the query, and with the parametrized queries most (if not all) threats are neutralized as special characters are escaped to make only the query you intended executable

Example:
Search Box: [ ] [ GO ]

select * from myTable where keywords like '%$searchTerm%'

Then the hacker inserts a '; to terminate the query and can write any other query they want.




回答5:


In response to your comment - 'what must i do?'

For starters you can validate the text boxes or whatever control will be used to allow input. If you're looking for a number make sure that they only put in numbers, if they are inputing a word make sure no punctuation is available. Take out characters like -- and ' unless it is absolutely needed. You can do all of this with ajax and/or javascript. Also,Here is an interesting Article on protection from injection. Also parameterized queries are a great option




回答6:


if you are not using parameterized queries but are concatenating you can get sql injection for any kind of SELECT, UPDATE, DELETE or INSERT statement




回答7:


SQL injection requires the SQL string to be combined with some user-controlled parameters, so if the Select statement is constant, it is immune to injections. On the other hand, if you're adding a "WHERE user_id =" + userIdString, then injection is possible.

Avoiding injections doesn't require stored procedures, and you shouldn't count on sanitizing your inputs. Instead, just bind to the parameters instead of manipulating strings.

Take a look at: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.aspx




回答8:


Any time you allow a user to give input data into a dynamic sql query you are at risk of injection. And a sqldatasource does not protect you from injection. Inserts,deletes, drops,etc will still occur.



来源:https://stackoverflow.com/questions/1099391/can-i-get-sql-injection-attack-from-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!