修复此特定 SQL 注入漏洞的示例代码 [英] Sample code to fix this particular SQL-injection hole

查看:32
本文介绍了修复此特定 SQL 注入漏洞的示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请先阅读全文

在这个答案中:如何防止动态 SQL 注入表名?

Pekka 指出了这段代码的原因:

Pekka points out why this code:

$clas=$_POST['clas'];
$query="SELECT * FROM $clas ";

无法使用 PDO 或 mysql-real_escape_string() 修复.

Cannot be repaired by using a PDO or mysql-real_escape_string().

谁能提供示例代码如何解决这个问题,以便新手可以粘贴该代码
(根据他的需要调整之后)并避免 SQL 注入.

Can anyone please provide sample code how to fix this so a newbie can paste that code
(after/adjusting it to his needs) and be safe from SQL-injection.

请不要解释SQL注入,注入和PDO我都知道,我只需要示例代码

推荐答案

您可以使用白名单来确保该值确实是您希望以这种方式访问​​的表之一.

You could use a whitelist to ensure that the value is indeed one of the tables you wish to be accessed in that way.

示例:

$allowed_tables = array('table1', 'table2');
$clas = $_POST['clas'];
if (in_array($clas, $allowed_tables)) {
    $query = "SELECT * FROM `$clas`";
}

请注意,从 GET 或 POST 参数直接构造 SQL 查询通常是一个坏主意,但白名单可以使其安全.

Note that constructing SQL queries directly from GET or POST parameters is usually a bad idea anyways, but a whitelist can make it safe.

这篇关于修复此特定 SQL 注入漏洞的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆