利用spring AOP 实现 sql注入检测

转 https://blog.csdn.net/weixin_41922289/article/details/88267790

利用spring AOP 实现 sql注入检测

 

什么是sql注入?

所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。 [1] 比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击

sql注入导致原因?
SQL注入的产生原因通常表现在以下几方面:
①不当的类型处理;
②不安全的数据库配置;
③不合理的查询集处理;
④不当的错误处理;
⑤转义字符处理不合适;
⑥多个提交处理不当。

简单例子
在这里插入图片描述
利用spring的aop做了一个简单的检测sql注入的例子

    @Aspect
    @Component
    public class LogAspect {
        /**
         * 切面类
         */
        private Logger logger = LoggerFactory.getLogger(getClass());
    
        // 存在SQL注入风险
        private static final String IS_SQL_INJECTION = "输入参数存在SQL注入风险";
    
        private static final String UNVALIDATED_INPUT = "输入参数含有非法字符";
    
        private static final String ERORR_INPUT = "输入的参数非法";
    
        //切入点
        @Pointcut("execution(public * com.kevin.library.controller.*.*(..))")
        public void webLog() {
        }
    
        //前置通知
        @Before("webLog()")
        public void before(JoinPoint joinPoint)throws Throwable {
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            logger.info("URL : " + request.getRequestURL().toString());
            logger.info("HTTP_METHOD : " + request.getMethod());
            logger.info("IP : " + request.getRemoteAddr());
            logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
        }
    
        //后置通知
        @AfterReturning(returning = "ret", pointcut = "webLog()")
        public void afterReturning(Object ret) throws Throwable {
            // 处理完请求,返回内容
            logger.info("方法的返回值 : " + ret);
        }
    
        //后置异常通知
        @AfterThrowing("webLog()")
        public void returnThrowing(JoinPoint joinPoint) {
            logger.info("方法异常时执行");
        }
    
        //最终通知
        @After("webLog()")
        public void after(JoinPoint joinPoint) {
            logger.info("方法最终执行");
        }
    
        //环绕通知
        @Around("webLog()")
        public Object arround(ProceedingJoinPoint pjp) {
            logger.info("方法环绕start.....");
            try {
                Object[] args =pjp.getArgs();// 参数
                String str = String.valueOf(args);
    
                if (!IllegalStrFilterUtil.sqlStrFilter(str)) {
                    logger.info(IS_SQL_INJECTION);
                    new RuntimeException(ERORR_INPUT);
                }
                if (!IllegalStrFilterUtil.isIllegalStr(str)) {
                    logger.info(UNVALIDATED_INPUT);
                    new RuntimeException(ERORR_INPUT);
                }
                Object o =  pjp.proceed();
                logger.info("方法环绕proceed,结果是 :" + o);
                return o;
            } catch (Throwable e) {
                e.printStackTrace();
                return null;
            }
        }
    
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

检测工具类

    public class IllegalStrFilterUtil {
    
        private static Logger logger = LoggerFactory.getLogger(IllegalStrFilterUtil.class);
    
        private static final String REGX = "!|!|@|◎|#|#|(\\$)|¥|%|%|(\\^)|……|(\\&)|※|(\\*)|×|(\\()|(|(\\))|)|_|——|(\\+)|+|(\\|)|§ ";
    
        /**
         * 对常见的sql注入攻击进行拦截
         *
         * @param input
         * @return
         *  true 表示参数不存在SQL注入风险
         *  false 表示参数存在SQL注入风险
         */
        public static Boolean sqlStrFilter(String input) {
            if (input == null || input.trim().length() == 0) {
                return false;
            }
            input = input.toUpperCase();
    
            if (input.indexOf("DELETE") >= 0 || input.indexOf("ASCII") >= 0 || input.indexOf("UPDATE") >= 0 || input.indexOf("SELECT") >= 0
                    || input.indexOf("'") >= 0 || input.indexOf("SUBSTR(") >= 0 || input.indexOf("COUNT(") >= 0 || input.indexOf(" OR ") >= 0
                    || input.indexOf(" AND ") >= 0 || input.indexOf("DROP") >= 0 || input.indexOf("EXECUTE") >= 0 || input.indexOf("EXEC") >= 0
                    || input.indexOf("TRUNCATE") >= 0 || input.indexOf("INTO") >= 0 || input.indexOf("DECLARE") >= 0 || input.indexOf("MASTER") >= 0) {
                logger.error("该参数怎么SQL注入风险:sInput=" + input);
                return false;
            }
            logger.info("通过sql检测");
            return true;
        }
    
        /**
         * 对非法字符进行检测
         *
         * @param input
         * @return
         *  true 表示参数不包含非法字符
         *  false 表示参数包含非法字符
         */
        public static Boolean isIllegalStr(String input) {
    
            if (input == null || input.trim().length() == 0) {
                return false;
            }
            input = input.trim();
            Pattern compile = Pattern.compile(REGX, Pattern.CASE_INSENSITIVE);
            Matcher matcher = compile.matcher(input);
            logger.info("通过字符串检测");
            return matcher.find();
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值