This question just came into my mind and wasn't able to find this anywhere so thought this would be the best place to ask. This is just for education purpose. I use proper sanitation and haven't provided DROP permission for my real database.
Let's assume a database with all permissions and a simple insert query with three values
INSERT INTO test(a,b,c) VALUES('$a','$b','$c');
The above query is vulnerable to sql injection.
Let's assume the user input to be
- a',(select DATABASE()),'a')--
- begone2
- begone3
The resulting query would be this:
INSERT INTO test(a,b,c) VALUES('a',(select DATABASE()),'a')-- ','begone2','begone3')
This above query would execute and insert the database name into the table but my question is Will a attacker will able to drop the database without actually knowing the database name?, with a query like this:
INSERT INTO test(a,b,c) VALUES
('a',(DROP DATABASE (select DATABASE())),'a')-- ','begone2','begone3')
I tried running the above query and it throws a error. What's wrong with this query?
INSERT INTO tab VALUES (1,2,3); DROP DATABASE ...; --...Malicious user could do it with 2 steps (insert database name and read it) or by using dynamic SQL. So the correct answer is: use parametrized queries.