过滤表单输入
我有一个简单的联系表单,其中包含姓名、电子邮件、选择列表和文本区域。在我的邮件程序 php 脚本中,我尝试添加一个简单的过滤器来防止 SQL 注入或其他形式的黑客攻击。
例如,我正在使用
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
这好吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先告诉大家,大约85%的保护方法都是通过2个功能来完成的。
首先,如果有人向您的网站发送一些数据,例如
$_POST['name']
,并且您希望在 html 端使用此值,例如以下字符串:{$_POST['name']} 无效
那么您应该始终确保该值已通过htmlspecialchars,这将保护大多数 XSS 尝试
接下来是注入,如果
$_POST['name']
的值要进入您的数据库,只需确保您使用 mysql_real_escape_string 对该值。这将为您提供 100% 的 SQL 注入保护,但这意味着您的数据库无法运行用户的命令,这并不意味着文本应该是这样的。
在将数据插入数据库之前,您应该始终使用的函数是
这称为验证,仅用于确保用户提交的数据是您想要的,例如filter_var< /strong> 将用于验证他们输入的电子邮件是一封电子邮件,而不仅仅是一些 blah blah
我通常做的是运行一个干净的函数以确保所有估算数据都是干净的以 htmlspecialchars
为例:
然后执行以下操作以确保您免受 XSS 的影响:
因此,如果有人尝试提交
Test
那么该值将为转换为Firstly let me tell you that about 85% of protection methods are done with 2 functions.
Firstly if someone sends some data to your site such as
$_POST['name']
, and you wish to use this value back on html side such as<p>The following string: {$_POST['name']} is invalid</p>
then you should ALWAYS make sure that that value has been through htmlspecialchars, this will protect most of XSS AttemptsNext is injection, if the value of
$_POST['name']
is going into your database just make sure that you use mysql_real_escape_string on that value.that will give you 100% protection from sql injection, but all that means is your db cannot run commands from the user, that dont mean that the text is what it should be.
The functions that you should always use before inserting data into your database are
This is called Validation and is only needed for yout to make sure the data the user is submitting is what you want such as filter_var would be used to validate that the email they entered is an email and not just some blah blah
What i usually tent do do is to run a clean function to make sure that all imputed data is clean with htmlspecialchars
example:
Then do the following to make sure that your safe from XSS:
So if someone tried to submit
<a href='test'>Test</a>
then the value would be converted to<a href='test'>Test</a>
要测试有效性,请尝试使用 SQL 注入攻击来攻击您自己的站点。基本上,尝试传递像
' || 这样的字符串1=1
并查看是否出现错误。如果您收到错误,或者收到意外结果,则您的网站很容易受到攻击。否则,它可能正在工作;但可以肯定的是,请确保进行大量测试。To test the effectiveness, try attacking your own site with SQL injection attacks. Basically, try passing strings like
' || 1=1
and see if you get an error. If you get an error, or if you get an unexpected result, your site is open to attacks. Otherwise, it is probably working; but to be sure, make sure you do lots of testing.更好的选择是使用 mysqli 扩展和准备好的语句。然而,确实存在
mysql_real_escape_string()
函数,它专门“转义字符串中的特殊字符以在 SQL 语句中使用”。The better option is to use the mysqli extensions and prepared statements. However, there does exist the
mysql_real_escape_string()
function which specifically "escapes special characters in a string for use in an SQL statement".FILTER_SANITIZE_SPECIAL_CHARS
对 '"<>& 和 ASCII 值小于 32 的字符进行 HTML 转义。要获得htmlspecialchars()
的完整等效项,请使用FILTER_SANITIZE_FULL_SPECIAL_CHARS
相当于在设置了 ENT_QUOTES 的情况下调用 htmlspecialchars()。使用此函数应该使 mysql_real_escape_string() 的使用过时,但安全第一:)另请参阅:http://php.net/manual/en/filter.filters.sanitize.php 了解更多信息。
FILTER_SANITIZE_SPECIAL_CHARS
does HTML-escape '"<>& and characters with ASCII value less than 32. To have an full equivalent forhtmlspecialchars()
, useFILTER_SANITIZE_FULL_SPECIAL_CHARS
which is equivalent to calling htmlspecialchars() with ENT_QUOTES set. Using this function should make the use of mysql_real_escape_string() obsolete, but safety first :)see also: http://php.net/manual/en/filter.filters.sanitize.php for more information.