转义mysql表名

时间:2012-07-05 16:05:32

标签: mysql escaping

如何转义名为log\'; WAITFOR DELAY \'0:0:5\';--的表名?

我想删除该表。

这些都不起作用:

SHOW TABLES LIKE 'log\\\'; WAITFOR DELAY \\\'0:0:5\\\';--';
SHOW TABLES LIKE "log\\'; WAITFOR DELAY \\'0:0:5\\';--";
SHOW TABLES LIKE `log\\'; WAITFOR DELAY \\'0:0:5\\';--`;

最后一个给出错误,而其他人没有给出结果。错误是:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`log\\'; WAITFOR DELAY \\'0:0:5\\';--`' at line 1

3 个答案:

答案 0 :(得分:2)

这些语句将返回表名(如果表存在):

SHOW TABLES LIKE "log\\\\'%"

SHOW TABLES LIKE "log\\\\'; WAITFOR DELAY \\\\'0:0:5\\\\';--"

SELECT table_name FROM information_schema.tables 
 WHERE table_name LIKE "log\\\\'; WAITFOR DELAY \\\\'0:0:5\\\\';--"

要在SQL语句中引用表名,您需要使用反引号来包含名称,例如:

SELECT 1 FROM `log\'; WAITFOR DELAY \'0:0:5\';--` LIMIT 1 ;

RENAME TABLE `log\'; WAITFOR DELAY \'0:0:5\';--` TO `foo` ;

DROP TABLE `log\'; WAITFOR DELAY \'0:0:5\';--` ;

注意:在SQL语句中引用对象名称时,不需要转义名称中的反斜杠和特殊字符,只需将其括在反引号中即可。但是当那些反斜杠被解释为字符串文字时,需要对它们进行转义,因为它在LIKE谓词中。

(我去过那里并且做了那个,创造了一个不稳定的表名。)


要删除名称以log\'开头的所有表格,我会将其作为两个步骤进行处理。首先,我将生成DROP TABLE语句,然后执行这些语句。

SELECT CONCAT('DROP TABLE `',table_name,'`;') 
 FROM information_schema.tables
WHERE table_name LIKE "log\\\\'%"
  AND table_schema = DATABASE()

答案 1 :(得分:0)

`log\'; WAITFOR DELAY \'0:0:5\';--`

试试。

修改

编辑
`log\\'; WAITFOR DELAY \\'0:0:5\\';--`

`log\'; WAITFOR DELAY \'0:0:5\';--`

答案 2 :(得分:0)

表名实际上是否包含\字符,还是只是log'; WAITFOR DELAY '0:0:5';--?因为根据http://dev.mysql.com/doc/refman/5.0/en/identifiers.html \字符表中不允许使用字符。

这是否有效:

SHOW TABLES LIKE `log'; WAITFOR DELAY '0:0:5';--`;