|
@@ -0,0 +1,125 @@
|
|
|
+package com.om.utils;
|
|
|
+
|
|
|
+import com.om.entity.po.SecurityLog;
|
|
|
+import lombok.extern.log4j.Log4j;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class SecurityLogUtil {
|
|
|
+ private static final byte XOR_KEY = 0x33;
|
|
|
+ public static String decryptString(String encryptedHex) {
|
|
|
+ // 将16进制字符串转换为byte数组
|
|
|
+ byte[] encryptedBytes = new byte[encryptedHex.length() / 2];
|
|
|
+ for (int i = 0; i < encryptedHex.length(); i += 2) {
|
|
|
+ String hexPair = encryptedHex.substring(i, i + 2);
|
|
|
+ int decimal = Integer.parseInt(hexPair, 16);
|
|
|
+ encryptedBytes[i / 2] = (byte) (decimal & 0xff);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 对byte数组进行异或操作以解密
|
|
|
+ byte[] decryptedBytes = new byte[encryptedBytes.length];
|
|
|
+ for (int i = 0; i < encryptedBytes.length; i++) {
|
|
|
+ decryptedBytes[i] = (byte) (encryptedBytes[i] ^ XOR_KEY);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将解密后的byte数组转换回字符串
|
|
|
+ return new String(decryptedBytes, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static SecurityLog parseLogString(String logString) {
|
|
|
+ SecurityLog securityLog = new SecurityLog();
|
|
|
+ // 正则表达式
|
|
|
+ String regex = "\\[(.*?)\\] UserName: (.*?), IP: \\{.*?sourceAddress\":\"(.*?)\".*?\\}, EventType: (.*?), EventDetails: (.*?), EventLevel: (\\w+)";
|
|
|
+ Pattern pattern = Pattern.compile(regex);
|
|
|
+ Matcher matcher = pattern.matcher(logString);
|
|
|
+
|
|
|
+ if (matcher.find()) {
|
|
|
+ // 提取匹配到的数据
|
|
|
+ String timestampStr = matcher.group(1);
|
|
|
+ String userName = matcher.group(2);
|
|
|
+ String ipAddress = matcher.group(3);
|
|
|
+ String eventType = matcher.group(4);
|
|
|
+ String eventDetails = matcher.group(5);
|
|
|
+ String eventLevel = matcher.group(6);
|
|
|
+
|
|
|
+ // 将字符串时间戳转换为 LocalDateTime 对象(这里假设时间戳格式固定)
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ LocalDateTime timestamp = LocalDateTime.parse(timestampStr.substring(0, timestampStr.length()),formatter);
|
|
|
+
|
|
|
+ // 打印提取到的数据
|
|
|
+ securityLog.setCreateTime(timestamp);
|
|
|
+ securityLog.setIp(ipAddress);
|
|
|
+ securityLog.setEventLevel(eventLevel);
|
|
|
+ securityLog.setEventDetails(eventDetails);
|
|
|
+ securityLog.setUserName(userName);
|
|
|
+ securityLog.setEventType(eventType);
|
|
|
+ } else {
|
|
|
+ log.error("没有匹配成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ return securityLog;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<SecurityLog> parseLogFile(MultipartFile file) {
|
|
|
+ List<SecurityLog> logList = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ // 获取 MultipartFile 的输入流
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ // 构造 InputStreamReader
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
|
|
+ // 构造 BufferedReader
|
|
|
+ BufferedReader reader = new BufferedReader(inputStreamReader);
|
|
|
+ // 读取文件直到到达末尾
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ SecurityLog securityLog = parseLogString(decryptString(line));
|
|
|
+ logList.add(securityLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭 BufferedReader
|
|
|
+ reader.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 捕获可能的异常
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return logList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String filePath = "security_log_en .txt"; // 指定要读取的文本文件路径
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建一个 BufferedReader 对象来读取文件
|
|
|
+ BufferedReader reader = new BufferedReader(new FileReader(filePath));
|
|
|
+ // 读取文件直到到达末尾
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ SecurityLog securityLog = parseLogString(decryptString(line));
|
|
|
+ System.out.println(securityLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭 BufferedReader
|
|
|
+ reader.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // 捕获可能的异常
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|