如何防止注入(html、php)?
我有一个
我想知道,当有人注入(例如) 来损坏页面或发送查询来损坏数据库时,我必须做什么?
有什么简单的方法呢?
我尝试搜索 < > ' " drop
using 但是虽然我使用了 \"
而不是 "
但它不起作用,在 PHP 中,我遇到了错误。
正在搜索这些手动输入字符的最佳方法(PHP 和 Javascript)?
Possible Duplicate:
What are the best practices for avoiding xss attacks in a PHP site
I have a <textarea>
and an <input>
for comments of my site.Obviously, I echo
them in an interface page and inserting into my database.
I want to know what do I have to do, when a person injects (for example) a <img>
to damage the page or sending a query to damage database?
What is a simple way?
I've tried to search < > ' " drop
using but although I used \"
instead of "
but it doesn't work and in PHP, I've got error.
Is searching these characters manually the best way (PHP and Javascript)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要实现的是防止某种形式的XSS(跨站点脚本攻击) 攻击。您正试图阻止持续的变化:
有很多选择可以防止它们。 OWASP 有一个简洁的解释。。通过它并找出答案。但大多数情况下,这是一个需要个人单独处理的大问题。
最好的方法是使用 HTMLPurifier,它既简单又容易。可能会有点慢。但额外的处理是值得的。给你一个例子,说明它的使用是多么简单,这里是一个基本代码:
PS:HTMLPurifier有“白名单”选项。利用它来发挥你的优势。
编辑:
要回答有关阻止格式错误的 SQL 注入攻击的问题,请参阅此问题:如何防止 PHP 中的 SQL 注入? 和 这个回答
引用:
What you are trying to acheive is to prevent a form of XSS (Cross site scripting attacks) attacks. You are trying to prevent the persistent variety:
There are numerous options to prevent them. OWASP has a neat explanation.. Go through it and find out. But mostly its a very big problem for an Individual to handle solely.
The best way is to use HTMLPurifier which is both simple and easy. It may be a bit slow. But the extra processing is worth it. To give you an example of how simple it is to use here is a basic code:
PS: HTMLPurifier has options to "whitelist". Use that for your advantage.
EDIT:
To answer your question on stopping malformed SQL Injection attacks refer to this question: How can I prevent SQL injection in PHP? and this answer
Quote:
我使用这个从来没有遇到过问题:
i never had problems using this:
使用 addslashes($your_variable) 函数。这将在特殊字符之前添加反斜杠,并使用
stripslashes($db_result)
删除不需要的斜杠。http://php.net/manual/en/function.addslashes.php
您还可以使用
mysql_real_escape_string()
http://php.net/manual/en/function.mysql -real-escape-string.php
Use
addslashes($your_variable)
functon. this will add a back slashes before special character and usestripslashes($db_result)
to wipe out the unwanted slashes.http://php.net/manual/en/function.addslashes.php
You can also use
mysql_real_escape_string()
http://php.net/manual/en/function.mysql-real-escape-string.php