保护用户数据 - 用于方法调用、SQL 和文件调用
我在方法调用、SQL 查询和文件调用中使用 $_GET
、$_POST
和 $_COOKIE
变量 - 并且有必要转义 /重写此用户数据以获得更好的安全性(避免注入攻击等)。您建议如何完成此操作?
来自内置转义函数的一些想法......让果汁流动:
- 添加反斜杠到:
\x00、\n、\r、\、'、"
和\x1a 使字符串对于 SQL 查询是安全的 - 如 mysql_real_escape_string() 中所示,
- 将接受的字符数限制为
[a-zA-Z0-9 _-\.]
(其中“”)。 \.
”是转义的“.”-点)。
感谢您的输入。谢谢。
I am using $_GET
, $_POST
and $_COOKIE
variables in method calls, SQL queries and file calls - and it is necessary to escape / rewrite this user-data for better security (avoid injection attacks and the like). How would you recommend this is done?
Some ideas from built-in escape function ... to get the juices flowing:
- Add backslashes to:
\x00, \n, \r, \, ', "
and\x1a
to make the string safe for SQL queries - as in mysql_real_escape_string(). - Limit the number of accepted characters to
[a-zA-Z0-9 _-\.]
(where "\.
" is an escaped "."-dot).
Your inputs are appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于转义也取决于您发送数据的系统,我的建议是使用 PHP 提供的、专门为每个系统创建的函数。
例如:
mysql_real_escape_string
或mysqli_real_escape_string
,或PDO::quote
htmlspecialchars
或 htmlentities。不管怎样:不要重新发明轮子!
对于多种输出已经存在转义函数/方法:使用它们!
As escaping depends on the system you are sending the data too, my suggestion would be to use the functions provided by PHP, specifically created for each system.
For instance :
mysql_real_escape_string
, ormysqli_real_escape_string
, orPDO::quote
htmlspecialchars
or htmlentities.Either way : don't re-invent the wheel !
There are escaping functions/methods that already exists for many kind of output : use those !
另请注意,您必须转义的东西(如果用户输入)是图像位置等
如果有人用此热链接到图像(例如头像)
然后您视图中的代码是
如果您以管理员身份登录,那么您可能会意外删除用户。当然,希望这种删除是通过帖子完成的,但它仍然可以被规避。
在这种情况下,
htmlspecialchars()
将无济于事。您可以通过强制所有数据更改方法都与帖子一起使用来使攻击者变得更加困难,并且可以通过为每个删除操作生成令牌并在删除之前验证它来使攻击几乎不可能。
Also note that somethings you must escape (if user entered) are image locations etc
If someone hot linked to an image (for example an avatar) with this
and then the code in your view is
Then you might be accidentally deleting a user if you are logged in as admin. Of course, hopefully that sort of deletion is done with a post, but it can still be circumvented.
In this case,
htmlspecialchars()
won't help.You can make it harder for an attacker by enforcing all data changing methods to be with a post, and you can make it almost impossible by generating a token for every delete action, and verifying it before deleting.
我这样使用:
在 MySQL 查询中
I'm using like this:
And in MySQL query