提问人:Kaushal Shakya 提问时间:7/29/2019 最后编辑:Kaushal Shakya 更新时间:7/30/2019 访问量:340
使用PHP编码的网站搜索(使用.txt文件作为索引文件)是否容易受到任何攻击(如SQL注入和XSS)?
Whether a website search coded with PHP (with .txt file as indexing file) vulnerable to any attacks (like SQL Injection & XSS)?
问:
我有一个用PHP编码的网站搜索。它本质上是一个 PHP-AJAX 搜索,在搜索输入字段的事件上触发。触发对 PHP 文件的 AJAX 调用,该文件使用 PHP 的函数读取包含索引的文件。onkeyup
onkeyup
indexes-file.txt
file()
虽然,这里我不是在处理数据库,所以我认为没有机会进行SQL注入或XSS攻击(如果我错了,请纠正我)。
此外,我还了解和功能、它们的重要性和用例。我想知道的是这个特定的 PHP-AJAX 方法是否容易受到攻击。mysqli_real_escape_string()
htmlentities()
此外,除了服务器端漏洞之外,此类情况下是否还存在任何其他类型的漏洞?
功能是: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 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);
?>
答:
如果你不处理数据库,它可能不容易受到 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 标签注入在现场执行 阅读更多内容
对于数据库,应将 PDO 与预准备语句一起使用
防止 SQL 注入
使用正确设置连接 请注意,使用 PDO 访问 MySQL 数据库时,默认情况下不使用真正的准备语句。要解决此问题,您必须禁用预准备语句的模拟。使用 PDO 创建连接的示例如下: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));
?>
评论
htmlentities
评论
$var1
php.ini