Files
HaE/src/main/java/burp/action/ExtractContent.java

140 lines
5.9 KiB
Java
Raw Normal View History

2021-06-10 22:59:27 +08:00
package burp.action;
2021-09-07 22:09:42 +08:00
import java.nio.charset.StandardCharsets;
2021-06-10 22:59:27 +08:00
import java.util.*;
2022-02-21 09:31:03 +08:00
import burp.Config;
2021-06-10 22:59:27 +08:00
import dk.brics.automaton.Automaton;
import dk.brics.automaton.AutomatonMatcher;
import dk.brics.automaton.RegExp;
import dk.brics.automaton.RunAutomaton;
import jregex.Matcher;
import jregex.Pattern;
2022-05-04 23:17:24 +08:00
/**
2021-06-10 22:59:27 +08:00
* @author EvilChen
*/
public class ExtractContent {
2022-06-23 15:34:22 +08:00
public Map<String, Map<String, Object>> matchRegex(byte[] content, String headers, byte[] body, String scopeString, String host) {
2021-06-10 22:59:27 +08:00
Map<String, Map<String, Object>> map = new HashMap<>(); // 最终返回的结果
2022-02-21 09:31:03 +08:00
Config.ruleConfig.keySet().forEach(i -> {
2021-06-10 22:59:27 +08:00
String matchContent = "";
2022-02-21 09:31:03 +08:00
for (Object[] objects : Config.ruleConfig.get(i)) {
2021-06-10 22:59:27 +08:00
// 遍历获取规则
2021-07-06 18:33:11 +08:00
List<String> result = new ArrayList<>();
2021-06-10 22:59:27 +08:00
Map<String, Object> tmpMap = new HashMap<>();
String name = objects[1].toString();
boolean loaded = (Boolean) objects[0];
String regex = objects[2].toString();
String color = objects[3].toString();
String scope = objects[4].toString();
String engine = objects[5].toString();
2022-05-27 13:29:32 +08:00
boolean sensitive = (Boolean) objects[6];
2021-06-10 22:59:27 +08:00
// 判断规则是否开启与作用域
2022-12-18 16:12:16 +08:00
if (loaded && (scope.contains(scopeString) || scope.contains("any"))) {
2021-06-10 22:59:27 +08:00
switch (scope) {
case "any":
case "request":
case "response":
2021-09-07 22:09:42 +08:00
matchContent = new String(content, StandardCharsets.UTF_8).intern();
2021-06-10 22:59:27 +08:00
break;
2022-12-18 16:12:16 +08:00
case "any header":
2021-06-10 22:59:27 +08:00
case "request header":
case "response header":
matchContent = headers;
break;
2022-12-18 16:12:16 +08:00
case "any body":
2021-06-10 22:59:27 +08:00
case "request body":
case "response body":
2021-09-07 22:09:42 +08:00
matchContent = new String(body, StandardCharsets.UTF_8).intern();
2021-06-10 22:59:27 +08:00
break;
2022-02-21 09:31:03 +08:00
default:
2022-12-18 16:12:16 +08:00
return;
2021-06-10 22:59:27 +08:00
}
2022-02-21 09:31:03 +08:00
if ("nfa".equals(engine)) {
2022-05-27 13:29:32 +08:00
Pattern pattern;
// 判断规则是否大小写敏感
if (sensitive) {
pattern = new Pattern(regex);
} else {
pattern = new Pattern(regex, Pattern.IGNORE_CASE);
}
2022-06-23 15:34:22 +08:00
2021-06-10 22:59:27 +08:00
Matcher matcher = pattern.matcher(matchContent);
while (matcher.find()) {
// 添加匹配数据至list
// 强制用户使用()包裹正则
result.add(matcher.group(1));
}
} else {
2021-09-07 22:09:42 +08:00
RegExp regexp = new RegExp(regex);
Automaton auto = regexp.toAutomaton();
2021-06-10 22:59:27 +08:00
RunAutomaton runAuto = new RunAutomaton(auto, true);
AutomatonMatcher autoMatcher = runAuto.newMatcher(matchContent);
while (autoMatcher.find()) {
// 添加匹配数据至list
// 强制用户使用()包裹正则
result.add(autoMatcher.group());
}
}
// 去除重复内容
HashSet tmpList = new HashSet(result);
result.clear();
result.addAll(tmpList);
if (!result.isEmpty()) {
tmpMap.put("color", color);
tmpMap.put("data", String.join("\n", result));
// 初始化格式
map.put(name, tmpMap);
}
}
}
});
2022-06-29 15:17:42 +08:00
// 将提取的数据存放到全局变量中
2022-06-23 15:34:22 +08:00
if (!host.isEmpty()) {
map.keySet().forEach(i -> {
Map<String, Object> tmpMap = map.get(i);
List<String> dataList = Arrays.asList(tmpMap.get("data").toString().split("\n"));
2022-12-18 15:09:28 +08:00
// 组合通配符Host
String anyHost = host.replace(host.split("\\.")[0], "*");
2022-06-23 15:34:22 +08:00
// 判断Host是否存在如存在则进行数据更新反之则新增数据
if (Config.globalDataMap.containsKey(host)) {
Map<String, List<String>> gRuleMap = Config.globalDataMap.get(host);
// 判断匹配规则是否存在逻辑同Host判断
if (gRuleMap.containsKey(i)) {
List<String> gDataList = gRuleMap.get(i);
2022-12-18 15:09:28 +08:00
List<String> mergeDataList = new ArrayList<>(gDataList);
2022-06-23 15:34:22 +08:00
// 合并两个List
mergeDataList.addAll(dataList);
// 去重操作
HashSet tmpList = new HashSet(mergeDataList);
mergeDataList.clear();
mergeDataList.addAll(tmpList);
// 替换操作
gRuleMap.replace(i, gDataList, mergeDataList);
} else {
gRuleMap.put(i, dataList);
}
2022-12-18 15:09:28 +08:00
} else if (!Config.globalDataMap.containsKey(anyHost)) {
// 添加通配符Host
Config.globalDataMap.put(anyHost, new HashMap<>());
}
else {
2022-06-23 15:34:22 +08:00
Map<String, List<String>> ruleMap = new HashMap<>();
ruleMap.put(i, dataList);
2022-12-18 15:09:28 +08:00
// 添加单一Host
2022-06-23 15:34:22 +08:00
Config.globalDataMap.put(host, ruleMap);
}
});
}
2021-06-10 22:59:27 +08:00
return map;
}
}