如何查询 SQL 数据库中的恶意或可疑数据?
最近,我一直在对 PHP 应用程序进行安全检查,并且已经发现并修复了一个 XSS 漏洞(在验证输入和编码输出方面)。
如何查询数据库以确保其中不存在任何恶意数据?有问题的字段应该是带有允许的符号(-、#、空格)的文本,但不应该有任何特殊的 html 字符(<、"、'、> 等)。
我假设我应该在查询中使用正则表达式; 有没有人专门为此目的预先构建了正则表达式?
Lately I have been doing a security pass on a PHP application and I've already found and fixed one XSS vulnerability (both in validating input and encoding the output).
How can I query the database to make sure there isn't any malicious data still residing in it? The fields in question should be text with allowable symbols (-, #, spaces) but shouldn't have any special html characters (<, ", ', >, etc).
I assume I should use regular expressions in the query; does anyone have prebuilt regexes especially for this purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只关心非字母数字并且它是 SQL Server,您可以使用:
这将显示
MyField
包含除 az 和 0-9 之外的任何内容的任何行。编辑:
更新的模式将是:
LIKE '%[^a-z0-9!-# ]%' ESCAPE '!'
我必须添加
ESCAPE
字符,因为你想要允许破折号-
。If you only care about non-alphanumerics and it's SQL Server you can use:
This will show you any row where
MyField
has anything except a-z and 0-9.EDIT:
Updated pattern would be:
LIKE '%[^a-z0-9!-# ]%' ESCAPE '!'
I had to add the
ESCAPE
char since you want to allow dashes-
.出于同样的原因,您不应该根据黑名单(即非法字符列表)验证输入,我会尽量避免在搜索中执行相同的操作。我在不知道保存数据的字段的意图(即姓名、地址、“关于我”等)的情况下发表评论,但我的建议是构建您的查询来识别您做什么< /em> 希望在您的数据库中识别异常。
原因是 XSS 中使用了如此多不同的字符模式。看看XSS Cheat Sheet,您就会开始有所了解。特别是当您进入字符编码时,仅仅寻找尖括号和引号之类的东西并不会让您走得太远。
For the same reason that you shouldn't be validating input against a black-list (i.e. list of illegal characters), I'd try to avoid doing the same in your search. I'm commenting without knowing the intent of the fields holding the data (i.e. name, address, "about me", etc.), but my suggestion would be to construct your query to identify what you do want in your database then identify the exceptions.
Reason being there are just simply so many different character patterns used in XSS. Take a look at the XSS Cheat Sheet and you'll start to get an idea. Particularly when you get into character encoding, just looking for things like angle brackets and quotes is not going to get you too far.