2016-09-20 49 views
1

我需要分離下面的文件中的每個規則。 我怎樣才能在Java中做到這一點?如何將文件輸入分割爲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 
} 

ArrayList的[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*) 
} 

ArrayList的[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) 
} 

等。

我該怎麼做?

+0

Check out ['String.split(「regex」)'](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java。 lang.String))並搜索正則表達式的基本教程。他們非常強大/有用。 – qxz

回答

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. 如果真的是有這樣一個固定的佈局,然後在「解析」歸結爲了解「先來規則,然後是,這好像 ...」。含義:您將數據結構「硬編碼」到您的代碼中。例如:你完全「知道」第三行包含copyright = "some value"。然後你開始使用正則表達式(或簡單的字符串方法如indexOf(),substring())來提取你感興趣的信息。
  2. 如果文件格式實際上是某種「標準」(如XMl,JSON ,YAML,...),那麼你可能只需拿起一些第三方庫來解析這些文件。對於你的例子...我不能說;這絕對不是我熟悉的格式。
  3. 最糟糕的情況下,您需要編寫自己的解析器。編寫解析器是一個複雜的問題,但是「研究得很好」的話題,例如見here
+0

你好。感謝您的迴應。我編輯了最終需要的案例。你能告訴我如何將每個分離的規則添加到arraylist? – Shawn

+0

請看我更新的答案。我提供了一些代碼給你一些想法如何做到這一點。請注意:此代碼未編譯/測試;不要盲目地複製/粘貼它。一行一行地閱讀,直到你明白它應該做什麼**;然後相應地調整你自己的代碼! – GhostCat

+0

很棒!你真的很擅長java。 Upvoted您的解決方案 – Shawn