2

As part of a Secure Code course I am given a vulnerable Java system, where queries are built up using string concatintion, and so on. Following is the respective code line that I have to SQL inject to retrieve log-in data:

String comment;
int articleID;
//...
stmt.executeUpdate("INSERT INTO comment (text, article_id) VALUES ('" + comment + "', " + articleId + ")");

comment can be set to any text, it is just read plain-text from a textbox. Therefore, I thought about setting the textbox text to an SELECT statement, reading the complete login data (table user has columns login and password):

' || (SELECT login || password FROM user) || '

which in overall should result in the query

INSERT INTO comment (text, article_id) VALUES ('' || (SELECT login || password FROM user) || '', 4)

yet, this results in

org.hsqldb.HsqlException: cardinality violation

Question:

I assume this is because (SELECT login || password FROM user) is not a single string, but a result set, and may therefore not be concatenated using ||.

Is it possible to convert the complete result set to one string in a way it can be used in this SQL injection scenario (in standard SQL / SQL that works on HSQLDB)?

1 Answer 1

2

You are correct, as there is obviously more than one user and multiple rows are returned from the SELECT.

You can add LIMIT 1 to the SELECT to get the first row. You can also create a string array from the inner SELECT. See the guide.

In HSQLDB each database user has separate access rights to the tables. In a real deployment the user that inserts comments will not even see the existence of the table that contains the password, let alone select from it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.