XSS攻击的危害性
,也通过
模拟案例了解了XSS攻击原理
,这里就介绍一下如何防御XSS攻击。
采用Filter技术,对所有参数都进行过滤,处理方案为:
1. 含有html标签做转义。
2. 含有敏感的html脚本,直接处理掉。
XSS Filter源码:
package com.what21.filter.xss;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
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;
public class XSSFilter implements Filter {
// XSS处理Map
private static Map<String,String> xssMap = new LinkedHashMap<String,String>();
public void init(FilterConfig filterConfig) throws ServletException {
// 含有脚本: script
xssMap.put("[s|S][c|C][r|R][i|C][p|P][t|T]", "");
// 含有脚本 javascript
xssMap.put("[\\\"\\\'][\\s]*[j|J][a|A][v|V][a|A][s|S][c|C][r|R][i|I][p|P][t|T]:(.*)[\\\"\\\']", "\"\"");
// 含有函数: eval
xssMap.put("[e|E][v|V][a|A][l|L]\\((.*)\\)", "");
// 含有符号 <
xssMap.put("<", "<");
// 含有符号 >
xssMap.put(">", ">");
// 含有符号 (
xssMap.put("\\(", "(");
// 含有符号 )
xssMap.put("\\)", ")");
// 含有符号 '
xssMap.put("'", "'");
// 含有符号 "
xssMap.put("\"", """);
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 强制类型转换 HttpServletRequest
HttpServletRequest httpReq = (HttpServletRequest)request;
// 构造HttpRequestWrapper对象处理XSS
HttpRequestWrapper httpReqWarp = new HttpRequestWrapper(httpReq,xssMap);
//
chain.doFilter(httpReqWarp, response);
}
public void destroy() {
}
}
package com.what21.filter.xss;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public final class HttpRequestWrapper extends HttpServletRequestWrapper {
private Map<String, String> xssMap;
public HttpRequestWrapper(HttpServletRequest request) {
super(request);
}
public HttpRequestWrapper(HttpServletRequest request,
Map<String, String> xssMap) {
super(request);
this.xssMap = xssMap;
}
@Override
public String[] getParameterValues(String parameter) {
String[] values = super.getParameterValues(parameter);
if (values == null) {
return null;
}
int count = values.length;
// 遍历每一个参数,检查是否含有
String[] encodedValues = new String[count];
for (int i = 0; i < count; i++) {
encodedValues[i] = cleanXSS(values[i]);
}
return encodedValues;
}
@Override
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
/**
* 清除恶意的XSS脚本
*
* @param value
* @return
*/
private String cleanXSS(String value) {
Set<String> keySet = xssMap.keySet();
for(String key : keySet){
String v = xssMap.get(key);
value = value.replaceAll(key,v);
}
return value;
}
}
<filter>
<filter-name>XSSFilter</filter-name>
<filter-class>com.what21.filter.xss.XSSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XSSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
,也通过
模拟案例了解了XSS攻击原理
,这里就介绍一下如何防御XSS攻击。
采用Filter技术,对所有参数都进行过滤,处理方案为:
1. 含有html标签做转义。
2. 含有敏感的html脚本,直接处理掉。
XSS Filter源码:
package com.what21.filter.xss;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
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;
public class XSSFilter implements Filter {
// XSS处理Map
private static Map<String,String> xssMap = new LinkedHashMap<String,String>();
public void init(FilterConfig filterConfig) throws ServletException {
// 含有脚本: script
xssMap.put("[s|S][c|C][r|R][i|C][p|P][t|T]", "");
// 含有脚本 javascript
xssMap.put("[\\\"\\\'][\\s]*[j|J][a|A][v|V][a|A][s|S][c|C][r|R][i|I][p|P][t|T]:(.*)[\\\"\\\']", "\"\"");
// 含有函数: eval
xssMap.put("[e|E][v|V][a|A][l|L]\\((.*)\\)", "");
// 含有符号 <
xssMap.put("<", "<");
// 含有符号 >
xssMap.put(">", ">");
// 含有符号 (
xssMap.put("\\(", "(");
// 含有符号 )
xssMap.put("\\)", ")");
// 含有符号 '
xssMap.put("'", "'");
// 含有符号 "
xssMap.put("\"", """);
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 强制类型转换 HttpServletRequest
HttpServletRequest httpReq = (HttpServletRequest)request;
// 构造HttpRequestWrapper对象处理XSS
HttpRequestWrapper httpReqWarp = new HttpRequestWrapper(httpReq,xssMap);
//
chain.doFilter(httpReqWarp, response);
}
public void destroy() {
}
}
package com.what21.filter.xss;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public final class HttpRequestWrapper extends HttpServletRequestWrapper {
private Map<String, String> xssMap;
public HttpRequestWrapper(HttpServletRequest request) {
super(request);
}
public HttpRequestWrapper(HttpServletRequest request,
Map<String, String> xssMap) {
super(request);
this.xssMap = xssMap;
}
@Override
public String[] getParameterValues(String parameter) {
String[] values = super.getParameterValues(parameter);
if (values == null) {
return null;
}
int count = values.length;
// 遍历每一个参数,检查是否含有
String[] encodedValues = new String[count];
for (int i = 0; i < count; i++) {
encodedValues[i] = cleanXSS(values[i]);
}
return encodedValues;
}
@Override
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
/**
* 清除恶意的XSS脚本
*
* @param value
* @return
*/
private String cleanXSS(String value) {
Set<String> keySet = xssMap.keySet();
for(String key : keySet){
String v = xssMap.get(key);
value = value.replaceAll(key,v);
}
return value;
}
}
<filter>
<filter-name>XSSFilter</filter-name>
<filter-class>com.what21.filter.xss.XSSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XSSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
| |