【问题标题】:How to segment file input into portions in Java如何在Java中将文件输入分割成部分
【发布时间】:2016-09-20 02:42:57
【问题描述】:

我需要在下面的文件中分隔每个规则。 我如何在 Java 中做到这一点?

这是文件内容

rule apt_regin_2011_32bit_stage1 {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin 32 bit stage 1 loaders"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$key1={331015EA261D38A7}
$key2={9145A98BA37617DE}
$key3={EF745F23AA67243D}
$mz="MZ"
condition:
($mz at 0) and any of ($key*) and filesize < 300000
}


rule apt_regin_rc5key {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin RC5 decryption keys"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$key1={73 23 1F 43 93 E1 9F 2F 99 0C 17 81 5C FF B4 01}
$key2={10 19 53 2A 11 ED A3 74 3F C3 72 3F 9D 94 3D 78}
condition:
any of ($key*)
}



rule apt_regin_vfs {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin VFSes"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$a1={00 02 00 08 00 08 03 F6 D7 F3 52}
$a2={00 10 F0 FF F0 FF 11 C7 7F E8 52}
$a3={00 04 00 10 00 10 03 C2 D3 1C 93}
$a4={00 04 00 10 C8 00 04 C8 93 06 D8}
condition:
($a1 at 0) or ($a2 at 0) or ($a3 at 0) or ($a4 at 0)
}


rule apt_regin_dispatcher_disp_dll {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin disp.dll dispatcher"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$mz="MZ"
 $string1="shit"
 $string2="disp.dll"
 $string3="255.255.255.255"
 $string4="StackWalk64"
 $string5="imagehlp.dll"
condition:
($mz at 0) and (all of ($string*))
}

根据文件中所见,我需要将文件输入中的 4 条规则中的每一条都分开,知道我该怎么做吗? 请耐心等待我。我是新手 提前感谢!

将4条规则全部分离后,我需要将每条规则放入一个arraylist中。

例如: 数组列表[0]

rule apt_regin_2011_32bit_stage1 {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin 32 bit stage 1 loaders"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$key1={331015EA261D38A7}
$key2={9145A98BA37617DE}
$key3={EF745F23AA67243D}
$mz="MZ"
condition:
($mz at 0) and any of ($key*) and filesize < 300000
}

数组列表[1]

rule apt_regin_rc5key {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin RC5 decryption keys"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$key1={73 23 1F 43 93 E1 9F 2F 99 0C 17 81 5C FF B4 01}
$key2={10 19 53 2A 11 ED A3 74 3F C3 72 3F 9D 94 3D 78}
condition:
any of ($key*)
}

数组列表[2]

rule apt_regin_vfs {
meta:
copyright = "Kaspersky Lab"
 description = "Rule to detect Regin VFSes"
 version = "1.0"
 last_modified = "2014-11-18"
strings:
$a1={00 02 00 08 00 08 03 F6 D7 F3 52}
$a2={00 10 F0 FF F0 FF 11 C7 7F E8 52}
$a3={00 04 00 10 00 10 03 C2 D3 1C 93}
$a4={00 04 00 10 C8 00 04 C8 93 06 D8}
condition:
($a1 at 0) or ($a2 at 0) or ($a3 at 0) or ($a4 at 0)
}

等等。

我该怎么做?

【问题讨论】:

  • 查看String.split("regex") 并搜索有关正则表达式的基本教程。它们非常强大/有用。

标签: java delimiter


【解决方案1】:

仅作记录:如果您的问题要“分割”您输入中的“规则”,那么只需执行以下操作:

List<List<String>> sections = new ArrayList<>();
List<String> currentSection = null;

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  String line;
  while ((line = br.readLine()) != null) {
    if(line.startsWith("rule ")) {
      if (currentSection != null) {
        // we are finished with the previous section!
        sections.add(currentSection);
      }
      currentSection = new ArrayList<>();
      currentSection.add(line);
    } else {
      if(! line.trim().isEmpty()) {
        // any non-empty line goes into the current section
        currentSection.add(line);          
      }
    }
 }
} // end of try/while ... I am too lazy to count my braces ;-)
if (currentSelection != null) {
  // make sure to add the final section, too!
  sections.add(currentSelection); 
}

但是:你对你真正的要求不是很精确。我很确定您真正的问题不是“分段”该输入文件。

您的实际任务很可能是读取该文件,并且对于该文件中的每个部分,您需要获取其部分/全部内容以进行进一步处理。

换句话说:您实际上是在问“我如何解析/处理”这个输入。我们无法回答这个问题;因为你没有告诉我们你想用这些数据做什么。

本质上,这是您的选择空间:

  1. 如果真的有这么固定的布局,那么“解析”归结为理解“先到rule,再到meta,看起来像……” .含义:您将数据结构“硬编码”到代码中。示例:您完全“知道”第三行包含copyright = "some value"。然后开始使用正则表达式(或简单的 String 方法,如 indexOf()、substring())来提取您感兴趣的信息。
  2. 如果文件格式实际上是某种“标准”(例如 XMl、JSON、YAML 等),那么您可以简单地选择一些第三方库来解析此类文件。对于你的例子......我不能说;这绝对不是我熟悉的格式。
  3. 最坏的情况,您需要编写自己的解析器。编写解析器是一个复杂但“经过深入研究”的主题,例如,请参阅here

【讨论】:

  • 你好。感谢您的回复。我已经编辑了我最终需要它的案例。你能告诉我如何将每个单独的规则添加到 arraylist 中吗?
  • 请看我更新的答案。我输入了一些代码来给你一些想法如何做到这一点。请注意:此代码未经编译/测试;不要只是盲目地复制/粘贴它。逐行阅读,直到您了解应该做什么;然后相应地调整您自己的代码!
  • 太棒了!你真的很擅长java。支持您的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
相关资源
最近更新 更多