保护用户数据 - 用于方法调用、SQL 和文件调用

发布于 2024-08-05 23:21:32 字数 426 浏览 5 评论 0原文

我在方法调用、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-08-12 23:21:33

由于转义也取决于您发送数据的系统,我的建议是使用 PHP 提供的、专门为每个系统创建的函数。

例如:

不管怎样:不要重新发明轮子!

对于多种输出已经存在转义函数/方法:使用它们!

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 :

Either way : don't re-invent the wheel !

There are escaping functions/methods that already exists for many kind of output : use those !

人海汹涌 2024-08-12 23:21:33

另请注意,您必须转义的东西(如果用户输入)是图像位置等

如果有人用此热链接到图像(例如头像)

http://yoursite.com/admin/user/delete/1

然后您视图中的代码是

<img class="avatar" alt="<?php echo $userName; ?>" src="<?php $avatarUrl; ?>" />

如果您以管理员身份登录,那么您可能会意外删除用户。当然,希望这种删除是通过帖子完成的,但它仍然可以被规避。

在这种情况下,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

http://yoursite.com/admin/user/delete/1

and then the code in your view is

<img class="avatar" alt="<?php echo $userName; ?>" src="<?php $avatarUrl; ?>" />

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.

染年凉城似染瑾 2024-08-12 23:21:33

我这样使用:

function escape($sql) {
    // Stripslashes
    if (get_magic_quotes_gpc()) {
        $sql = stripslashes($sql);
    }
    //if this is the intedger
    if (!is_int($sql) || $sql == '0') {
        $sql = "'" . mysql_real_escape_string($sql) . "'";
    }
    return $sql;
}

在 MySQL 查询中

mysql_query("SELECT SQL_CACHE * FROM `page` WHERE `id` = ".escape($_GET['id'])." LIMIT 1");

I'm using like this:

function escape($sql) {
    // Stripslashes
    if (get_magic_quotes_gpc()) {
        $sql = stripslashes($sql);
    }
    //if this is the intedger
    if (!is_int($sql) || $sql == '0') {
        $sql = "'" . mysql_real_escape_string($sql) . "'";
    }
    return $sql;
}

And in MySQL query

mysql_query("SELECT SQL_CACHE * FROM `page` WHERE `id` = ".escape($_GET['id'])." LIMIT 1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文