parent
e1ac746f3f
commit
06dab595a5
@ -0,0 +1,65 @@
|
||||
package org.opsli.core.waf.filter;
|
||||
|
||||
|
||||
import org.opsli.core.waf.servlet.WafHttpServletRequestWrapper;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 防火墙
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2020-10-09
|
||||
*/
|
||||
public class WafFilter implements Filter {
|
||||
|
||||
private boolean enableXssFilter = false;
|
||||
private boolean enableSqlFilter = false;
|
||||
|
||||
private List<String> urlExclusion;
|
||||
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig config) throws ServletException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
String servletPath = httpServletRequest.getServletPath();
|
||||
|
||||
// 如果是排除url 则放行
|
||||
if (urlExclusion != null && urlExclusion.contains(servletPath)) {
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
// 执行过滤
|
||||
chain.doFilter(
|
||||
new WafHttpServletRequestWrapper((HttpServletRequest) request, enableXssFilter, enableSqlFilter),
|
||||
response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
// ============================
|
||||
|
||||
|
||||
public void setEnableXssFilter(boolean enableXssFilter) {
|
||||
this.enableXssFilter = enableXssFilter;
|
||||
}
|
||||
|
||||
public void setEnableSqlFilter(boolean enableSqlFilter) {
|
||||
this.enableSqlFilter = enableSqlFilter;
|
||||
}
|
||||
|
||||
public void setUrlExclusion(List<String> urlExclusion) {
|
||||
this.urlExclusion = urlExclusion;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package org.opsli.core.waf.util;
|
||||
|
||||
import org.opsli.common.exception.WafException;
|
||||
import org.opsli.core.msg.CoreMsg;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* SQL过滤
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2020-10-09
|
||||
*/
|
||||
public final class SQLFilterKit {
|
||||
|
||||
/**
|
||||
* SQL注入过滤
|
||||
*
|
||||
* @param str 待验证的字符串
|
||||
*/
|
||||
public static String stripSQL(String str) {
|
||||
if (StringUtils.isEmpty(str)) {
|
||||
return null;
|
||||
}
|
||||
//去掉'|"|;|\字符
|
||||
str = StringUtils.replace(str, "'", "");
|
||||
str = StringUtils.replace(str, "\"", "");
|
||||
str = StringUtils.replace(str, ";", "");
|
||||
str = StringUtils.replace(str, "\\", "");
|
||||
|
||||
//转换成小写
|
||||
str = str.toLowerCase();
|
||||
|
||||
//非法字符
|
||||
String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alter", "drop"};
|
||||
|
||||
//判断是否包含非法字符
|
||||
for (String keyword : keywords) {
|
||||
if (str.contains(keyword)) {
|
||||
throw new WafException(CoreMsg.WAF_EXCEPTION_SQL);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// ====================
|
||||
private SQLFilterKit(){}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package org.opsli.core.waf.util;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* XSS 过滤
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2020-10-09
|
||||
* */
|
||||
public final class XSSFilterKit {
|
||||
|
||||
/**
|
||||
* @Description 过滤XSS脚本内容
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public static String stripXSS(String value) {
|
||||
String rlt = null;
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
// NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
|
||||
// avoid encoded attacks.
|
||||
// value = ESAPI.encoder().canonicalize(value);
|
||||
|
||||
// Avoid null characters
|
||||
rlt = value.replaceAll("", "");
|
||||
|
||||
// Avoid anything between script tags
|
||||
Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Avoid anything in a src='...' type of expression
|
||||
/*scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE
|
||||
| Pattern.MULTILINE | Pattern.DOTALL);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE
|
||||
| Pattern.MULTILINE | Pattern.DOTALL);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");*/
|
||||
|
||||
// Remove any lonesome </script> tag
|
||||
scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Remove any lonesome <script ...> tag
|
||||
scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE
|
||||
| Pattern.MULTILINE | Pattern.DOTALL);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Avoid eval(...) expressions
|
||||
scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE
|
||||
| Pattern.MULTILINE | Pattern.DOTALL);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Avoid expression(...) expressions
|
||||
scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE
|
||||
| Pattern.MULTILINE | Pattern.DOTALL);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Avoid javascript:... expressions
|
||||
scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Avoid vbscript:... expressions
|
||||
scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
|
||||
// Avoid onload= expressions
|
||||
scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE
|
||||
| Pattern.MULTILINE | Pattern.DOTALL);
|
||||
rlt = scriptPattern.matcher(rlt).replaceAll("");
|
||||
}
|
||||
|
||||
return rlt;
|
||||
}
|
||||
|
||||
// ====================
|
||||
private XSSFilterKit(){}
|
||||
}
|
Loading…
Reference in new issue