SQL盲注(1)

简介

Web应用访问数据库的目的有很多。常见目的就是访问数据并呈现给用户。在这种情况下,攻击者就有可能会修改SQL语句并显示数据库中的任意信息,并将这些信息写入到Web服务器对HTTP请求的响应之中。有时不显示数据库的信息,但并不表示代码不会受到SQL注入攻击。

列如,用户输入任意用户名及密码时,会显示“Invalid username or password”,但如果将user' or '1'='1作为用户名的话,输入任意密码,会显示“Invalid password”。显然username字段更容易受到SQL注入攻击

在输入永假条件(user' or '1'='2)检查返回值的差异,进一步核实username字段更易受到攻击。

做完测试后可以得知,username字段更易受到SQL注入攻击,但password字段不易受到攻击,且无法绕开身份验证。

该情况下,并没有显示任何数据,我们只知道两件事。

  • 在username字段为真时,表单显示“Invalid password”
  • 在username字段为假时,表单显示“Invalid username or password”

这种情况,称为SQL盲注。

举例

假设本地的show.php页面接受一个id的get参数,返回相应id的商品信息,可以按下列请求来查看商品信息

http://127.0.0.1/show.php?id=1  --返回第一件商品信息

http://127.0.0.1/show.php?id=2  --返回第二件商品信息

http://127.0.0.1/show.php?id=3  --返回三件商品信息

http://127.0.0.1/show.php?id=4  --返回第四件商品信息

且本地做了部分安全考虑,在监测到错误输入时,会返回第一件商品信息。所以下列请求会返回第一件商品信息

http://127.0.0.1/show.php?id=attacker

http://127.0.0.1/show.php?id=attacker’

http://127.0.0.1/show.php?id=

http://127.0.0.1/show.php?id=999999999999999999999999999(不存在的产品id)

http://127.0.0.1/show.php?id=-1

减法测试

下列请求会返回第二件商品信息

http://127.0.0.1/show.php?id=3-1

http://127.0.0.1/show.php?id=4-2

http://127.0.0.1/show.php?id=5-3

由此可知,已将get参数传递给SQL语句并按下列方式执行:

select * from products where id=3-1

数据库计算减法的值并返回第二件商品信息

加法测试

如果要测试加法的话,因为加号(+)时URI的保留字,所以要对其进行编码,%2B代表对加号的URL编码.

http://127.0.0.1/show.php?id=2%2B3(对id=2+3进行URL编码)

http://127.0.0.1/show.php?id=1%2B4(对id=1+4进行URL编码)

利用永真及永假条件进行测试

or运算符

http://127.0.0.1/show.php?id=2 or 1=1  --返回第一件商品信息

http://127.0.0.1/show.php?id=2 or 1=2  --返回第二件商品信息

第一条请求中 or 1=1 要求返回所有商品信息,数据库监测到错误时,返回第一件商品信息

第二条请求中 and 1=1 对结果无影响

and运算符

http://127.0.0.1/show.php?id=2 and 1=1  --返回第二件商品信息

http://127.0.0.1/show.php?id=2 and 1=2  --返回第一件商品信息

猜你喜欢

转载自www.cnblogs.com/dubhe-/p/10036331.html