V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
warlock
V2EX  ›  数据库

SQL 语句的问题

  •  
  •   warlock · 2016-04-22 16:04:42 +08:00 · 1156 次点击
    这是一个创建于 2919 天前的主题,其中的信息可能已经有所发展或是发生改变。

    select * from ttt where name = 'shuibianshu' * 'xxooxxoo'

    这条语句为什么可以把 ttt 表里的全部数据查出来? 后面星号两边的字符串可以随便乱输。

    15 条回复    2016-08-04 10:54:45 +08:00
    EchoUtopia
        1
    EchoUtopia  
       2016-04-22 16:14:07 +08:00
    我试的时候返回的星号后面那个条件的查询结果
    SilentDepth
        2
    SilentDepth  
       2016-04-22 16:22:07 +08:00
    我是想说,那个字念 suí……
    lancerliu
        3
    lancerliu  
       2016-04-22 16:22:15 +08:00
    数据库类型和版本呢?
    我执行这条语句是报错的
    warlock
        4
    warlock  
    OP
       2016-04-22 16:23:16 +08:00
    @lancerliu Mysql 5.1.73
    warlock
        5
    warlock  
    OP
       2016-04-22 16:24:05 +08:00
    @SilentDepth 哈哈 我的平翘舌一直有问题 ;)
    cdxy
        6
    cdxy  
       2016-04-22 16:27:09 +08:00
    aaa' or 1=1 --

    注意--右边有空格,在 url 里面可以 --+

    完整语句:  select * from ttt where name='aaa' or 1=1 -- ' * 'xxx';
    qhxin
        7
    qhxin  
       2016-04-22 16:31:59 +08:00   ❤️ 1
    'shuibianshu' * 'xxooxxoo'===>0

    试试: select 'shuibianshu' * 'xxooxxoo' from ttt where name = 'shuibianshu' * 'xxooxxoo'
    warlock
        8
    warlock  
    OP
       2016-04-22 16:34:35 +08:00
    后面两个字符串中间的星号 是做了乘法运算 结果是 0 , 然后匹配字符串的时候,表里的字符串都被转成了 0 , 0=0 ,所以就都查出来了。 参考资料: http://stackoverflow.com/questions/22080382/mysql-why-comparing-a-string-to-0-gives-true
    ivvei
        9
    ivvei  
       2016-04-22 16:43:10 +08:00
    Mysql 奇葩问题就是多啊
    liyj144
        10
    liyj144  
       2016-04-22 16:44:07 +08:00
    因为'shuibianshu' * 'xxooxxoo' 的值是整数类型的 0 , 然后查询是 name 先做整型转换,然后和 0 对比,因为一般字符串(只要首字符不是数字的,比如 1qwe 这样的记录)都转成了 0 ,所以就造成了这种假象。
    lancerliu
        11
    lancerliu  
       2016-04-22 20:17:40 +08:00
    @liyj144 @warlock 是对的,你这么试试: select *,'shuibianshu' * 'xxooxxoo' from ttt where name = 'shuibianshu' * 'xxooxxoo',然后就可以看到'shuibianshu' * 'xxooxxoo'的结果是 0 ,所以你的 sql 相当于 select * from ttt where name = 0
    loading
        12
    loading  
       2016-04-22 20:24:48 +08:00
    这个是考试题?
    billlee
        13
    billlee  
       2016-04-22 20:39:11 +08:00
    看了这个,我只想说,弱类型真可怕。
    SunnySuo
        14
    SunnySuo  
       2016-08-04 10:38:26 +08:00
    楼主,我司招后端开发工程师( golang ), web 前端工程师、资深软件工程师,欢迎关注! http://v2ex.com/t/296530#reply3
    warlock
        15
    warlock  
    OP
       2016-08-04 10:54:45 +08:00
    @SunnySuo 然而我在魔都。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5249 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 08:32 · PVG 16:32 · LAX 01:32 · JFK 04:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.