When I started writing the first SQL statements in my programs, I was very comfortable defending myself against SQL-Injection using the very simple method my colleague showed me. He replaced all single quotes with two single quotes.
So, for example, there is a search field in which you can enter a username to search in custom. If you enter
Peter Barber
The SELECT statement will look like
SELECT * FROM Customers WHERE Customername = 'Peter' Barbershop'
If now the attacker inserts this:
';DROP TABLE FOO;
The operation will look like this:
SELECT * FROM Customers WHERE Customername = ''';DROP TABLE FOO;--'
He would not lose a single table, but did a search for a custom name for the user '; DROP TABLE FOO; - which, I believe, will not be found; -)
Now, after some time writing instructions and protecting against SQL-Injection using this method, I read that many developers use parameterized statements, but I never read an article that used our method. Therefore, there is definitely a good reason for this.
In what scenarios will the parameters be displayed, but our method does not work? What are the advantages of parameterized statements over our method?
thanks
Philipp
sql sql-injection parameterized
Philipp grathwohl
source share