In fact, I wanted to talk about this topic a long time ago. I found that many domestic PHP sites have XSS vulnerabilities. I accidentally saw an XSS vulnerability in PHP5 today, so I’d like to summarize it here. By the way, friends who use PHP5 are best to apply a patch or upgrade.
If you don’t understand what XSS is, you can read here or here (the Chinese version may be easier to understand).
Many domestic forums have cross-site scripting vulnerabilities. For example, here is a Google Hack+XSS attack example, targeting Discuz 4.0.0RC3. There are many such examples abroad, and even Google has appeared, but it was corrected in early December. Cross-site attacks are easy to construct and are very subtle and difficult to detect (usually stealing information and immediately jumping back to the original page).
I won’t explain here how to attack (and don’t ask me), but mainly how to prevent it. First of all, cross-site scripting attacks are caused by the lack of strict filtering of user input, so we must intercept possible dangers before all data enters our website and database. For illegal HTML codes including single and double quotes, you can use htmlentities().
$str = "A 'quote' is bold";
// Outputs: A 'quote' is < b>bold
echo htmlentities($str);
// Outputs: A 'quote' is bold
echo htmlentities($str, ENT_QUOTES);
?>
This will invalidate illegal scripts.
But please note that the default encoding of htmlentities() is ISO-8859-1. If your illegal script is encoded in other formats, it may not be filtered out, but the browser can recognize and execute it. I will first find a few sites to test this issue before talking about it.
这里提供一个过滤非法脚本的函数:
function RemoveXSS($val) {
// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
// this prevents some character re-spacing such as