Skip to content

Instantly share code, notes, and snippets.

@h4sh5
Created October 30, 2022 03:37
Show Gist options
  • Select an option

  • Save h4sh5/ee214d82d809519975b9f60cf40f1f9a to your computer and use it in GitHub Desktop.

Select an option

Save h4sh5/ee214d82d809519975b9f60cf40f1f9a to your computer and use it in GitHub Desktop.
parse mail logs and extract all base64 encoded attachments
#!/usr/bin/env python2
import sys
import os
import base64
file = sys.argv[1]
# os.mkdir('out')
with open(file,'rb') as f:
lastblock = ''
for block in f.read().split('\n\n'):
if 'attachment; filename' in lastblock:
filename = lastblock.split('filename=')[1].split('"')[1]
print(filename)
with open('out/'+filename,'w') as outf:
outf.write(base64.b64decode(block))
print 'written to ' 'out/'+filename
lastblock = block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment