简体   繁体   English

java-如何执行SQL注入以进行测试?

[英]java-How to perform SQL injection for testing purposes?

I have a web application that I am trying to "break".There's a login page that requires username and password input. 我有一个要尝试“破解”的Web应用程序。有一个登录页面,需要输入用户名和密码。 Let's say I have a table Auser that stores username's info in MySQL. 假设我有一个表Auser ,用于在MySQL中存储用户名信息。

When I hit Login after keying the credentials,it executes this line of code: 键入凭据后,单击“登录”时,它将执行以下代码行:

String sql = "select object(o) from Auser as o where ausername='" + username + "'";

Now, I know not using preparedStatement makes SQL query vulnerable to SQL injection and I want to perform such a stunt. 现在,我知道不使用preparedStatement会使SQL查询容易受到SQL注入的攻击,我想执行这种特技。 I created a dummy table called test for the purpose of able to drop this table via the injection command. 我创建了一个名为test的虚拟表,目的是能够通过注入命令删除该表。

I tried various ways like in my username input(root is the username): 我尝试了各种方式,例如在用户名输入中(root是用户名):

root` DROP TABLE test;

And it didn't work. 而且它没有用。 Is there a way to make my injection successful? 有没有办法使我的注射成功?

Update : 更新

Just extra info, my username column is VARCHAR(255) and my method for getting the username is below: 只是额外的信息,我的用户名列为VARCHAR(255)而我的获取用户名的方法如下:

public Auser get(String username, boolean moreInfo) {
 try {
  Auser u = null;
  String sql = "select object(o) from Auser as o where ausername='" + username + "'";
  List resList = em.createQuery(sql).getResultList();
  if (resList == null) { // null check for sql query / library error
   msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
    this.getClass().getName(), "get[" + username + "]", "query error AUSER.");
  } else if (resList.isEmpty()) {
   msg = "User " + username + " not found.";
  } else {
   u = (Auser) resList.get(0);
  }
  return u;
 } catch (Exception e) {
  msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
   this.getClass().getName(), "get[" + username + "]", e.getMessage());
  return null;
 }
}

Seems every solution, I tried keeps throwing IllegalArgumetnException and the table still remains.I just want to exploit the vulnerabilities of my program,it can be any kind of injection whether dropping a table, returning all users info,etc. 似乎每个解决方案,我都尝试不断抛出IllegalArgumetnException ,并且表仍然存在。我只想利用程序的漏洞,是否可以删除表,返回所有用户信息等都是任何注入。

The EntityManager has some (very) basic protection built in that won't run more than one command in the same SQL statement. EntityManager内置一些(非常)基本保护,不会在同一SQL语句中运行多个命令。

This will protect you from Robert'); DROP TABLE Students; -- 这将保护您免受Robert'); DROP TABLE Students; -- Robert'); DROP TABLE Students; -- Robert'); DROP TABLE Students; -- , but it won't protect from attackers trying to expand/alter the one query that's being run. Robert'); DROP TABLE Students; -- ,但它无法防止攻击者试图扩展/更改正在运行的一个查询。

For example, in your code an attacker could get the details of another user by entering the username ' OR 1 = 1 -- ; 例如,在您的代码中,攻击者可以通过输入用户名' OR 1 = 1 -- ;获取其他用户的详细信息。 This would make the SQL string being executed 这将使SQL字符串被执行

select object(o) from Auser as o where ausername='' OR 1 = 1 --'

which will select every user in the table (note that the -- at the end of the input will comment out everything after the injected code), and your method will return the first user in the result list This will potentially give the attacker details about another user that they should not have access to. 它将选择表中的每个用户(请注意,输入末尾的--将注释掉注入代码之后的所有内容),并且您的方法将返回结果列表中的第一个用户。这可能会为攻击者提供有关以下内容的详细信息他们不应该访问的另一个用户。 If the first account is an administrator account then they may also have access they should not have. 如果第一个帐户是管理员帐户,那么他们也可能具有他们不应该拥有的访问权限。

An attacker can also learn the structure of the table this way - they can try strings like ' and IS_ADMIN = IS_ADMIN -- , or ' OR ID = 0 -- . 攻击者还可以通过这种方式学习表的结构-他们可以尝试使用诸如' and IS_ADMIN = IS_ADMIN --' OR ID = 0 --' and IS_ADMIN = IS_ADMIN --字符串。 If they try enough of these (and attacks like this can be easily automated) they will find valid column names when the query doesn't throw an error. 如果他们尝试了足够多的这些操作(并且这样的攻击可以很容易地实现自动化),那么当查询没有引发错误时,他们将找到有效的列名。 They can potentially then make a more targeted injection attack to gain access to an admin account. 然后,他们可能会进行更有针对性的注入攻击,以访问管理员帐户。

They might also learn things from the error message returned from a failed attempt, such as the DB platform, which can make attacks easier. 他们还可以从失败的尝试(例如数据库平台)返回的错误消息中学到东西,这可以使攻击变得更容易。

String sql = "select object(o) from Auser as o where ausername='" + username + "'";

If you want to delete the test table 如果要删除测试表

username = "x'; DROP TABLE test AND '1'='1"

If you want to see all fields of all ausers entries 如果要查看所有用户条目的所有字段

username = "x' OR '1'='1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM