3
我的老师和我正在讨论是否有可能将SQL注入预准备语句。我明白,通常你不能,但教授坚持使用sql连接而不是使用(?)。SQL注入和预处理语句
现在我试图打破我的代码,但我没有运气。
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;
}
插入我曾尝试:
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输出(在Java错误结果):
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输出(按预期工作):
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';
错误消息:
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
服务器:Tomcat的7
数据库:MySQL的
IDE:Eclipse的
语言:Java的
所以,请帮助我打破我的代码!
准备好的陈述包括通过其他方式转义价值。如果值已经转义,则应使用连接。 – kirilloid 2012-03-29 19:49:39
虽然我认为这是一个很好的问题,可以使用SQL注入攻击来准备语句......它并不是真正的表达方式......您可能希望使它更像是一个问题,以至于答案可以提供。 – scunliffe 2012-03-29 19:50:23
即使您能够将SQL注入该语句中,您也可以通过编写代码的方式来执行此操作。这是*不是*准备好的声明。 – NullUserException 2012-03-29 19:52:37