mirror of
https://codeberg.org/mclemens/esm2markdown.git
synced 2025-01-05 20:36:33 -05:00
massive code restructuring
This commit is contained in:
parent
b4f6f592d2
commit
853a3eacdf
143
esm2markdown.py
143
esm2markdown.py
@ -34,6 +34,10 @@ sort_rules = True
|
|||||||
toc = True
|
toc = True
|
||||||
imagepath = "images"
|
imagepath = "images"
|
||||||
|
|
||||||
|
mklines = []
|
||||||
|
|
||||||
|
|
||||||
|
# Reads ini file, sets global variables
|
||||||
def readConfig():
|
def readConfig():
|
||||||
|
|
||||||
# Overwrite variables with settings from ini file
|
# Overwrite variables with settings from ini file
|
||||||
@ -50,28 +54,34 @@ def readConfig():
|
|||||||
|
|
||||||
|
|
||||||
# Generates a line containing linebreaks, indented lists, styles etc.
|
# Generates a line containing linebreaks, indented lists, styles etc.
|
||||||
def line(level,key,value):
|
def addLine(typ,level,key,value):
|
||||||
|
|
||||||
lvl = ""
|
lvl = ""
|
||||||
output = ""
|
output = ""
|
||||||
valout = ""
|
valout = ""
|
||||||
|
|
||||||
if level == 1: lvl = "* "
|
|
||||||
elif level == 2: lvl = " * "
|
|
||||||
elif level == 3: lvl = " * "
|
|
||||||
else: lvl = ""
|
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
value = unquote(value)
|
value = unquote(value)
|
||||||
|
|
||||||
if key:
|
if key:
|
||||||
if value == "N/A": output = lvl + key_style + key + key_style + "\n"
|
if typ == "list":
|
||||||
elif value: output = lvl + key_style + key + key_style + " " + \
|
if level == 1: lvl = "* "
|
||||||
value_style + value + value_style + "\n"
|
elif level == 2: lvl = " * "
|
||||||
else: output = ""
|
elif level == 3: lvl = " * "
|
||||||
|
if value == "N/A": output = lvl + key_style + key + key_style + "\n"
|
||||||
|
elif value: output = lvl + key_style + key + key_style + " " + \
|
||||||
|
value_style + value + value_style + "\n"
|
||||||
|
elif typ == "header":
|
||||||
|
if level == 1: lvl = "# "
|
||||||
|
elif level == 2: lvl = "## "
|
||||||
|
elif level == 3: lvl = "### "
|
||||||
|
elif level == 4: lvl = "#### "
|
||||||
|
output = "\n" + lvl + key + "\n"
|
||||||
|
elif typ == "none":
|
||||||
|
output = key + "\n"
|
||||||
|
|
||||||
output = re.sub('\$\$',"!",output)
|
output = re.sub('\$\$',"!",output)
|
||||||
return output
|
mklines.append(output)
|
||||||
|
|
||||||
|
|
||||||
# Sorts input XML alphabetically based on Rule Names
|
# Sorts input XML alphabetically based on Rule Names
|
||||||
@ -94,7 +104,7 @@ def sortXML(xmlfile):
|
|||||||
return root
|
return root
|
||||||
|
|
||||||
|
|
||||||
# Generate Markdown Syntax for Images
|
# Generates Markdown Syntax for Images
|
||||||
def addImage(rulename):
|
def addImage(rulename):
|
||||||
|
|
||||||
out = ""
|
out = ""
|
||||||
@ -106,7 +116,7 @@ def addImage(rulename):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
# Generate dict object with relations between triggers and match blocks
|
# Generates dict object with relations between triggers and match blocks
|
||||||
def getRelationDict(cdata):
|
def getRelationDict(cdata):
|
||||||
|
|
||||||
rel = {}
|
rel = {}
|
||||||
@ -133,7 +143,8 @@ def getRelationDict(cdata):
|
|||||||
|
|
||||||
return rel
|
return rel
|
||||||
|
|
||||||
# populate Graph Object with trigger nodes and edges
|
|
||||||
|
# Populates Graph object with trigger nodes and edges
|
||||||
def addTriggersToGraph(cdata,G):
|
def addTriggersToGraph(cdata,G):
|
||||||
|
|
||||||
# get dictionary with all element relations
|
# get dictionary with all element relations
|
||||||
@ -168,6 +179,8 @@ def addTriggersToGraph(cdata,G):
|
|||||||
G.add_edge(reldict[key],key,splines='ortho', nodesep=0.2)
|
G.add_edge(reldict[key],key,splines='ortho', nodesep=0.2)
|
||||||
return G
|
return G
|
||||||
|
|
||||||
|
|
||||||
|
# Generates png file visualizing match block relationships
|
||||||
def generateGraph(cdata,dMatchBlocks,rid):
|
def generateGraph(cdata,dMatchBlocks,rid):
|
||||||
|
|
||||||
dependencies = True
|
dependencies = True
|
||||||
@ -204,12 +217,19 @@ def generateGraph(cdata,dMatchBlocks,rid):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# Main Function
|
# Walks through list 'mklines' and write content to outfile
|
||||||
def main(xmlfile,outfile):
|
def writeMarkdownFile(outfile):
|
||||||
|
|
||||||
readConfig()
|
|
||||||
|
|
||||||
file = open(outfile,"w")
|
file = open(outfile,"w")
|
||||||
|
for l in mklines:
|
||||||
|
file.write(l)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
|
||||||
|
# Parses and procresses XML file
|
||||||
|
def parseXML(xmlfile):
|
||||||
|
|
||||||
|
mklines.clear
|
||||||
|
|
||||||
if sort_rules:
|
if sort_rules:
|
||||||
root = sortXML(xmlfile)
|
root = sortXML(xmlfile)
|
||||||
@ -217,46 +237,46 @@ def main(xmlfile,outfile):
|
|||||||
root = etree.parse(xmlfile)
|
root = etree.parse(xmlfile)
|
||||||
|
|
||||||
if toc:
|
if toc:
|
||||||
file.write("\n# Correlation Rule Overview\n\n")
|
addLine("header",1,"Correlation Rule Overview","")
|
||||||
for rule in root.getiterator('rule'):
|
for rule in root.getiterator('rule'):
|
||||||
file.write(line(1,rule.findtext('message'),"N/A"))
|
addLine("list",1,rule.findtext('message'),"N/A")
|
||||||
|
|
||||||
for rule in root.getiterator('rule'):
|
for rule in root.getiterator('rule'):
|
||||||
|
|
||||||
dMatchBlocks = {}
|
dMatchBlocks = {}
|
||||||
|
|
||||||
# Get CDATA
|
# Get CDATA
|
||||||
text = rule.findtext('text')
|
text = rule.findtext('text')
|
||||||
cdata = etree.fromstring(text)
|
cdata = etree.fromstring(text)
|
||||||
|
|
||||||
# Print rule name as header
|
# Print rule name as header
|
||||||
rulename = rule.findtext('message')
|
addLine("header",1,rule.findtext('message'),"")
|
||||||
file.write("\n# " + rulename + "\n")
|
|
||||||
# Print rule description
|
# Print rule description
|
||||||
description = rule.findtext('description')
|
addLine("header",2,"Description","")
|
||||||
file.write("\n## Description\n")
|
addLine("none","",rule.findtext('description'),"")
|
||||||
file.write(description +"\n")
|
|
||||||
# Print rule information (ID, Normalization, Severity, Tags, Group By)
|
# Print rule information (ID, Normalization, Severity, Tags, Group By)
|
||||||
file.write("\n## General Information\n")
|
addLine("header",2,"General Information","")
|
||||||
file.write(line(1,"Rule ID:",rule.findtext('id')))
|
addLine("list",1,"Rule ID:",rule.findtext('id'))
|
||||||
file.write(line(1,"Normalization ID:",rule.findtext('normid')))
|
addLine("list",1,"Normalization ID:",rule.findtext('normid'))
|
||||||
file.write(line(1,"Severity:",rule.findtext('severity')))
|
addLine("list",1,"Severity:",rule.findtext('severity'))
|
||||||
for tags in rule.getiterator('tag'):
|
for tags in rule.getiterator('tag'):
|
||||||
file.write(line(1,"Tag:",tags.text))
|
addLine("list",1,"Tag:",tags.text)
|
||||||
for rs in cdata.getiterator('ruleset'):
|
for rs in cdata.getiterator('ruleset'):
|
||||||
file.write(line(1,"Group By:",rs.get('correlationField')))
|
addLine("list",1,"Group By:",rs.get('correlationField'))
|
||||||
file.write("\n## Correlation Details\n")
|
addLine("header",2,"Correlation Details","")
|
||||||
file.write(addImage(rule.findtext('id')))
|
# Insert diagram into text
|
||||||
|
mklines.append(addImage(rule.findtext('id')))
|
||||||
parameters = False
|
parameters = False
|
||||||
# Print rule parameters
|
# Print rule parameters
|
||||||
for param in cdata.getiterator('param'):
|
for param in cdata.getiterator('param'):
|
||||||
if not parameters:
|
if not parameters:
|
||||||
file.write("\n### Parameters\n")
|
addLine("header",3,"Parameters","")
|
||||||
parameters = True
|
parameters = True
|
||||||
file.write(line(1,param.get('name'),"N/A"))
|
addLine("list",1,param.get('name'),"N/A")
|
||||||
file.write(line(2,"Description:",param.get('description')))
|
addLine("list",2,"Description:",param.get('description'))
|
||||||
file.write(line(2,"Default Value:",param.get('defaultvalue')))
|
addLine("list",2,"Default Value:",param.get('defaultvalue'))
|
||||||
|
|
||||||
|
addLine("header",3,"Rules","")
|
||||||
file.write("\n### Rules\n")
|
|
||||||
|
|
||||||
# Parse CDATA element and print correlation rule match blocks
|
# Parse CDATA element and print correlation rule match blocks
|
||||||
for r in cdata.getiterator('rule'):
|
for r in cdata.getiterator('rule'):
|
||||||
@ -270,33 +290,33 @@ def main(xmlfile,outfile):
|
|||||||
|
|
||||||
# Walk through all rules except Root Rule
|
# Walk through all rules except Root Rule
|
||||||
if not r.get('name') == "Root Rule":
|
if not r.get('name') == "Root Rule":
|
||||||
file.write("\n#### " + r.get('name').title().replace("_", " ") + "\n")
|
addLine("header",4,r.get('name').title().replace("_", " "),"")
|
||||||
override = r.get('correlationField')
|
override = r.get('correlationField')
|
||||||
for e in r.iter():
|
for e in r.iter():
|
||||||
if str(e.tag) == 'activate':
|
if str(e.tag) == 'activate':
|
||||||
file.write(line(1,"Activate:",e.get('type')))
|
addLine("list",1,"Activate:",e.get('type'))
|
||||||
if override:
|
if override:
|
||||||
file.write(line(1,"Override Group By:",override))
|
addLine("list",1,"Override Group By:",override)
|
||||||
if str(e.tag) == 'action':
|
if str(e.tag) == 'action':
|
||||||
if e.get('type') == "TRIGGER":
|
if e.get('type') == "TRIGGER":
|
||||||
file.write(line(1,"Action:","Trigger"))
|
addLine("list",1,"Action:","Trigger")
|
||||||
# Find parent trigger of current rule
|
# Find parent trigger of current rule
|
||||||
for trigger in cdata.getiterator('trigger'):
|
for trigger in cdata.getiterator('trigger'):
|
||||||
if e.get('trigger') == trigger.get('name'):
|
if e.get('trigger') == trigger.get('name'):
|
||||||
parent = trigger.get('name')
|
parent = trigger.get('name')
|
||||||
file.write(line(2,"Timeout:",trigger.get('timeout')))
|
addLine("list",2,"Timeout:",trigger.get('timeout'))
|
||||||
file.write(line(2,"Time Units:",trigger.get('timeUnit')))
|
addLine("list",2,"Time Units:",trigger.get('timeUnit'))
|
||||||
file.write(line(2,"Threshold:",trigger.get('threshold')))
|
addLine("list",2,"Threshold:",trigger.get('threshold'))
|
||||||
file.write(line(2,"Sequence:",trigger.get('ordered')))
|
addLine("list",2,"Sequence:",trigger.get('ordered'))
|
||||||
else:
|
else:
|
||||||
file.write(line(1,"Action","N/A"))
|
addLine("list",1,"Action","N/A")
|
||||||
file.write(line(2,"NOT IMPLEMENTED","N/A"))
|
addLine("list",2,"NOT IMPLEMENTED","N/A")
|
||||||
if str(e.tag) == 'match':
|
if str(e.tag) == 'match':
|
||||||
matchtype = e.get('matchType')
|
matchtype = e.get('matchType')
|
||||||
file.write(line(1,"Match Type:",matchtype))
|
addLine("list",1,"Match Type:",matchtype)
|
||||||
file.write(line(2,"Count:",e.get('count')))
|
addLine("list",2,"Count:",e.get('count'))
|
||||||
if str(e.tag) == 'matchFilter':
|
if str(e.tag) == 'matchFilter':
|
||||||
file.write(line(1,"Match Filter","N/A"))
|
addLine("list",1,"Match Filter","N/A")
|
||||||
if str(e.tag) == 'singleFilterComponent':
|
if str(e.tag) == 'singleFilterComponent':
|
||||||
t = e.get('type')
|
t = e.get('type')
|
||||||
if str(e.tag) == 'filterData':
|
if str(e.tag) == 'filterData':
|
||||||
@ -305,9 +325,9 @@ def main(xmlfile,outfile):
|
|||||||
if (e.get('name') == "value"):
|
if (e.get('name') == "value"):
|
||||||
v = e.get('value')
|
v = e.get('value')
|
||||||
if o and v and t:
|
if o and v and t:
|
||||||
file.write(line(2,"Filter Component","N/A"))
|
addLine("list",2,"Filter Component","N/A")
|
||||||
file.write(line(3,"Condition:","'" + t + "' " + o + " '" \
|
addLine("list",3,"Condition:","'" + t + "' " + o + " '" \
|
||||||
+ v + "'"))
|
+ v + "'")
|
||||||
# Set nice label, add rule as graphviz node,
|
# Set nice label, add rule as graphviz node,
|
||||||
# add edge between trigger and node
|
# add edge between trigger and node
|
||||||
label = t + r"\n" + o + r"\n" + v
|
label = t + r"\n" + o + r"\n" + v
|
||||||
@ -321,14 +341,19 @@ def main(xmlfile,outfile):
|
|||||||
nicename = r.get('name').title().replace("_", " ")
|
nicename = r.get('name').title().replace("_", " ")
|
||||||
dMatchBlocks[nicename] = {'parent': parent, 'shapecol': shapecol}
|
dMatchBlocks[nicename] = {'parent': parent, 'shapecol': shapecol}
|
||||||
|
|
||||||
generateGraph(cdata,dMatchBlocks,rule.findtext('id'))
|
mklines.append("\n\\newpage\n")
|
||||||
file.write("\n\\newpage\n")
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
|
generateGraph(cdata,dMatchBlocks,rule.findtext('id'))
|
||||||
|
|
||||||
|
|
||||||
|
# Main function
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
|
|
||||||
if len(sys.argv) != 3:
|
if len(sys.argv) != 3:
|
||||||
print('Invalid Numbers of Arguments. Script will be terminated.')
|
print('Invalid Numbers of Arguments. Script will be terminated.')
|
||||||
print('Usage: python esm2markdown <rule xml file> <output file>')
|
print('Usage: python esm2markdown <rule xml file> <output file>')
|
||||||
print('Example: python esm2markdown RuleExport.xml documentation.mk')
|
print('Example: python esm2markdown RuleExport.xml documentation.mk')
|
||||||
else:
|
else:
|
||||||
main(sys.argv[1],sys.argv[2]);
|
readConfig()
|
||||||
|
parseXML(sys.argv[1])
|
||||||
|
writeMarkdownFile(sys.argv[2])
|
||||||
|
Loading…
Reference in New Issue
Block a user