繁体   English   中英

清理 PHP 中的查询字符串

[英]Sanitize query string in PHP

我有一个带有查询字符串的网页。

在 PHP 我有:

$querystring=$_SERVER["QUERY_STRING"];
echo "<html><head></head><body>
<a href='index.php?$querystring'>test</a>
</body></html>";

我需要清理查询字符串吗?
如果是,我该如何消毒,如果不消毒,可能会受到哪些攻击?

如果您运行的是 PHP >= 5.2.0,请使用filter_inputfilter_input_array

假设您的 URL 和查询字符串类似于http://example.com/?liquor=gin&mixer=tonic&garnish=lime

要过滤,您可以执行以下操作。

/*
 FILTER_SANITIZE_STRING removes most dangerous characters. That may 
 not always be what you want. Read the PHP filters docs. 

 We are also overwriting the $_GET array (the query string) with the sanitized
 versions of these variables.
*/

$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);

/* 
rebuild query string using white listed variables, 
not $_GET to prevent variable injection as Mārtiņš Briedis 
suggests above.
*/

$qv['liquor']  = $_GET['liquor'];
$qv['mixer']   = $_GET['mixer'];
$qv['garnish'] = $_GET['garnish'];

# build and URL encode the query string using the above array.
$querystring = http_build_query( $qv );

您应该使用htmlspecialchars($query, ENT_QUOTES)来防止任何 XSS 攻击。

echo "<html><head></head><body>
<a href='index.php?".htmlspecialchars($querystring, ENT_QUOTES)."'>test</a>
</body></html>"

但是,您仍然应该将任何参数列入白名单,因为聪明的攻击者可以伪造查询并尝试CSRF攻击。

假设您在 PHP 5.x 中将查询参数作为变量访问,如下所示,但容易出现 XSS

易受 XSS 攻击

<?php
// http://example.com/mypage.php?a=hi&b=wow&c=<script type='text/javascript'>alert('XSS Attacked!');</script>

try{
    $q = $_SERVER['QUERY_STRING'];
    parse_str( $q, $arr );
    extract($arr);
    echo '<pre>';
    echo 'a is = ' . $a;
    echo PHP_EOL;
    echo 'b is = ' . $b;
    echo PHP_EOL;
    echo 'c is = ' . $c;
    echo '</pre>';

}
catch(Exception $e){
    error_log($e->getMessage());
}


?>

防止来自 $_SERVER['QUERY_STRING'] 的 XSS

为了防止来自$_SERVER['QUERY_STRING'] XSS,

  • 使用htmlentities读取$_SERVER['QUERY_STRING']并使用html_entity_decode解码查询字符串。
  • 使用parse_str提取查询参数的键值数组。
  • 使用filter_var_array过滤和清理数组,将数组作为第一个参数进行清理,将FILTER_SANITIZE_ENCODED作为第二个参数。
  • 使用extract来制作具有各自值的键 php 变量。
<?php
// http://example.com/mypage.php?a=hi&b=wow&c=<script type='text/javascript'>alert('XSS Attacked!');</script>
try{
    $q = htmlentities($_SERVER['QUERY_STRING']);
    parse_str( html_entity_decode($q), $arr );
    $arr=filter_var_array($arr, FILTER_SANITIZE_ENCODED);
    extract($arr);
    echo '<pre>';
    echo 'a is = ' . $a;
    echo PHP_EOL;
    echo 'b is = ' .  $b;
    echo PHP_EOL;
    echo 'c is = ' .  $c;
    echo '</pre>';

}
catch(Exception $e){
    error_log($e->getMessage());
}

?>

在这种情况下,您应该使用 urlencode 函数。

例如,当您要在链接的标题处输出查询参数的值时,htmlspecialchars/htmlentities 更合适,但在 href/src 属性处不输出。

您可以使用多种方式清理查询,但这不是这样做的地方。 即使您通过 GET 发送安全查询,有人也可以更改地址栏上的查询或使用篡改数据。 您必须清理 index.php(或处理数据的任何地方)。 如果您使用的是 MySQL,则必须以这种方式进行清理:

$field = mysql_real_scape($_GET['field']);

暂无
暂无

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

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