微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – SQL注入攻击

如何保护我的网站免受sql注入攻击?我使用PHPMysqL.我必须改变我的mySQL查询吗?

例如,我有一个这样的查询

<?PHP
$q=$_GET["q"];// im getting the Q value from the other form,from drop down Box[displaying data using Ajax
$a1=$_POST['hosteladmissionno'];
$a2=$_POST['student_name'];
$a3=$_POST['semester'];
$con = MysqL_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . MysqL_error());
  }

MysqL_select_db("hostel", $con);

$a1="SELECT  hosteladmissionno,student_name,semester FROM registration 
WHERE mess_type ".$q."' AND  status_flag=1";

$result = MysqL_query($a1);

if ($result === false) {

    die(MysqL_error());
}
echo "<table border='1' width=80%>
<tr>
 <th width=5%> S.No</th>
<th width=10%>H.Admin No</th>
<th width=10%>Student Name</th>
<th width=5%>No of Days</th>
</tr>";
 $i=0;
while($row = MysqL_fetch_array($result))
  {
  $i=$i+1;
  echo "<tr>";
  echo "<td  align=center>" .$i."</td>";
  echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>";
  echo "<td size=35  align=center>" . $row['student_name'] . "</td>";
  echo "<td  align=center>  <input type='text' name='days' size=2> </td> ";
  echo "</tr>";
  }
echo "</table>";

MysqL_close($con);
?>

我是否必须在查询中包含任何更改以避免注入攻击?任何建议都会有所帮助.谢谢.干杯!

解决方法:

您的查询出现(编辑:在查询的第一个版本中出现)完全是静态的 – 即它不使用任何用户提供的数据.在这种情况下,没有sql注入的风险.

sql注入攻击涉及获取用户输入并将其直接包含在SQL查询中,而不是使用参数化sql语句并包含用户提供的值的首选方法. (我不知道在PHP中如何完成的细节…我当然希望它是可能的.)

编辑:好的,现在你已经改变了你的代码,包括

$a1="SELECT  hosteladmissionno,student_name,semester FROM registration 
WHERE mess_type ".$q."' AND  status_flag=1";

从文本框中检索$q的位置.现在我假设你真的意味着第二行:

WHERE mess_type='".$q."' AND  status_flag=1";

但这仍然容易受到sql注入攻击.假设q的值是:

' OR 'x'='x

然后,您的sql语句将最终为

SELECT hosteladmissionno,student_name,semester FROM registration 
WHERE mess_type='' OR 'x'='x' AND  status_flag=1

这显然不是你追求的逻辑.

您应该使用值的参数,如此PHP prepared statement page所示.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐