i'm trying to parse XML file to txt file (mainly to get the Text's body), but the for loop wouldn't run hence wouldn’t append results to the file, i know i'm missing something in the XML I tried to create an outer for loop in which it will findall MAEC_Bundle before finding the behaviours (I think because it’s the root ?).
this is the XML file
<MAEC_Bundle xmlns:ns1="http://xml/metadataSharing.xsd" xmlns="http://maec.mitre.org/XMLSchema/maec-core-1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maec.mitre.org/XMLSchema/maec-core-1 file:MAEC_v1.1.xsd" id="maec:thug:bnd:1" schema_version="1.100000">
<Analyses>
<Analysis start_datetime="2019-11-25 21:41:59.491211" id="maec:thug:ana:2" analysis_method="Dynamic">
<Tools_Used>
<Tool id="maec:thug:tol:1">
<Name>Thug</Name>
<Version>0.9.40</Version>
<Organization>The Honeynet Project</Organization>
</Tool>
</Tools_Used>
</Analysis>
</Analyses>
<Behaviors>
<Behavior id="maec:thug:bhv:4">
<Description>
<Text>[window open redirection] about:blank -> http://desbloquear.celularmovel.com/</Text>
</Description>
<Discovery_Method tool_id="maec:thug:tol:1" method="Dynamic Analysis"/>
</Behavior>
<Behavior id="maec:thug:bhv:5">
<Description>
<Text>[HTTP] URL: http://desbloquear.celularmovel.com/ (Status: 200, Referer: None)</Text>
</Description>
<Discovery_Method tool_id="maec:thug:tol:1" method="Dynamic Analysis"/>
</Behavior>
<Behavior id="maec:thug:bhv:6">
<Description>
<Text>[HTTP] URL: http://desbloquear.celularmovel.com/ (Content-type: text/html, MD5: f1fb042c62910c34be16ad91cbbd71fa)</Text>
</Description>
<Discovery_Method tool_id="maec:thug:tol:1" method="Dynamic Analysis"/>
</Behavior>
<Behavior id="maec:thug:bhv:7">
<Description>
<Text>[meta redirection] http://desbloquear.celularmovel.com/ -> http://desbloquear.celularmovel.com/cgi-sys/defaultwebpage.cgi</Text>
</Description>
<Discovery_Method tool_id="maec:thug:tol:1" method="Dynamic Analysis"/>
</Behavior>
<Behavior id="maec:thug:bhv:8">
<Description>
<Text>[HTTP] URL: http://desbloquear.celularmovel.com/cgi-sys/defaultwebpage.cgi (Status: 200, Referer: http://desbloquear.celularmovel.com/)</Text>
</Description>
<Discovery_Method tool_id="maec:thug:tol:1" method="Dynamic Analysis"/>
</Behavior>
<Behavior id="maec:thug:bhv:9">
<Description>
<Text>[HTTP] URL: http://desbloquear.celularmovel.com/cgi-sys/defaultwebpage.cgi (Content-type: text/html, MD5: a28fe921afb898e60cc334e06f71f46e)</Text>
</Description>
<Discovery_Method tool_id="maec:thug:tol:1" method="Dynamic Analysis"/>
</Behavior>
</Behaviors>
<Pools/>
</MAEC_Bundle>
this is the code for parsing in python, the code below only writes operation to the file but does not enter the loop
import xml.etree.ElementTree as ET
def logsParsing():
tree = ET.parse(
'analysis.xml')
root = tree.getroot()
with open('sample1.txt', 'w') as f:
f.write('Operation\n')
with open('sample1.txt', 'a') as f:
for behavior in root.findall('Behaviors'):
operation = behavior.find('Behavior').find('Description').find('Text').text
line_to_write = operation + '\n'
f.write(line_to_write)
f.close()
logsParsing()