說明
在doFilter
設(shè)置XSS
過濾防護(hù)的方案可以在請求入口處做統(tǒng)一過濾屑柔,是一個(gè)能解決根本問題的方案联贩。
關(guān)于過濾規(guī)則在第3個(gè)文件歹茶,其防護(hù)規(guī)則可以繼續(xù)補(bǔ)充完善笛丙,也可以根據(jù)實(shí)際情況進(jìn)行修改漾脂。
參考鏈接:https://www.cnblogs.com/yangxiong/p/8204038.html(預(yù)防XSS攻擊,(參數(shù)/響應(yīng)值)特殊字符過濾 - 余生莫度 - 博客園)
代碼參考
- IllegalCharacterFilter.java
package com.lenovo.common.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
* 非法字符過濾器胚鸯,用來處理request.getParamater中的非法字符骨稿。如<script>alert('123');</script>
*/
public class IllegalCharacterFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
request = new MHttpServletRequest(request);
chain.doFilter(request, res);
}
@Override
public void destroy() {}
}
- MHttpServletRequest.java
package com.lenovo.common.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import com.lenovo.common.utils.XssShieldUtil;
/**
* 參數(shù)特殊字符過濾
*/
public class MHttpServletRequest extends HttpServletRequestWrapper {
public MHttpServletRequest(HttpServletRequest request) {
super(request);
}
@Override
public String getParameter(String name) {
// 返回值之前 先進(jìn)行過濾
return XssShieldUtil.stripXss(super.getParameter(XssShieldUtil.stripXss(name)));
}
@Override
public String[] getParameterValues(String name) {
// 返回值之前 先進(jìn)行過濾
String[] values = super.getParameterValues(XssShieldUtil.stripXss(name));
if(values != null){
for (int i = 0; i < values.length; i++) {
values[i] = XssShieldUtil.stripXss(values[i]);
}
}
return values;
}
}
- XssShieldUtil.java
package com.lenovo.common.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
/**
* 處理非法字符
*/
public class XssShieldUtil {
private static List<Pattern> patterns = null;
private static List<Object[]> getXssPatternList() {
List<Object[]> ret = new ArrayList<Object[]>();
ret.add(new Object[]{"<(no)?script[^>]*>.*?</(no)?script>", Pattern.CASE_INSENSITIVE});
ret.add(new Object[]{"eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
ret.add(new Object[]{"expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
ret.add(new Object[]{"(javascript:|vbscript:|view-source:)*", Pattern.CASE_INSENSITIVE});
ret.add(new Object[]{"<(\"[^\"]*\"|\'[^\']*\'|[^\'\">])*>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
ret.add(new Object[]{"(window\\.location|window\\.|\\.location|document\\.cookie|document\\.|alert\\(.*?\\)|window\\.open\\()*", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
ret.add(new Object[]{"<+\\s*\\w*\\s*(oncontrolselect|oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|ondragover|ondragstart|ondrop|onerror=|onerroupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmousout|onmouseover|onmouseup|onmousewheel|onmove|onmoveend|onmovestart|onabort|onactivate|onafterprint|onafterupdate|onbefore|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditocus|onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|onpaste|onpropertychange|onreadystatechange|onreset|onresize|onresizend|onresizestart|onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload)+\\s*=+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL});
return ret;
}
private static List<Pattern> getPatterns() {
if (patterns == null) {
List<Pattern> list = new ArrayList<Pattern>();
String regex = null;
Integer flag = null;
int arrLength = 0;
for(Object[] arr : getXssPatternList()) {
arrLength = arr.length;
for(int i = 0; i < arrLength; i++) {
regex = (String)arr[0];
flag = (Integer)arr[1];
list.add(Pattern.compile(regex, flag));
}
}
patterns = list;
}
return patterns;
}
public static String stripXss(String value) {
if(StringUtils.isNotBlank(value)) {
Matcher matcher = null;
for(Pattern pattern : getPatterns()) {
matcher = pattern.matcher(value);
if(matcher.find()) { // 匹配
value = matcher.replaceAll(""); // 刪除相關(guān)字符串
}
}
value = value.replaceAll("<", "<").replaceAll(">", ">");
}
return value;
}
/**
* 測試案例(可刪除)
*/
public static void main(String[] args) {
String value = null;
value = XssShieldUtil.stripXss("<script language=text/javascript>alert(document.cookie);</script>");
System.out.println("type-1: '" + value + "'");
value = XssShieldUtil.stripXss("<script src='' onerror='alert(document.cookie)'></script>");
System.out.println("type-2: '" + value + "'");
value = XssShieldUtil.stripXss("</script>");
System.out.println("type-3: '" + value + "'");
value = XssShieldUtil.stripXss(" eval(abc);");
System.out.println("type-4: '" + value + "'");
value = XssShieldUtil.stripXss(" expression(abc);");
System.out.println("type-5: '" + value + "'");
value = XssShieldUtil.stripXss("<img src='' onerror='alert(document.cookie);'></img>");
System.out.println("type-6: '" + value + "'");
value = XssShieldUtil.stripXss("<img src='' onerror='alert(document.cookie);'/>");
System.out.println("type-7: '" + value + "'");
value = XssShieldUtil.stripXss("<img src='' onerror='alert(document.cookie);'>");
System.out.println("type-8: '" + value + "'");
value = XssShieldUtil.stripXss("<script language=text/javascript>alert(document.cookie);");
System.out.println("type-9: '" + value + "'");
value = XssShieldUtil.stripXss("<script>window.location='url'");
System.out.println("type-10: '" + value + "'");
value = XssShieldUtil.stripXss(" onload='alert(\"abc\");");
System.out.println("type-11: '" + value + "'");
value = XssShieldUtil.stripXss("<img src=x<!--'<\"-->>");
System.out.println("type-12: '" + value + "'");
value = XssShieldUtil.stripXss("<=img onstop=");
System.out.println("type-13: '" + value + "'");
}
}