Search code examples
sql-injection

Get the ResultSet of an SQL injection


Suppose the server side code is something like that:

String id = getIdFromHttpRequest();
String value = getValueFromHttpRequest();

ResultSet rs = new ResultSet();
String query = "INSERT INTO users VALUES ('" + id + "', '" + value + "');"
rs = SQL.doQuery(query); // i know it's not the syntax, but the point is clear

Well, the injection is easy, I can make it execute an SQL command, but the problem is I want to see the result set (I inject SELECT command).

Is there a way of doing so?


Solution

  • You probably cannot achieve this.

    As you know, an INSERT statement has no result set, even if you use SQL injection. At best, you could make it execute a SELECT as a scalar subquery. It's not hard to spoof your example to execute the following:

    INSERT INTO users VALUES ('8675309', '' || (SELECT ...blah blah...) || '');
    

    But that still would not return a result set, because INSERT never has a result set.

    You would need to execute a second query to do that. Some query interfaces do support multi-query in a single call to doQuery(), but this is not always true (depends on the brand of database you use, and possibly some configuration options).

    INSERT INTO users VALUES (...whatever...);
    SELECT * FROM secure_table WHERE (id = '8675309');
    

    With SQL injection, you can manipulate the SQL, but you can't manipulate the rest of the code in the application that runs the SQL. In the example you show, the app is designed to run an INSERT query, not an INSERT followed by a SELECT. The app would have no reason to fetch a result set after executing an INSERT.

    It's hard to imagine how you could use SQL injection alone to trick the code you show into fetching and displaying a result set.

    I don't think it is possible to use SQL injection do read data by exploiting a non-reading query.