赞
踩
概念:存储型XSS是指应用程序通过Web请求获取不可信赖的数据,并且在未检验数据是否存在XSS代码的情况下,将其存入数据库。当程序下一次从数据库中获取该数据时,致使页面再次执行XSS代码。存储型XSS可以持续攻击用户,在用户提交了包含XSS代码的数据存储到数据库后,每当用户在浏览网页查询对应数据库中的数据时,那些包含XSS代码的数据就会在服务器解析并加载,当浏览器读到XSS代码后,会当做正常的HTML和JS解析并执行,于是发生存储型XSS攻击。
例:下面JSP代码片段的功能是根据一个已知用户雇员ID(id)从数据库中查询出该用户的地址,并显示在JSP页面上。
<%
...
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from users where id =" + id);
String address = null;
if (rs != null) {
rs.next();
address = rs.getString("address");
}
%>
家庭地址: <%= address %>
如果address的值是由用户提供的,且存入数据库时没有进行合理的校验,那么攻击者就可以利用上面的代码进行存储型XSS攻击。
修复建议:为了避免存储型XSS攻击,建议采用以下方式进行防御:
<、>、'、"
以及<script>、javascript
等进行过滤。//HTML encode
ESAPI.encoder().encodeForHTML(inputData);
//HTML attribute encode
ESAPI.encoder().encodeForHTMLAttribute(inputData);
//JavaScript encode
ESAPI.encoder().encodeForJavaScript(inputData);
//CSS encode
ESAPI.encoder().encodeForCSS(inputData);
//URL encode
ESAPI.encoder().encodeForURL(inputData);
response.setHeader("Set-Cookie","cookiename=cookievalue; path=/; Domain=domainvaule; Max-age=seconds; HttpOnly");
概念:反射型XSS是指应用程序通过Web请求获取不可信赖的数据,并在未检验数据是否存在恶意代码的情况下,将其发送给用户。反射型XSS一般可以由攻击者构造带有恶意代码参数的URL来实现,在构造的URL地址被打开后,其中包含的恶意代码参数被浏览器解析和执行。这种攻击的特点是非持久化,必须用户点击包含恶意代码参数的链接时才会触发。
例:下面JSP代码片段的功能是从HTTP请求中读取输入的用户名(username)并显示到页面。
<%
String name= request.getParameter("username"); %>
...
姓名: <%= name%>
如果name里有包含恶意代码,那么Web浏览器就会像显示HTTP响应那样执行该代码,应用程序将受到反射型XSS攻击。
修复建议:为了避免反射型XSS攻击,建议采用以下方式进行防御:
<、>、'、"
以及<script>、javascript
等进行过滤。//HTML encode
ESAPI.encoder().encodeForHTML(inputData);
//HTML attribute encode
ESAPI.encoder().encodeForHTMLAttribute(inputData);
//JavaScript encode
ESAPI.encoder().encodeForJavaScript(inputData);
//CSS encode
ESAPI.encoder().encodeForCSS(inputData);
//URL encode
ESAPI.encoder().encodeForURL(inputData);
response.setHeader("Set-Cookie","cookiename=cookievalue; path=/; Domain=domainvaule; Max-age=seconds; HttpOnly");
import org.owasp.esapi.ESAPI;
import org.springframework.core.codec.EncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 跨站脚本和存储型XSS攻击防御工具类
* @author zyw
* @date 16:52:11
*/
public class XSSUtils {
public static Pattern scriptPattern1 = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
public static Pattern scriptPattern2 = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static Pattern scriptPattern3 = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static Pattern scriptPattern4 = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
public static Pattern scriptPattern5 = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static Pattern scriptPattern6 = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static Pattern scriptPattern7 = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static Pattern scriptPattern8 = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
public static Pattern scriptPattern9 = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
public static Pattern scriptPattern10 = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
public static Pattern scriptPattern11 = Pattern.compile(".*<.*", Pattern.CASE_INSENSITIVE );
public static Pattern scriptPattern12 = Pattern.compile("<.*>.*</.*>", Pattern.CASE_INSENSITIVE);
public static String striptXSS(String value) {
if (value != null) {
//脚本匹配
Matcher matcher = scriptPattern12.matcher(value);
if (matcher.find()) {
//截取匹配的部分
String group = matcher.group();
//采用OWASP ESAPI对数据输出HTML上下文中不同位置
value = value.replace(group, owaspEsapi(group));
}
value = value.replaceAll("", "");
value = scriptPattern1.matcher(value).replaceAll("");
value = scriptPattern2.matcher(value).replaceAll("");
value = scriptPattern3.matcher(value).replaceAll("");
value = scriptPattern4.matcher(value).replaceAll("");
value = scriptPattern5.matcher(value).replaceAll("");
value = scriptPattern6.matcher(value).replaceAll("");
value = scriptPattern7.matcher(value).replaceAll("");
value = scriptPattern8.matcher(value).replaceAll("");
value = scriptPattern9.matcher(value).replaceAll("");
value = scriptPattern10.matcher(value).replaceAll("");
value = scriptPattern11.matcher(value).replaceAll("");
}
return value;
}
/**
* 采用OWASP ESAPI对数据输出HTML上下文中不同位置
*
* @param data
* @return
* @throws EncodingException
*/
public static String owaspEsapi(String data){
try {
//HTML encode
data = ESAPI.encoder().encodeForHTML(data);
//HTML attribute encode
data = ESAPI.encoder().encodeForHTMLAttribute(data);
//JavaScript encode
data = ESAPI.encoder().encodeForJavaScript(data);
//CSS encode
data = ESAPI.encoder().encodeForCSS(data);
//URL encode
data = ESAPI.encoder().encodeForURL(data);
}catch (EncodingException e){
}finally {
return data;
}
}
}
注:这里需要对传输参数为文件的接口做针对性的处理,同时接口中的参数形式也分很多种,需要
/**
* 请求过滤业务实现类
*/
@Slf4j
public class XssRequestWrappers extends HttpServletRequestWrapper {
@Getter
@Setter
private String requestBodyStr;
//用于将流保存下来
private byte[] requestBody;
private CommonsMultipartResolver multiparResolver = new CommonsMultipartResolver();
public XssRequestWrappers(HttpServletRequest request) {
super(request);
String type = request.getHeader("Content-Type");
//处理上传文件接口
if (!StringUtils.isEmpty(type) && type.contains("multipart/form-data")) {
MultipartHttpServletRequest multipartHttpServletRequest = multiparResolver.resolveMultipart(request);
Map<String, String[]> stringMap = multipartHttpServletRequest.getParameterMap();
if (!stringMap.isEmpty()) {
for (String key : stringMap.keySet()) {
String value = multipartHttpServletRequest.getParameter(key);
XSSUtils.striptXSS(key);
XSSUtils.striptXSS(value);
}
}
super.setRequest(multipartHttpServletRequest);
} else {
//其他接口
//获取所有参数键值对的map集合
Map parameterMap = request.getParameterMap();
parameterMap = getParameterMap();
//获取请求的body
try {
requestBody = StreamUtils.copyToByteArray(request.getInputStream());
String requestBodyStr = XSSUtils.striptXSS(IOUtils.toString(requestBody, "utf-8"));
requestBody = requestBodyStr.getBytes("UTF8");
}catch (IOException e){
}
}
}
@Override
public ServletInputStream getInputStream() {
final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener listener) {
}
@Override
public int read() {
return bais.read();
}
};
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@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] = XSSUtils.striptXSS(values[i]);
}
return encodedValues;
}
@Override
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
return XSSUtils.striptXSS(value);
}
@Override
public String getHeader(String name) {
String value = super.getHeader(name);
return XSSUtils.striptXSS(value);
}
@Override
public Map<String, String[]> getParameterMap() {
Map<String, String[]> map1 = super.getParameterMap();
Map<String, String[]> escapseMap = new HashMap<String, String[]>();
Set<String> keys = map1.keySet();
for (String key : keys) {
String[] valArr = map1.get(key);
if (valArr != null && valArr.length > 0) {
String[] escapseValArr = new String[valArr.length];
for (int i = 0; i < valArr.length; i++) {
String escapseVal = XSSUtils.striptXSS(valArr[i]);
escapseValArr[i] = escapseVal;
}
escapseMap.put(key, escapseValArr);
}
}
return escapseMap;
}
}
```java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* 返回值输出代理类
*
* @Title: ResponseWrapper
* @Description:
* @author zyw
* @date 9:52:11
*/
public class ResponseWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
public ResponseWrapper(HttpServletResponse httpServletResponse)
{
super(httpServletResponse);
buffer = new ByteArrayOutputStream();
out = new WrapperOutputStream(buffer);
}
@Override
public ServletOutputStream getOutputStream()
throws IOException
{
return out;
}
@Override
public void flushBuffer()
throws IOException
{
if (out != null)
{
out.flush();
}
}
public byte[] getContent()
throws IOException
{
flushBuffer();
return buffer.toByteArray();
}
class WrapperOutputStream extends ServletOutputStream
{
private ByteArrayOutputStream bos;
public WrapperOutputStream(ByteArrayOutputStream bos)
{
this.bos = bos;
}
@Override
public void write(int b)
throws IOException
{
bos.write(b);
}
@Override
public boolean isReady()
{
// TODO Auto-generated method stub
return false;
}
@Override
public void setWriteListener(WriteListener arg0)
{
// TODO Auto-generated method stub
}
}
}
/**
* 存储型XSS、跨站脚本过滤器
*
* @author zyw
* @since 2022-09-01 20:09:31
*/
@Component
@WebFilter(filterName = "XSSFilter",
/**
* 通配符(*)表示对所有的web资源进行拦截
*/
urlPatterns = "/*"
)
public class XSSFilter implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {
System.out.println("初始化过滤器!");
}
@Override
public void destroy() {
System.out.println("销毁过滤器!");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
PrintWriter writer = response.getWriter();
//过滤请求
XssRequestWrappers xssRequestWrappers = new XssRequestWrappers((HttpServletRequest) request);
//过滤返回
ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse) response);//转换成代理类
chain.doFilter(xssRequestWrappers, wrapperResponse);
byte[] content = wrapperResponse.getContent();//获取返回值
//判断是否有值
if (content.length > 0) {
String str = new String(content, "UTF-8");
String ciphertext = null;
try {
//......根据需要处理返回值
ciphertext = XSSUtils.striptXSS(str);
} catch (Exception e) {
e.printStackTrace();
}
//把返回值输出到客户端
writer.append(ciphertext);
writer.close();
}
}
}
可以看出已对其进行恰当的输出编码
可以看到已对其进行恰当的输出编码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。