织梦DedeCMS 如何修补XSS跨站脚本攻击

[复制链接]
查看1973 | 回复1 | 2019-8-23 20:58 | 显示全部楼层 |阅读模式
XSS又叫CSS (Cross Site Script) ,跨站脚本攻击。它指的是恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意攻击用户的特殊目的。XSS属于被动式的攻击,因为其被动且不好利用,所以许多人常忽略其危害性。而本文主要讲的是利用XSS得到目标服务器的shell。技术虽然是老技术,但是其思路希望对大家有帮助。

  首先打开文件:/include/common.func.php

  在文件中加入函数:
Copy to ClipboardLiehuo.Net Codes引用的内容:
  1. function RemoveXSS($val) {
  2. $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  3. $search = 'abcdefghijklmnopqrstuvwxyz';
  4. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  5. $search .= '1234567890!@#$%^&*()';
  6. $search .= '~`";:?+/={}[]-_|\'\\';
  7. for ($i = 0; $i < strlen($search); $i++) {
  8. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val);
  9. $val = preg_replace('/<span style="background-color: rgb(255, 255, 255);">(&#0{0,8}'</span>.ord($search[$i]).';?)/', $search[$i], $val);
  10. }
  11. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  12. $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
  13. $ra = array_merge($ra1, $ra2);

  14. $found = true;
  15. while ($found == true) {
  16. $val_before = $val;
  17. for ($i = 0; $i < sizeof($ra); $i++) {
  18. $pattern = '/';
  19. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  20. if ($j > 0) {
  21. $pattern .= '(';
  22. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  23. $pattern .= '|';
  24. $pattern .= '|<span style="background-color: rgb(255, 255, 255);">(&#0{0,8}</span>([9|10|13]);)';
  25. $pattern .= ')*';
  26. }
  27. $pattern .= $ra[$i][$j];
  28. }
  29. $pattern .= '/i';
  30. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
  31. $val = preg_replace($pattern, $replacement, $val);
  32. if ($val_before == $val) {
  33. $found = false;
  34. }
  35. }
  36. }
  37. return $val;
  38. }
复制代码


然后在过滤显示的标签中修改,例如:/templets/default/search.htm,将:

Copy to ClipboardLiehuo.Net Codes引用的内容:
<p>
搜索 <strong class="fc_03c">{dede:global name='keyword'/}</strong> 的结果
</p>
修改为:

Copy to ClipboardLiehuo.Net Codes引用的内容:
<p>
搜索 <strong class="fc_03c">{dede:global name='keyword' function='RemoveXSS(@me)'/}</strong> 的结果
</p>
这样就可以了,当然其他地方显示的地方也可以通过这个函数过滤掉。

香蕉 | 2019-8-23 21:06 | 显示全部楼层
不错,支持一下,教程网!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-16