1
votes

I want to learn how SQLInjection is working with the PostgresqlDb. I am using the Npgsql in C#.

So this is my Query, which is called, when I rename a folder:

cmd.CommandText = "UPDATE allfolder.folder SET folder_name = '" + foldernamenew + "'";
cmd.ExecuteNonQuery();

I now tried to pass the following value into the textfield:

abcdef; INSERT INTO allfolder.folder (id, folder_name) VALUES (56,"aaaaaaa");

Then AJAX is fired.

The output I assumed is, that all folders in the table has the folder_name "abcdef" and that I have a new folder with the id "56" called "aaaaaa". This is not the case, because the semicolon is not recognized as delimiter and so the name of each folder_name are "abcdef; INSERT INTO ....". I do not want to change it on DB side that multiqueries are allowed.

So my questions are:

How do I need to prepare the SQL statement, that I achieve a Injection? Is an injection even possible, when you are not allowed to execute a second qry because the semicolon isn't recognized?

I am only talking about Npgsql and postgres.

2
You need to escape quote first, obviously. - arrowd
Without parameterized queries, injection is always possible. I'd try '; DROP TABLE allfolder.folder;-- though. A lot easier. More detectable too - Panagiotis Kanavos

2 Answers

1
votes

As Laurenz mentioned above, this should work:

abcdef'; INSERT INTO allfolder.folder (id, folder_name) VALUES (56,'aaaaaaa'); -- test

Make sure, that you don't mix single and double quotes. A common mistake in SQL. You need the single quote before the semicolon, otherwise it is in the textstring, because you have no delimiter.

0
votes

“Multiqueries” (several queries in one line, separated by semicolon) are always allowed in PostgreSQL.

Your problem are the multiple errors in your SQL:

  • Missing single quote after abcdef.
  • Double quotes instead of single quotes around aaaaaaa.
  • No single quote or line comment at the end of the statement.

Speaking pointedly: you have to learn SQL before you can learn SQL injection.