I was curious if it's possible to protect against an SQL injection attack by removing all spaces from an String input?
I have been reading up on SQL Injection at OWASP, but they don't mention anything about removing spaces, so I was curious why it would, or would not work?
I was looking at this question, which asks about trim
, and the top answer says this:
No, adding a trim will not prevent sql injection.
trim
only removes space on the outside of your string.Select * from aTable where name like '%'+@SearchString+'%'
If you @SearchString held something like
'' update aTable set someColumn='JackedUpValue' where someColumn like '
Then when you put it all together and execute it dynamically you would get
Select * from aTable where name like '%' update aTable set someColumn='JackedUpValue' where someColumn like '%'
However if you took that search string
update aTable set someColumn='JackedUpValue' where someColumn like
and performed the operation shown in this question, wouldn't you get
updateaTablesetsomeColumn='JackedUpValue'wheresomeColumnlike
which should not execute, right?
I'm curious if there is any form of SQL injection that could defeat this? Are there one word dangerous commands? If this can be defeated, would removing spaces at least help a bit defense?
Note: I'm not suggesting this be my only form of defense, but just curious where this form of "Defense" fits in the spectrum of good to useless.
EDIT: Thanks for all of the answers thus far. Since there was mention of other characters being used to replace a space i.e., %20
in a URL, so I was curious if we sanitized the input to have no special characters, and only display ASCII Characters from 0 - 9
and a(A) - z(Z)
?
This also might be better for another question, but if you did have a query as a URL, or with a special character, at what point would that character turn from %20
into a space? Is it possible to remove the space after it turns into a space, if we didn't sanitize the input for special characters and such?
Ultimately, as shown in a few answers, there are smaller commands that could do things, e.g. id=1
from wireghoul's answer, or OR(1=1)OR'a'='
from JimmyJames' answer.
The thing is, if I limit the characters (as mentioned above) it should prevent any of this =
business, right?
Curious if there are other ways to defeat this?