简体   繁体   English

使用php时如何防止SQL注入?

[英]how to protect from sql injection when using php?id=

Hello I need help finding a way to protect from sql injection on my current project, Im making bash tutorial site but ive run into a problem. 您好,我需要帮助找到一种方法来防止当前项目上的sql注入,我正在制作bash教程站点,但我遇到了问题。 I put most my content in database and depending on what link the user clicks it will pull different data onto the page. 我将大部分内容放在数据库中,根据用户单击的链接,它将不同的数据拖到页面上。 This is how im doing it 我就是这样做的

<a href="bash_cmds.php?id=1">apt-get </a><br>

And on bash_cmds.php 并在bash_cmds.php上

<?php
 require_once("connections/connect.php");
  $dbcon = new connection();
  $bash = $_REQUEST['id'];

  $query2 = "SELECT * FROM bash_cmds WHERE id = $bash ";
  $results = $dbcon->dbconnect()->query($query2);

  if($results){

  while($row = $results->fetch(PDO::FETCH_ASSOC)){
  $bash_cmd = $row['bash_command'];
  $how = $row['how_to'];
  } 
  } else { return false; }
  ?>

  <?php echo $bash_cmd ?>
  <br />
  <table>
<tr><td><?php echo $how ?> </td></tr>

</table>

However this leaves me vulnerable to sql injection, I ran sqlmap and was able to pull all databases and tables. 但是,这使我容易受到sql注入的影响,我运行了sqlmap并能够提取所有数据库和表。 Can someone please help I would appreciate it a lot the infomation would be invaluable. 有人可以帮忙吗,我非常感谢您提供的信息非常宝贵。

There are a couple of ways to do this. 有两种方法可以做到这一点。 I believe the best way is to use some database abstraction layer (there's a good one built into PHP called PDO) and use its prepared statements API . 我相信最好的方法是使用一些数据库抽象层(PHP中内置了一个很好的名为PDO的层) 并使用其准备好的语句API You can read more about PDO here , and you can see the particular function which binds a value to a ? 您可以在此处阅读有关PDO的更多信息,并且可以看到将值绑定到?的特定功能? placeholder here . 这里的占位符。

Alternatively, you could use the mysqli_real_escape_string API function, which should escape any SQL inside your $bash variable. 另外,您可以使用mysqli_real_escape_string API函数,该函数应转义$bash变量中的所有SQL。

Of course, in this particular case, simply ensuring the ID is an integer with (int) or intval() would be good enough, but the danger of using this approach in general is that it's easy to forget to do this one time, which is all it takes for your application to be vulnerable. 当然,在这种情况下,只需使用(int)intval()来确保ID是整数就足够了,但是通常使用这种方法的危险在于,很容易忘记一次执行此操作,这您的应用程序容易受到攻击。 If you use something like PDO, it's more "safe by default," one might say - it's more difficult to accidentally write vulnerable code. 有人可能会说,如果您使用PDO之类的东西,则默认情况下更“安全”-意外编写易受攻击的代码会更困难。

You could bind the values to a prepared statement . 您可以将值绑定准备好的语句

But for something simple as a numeric variable a cast to an integer would be good enough: 但是对于简单的数字变量来说,将其转换为整数就足够了:

$bash = (int) $_REQUEST['id'];

Using this, only a number would get stored into $bash . 使用这个,只有一个数字将被存储到$bash Even if someone enters ?id=--%20DROP%20TABLE%20xy; 即使有人输入?id=--%20DROP%20TABLE%20xy; , as this will get casted to 1 ; ,因为这将被强制转换为1 ;

I've found one of the easiest ways to protect against injection is to use prepared statements . 我发现防止注入的最简单方法之一就是使用准备好的语句

You can do this in PHP via PDO, as CmdrMoozy suggested. 您可以按照CmdrMoozy的建议通过PDO在PHP中执行此操作。

Prepared statements are more secure because the placeholders ? 预备语句更安全,因为占位符? can only represent values, and not variables (ie: will never be interpreted as a table name, server variable, column name, etc. It {currently} can't even represent a list of values). 只能代表值,而不代表变量(即:永远不会被解释为表名,服务器变量,列名等。{当前}甚至不能代表值列表)。 This immediately makes any modification to the logic of the query immutable, leaving only possible unwanted values as injection possibilities (looking for an id of 'notanid'), which in most cases isn't a concern (they'd just get a blank/wrong/error page, their fault for trying to hack your site). 这立即使对查询逻辑的任何修改都变得不可变,仅保留了可能的不需要的值作为注入可能性(查找“ notanid”的id),这在大多数情况下都不是问题(它们只会变成空白/错误/错误页面,即他们试图入侵您的网站的错误)。

Addendum: These restrictions are what is in place when the prepared statements are done on the server. 附录:这些限制是在服务器上执行准备好的语句时要执行的限制。 When prepared statements are simulated by a library instead of actually being server side the same may not be true, but often many of these are emulated. 当由库模拟准备好的语句而不是实际上是在服务器端时,这可能不是正确的,但通常其中许多都是模拟的。

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

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