在没有预先准备的语句的情况下清理 MySQL 查询(PHP + 旧的 mysql 模块)
注意:我看过这个问题: Preventing SQL Injection without prepared statements (JDBC )。正如我所预料的那样......答案是使用准备好的语句。我处于不同的情况......所以我想知道最好的途径。
我正在使用下载的脚本(phpsimplechat),作者在其中编写了自己的简单 SQL 层(注意:它支持 PostgreSQL 和 MySQL)。不幸的是,我对其进行了一些测试,发现它很容易受到 SQL 注入攻击。从功能的角度来看,该脚本做了我想要的一切,所以我想挽救它。
幸运的是,它是开源的...但我不想重写所有 SQL 查询以使用 phpsimplechat 中的准备好的语句。 3rd 方库使用自己的 SQL 层而不是 PDO...并且在其下使用旧的 mysql 模块(因此,我不能使用准备好的语句。即使我更改了 mysql -> mysqli,我也必须处理“ dbQuery”层他放在所有代码之上)。我不需要 PostgreSQL 代码,因此答案可以是 MySQL 特定的。
我读到,addslashes 不足以防范所有 SQL 注入尝试。 mysql_real_escape_string 使用安全吗?
Note: I've looked at this question: Preventing SQL injection without prepared statements (JDBC) . And as I somewhat expected... the answer is to use prepared statements. I'm in a different set of circumstances... so I'd like to know the best path for this.
I'm using a downloaded script (phpsimplechat) where the author wrote his own simple SQL layer (notice: it supports PostgreSQL and MySQL). Unfortunately, I've ran some tests on it and it is vulnerable to SQL Injection. The script does everything I want simply from a features standpoint, so I'd like to salvage it.
Fortunately, it is open source... but I'd rather not rewrite all of the SQL queries to use prepared statements in phpsimplechat. The 3rd party library uses its own SQL layer instead of PDO... and under that uses the older mysql module (thus, I can't use prepared statements. Even if I changed mysql -> mysqli, I have to deal with "dbQuery" layer he put on top of all of his code). I do NOT need the PostgreSQL code, so answers can be MySQL specific.
I've read that addslashes is insufficient to protect against all SQL Injection attempts. Is mysql_real_escape_string safe to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您正确使用它,
mysql_real_escape_string
就保证是安全的,即确保查询中出现的所有字符串都被转义。Yes,
mysql_real_escape_string
is guaranteed to be safe if you use it correctly, i.e. make sure that all strings appearing in queries are escaped.