-1

我有一个用 PHP 编码的网站搜索。onkeyup它本质上是一个 PHP-AJAX 搜索,在搜索输入字段的事件中触发。使用 PHP 的函数触发对 PHP 文件的 AJAX 调用,该文件读取包含索引的文件onkeyupindexes-file.txtfile()

虽然,这里我不是在处理数据库,所以我认为没有 SQL 注入或 XSS 攻击的机会(如果我错了,请纠正我)。

此外,我了解mysqli_real_escape_string()htmlentities()功能、它们的重要性和用例。我想知道的是这种特定的 PHP-AJAX 方法是否易受攻击。

此外,除了服务器端漏洞之外,这种情况下是否存在任何其他类型的漏洞?

onkeyup功能是:

function results(str) {
  var search_term = $("#search")
    .val()
    .trim();

  if (search_term == "") {
    // ...
  } else {
    $.ajax({
      url: "websearch.php",
      type: "post",
      data: {
        string: search_term
      },
      dataType: "json",
      success: function(returnData) {
        for(var i in returnData) {
                for(var j in returnData[i]) {
                    $('#results').append('<div><a target="_blank" href="'+returnData[i][j]+'">'+Object.keys(returnData[i])+'</a></div>');
                }
            }
      }
    });
  }
}

包含indexes-file.txt

books*books.php  
newspaper*newspaper.php  
download manual*manual.php  
...

我的websearch.php文件包含:

<?php
    error_reporting(0);
    $indexes = 'indexes-file.txt';
    $index_array = file($indexes, FILE_IGNORE_NEW_LINES);

    foreach($index_array as $st) {
        $section = explode('*', $st);
        $k = $section[0];
        $kklink = $section[1];
        $l_arr[] = array($k => $kklink);
    }

    //Get the search term from "string" POST variable.
    $var1 = isset($_POST['string']) ? trim($_POST['string']) : '';

    $webresults = array();

    //Loop through our lookup array.

    foreach($l_arr as $kk){
        //If the search term is present.
         if(stristr(key($kk), $var1)){
             //Add it to the results array.
            foreach($kk as $value) {
                 $webresults[] = array(key($kk) => $value);
            }
        }
     }

    //Display the results in JSON format so to parse it with JavaScript.
    echo json_encode($webresults);
?>
4

1 回答 1

0

如果您不处理数据库,它可能不容易受到 sql 注入的攻击,但它可能容易受到 xss 的攻击,以防止 xss 和脚本执行,您应该使用:

防止 XSS

PHP htmlentities() 函数

过滤输入的基本示例

<?php 

echo '<script>alert("vulnerable");</script>'; //vulnerable to xss
?>

使用 htmlentities() 过滤输入

<?php 
$input = '<script>alert("vulnerable");</script>';
echo  htmlentities($input); //not vulnerable to external input code injection scripts
?>

因此它可以防止脚本和 html 标签注入在站点上执行 阅读更多here

对于数据库,您应该将 pdo 与准备好的语句一起使用

防止 SQL 注入

使用 PDO 正确设置连接 注意,当使用 PDO 访问 MySQL 数据库时,默认情况下不使用实际准备好的语句。要解决此问题,您必须禁用准备好的语句的模拟。使用 PDO 创建连接的示例是:

$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'password');

$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

在这里阅读更多

修复你的代码

<?php
    error_reporting(0);
    $indexes = 'indexes-file.txt';
    $index_array = file($indexes, FILE_IGNORE_NEW_LINES);

    foreach($index_array as $st) {
        $section = explode('*', $st);
        $k = $section[0];
        $kklink = $section[1];
        $l_arr[] = array($k => $kklink);
    }

    //Get the search term from "string" POST variable.
    $var1 = isset($_POST['string']) ? trim($_POST['string']) : '';

    $webresults = array();

    //Loop through our lookup array.

    foreach($l_arr as $kk){
        //If the search term is present.
         if(stristr(key($kk), $var1)){
             //Add it to the results array.
            foreach($kk as $value) {
                 $webresults[] = array(key($kk) => $value);
            }
        }
     }

    //Display the results in JSON format so to parse it with JavaScript.
   echo htmlentities(json_encode($webresults));
    //fixed 

?>

每次您从外部使用回声时htmlentities

echo htmlentities(json_encode($webresults));

我用演示 json 字符串测试的你的数组问题它工作正常

<?php 
$webresults = 
'
{  "aliceblue": "#f0f8ff",
  "antiquewhite": "#faebd7",
  "aqua": "#00ffff",
  "aquamarine": "#7fffd4",
  "azure": "#f0ffff",
  "beige": "#f5f5dc",
  "bisque": "#ffe4c4",
  "black": "#000000",

}';

echo htmlentities(json_encode($webresults));

 ?>
于 2019-07-29T19:22:50.573 回答