[实践总结] 限制正则表达式匹配次数/时间 防止DoS攻击

思路

1、优化正则表达式

2、正则表达式无法优化的话,可以考虑限制匹配次数,或者限制匹配时间

限制 匹配次数

csharp 复制代码
public class CountedCharSequence implements CharSequence {
    private final CharSequence charSequence;
    private long count;

    public CountedCharSequence(CharSequence charSequence, long count) {
        this.charSequence = charSequence;
        this.count = count;
    }

    @Override
    public String toString() {
        return charSequence.toString();
    }

    @Override
    public int length() {
        return charSequence.length();
    }

    @Override
    public char charAt(int index) {
        if (index <= 0) {
            throw new IllegalArgumentException("Regex match over max " + count + " times");
        }
        count--;
        return charSequence.charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return charSequence.subSequence(start, end);
    }
}

模拟Dos攻击

csharp 复制代码
String str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

long start = System.nanoTime();
Pattern.compile("^(a+)+$").matcher(str).matches();
System.out.println(((System.nanoTime() - start)) + " nano seconds");

执行用时
1399200 nano seconds

超过 1亿 就会报错

csharp 复制代码
long start1 = System.nanoTime();

try {
    Pattern.compile("^(a+)+$").matcher(new CountedCharSequence(str, 100_000_000)).matches();
} catch (Exception e) {
    System.out.println(e.getMessage());
}

System.out.println(((System.nanoTime() - start1)) + " nano seconds");

执行用时
Regex match over max 100000000 times
601800 nano seconds

限制 匹配时间

csharp 复制代码
import java.util.regex.Pattern;

public final class TimedCharSequence implements CharSequence {
    private final CharSequence sequence;
    private final long timestamp;

    public TimedCharSequence(CharSequence sequence, long nanoseconds) {
        this.sequence = sequence;
        this.timestamp = System.nanoTime() + nanoseconds;
    }

    @Override
    public String toString() {
        return sequence.toString();
    }

    @Override
    public int length() {
        return sequence.length();
    }

    @Override
    public char charAt(int index) {
        if (timestamp < System.nanoTime()) {
            throw new IllegalStateException("Regex match timeout");
        }
        return sequence.charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return sequence.subSequence(start, end);
    }
}

模拟Dos攻击

csharp 复制代码
String str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

long start = System.nanoTime();
Pattern.compile("^(a+)+$").matcher(str).matches();
System.out.println(((System.nanoTime() - start)) + " nano seconds");

执行用时:
1385400 nano seconds

超过 给定时间间隔就报错

csharp 复制代码
long start1 = System.nanoTime();

try {
    Pattern.compile("^(a+)+$").matcher(new TimedCharSequence(str, 10_000)).matches();
} catch (Exception e) {
    System.out.println(e.getMessage());
}

System.out.println(((System.nanoTime() - start1)) + " nano seconds");

执行用时:
Regex match timeout
123100 nano seconds

[Ref]

[Ref] 限制正则表达式执行时间

相关推荐
菜的不敢吱声2 分钟前
swift学习第一天
开发语言·学习·swift
培林将军4 分钟前
C语言指针
c语言·开发语言·算法
云栖梦泽7 分钟前
鸿蒙分布式应用全链路性能调优实战
开发语言·鸿蒙系统
jyd01249 分钟前
Spring Boot 文件上传大小限制问题:413 与 MaxUploadSizeExceededException 解决方案
java·spring boot·后端
yongui4783412 分钟前
基于卡尔曼滤波的电池荷电状态(SOC)估计的MATLAB实现
开发语言·算法·matlab
絔宝12 分钟前
Eclipse配置 Maven 国内镜像
java·eclipse·maven
虾说羊19 分钟前
Spring Boot前后端分离项目部署
java·spring boot·后端
yuan1999721 分钟前
H264视频压缩matlab帧内预测帧间预测熵编码
开发语言·matlab
aini_lovee23 分钟前
基于MATLAB GUI的信号处理系统设计与实现
开发语言·matlab·信号处理
kylezhao201934 分钟前
C#上位机实现权限管理
开发语言·c#