Hi all,
Check out my implementation here to be used on the .eml mail file attachment (if available for you):
https://github.com/jjfallete/resilient/blob/master/functions/utilities/utility_email_file_parser.pyIt works very well. In your post processor here is some content to get started:
# Get each header element and it's value
for item, content in results.mail_items:
if item.lower() == 'to':
for to in content.split(','):
if '@' not in content and '<' in content and '>' in content: to_address = content.split('>')[0].split('<')[-1]
else: to_address = ((to.split('@')[0].split('<')[-1] + '@' + to.split('@')[-1].split('>')[0]).replace('"', '').replace("'", ''))
try: to_name = to.split('"')[1].split('"')[-1]
except: to_name = to_address
to_list.append([to_address, to_name])
elif item.lower() == 'from':
if '@' not in content and '<' in content and '>' in content: from_address = content.split('>')[0].split('<')[-1]
else: from_address = (content.split('@')[0].split('<')[-1] + '@' + content.split('@')[-1].split('>')[0]).replace('"', '').replace("'", '')
try: from_name = content.replace("'", '"').split('"')[1].split('"')[-1] # content.rsplit(' ')[-1].replace('<', '').replace('>', '')
except: from_name = from_address
# ...
# Clean returned HTML for rich-text description, note, etc.
email_body_cleaned = ''
email_body = (results.body).strip()
for line in email_body.splitlines():
line = line.replace('<a ', ' <a ')
while ' ' in line: line = line.replace(' ', ' ') # Replace two spaces with one space
email_body_cleaned += (line)
email_body_cleaned = helper.createRichText(email_body_cleaned.strip())
# ...
for url in results.urls:
pattern = re.compile("(http|https|file|gopher|ftp):\\/\\/[^\\s]+")
if pattern.match(url): incident.addArtifact('URL', url, 'URL from email body')
else: incident.addArtifact('DNS Name', url, 'URL from email body') # Use a different artifact type. Or... should we append http:// to start and do as 'URL' ??? TBD
------------------------------
Jared Fagel
Cyber Security Analyst Intern
Public Utility
------------------------------
Original Message:
Sent: 02-25-2019 05:13 PM
From: Ryan Terry
Subject: Email Parser for EML attachments
I am already using the Utilities:Email Parser function, but I am wondering if anyone has a good way to parse the email attachment of an .EML file (base64) and add it as an attached file to the incident. I am able to parse the other headers I need to create Artifacts, but am stuck on the attachment part.
------------------------------
Ryan Terry
------------------------------