Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import mailparser
- import hashlib
- import traceback
- import base64
- from mailparser.mailparser import MailParser
- def write_sample(binary, payload, path, filename):
- if not os.path.exists(path):
- os.makedirs(path)
- sample = os.path.join(path, filename)
- if binary:
- with open(sample, "wb") as f:
- f.write(base64.b64decode(payload))
- else:
- with open(sample, "w") as f:
- f.write(payload)
- def write_attachments(attachments, base_path):
- for a in attachments:
- realdir=os.path.dirname(os.path.join(base_path,a["filename"]))
- os.makedirs(realdir,exist_ok=True)
- write_sample(
- binary=a["binary"],
- payload=a["payload"],
- path=base_path,
- filename=a["filename"],
- )
- for directory, dirnames, filenames in os.walk("."):
- for f in filenames:
- filename=os.path.join(directory,f)
- print("Handling {}".format(filename))
- try:
- with open(filename,"r",encoding="latin-1") as infile:
- data=infile.read()
- h=hashlib.sha256(data.encode('utf-16')).hexdigest()
- outpath="../{}/".format(h)
- os.makedirs(outpath,exist_ok=True)
- mail = mailparser.parse_from_string(data)
- with open("mail.txt","w",encoding="utf-8") as outfile:
- outfile.write("filename:{}\n".format(f)))
- outfile.write("\n".join(mail.text_plain))
- write_attachments(mail.attachments,outpath)
- except Exception as e:
- print(e)
- print(traceback.format_exc())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement