如何在php中修补XSS漏洞_PHP

WBOY
Release: 2016-06-01 12:20:55
Original
1235 people have browsed it

XSS漏洞

在PHP中修补XSS漏洞,我们可以使用三个PHP函数。

这些函数主要用于清除HTML标志,这样就没办法注入代码了。使用更多的函数是htmlspecialchars() ,它可以将所有的""符号转换成";"。其它可供选择的函数还有htmlentities(), 它可以用相应的字符实体(entities)替换掉所有想要替换掉的特征码(characters)。

PHP Code:

// 这里的代码主要用于展示这两个函数之间输出的不同

$input = '';

echo htmlspecialchars($input) . '
';

echo htmlentities($input);

?>

htmlentities()的另一个例子

PHP Code:

$str = "A 'quote' is bold";

echo htmlentities($str);

echo htmlentities($str, ENT_QUOTES);

?>

第一个显示: A 'quote' is bold

第二个显示:A 'quote' is bold

htmlspecialchars()使用实例

PHP Code:

$new = htmlspecialchars("Test", ENT_QUOTES);

echo $new;

?>

显示: Test

strip_tags()函数代替.删除所有的HTML元素(elements),除了需要特别允许的元素之外,如:, 或

.

strip_tags()使用实例

PHP Code:

$text = '

Test paragraph.

Other text';
echo strip_tags($text);

echo "\n";

// allow


echo strip_tags($text, '

');

?>

现在我们至少已经知道有这些函数了,当我们发现我们的站点存在XSS漏洞时就可以使用这些代码了。我最近在我的站点上的GoogleBig(一个Mybb论坛的插件)视频部分发现了一个XSS漏洞,因此我就在想如何使用这些函数写段代码来修补这个搜索漏洞。

首先我发现问题出在search.php这一文件上,现在让我们看看这个查询及输出查询结果中的部分代码研究一下:

PHP Code:

function search($query, $page)

{

global $db, $bgcolor2, $bgcolor4, $sitename, $io_db, $module_url, $list_page_items, $hm_index;

$option = trim($option);

$query = trim($query);

$query = FixQuotes(nl2br(filter_text($query)));

$db->escape_string($query);

$db->escape_string($option);

alpha_search($query);

...

在这种情况下,我们通过使用$query这一值作为变量,然后使用htmlentities()这一函数:

PHP Code:

$query = FixQuotes(nl2br(filter_text(htmlentities($query))));

如果你对这三种函数还有有疑问可以使用PHP手册来查看:

http://it.php.net/htmlentities

http://it2.php.net/htmlspecialchars

http://it2.php.net/strip_tags

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!