-
Notifications
You must be signed in to change notification settings - Fork 20
/
mbox-extract-attachments.py
executable file
·170 lines (135 loc) · 4.96 KB
/
mbox-extract-attachments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mbox-extract-attachments.py - Extract attachments from mbox files - 16/March/2012
# Copyright (C) 2012 Pablo Castellano <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Notes (RFC 1341):
# The use of a Content-Type of multipart in a body part within another multipart entity is explicitly allowed. In such cases, for obvious reasons, care must be taken to ensure that each nested multipart entity must use a different boundary delimiter. See Appendix C for an example of nested multipart entities.
# The use of the multipart Content-Type with only a single body part may be useful in certain contexts, and is explicitly permitted.
# The only mandatory parameter for the multipart Content-Type is the boundary parameter, which consists of 1 to 70 characters from a set of characters known to be very robust through email gateways, and NOT ending with white space. (If a boundary appears to end with white space, the white space must be presumed to have been added by a gateway, and should be deleted.) It is formally specified by the following BNF
# Related RFCs: 2047, 2044, 1522
__author__ = "Pablo Castellano <[email protected]>"
__license__ = "GNU GPLv3+"
__version__ = 1.2
__date__ = "12/04/2012"
import mailbox
import base64
import os
import sys
import email
BLACKLIST = ('signature.asc', 'message-footer.txt', 'smime.p7s')
VERBOSE = 1
attachments = 0 #Count extracted attachment
skipped = 0
# Search for filename or find recursively if it's multipart
def extract_attachment(payload):
global attachments, skipped
filename = payload.get_filename()
if filename is not None:
print "\nAttachment found!"
if filename.find('=?') != -1:
ll = email.header.decode_header(filename)
filename = ""
for l in ll:
filename = filename + l[0]
if filename in BLACKLIST:
skipped = skipped + 1
if (VERBOSE >= 1):
print "Skipping %s (blacklist)\n" %filename
return
# Puede no venir especificado el nombre del archivo??
# if filename is None:
# filename = "unknown_%d_%d.txt" %(i, p)
content = payload.as_string()
# Skip headers, go to the content
fh = content.find('\n\n')
content = content[fh:]
# if it's base64....
if payload.get('Content-Transfer-Encoding') == 'base64':
content = base64.decodestring(content)
# quoted-printable
# what else? ...
print "Extracting %s (%d bytes)\n" %(filename, len(content))
n = 1
orig_filename = filename
while os.path.exists(filename):
filename = orig_filename + "." + str(n)
n = n+1
try:
fp = open(filename, "w")
# fp = open(str(i) + "_" + filename, "w")
fp.write(content)
except IOError:
print "Aborted, IOError!!!"
sys.exit(2)
finally:
fp.close()
attachments = attachments + 1
else:
if payload.is_multipart():
for payl in payload.get_payload():
extract_attachment(payl)
###
print "Extract attachments from mbox files"
print "Copyright (C) 2012 Pablo Castellano"
print "This program comes with ABSOLUTELY NO WARRANTY."
print "This is free software, and you are welcome to redistribute it under certain conditions."
print
if len(sys.argv) < 2 or len(sys.argv) > 3:
print "Usage: %s <mbox_file> [directory]" %sys.argv[0]
sys.exit(0)
filename = sys.argv[1]
directory = os.path.curdir
if not os.path.exists(filename):
print "File doesn't exist:", filename
sys.exit(1)
if len(sys.argv) == 3:
directory = sys.argv[2]
if not os.path.exists(directory) or not os.path.isdir(directory):
print "Directory doesn't exist:", directory
sys.exit(1)
mb = mailbox.mbox(filename)
nmes = len(mb)
os.chdir(directory)
for i in range(len(mb)):
if (VERBOSE >= 2):
print "Analyzing message number", i
mes = mb.get_message(i)
em = email.message_from_string(mes.as_string())
subject = em.get('Subject')
if subject.find('=?') != -1:
ll = email.header.decode_header(subject)
subject = ""
for l in ll:
subject = subject + l[0]
em_from = em.get('From')
if em_from.find('=?') != -1:
ll = email.header.decode_header(em_from)
em_from = ""
for l in ll:
em_from = em_from + l[0]
if (VERBOSE >= 2):
print "%s - From: %s" %(subject, em_from)
filename = mes.get_filename()
# Puede tener filename siendo multipart???
if em.is_multipart():
for payl in em.get_payload():
extract_attachment(payl)
else:
extract_attachment(em)
print "\n--------------"
print "Total attachments extracted:", attachments
print "Total attachments skipped:", skipped