/
MboxParser.java
executable file
·392 lines (360 loc) · 14.8 KB
/
MboxParser.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Autopsy Forensic Browser
*
* Copyright 2011-2014 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.autopsy.thunderbirdparser;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.CharConversionException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.james.mime4j.dom.BinaryBody;
import org.apache.james.mime4j.dom.Body;
import org.apache.james.mime4j.dom.Entity;
import org.apache.james.mime4j.dom.Message;
import org.apache.james.mime4j.dom.Multipart;
import org.apache.james.mime4j.dom.TextBody;
import org.apache.james.mime4j.dom.address.AddressList;
import org.apache.james.mime4j.dom.address.Mailbox;
import org.apache.james.mime4j.dom.address.MailboxList;
import org.apache.james.mime4j.dom.field.ContentDispositionField;
import org.apache.james.mime4j.dom.field.ContentTypeField;
import org.apache.james.mime4j.mboxiterator.CharBufferWrapper;
import org.apache.james.mime4j.mboxiterator.MboxIterator;
import org.apache.james.mime4j.message.DefaultMessageBuilder;
import org.apache.james.mime4j.stream.MimeConfig;
import org.apache.tika.parser.txt.CharsetDetector;
import org.apache.tika.parser.txt.CharsetMatch;
import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.ingest.IngestServices;
/**
* A parser that extracts information about email messages and attachments from
* a mbox file.
*
* @author jwallace
*/
class MboxParser {
private static final Logger logger = Logger.getLogger(MboxParser.class.getName());
private DefaultMessageBuilder messageBuilder;
private IngestServices services;
private StringBuilder errors;
/**
* The mime type string for html text.
*/
private static final String HTML_TYPE = "text/html"; //NON-NLS
/**
* The local path of the mbox file.
*/
private String localPath;
MboxParser(IngestServices services, String localPath) {
this.services = services;
this.localPath = localPath;
messageBuilder = new DefaultMessageBuilder();
MimeConfig config = MimeConfig.custom().setMaxLineLen(-1).build();
// disable line length checks.
messageBuilder.setMimeEntityConfig(config);
errors = new StringBuilder();
}
static boolean isValidMimeTypeMbox(byte[] buffer) {
return (new String(buffer)).startsWith("From "); //NON-NLS
}
/**
* Parse the mbox file and get the email messages.
* @param mboxFile
* @return a list of the email messages in the mbox file.
*/
List<EmailMessage> parse(File mboxFile) {
// Detect possible charsets
List<CharsetEncoder> encoders = getPossibleEncoders(mboxFile);
CharsetEncoder theEncoder = null;
Iterable<CharBufferWrapper> mboxIterator = null;
// Loop through the possible encoders and find the first one that works.
// That will usually be one of the first ones.
for (CharsetEncoder encoder : encoders) {
try {
mboxIterator = MboxIterator.fromFile(mboxFile).charset(encoder.charset()).build();
theEncoder = encoder;
break;
} catch (CharConversionException | UnsupportedCharsetException ex) {
// Not the right encoder
} catch (IllegalArgumentException ex) {
// Not the right encoder
} catch (IOException ex) {
logger.log(Level.WARNING, "couldn't find mbox file.", ex); //NON-NLS
addErrorMessage(NbBundle.getMessage(this.getClass(), "MboxParser.parse.errMsg.failedToReadFile"));
return new ArrayList<>();
}
}
// If no encoders work, post an error message and return.
if (mboxIterator == null || theEncoder == null) {
addErrorMessage(NbBundle.getMessage(this.getClass(), "MboxParser.parse.errMsg.couldntFindCharset"));
return new ArrayList<>();
}
List<EmailMessage> emails = new ArrayList<>();
long failCount = 0;
// Parse each message and extract an EmailMessage structure
for (CharBufferWrapper message : mboxIterator) {
try {
Message msg = messageBuilder.parseMessage(message.asInputStream(theEncoder.charset()));
emails.add(extractEmail(msg));
} catch (RuntimeException | IOException ex) {
logger.log(Level.WARNING, "Failed to get message from mbox: {0}", ex.getMessage()); //NON-NLS
failCount++;
}
}
if (failCount > 0) {
addErrorMessage(
NbBundle.getMessage(this.getClass(), "MboxParser.parse.errMsg.failedToParseNMsgs", failCount));
}
return emails;
}
String getErrors() {
return errors.toString();
}
/**
* Use the information stored in the given mime4j message to populate an
* EmailMessage.
*
* @param msg
* @return
*/
private EmailMessage extractEmail(Message msg) {
EmailMessage email = new EmailMessage();
// Basic Info
email.setSender(getAddresses(msg.getFrom()));
email.setRecipients(getAddresses(msg.getTo()));
email.setBcc(getAddresses(msg.getBcc()));
email.setCc(getAddresses(msg.getCc()));
email.setSubject(msg.getSubject());
email.setSentDate(msg.getDate());
email.setLocalPath(localPath);
// Body
if (msg.isMultipart()) {
handleMultipart(email, (Multipart) msg.getBody());
} else {
handleTextBody(email, (TextBody) msg.getBody(), msg.getMimeType());
}
return email;
}
/**
* Handle a multipart mime message.
* Recursively calls handleMultipart if one of the body parts is another
* multipart. Otherwise, calls the correct method to extract information out
* of each part of the body.
*
* @param email
* @param multi
*/
private void handleMultipart(EmailMessage email, Multipart multi) {
for (Entity e : multi.getBodyParts()) {
if (e.isMultipart()) {
handleMultipart(email, (Multipart) e.getBody());
} else if (e.getDispositionType() != null
&& e.getDispositionType().equals(ContentDispositionField.DISPOSITION_TYPE_ATTACHMENT)) {
handleAttachment(email, e);
} else if (e.getMimeType().equals(HTML_TYPE) ||
e.getMimeType().equals(ContentTypeField.TYPE_TEXT_PLAIN)) {
handleTextBody(email, (TextBody) e.getBody(), e.getMimeType());
} else {
// Ignore other types.
}
}
}
/**
* Extract text out of a body part of the message.
*
* Handles text and html mime types. Throws away all other types. (only other
* example I've seen is text/calendar)
* @param email
* @param tb
* @param type The Mime type of the body.
*/
private void handleTextBody(EmailMessage email, TextBody tb, String type) {
BufferedReader r;
try {
r = new BufferedReader(tb.getReader());
StringBuilder bodyString = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
bodyString.append(line).append("\n");
}
switch (type) {
case ContentTypeField.TYPE_TEXT_PLAIN:
email.setTextBody(bodyString.toString());
break;
case HTML_TYPE:
email.setHtmlBody(bodyString.toString());
break;
default:
// Not interested in other text types.
break;
}
} catch (IOException ex) {
logger.log(Level.WARNING, "Error getting text body of mbox message", ex); //NON-NLS
}
}
/**
* Extract the attachment out of the given entity.
* Should only be called if e.getDispositionType() == "attachment"
* @param email
* @param e
*/
private void handleAttachment(EmailMessage email, Entity e) {
String outputDirPath = ThunderbirdMboxFileIngestModule.getModuleOutputPath() + File.separator;
String filename = e.getFilename();
// sanitize name. Had an attachment with a Japanese encoded path that
// invalid characters and attachment could not be saved.
filename = filename.replaceAll("\\?", "_");
filename = filename.replaceAll("<", "_");
filename = filename.replaceAll(">", "_");
filename = filename.replaceAll(":", "_");
filename = filename.replaceAll("\"", "_");
filename = filename.replaceAll("/", "_");
filename = filename.replaceAll("\\\\", "_");
filename = filename.replaceAll("|", "_");
filename = filename.replaceAll("\\*", "_");
// also had some crazy long names, so make random one if we get those.
// also from Japanese image that had encoded name
if (filename.length() > 64) {
filename = UUID.randomUUID().toString();
}
String uniqueFilename = filename + "-" + email.getSentDate();
String outPath = outputDirPath + uniqueFilename;
FileOutputStream fos;
BinaryBody bb;
try {
fos = new FileOutputStream(outPath);
} catch (FileNotFoundException ex) {
addErrorMessage(
NbBundle.getMessage(this.getClass(),
"MboxParser.handleAttch.errMsg.failedToCreateOnDisk", outPath));
logger.log(Level.INFO, "Failed to create file output stream for: " + outPath, ex); //NON-NLS
return;
}
try {
Body b = e.getBody();
if (b instanceof BinaryBody) {
bb = (BinaryBody) b;
bb.writeTo(fos);
} else {
// This could potentially be other types. Only seen this once.
}
} catch (IOException ex) {
logger.log(Level.INFO, "Failed to write mbox email attachment to disk.", ex); //NON-NLS
addErrorMessage(NbBundle.getMessage(this.getClass(), "MboxParser.handleAttch.failedWriteToDisk", filename));
return;
} finally {
try {
fos.close();
} catch (IOException ex) {
logger.log(Level.INFO, "Failed to close file output stream", ex); //NON-NLS
}
}
EmailMessage.Attachment attach = new EmailMessage.Attachment();
attach.setName(filename);
attach.setLocalPath(ThunderbirdMboxFileIngestModule.getRelModuleOutputPath()
+ File.separator + uniqueFilename);
attach.setSize(new File(outPath).length());
email.addAttachment(attach);
}
/**
* Get a String representation of the MailboxList (which is a list of email
* addresses).
* @param mailboxList
* @return
*/
private String getAddresses(MailboxList mailboxList) {
if (mailboxList == null) {
return "";
}
StringBuilder addresses = new StringBuilder();
for (Mailbox m : mailboxList) {
addresses.append(m.toString()).append("; ");
}
return addresses.toString();
}
/**
* Get a String representation of the AddressList (which is a list of email
* addresses).
* @param addressList
* @return
*/
private String getAddresses(AddressList addressList) {
return (addressList == null) ? "" : getAddresses(addressList.flatten());
}
/**
* Get a list of the possible encoders for the given mboxFile using Tika's
* CharsetDetector. At a minimum, returns the standard built in charsets.
* @param mboxFile
* @return
*/
private List<CharsetEncoder> getPossibleEncoders(File mboxFile) {
InputStream is;
List<CharsetEncoder> possibleEncoders = new ArrayList<>();
possibleEncoders.add(StandardCharsets.ISO_8859_1.newEncoder());
possibleEncoders.add(StandardCharsets.US_ASCII.newEncoder());
possibleEncoders.add(StandardCharsets.UTF_16.newEncoder());
possibleEncoders.add(StandardCharsets.UTF_16BE.newEncoder());
possibleEncoders.add(StandardCharsets.UTF_16LE.newEncoder());
possibleEncoders.add(StandardCharsets.UTF_8.newEncoder());
try {
is = new BufferedInputStream(new FileInputStream(mboxFile));
} catch (FileNotFoundException ex) {
logger.log(Level.WARNING, "Failed to find mbox file while detecting charset"); //NON-NLS
return possibleEncoders;
}
try {
CharsetDetector detector = new CharsetDetector();
detector.setText(is);
CharsetMatch[] matches = detector.detectAll();
for (CharsetMatch match : matches) {
try {
possibleEncoders.add(Charset.forName(match.getName()).newEncoder());
} catch (UnsupportedCharsetException | IllegalCharsetNameException ex) {
// Don't add unsupported charsets to the list
}
}
return possibleEncoders;
} catch (IOException | IllegalArgumentException ex) {
logger.log(Level.WARNING, "Failed to detect charset of mbox file.", ex); //NON-NLS
return possibleEncoders;
} finally {
try {
is.close();
} catch (IOException ex) {
logger.log(Level.INFO, "Failed to close input stream"); //NON-NLS
}
}
}
private void addErrorMessage(String msg) {
errors.append("<li>").append(msg).append("</li>"); //NON-NLS
}
}