1.sql注入
sql注入:
把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令
解决方法:
1)无论是直接使用数据库还是使用如mybatis组件,使用sql的预编译,不要用拼接字符串。
2)后台过滤检测:使用正则表达式过滤传入的参数**;**.字符串过滤
3)前端检测sql常见关键字,如or and drop之类的
2.xss攻击
xss攻击:
其原理是攻击者向有XSS漏洞的网站中输入(传入)恶意的HTML代码,当其它用户浏览该网站时,这段HTML代码会自动执行,从而达到攻击的目的。如,盗取用户Cookie、破坏页面结构、重定向到其它网站等
解决方法:对用户输入的表单信息进行检测过滤
3.csrf/cros
关于csrf/cros攻击的详细介绍:
https://www.cnblogs.com/lailailai/p/4528092.html
关于跨域的详细介绍:
https://www.cnblogs.com/keyi/p/6726089.html
CSRF - Cross-Site Request Forgery - 跨站请求伪造:
攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在未授权的情况下执行在权限保护之下的操作
CORS - Cross Origin Resourse-Sharing - 跨站资源共享:
恶意访问内网敏感资源
解决方法:
有效的解决办法是通过多种条件屏蔽掉非法的请求,例如HTTP头、参数等
1、不信任未经身份验证的跨域请求,应该首先验证Session ID或者Cookie。
2、对于请求方来说验证接收的数据有效性,服务方仅暴露最少最必须的功能。
3、通过多种条件屏蔽掉非法的请求,例如HTTP头、参数等。
服务端代码跨域设置,设置了nginx转发,也可设置在nginx中:
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.yxkj.common.core.config.xssconfig.XssAndSqlHttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;/*** @Auther: * @Date: 2023/7/17 15:19* @Description:*/@WebFilter
public class CrosXssFilter implements Filter {private static final Logger logger = LoggerFactory.getLogger(CrosXssFilter.class);@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");//跨域设置if(response instanceof HttpServletResponse){HttpServletResponse httpServletResponse=(HttpServletResponse)response;//通过在响应 header 中设置 ‘*’ 来允许来自所有域的跨域请求访问。httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");//通过对 Credentials 参数的设置,就可以保持跨域 Ajax 时的 Cookie//设置了Allow-Credentials,Allow-Origin就不能为*,需要指明具体的url域//httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");//请求方式httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");//(预检请求)的返回结果(即 Access-Control-Allow-Methods 和Access-Control-Allow-Headers 提供的信息) 可以被缓存多久httpServletResponse.setHeader("Access-Control-Max-Age", "86400");//首部字段用于预检请求的响应。其指明了实际请求中允许携带的首部字段httpServletResponse.setHeader("Access-Control-Allow-Headers", "*");}//sql,xss过滤HttpServletRequest httpServletRequest=(HttpServletRequest)request;XssHttpServletRequestWrapper xssHttpServletRequestWrapper=new XssHttpServletRequestWrapper(httpServletRequest);chain.doFilter(xssHttpServletRequestWrapper, response);}@Overridepublic void destroy() {}}
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.yxkj.common.core.util.other.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.util.HtmlUtils;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;/*** @Auther: * @Date: 2023/7/17 15:20* @Description:*//*** 防止sql注入,xss攻击* 前端可以对输入信息做预处理,后端也可以做处理。*/
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {private final Logger log = LoggerFactory.getLogger(getClass());private static String key = "and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+";private static Set<String> notAllowedKeyWords = new HashSet<String>(0);private static String replacedString="INVALID";static {String keyStr[] = key.split("\\|");for (String str : keyStr) {notAllowedKeyWords.add(str);}}private String currentUrl;public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) {super(servletRequest);currentUrl = servletRequest.getRequestURI();}/**覆盖getParameter方法,将参数名和参数值都做xss过滤。* 如果需要获得原始的值,则通过super.getParameterValues(name)来获取* getParameterNames,getParameterValues和getParameterMap也可能需要覆盖*/@Overridepublic String getParameter(String parameter) {String value = super.getParameter(parameter);if (value == null) {return null;}return cleanXSS(value);}@Overridepublic 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;}@Overridepublic Map<String, String[]> getParameterMap(){Map<String, String[]> values=super.getParameterMap();if (values == null) {return null;}Map<String, String[]> result=new HashMap<>();for(String key:values.keySet()){String encodedKey=cleanXSS(key);int count=values.get(key).length;String[] encodedValues = new String[count];for (int i = 0; i < count; i++){encodedValues[i]=cleanXSS(values.get(key)[i]);}result.put(encodedKey,encodedValues);}return result;}/*** 覆盖getHeader方法,将参数名和参数值都做xss过滤。* 如果需要获得原始的值,则通过super.getHeaders(name)来获取* getHeaderNames 也可能需要覆盖*/@Overridepublic String getHeader(String name) {String value = super.getHeader(name);if (value == null) {return null;}return cleanXSS(value);}private String cleanXSS(String valueP) {// You'll need to remove the spaces from the html entities belowString value = valueP.replaceAll("<", "<").replaceAll(">", ">");value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");value = value.replaceAll("'", "& #39;");value = value.replaceAll("eval\\((.*)\\)", "");value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");value = value.replaceAll("script", "");value = cleanSqlKeyWords(value);return value;}private String cleanSqlKeyWords(String value) {String paramValue = value;for (String keyword : notAllowedKeyWords) {if (paramValue.length() > keyword.length() + 4&& (paramValue.contains(" "+keyword)||paramValue.contains(keyword+" ")||paramValue.contains(" "+keyword+" "))) {paramValue = StringUtils.replace(paramValue, keyword, replacedString);log.error(this.currentUrl + "已被过滤,因为参数中包含不允许sql的关键词(" + keyword+ ")"+";参数:"+value+";过滤后的参数:"+paramValue);}}return paramValue;}}