2014-11-04 49 views
0

我正在爲perl中的Ossec規則文件編寫解析器。Perl解析格式不正確的XML文件

我使用XML :: Simple,它一般工作正常,但有些規則文件的問題是畸形的,而不是頂級<group> <\group>有幾個。

我已閱讀perlfaq:http://perldoc.perl.org/perlfaq6.html#How-can-I-pull-out-lines-between-two-patterns-that-are-themselves-on-different-lines%3f

我可以使用例如,有匹配的文本,但所有的比賽分組。

CLI的代碼,我有這個操作是:

perl -ne 'print if /^<group name/ .. /^<\\group>/' attack_rules.xml 

如何分隔不同的比賽嗎?

這裏是一個示例XML格式不正確:

<!-- Privilege scalation messages --> 
<group name="syslog,elevation_of_privilege,"> 
    <rule id="40501" level="15" timeframe="300" frequency="2"> 
    <if_group>adduser</if_group> 
    <if_matched_group>attacks</if_matched_group> 
    <description>Attacks followed by the addition </description> 
    <description>of an user.</description> 
    </rule> 
</group> <!-- SYSLOG, ELEVATION_OF_PRIVILEGE, --> 



<!-- Scan signatures --> 
<group name="syslog,recon,"> 
    <rule id="40601" level="10" frequency="10" timeframe="90" ignore="90"> 
    <if_matched_group>connection_attempt</if_matched_group> 
    <description>Network scan from same source ip.</description> 
    <same_source_ip /> 
    <info type="link">http://project.honeynet.org/papers/enemy2/</info> 
    </rule> 
</group> <!-- SYSLOG,SCANS --> 
+3

難道你不能把整個內容包裝到' ...'? – choroba 2014-11-04 21:13:44

+0

像從文件句柄加載文件到變量並在開始和結束時連接字符串?這聽起來可行 – Bruno9779 2014-11-04 21:16:34

+0

我正在用這個解決方案。其實我要加載所有文件爲FH,並在每個文件中包裹「root」,這樣我就避免了我一直使用的醜陋系統(xmllint $文件)。 如果您在答案中編寫此解決方案,我會接受它 – Bruno9779 2014-11-04 21:21:16

回答

1

如果唯一的問題是「重根」,你可以換一個<root>...</root>每個文件的內容,並解析結果。

相關問題