首页 > 解决方案 > SQLite - if I block " from strings, can I be SQL injected?

问题描述

I'm using this code (python using sqlite3) to add data to the table:

''' INSERT INTO TABLE (USERNAME) VALUES ("''' + data + '''")'''

If I block ", then (to the best of my knowledge) it should be impossible to exit the string, subsequently making it impossible to SQL inject.

My questions are these: Does this stop users from being able to inject SQL? If no, should I add more to the blacklist or create a whitelist?

All help is greatly appreciated.

标签: sqlite

解决方案


如果您data在让它命中插入语句之前进行消毒,确保单引号不会出现在任何地方,那么理论上 SQL 注入是不可能的。无论data注入什么都应该被视为字符串文字,从而使任何注入的 SQL 命令无效。但是,攻击者可能仍有办法解决此问题。

你最好的办法就是仅仅依靠使用准备好的语句来避免 SQL 注入。攻击者变得越来越聪明,并且可能仍然有一种方法可以通过其他方式注入您当前的插入语句。


推荐阅读