These methods know how to clean the content, they remove any potentially dangerous text. As there is expected something very limited, ie just numbers or a text like string (in quotes), anything that escapes from this is considered garbage and is discarded. So elements that are part of the SQL syntax cannot be injected into the query by changing its content.
The injection problem occurs by constructing the query text freely, accepting anything as something valid. Protection occurs precisely because it allows only some very specific parts to be used flexibly and come from an external source.
The problem can occur in any normal programming language and the error is always of the programmer, because he doesn’t know how to avoid the problem manually or because he doesn’t use the ready-made tools that the library or language offers to insert the parameterized data and only them, without letting other data especially enter some that can be confused with a valid SQL that can be executed.
Here comes the famous XKCD
It is very common for the programmer to concatenate strings to create the query that will be sent to the database naively, then it does something like this:
query = "SELECT * FROM Students WHERE (nome = '" + GetByName + "' AND status = 'ativo')";
Where this variable nome
is a text that came from an external source, for example came from a POST
or GET
HTTP on a website a web application. None of this has been sanitized and concatenation occurs like this. Something like this is valid:
http://site.com/Student?GetByName=Joao
So too:
http://site.com/Student?GetByName=Robert');%20DROP%20TABLE%20STUDENTS;%20--
There the malicious subject or with a name a little out of the ordinary can cause damage to your database. Assuming the example of this comic string would be Robert'); DROP TABLE STUDENTS; --
, concatenated like this:
SELECT * FROM Students WHERE (nome = 'Robert'); DROP TABLE STUDENTS; -- ' AND status = 'ativo')
This is a perfectly valid command that will:
- select all table columns
Students
applying a filter where the column nome
have the text Robert
- soon after closes this
SELECT
with the ;
and starts another command where the table Students
is erased completely
- Finally there is a comment that can have any text that will not give error, it is all ignored by the database.
If you want an easier read:
SELECT * FROM Students WHERE (nome = 'Robert');
DROP TABLE STUDENTS;
-- ' AND status = 'ativo')
Note that I used an agnostic syntax of language in string up there, almost everyone would have the command like this (there is no SQL injection without a code in a programming language at some level).
Obviously any SQL code can be injected and not only to erase a table that is a little useful. There are millions of systems out there (mostly web) that are being misused causing problems and small losses without the person noticing for years. In general who does this takes care not to arouse too much attention to do for too long and get greater benefits. In sum the losses are enormous. There are cases where you don’t even need to change anything, just having access to information without the right can cause a lot of damage. Example:
"SELECT * FROM Correntistas WHERE numConta = '" + numConta + "';"
That’s how it’s done:
http://site.com/Correntistas?numConta=1%20OR%20TRUE
I put in the Github for future reference.
The example is naive but shows that you can do anything, just understand how is the query on the other side and almost all vary very little, all hackerLamer knows how to do it with your foot in the back. It’s one of the easiest things to violate that exists and the most neglected.
Because it can take a lot of work and you don’t see all the details that are necessary to ensure that you don’t have the injection, it’s best to use a ready-made function of the language library or the database access component. Each will have their own way of doing this and the example used in the question is the correct way of doing it in C#.
Another source of poor Bobby Tables' joke is his website showing the problem and solution in various languages.
That’s why I’ve seen people with names Null
have problems, nor was it Injection purposeful.
It is more common than it seems. It occurs more among amateurs but turns and moves some great software made by experts falls into this. Note that today the solution is simple, one only needs to remember to use. The ones that may seem more elaborate, the manuals tend to fail, use the basics, the ready, as long as you can trust the tool you use (some well-known are not reliable). So understand everything on the subject to be safer.
Here’s more about:
http://answall.com/questions/9734/como-evitar-sql-injection-em-minha-aplica%C3%A7%C3%A3o-php This post can help you.
– Guilherme Lima