Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL injection and prepared statements

My teacher and I are having a debate about whether it is possible to SQL inject into a prepared statement. I understand that normally you couldn't, but the professor insists on using sql concatenation instead of using (?).

Now I am trying to break my code, but I am having no luck.

public Users getUserByUsername(String username) throws SQLException {
    StringBuffer sql = new StringBuffer();

    sql.append("select * from users as  u, user_type_lookup as l, user_types as t ");
    sql.append("where u.users_id=l.user_id and l.type_id=t.user_types_id and u.username='");
    sql.append(username);
    sql.append("';");

    System.out.println(sql.toString());

    PreparedStatement ps = conn.prepareStatement(sql.toString());
    ResultSet rs = ps.executeQuery(sql.toString());

    if (!rs.next()) {
        return null;
    }

    String password = rs.getString("password");
    String type = rs.getString("description");
    int id = rs.getInt("users_id");
    int incorrect_logins = rs.getInt("incorrect_logins");
    Time wait_time = rs.getTime("wait_time");

    Users u = new Users(id, username, password, type, incorrect_logins,
            wait_time);
    return u;
}

Inserts I have tried:

string: '; DELETE FROM users WHERE 1 or users_id = '
string: ';delete from users where username<>'
//The only one that worked    
string: stan' or 'a'<>'b

SQL output(Results in a java error):

select * from users as  u, user_type_lookup as l, user_types as t where u.users_id=l.user_id and l.type_id=t.user_types_id and u.username=''; DELETE FROM users WHERE 1 or users_id = '';

SQL output (works as intended):

select * from users as  u, user_type_lookup as l, user_types as t where u.users_id=l.user_id and l.type_id=t.user_types_id and u.username='stan';

Error message:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your    
SQL syntax; check the manual that corresponds to your MySQL server version for the   
right syntax to use near 'DELETE FROM users WHERE 1 or users_id = ''' at line 1

Server: Tomcat 7

Database: MySQL

IDE: Eclipse

Language: Java

So please help me break my code!

like image 915
user1301708 Avatar asked Jul 11 '26 16:07

user1301708


1 Answers

You can't add a separate statement inside the SQL of the prepared-statement, but you can break it by, for example:

  • using ' OR 'x' = 'x as the username (so that the query will do a Cartesian join across all users and types mappings between them); this will greatly harm performance if users and user_type_lookup are large tables, and would be an excellent start on a denial-of-service attack.
  • using ' OR (SELECT stored_procedure_that_deletes_things()) = 1 (so that the query will invoke a stored-procedure that has deleterious effects).
like image 64
ruakh Avatar answered Jul 14 '26 04:07

ruakh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!