简体   繁体   English

SQL注入| 更改id值

[英]SQL injection | Changing id values

I'm stuck with an sql injection problem. 我遇到了SQL注入问题。 My page displays the table's data as follows: 我的页面显示表格的数据如下:

<input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>" /><?php echo $row['data']; ?><br />

How can I be sure that the submitted value is the id of this specific row an not a random number "injected" in the page? 如何确保提交的值是此特定行的ID而不是页面中“注入”的随机数?

Obviously, I would start checking that the id returns true to is_numeric() / get it through mysql_real_escape_string(). 显然,我会开始检查id返回trueis_numeric() /通过mysql_real_escape_string().获取它mysql_real_escape_string().

Then, I thought of two options: 然后,我想到了两个选择:

  • Adding a hidden input with a copy of the $row['data'] so that I can check the correspondence between the id and the data before any mysql_query() 添加带有$ row ['data']副本的隐藏输入,以便我可以在任何mysql_query()之前检查id和数据之间的对应关系

  • Changing the row's id from an auto incremented number to a large random number, so that I could lower the chance of a lucky hit. 将行的id从自动递增的数字更改为一个大的随机数,这样我就可以降低幸运命中的几率。

Do I have it wrong? 我错了吗? Any better idea? 有什么好主意吗? Thanks for your help! 谢谢你的帮助!

You can't. 你不能。 Both of your options are fundamentally flawed as well: 您的两个选项都存在根本缺陷:

  • One that can change a checkbox's value can very well change a hidden input's value. 可以更改复选框值的一个可以很好地更改隐藏输入的值。
  • Your "random IDs" can still be saw on Dev Tools , Firebug or similar tool. 您的“随机ID”仍然可以在Dev Tools ,Firebug或类似工具上看到。

Instead of worrying about how the user sent the data, you should worry whether the data is valid and whether the user has permission for the given action. 您不必担心用户如何发送数据,而应该担心数据是否有效以及用户是否拥有给定操作的权限。


Also, is_numeric is not my favorite as it will return true for hex and exponential notation. 此外, is_numeric不是我喜欢的,因为它会返回true的十六进制和指数描述。 I'd recommend checking with ctype_digit or simply do an (int) cast, eg: 我建议用ctype_digit检查或者只是做一个(int) ctype_digit ,例如:

if (!isset($_POST['id'])) die('invalid data');
$id = (int) $_POST['id'];
if ($id == 0) die('invalid id');

Non-numeric strings are converted to 0 and auto increment fields usually have 1 as the first value. 非数字字符串转换为0 ,自动增量字段通常为1作为第一个值。 In case 0 is a valid value you'll need to tweak the code above, eg: 如果0是有效值,您需要调整上面的代码,例如:

if (!isset($_POST['id']) || !ctype_digit($_POST['id'])) die('invalid data');
$id = (int) $_POST['id'];

Afterwards, check whether the given ID exists in your DB. 然后,检查数据库中是否存在给定的ID。 Do the proper permission checks and that's it. 做适当的权限检查,就是这样。


It doesn't matter how your server got the data, what matters is the data being valid and the user having permission to perform the given operation. 服务器如何获取数据并不重要,重要的是数据是否有效以及用户是否有权执行给定的操作。 Anything that you do in the front-end/interface can be easily changed and manipulated by a hacker or any mid-experienced web developer. 您在前端/界面中执行的任何操作都可以由黑客或任何有经验的Web开发人员轻松更改和操作。

Focus into restringing non-authorized access and keeping your DB integrity. 专注于重新分配非授权访问并保持数据库完整性。 It doesn't matter whether the request is being made from your page, from a tampered page or through a terminal, all the headers and posted data can be easily reproduced to look like a request being made from your page. 无论是从您的页面,从篡改的页面还是通过终端发出请求都无关紧要,所有标题和发布的数据都可以轻松地再现, 看起来就像是从您的页面发出的请求。


After all this, I'm not sure whether you can call this "SQL Injection". 毕竟,我不确定你是否可以称之为“SQL注入”。 Your application's function requires some input which includes an integer value. 您的应用程序的功能需要一些包含整数值的输入。 Now what is left is checking whether the necessary input has been provided and is valid. 现在剩下的就是检查是否已提供必要的输入并且是否有效。 All user input must be treated as unsafe and be properly validated and escaped before being throw into a query. 必须将所有用户输入视为不安全,并在投入查询之前进行适当的验证和转义。

Also, look into PDO which handles value escaping pretty well. 另外,请研究处理价值逃逸的PDO The mysql_* extension and mysql_real_escape_string function are deprecated and very human-error prone. 不推荐使用mysql_*扩展和mysql_real_escape_string函数,并且非常容易出错。

As for preventing against SQL injection, the linked thread in the question's comments is a good read. 至于防止SQL注入,问题注释中的链接线程是一个很好的读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM