SQL关键字检测持续优化,太难了

麻烦死了,用正则是不是简单些

java 复制代码
public class SQLParamChecker {
    // 关键字
    private static List<String> SPECIAL_WORDS;

    private static BloomFilter<String> FILTER;

    static {
        FILTER = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), 100000, 0.01);

        SPECIAL_WORDS = ZYFileUtils.readClassPathFile2List("sql_special_key.txt");
        // 单词的长短倒序,先判断长的,再判断误的,避免长的包含短的误关  如into  in这种,先判断into,再判断in
        SPECIAL_WORDS.sort(Comparator.comparing(String::length).reversed());
    }
    
  public static void main(String[] args) {
        String sql1 = "select Icollect'and(select*from(select+sleep(2))a/**/union/**/select+1)=' select";
        String sql2 = "select ";
        checkExistsIllegalSQL(sql1);
        checkExistsIllegalSQL(sql2);
        checkExistsIllegalSQL(sql2);
    }

    public static void checkExistsIllegalSQL(String context) {
        if (ZYStrUtils.isNull(context)) {
            return;
        }

        if (FILTER.mightContain(context)) {
            return;
        }

        // 统一转为小写
        String text = context.toLowerCase();

        // 检查是否存在关键字
        if (hasSpecialWord(text)) {
            throw new IllegalSqlException();
        }
        if (context.length() < 30) {
            //  小于30(经验值)的短参数直接跳过去,用布隆过滤器不知道有没有问题,会有一定的内存溢出风险,比起大量循环判断来说。还是值得一试
            FILTER.put(context);
        }
    }
    private static boolean hasSpecialWord(String text) {
        for (String specialWord : SPECIAL_WORDS) {
            // 等于关键字无所谓
            if (text.length() == specialWord.length()) {
                return false;
            }
            // 找到关键字索引
            List<Integer> indexs = findPlaceLocal(text, specialWord);
            if (indexs.size() == 0) {
                continue;
            }
            for (Integer index : indexs) {
                // 前置检查
                boolean isPreSpecialSymbol;
                if (index == 0) {
                    // 关键字开头
                    isPreSpecialSymbol = true;
                } else {
                    String preLatter = text.substring(index - 1, index);
                    // 前置字母被关键字包裹
                    isPreSpecialSymbol = isSpecialSymbol(preLatter);
                }

                // 后置检查
                int beginIndex = index + specialWord.length();
                int endIndex = beginIndex + 1;
                boolean isAfterSpecialSymbol;
                if (endIndex >= text.length()) {
                    // 关键字结尾
                    isAfterSpecialSymbol = true;
                } else {
                    String postLetter = text.substring(beginIndex, endIndex);
                    // 后置字母被关键字包裹
                    isAfterSpecialSymbol = isSpecialSymbol(postLetter);
                }

                // 检测到关键字,并且关键字的前后是非字母相连,判断为sql关键字   (select *这种 xxxselectxx 这种不算
                if (isPreSpecialSymbol && isAfterSpecialSymbol) {
                    return true;
                }
            }
        }
        return false;
    }

    private static List<Integer> findPlaceLocal(String text, String word) {
        int index = 0;
        List<Integer> placeIndexs = new ArrayList<>();
        while (index != -1) {
            index = ZYStrUtils.indexOf(text, word, index, false);
            if (index != -1) {
                placeIndexs.add(index);
                index = index + word.length();
            }
        }
        return placeIndexs;
    }


    private static boolean isSpecialSymbol(String letter) {
        char aChar = letter.toCharArray()[0];
        char line='_';
        // 数字、字母、下划线
        boolean isLatter =   aChar <= 9 || (aChar >= 97 && aChar <= 122) || aChar == line;;
        return !isLatter;
    }
}
相关推荐
Databend15 分钟前
大数据是不是凉了?
数据库
mask哥16 分钟前
详解最新链路追踪skywalking框架介绍、架构、环境本地部署&配置、整合微服务springcloudalibaba 、日志收集、自定义链路追踪、告警等
java·spring cloud·架构·gateway·springboot·skywalking·链路追踪
XU磊26019 分钟前
javaWeb开发---前后端开发全景图解(基础梳理 + 技术体系)
java·idea
学也不会22 分钟前
雪花算法
java·数据库·oracle
晓华-warm31 分钟前
国产免费工作流引擎star 5.9k,Warm-Flow版本升级1.7.0(新增大量好用功能)
java·中间件·流程图·开源软件·flowable·工作流·activities
凭君语未可34 分钟前
介绍 IntelliJ IDEA 快捷键操作
java·ide·intellij-idea
码上飞扬1 小时前
Java大师成长计划之第5天:Java中的集合框架
java·开发语言
24k小善1 小时前
FlinkUpsertKafka深度解析
java·大数据·flink·云计算
瀚高PG实验室1 小时前
数据库未正常关闭后,再次启动时只有主进程,数据库日志无输出
数据库
EnigmaCoder1 小时前
java面向对象编程【高级篇】之多态
java·开发语言