简体   繁体   English

SQL注入可检索与查询中不同的表中的所有数据

[英]SQL injection that retrieves all data in a different table that the one in the query

I own a PHP system that works on a virtual server "Wamp" on windows XP. 我拥有一个在Windows XP的虚拟服务器“ Wamp”上运行的PHP系统。 I am trying to detect the vulnerabilities in my system using SQL injection technique. 我正在尝试使用SQL注入技术检测系统中的漏洞。

I have tried the basic ways of injection such as " ' OR 1=1 " and I detected several problems. 我尝试了诸如“'OR 1 = 1”之类的基本注入方式,并且发现了几个问题。 However, I am stuck on the following issue: 但是,我陷入了以下问题:

I have the following line of code in my system: 我的系统中有以下代码行:

$sql2="SELECT * FROM $table1 WHERE question_id='$id'";

After this line of code, I execute the query and display the result like this: 在这行代码之后,我执行查询并显示如下结果:

$result2=mysql_query($sql2);

while($rows=mysql_fetch_array($result2)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong>ID</strong></td>
<td bgcolor="#F8F7F1">:</td>
<td bgcolor="#F8F7F1"><?php echo $rows['a_id']; ?></td>
</tr>
<tr>
.......
.......

Note that the value of the variable "$id" is sent from the previous page as _GET[] parameter. 请注意,变量“ $ id”的值从上一页作为_GET []参数发送。

My problem is, I am trying to inject the parameter id=2? 我的问题是,我正在尝试注入参数id = 2? (2 is an example) that is being sent from the previous page. (示例是2)是从上一页发送的。 Here's an example: 这是一个例子:

http://localhost/PHPforum/view_topic.php?id=2

I am looking for a way to inject the value of the variable "$id" in order to make it display the data of different table (table2) instead of table1. 我正在寻找一种注入变量“ $ id”的值的方法,以使其显示不同表(table2)而不是table1的数据。 It's fine that I display both tables but I need table2 data to be displayed by that injection. 我可以显示两个表,但是我需要通过该注入来显示table2数据。

How can I possibly do that? 我该怎么做?

Again, I am not trying to attack any system, It's a system that I own and I am trying to detect its vulnerabilities. 同样,我并没有尝试攻击任何系统,而是我拥有的系统,并且我试图检测其漏洞。

From the mere proof of concept of the SQL injection vulnerability, you would first identify the context the injection happens in. Although in most cases the injection is in the WHERE clause, there are also other possible positions. 仅从SQL注入漏洞的概念证明,您首先将确定注入发生的上下文。尽管在大多数情况下,注入位于WHERE子句中,但也存在其他可能的位置。

Next you would try to identify the number of columns that are selected. 接下来,您将尝试识别所选的列数。 Since this is MySQL, you can use ORDER BY x and specify the number of the column you want the results to be ordered by. 由于这是MySQL,因此可以使用ORDER BY x并指定要对结果进行排序的列号。 So start of with 2' ORDER BY 1 -- , 2' ORDER BY 2 -- , until one fails. 因此,以2' ORDER BY 1 --2' ORDER BY 2 -- ,直到一个失败。

Then you can join the original result set with another one that you injected with UNION SELECT like this: 然后,您可以将原始结果集与通过UNION SELECT注入的另一个结果集合并,如下所示:

' UNION SELECT 1, 2, 3, …, n --

This is just a simple test and only the injected result set should show up. 这只是一个简单的测试,只有注入的结果集应该显示出来。 The numbers give you a hint which values are reflected where. 数字提示您哪些值反映在哪里。 From here on you can select any other value from other tables, views, or other data sources. 从这里开始,您可以从其他表,视图或其他数据源中选择任何其他值。 A good hint for what information is worthy can give you pentestmonkey's MySQL SQL Injection Cheat Sheet . 关于什么信息值得的一个很好的提示,可以为您提供pentestmonkey的MySQL SQL Injection Cheat Sheet

The usual way is to use UNION to combine the results from two different queries. 通常的方法是使用UNION合并来自两个不同查询的结果。 You might have to play around a bit to get the correct number of columns and charsets to allow the query to go through. 您可能需要花一点时间才能获得正确数量的列和字符集,以允许查询通过。

There are several good cheat sheets on the web for SQL injection that you can use as a reference for attack vectors. Web上有几个不错的SQL注入备忘单 ,您可以将其用作攻击媒介的参考。

You should really consider using a library that allows you to use parameterized queries (such as prepared statements) for querying the database instead of building (and escaping) the queries yourself. 您应该真正考虑使用允许您使用参数化查询(例如准备好的语句)来查询数据库的库,而不是自己构建(和转义)查询。 PDO and mysqli are both available for PHP and will allow prepared statements. PDO和mysqli均可用于PHP,并允许准备好的语句。

This works technically, but the application logic will probably not show the values of a different table: 这在技术上是可行的,但应用程序逻辑可能不会显示其他表的值:

http://localhost/PHPforum/view_topic.php?id=2;select * from table2

If you know all the table names and field names, you could do: 如果您知道所有表名和字段名,则可以执行以下操作:

http://localhost/PHPforum/view_topic.php?id=2;select table2id as table1id, table2value as table1value, .. from table2

You may have to urlencode the semicolon and the spaces so that it works. 您可能必须对分号和空格进行urlencode,以使其起作用。

SQL Injection vulnerabilities are actually not that difficult to find, IMHO. 实际上,SQL Injection漏洞并不难找到,恕我直言。

The simple rule to obey is this: 要遵循的简单规则是:

Don't concatenate a user-supplied value into a string which is passed to the database for execution. 不要将用户提供的值连接到字符串中,该字符串将传递给数据库以执行。

If you break this rule, ie concatenate a user-supplied value into a query string, you've created a SQL Injection vulnerability. 如果违反此规则,即将用户提供的值连接到查询字符串中,则说明您已经创建了一个SQL Injection漏洞。

There are two ways around the problem: 解决此问题的方法有两种:

  1. Use a library which allows you to bind parameter values into query strings. 使用允许您将参数值绑定到查询字符串中的库。 These libraries use API calls to pass parameter values to the database when queries are executed rather than concatenating the values into the statement that the database executes. 这些库在执行查询时使用API​​调用将参数值传递给数据库,而不是将值连接到数据库执行的语句中。

  2. Check all of your code to see where values concatenated into query strings are coming from. 检查所有代码,以查看连接到查询字符串中的值的来源。

I always choose approach 1, then I know I'm always safe. 我总是选择方法1,那么我就知道自己永远是安全的。

You can continue with approach 2, but it's like trying to bail out a boat with a hole in it. 您可以继续进行方法2,但这就像试图将有孔的船救出来。 The water will keep on coming in, or, in your case, the code will have to be examined for every change you make. 水将不断涌入,或者,对于您而言,每次更改都必须检查代码。

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

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