Skip to content

Instantly share code, notes, and snippets.

@norandom
Last active January 23, 2024 19:19
Show Gist options
  • Save norandom/edb4b55dcf2d10066f86bcc27b30bd8f to your computer and use it in GitHub Desktop.
Save norandom/edb4b55dcf2d10066f86bcc27b30bd8f to your computer and use it in GitHub Desktop.
"""
Copyright (c) 2024 Marius Ciepluch
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import json
from lxml import etree
from concurrent.futures import ThreadPoolExecutor
import sys
def sanitize_data(data):
cleaned_data = data.strip() if data else None
return cleaned_data
def categorize_data(name, text):
# Categorize data based on its name
# this is for data enrichment later...
if name.lower() in ["logonid", "terminalsessionid", "integritylevel", "parentprocessid"]:
return {"Name": name, "Value": text}
elif name.lower() == "timecreated" and text:
return {"Name": name, "Value": text}
elif name.lower() in ["guid", "parentprocessguid"]:
return {"Name": name, "Value": text}
elif "hash" in name.lower():
return {"Name": name, "Value": text}
elif name.lower() == "parentimage":
return {"Name": name, "Value": text}
elif name.lower() == "portnumber" and text.isnumeric() and 0 <= int(text) <= 65535:
return {"Name": name, "Value": text}
elif name.lower() == "linuxpid" and text.isnumeric():
return {"Name": name, "Value": text}
else:
return {"Name": name, "Value": text}
def process_xml(xml_content):
root = etree.fromstring(xml_content)
event_data = {}
for child in root.iter():
tag = child.tag
text = sanitize_data(child.text)
if tag == "Data":
# Extract the "Name" attribute from the <Data> element
name = child.get("Name")
if name:
# Categorize and enhance the description for each data element
data_category = categorize_data(name, text)
event_data.setdefault("Data", []).append(data_category)
else:
event_data[tag] = text
json_string = json.dumps(event_data, indent=2)
print(json_string)
def process_xml_documents(xml_documents):
with ThreadPoolExecutor() as executor:
executor.map(process_xml, xml_documents)
if __name__ == "__main__":
xml_documents = sys.stdin.read().split("</Event>")
xml_documents = [xml_doc + "</Event>" for xml_doc in xml_documents if "<Event>" in xml_doc]
process_xml_documents(xml_documents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment