2011-06-09 21:21:06 +00:00
|
|
|
/* mime_file.c
|
|
|
|
*
|
|
|
|
* MIME file format decoder for the Wiretap library.
|
|
|
|
*
|
2011-07-04 01:39:13 +00:00
|
|
|
* This is for use with Wireshark dissectors that handle file
|
|
|
|
* formats (e.g., because they handle a particular MIME media type).
|
|
|
|
* It breaks the file into chunks of at most WTAP_MAX_PACKET_SIZE,
|
|
|
|
* each of which is reported as a packet, so that files larger than
|
|
|
|
* WTAP_MAX_PACKET_SIZE can be handled by reassembly.
|
|
|
|
*
|
|
|
|
* The "MIME file" dissector does the reassembly, and hands the result
|
|
|
|
* off to heuristic dissectors to try to identify the file's contents.
|
|
|
|
*
|
2011-06-09 21:21:06 +00:00
|
|
|
* Wiretap Library
|
|
|
|
* 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 2
|
|
|
|
* 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, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-06-09 21:21:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "wtap-int.h"
|
|
|
|
#include "file_wrappers.h"
|
2014-07-15 23:40:46 +00:00
|
|
|
#include <wsutil/buffer.h>
|
2011-06-09 21:21:06 +00:00
|
|
|
#include "mime_file.h"
|
|
|
|
|
|
|
|
typedef struct {
|
2011-09-01 09:43:10 +00:00
|
|
|
const guint8 *magic;
|
2011-06-09 22:01:57 +00:00
|
|
|
guint magic_len;
|
2011-06-09 21:21:06 +00:00
|
|
|
} mime_files_t;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Written by Marton Nemeth <nm127@freemail.hu>
|
|
|
|
* Copyright 2009 Marton Nemeth
|
|
|
|
* The JPEG and JFIF specification can be found at:
|
|
|
|
*
|
|
|
|
* http://www.jpeg.org/public/jfif.pdf
|
|
|
|
* http://www.w3.org/Graphics/JPEG/itu-t81.pdf
|
|
|
|
*/
|
2011-09-01 09:43:10 +00:00
|
|
|
static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
|
2011-06-09 21:21:06 +00:00
|
|
|
0xFF /* start of the next marker */
|
|
|
|
};
|
|
|
|
|
2012-04-04 20:52:52 +00:00
|
|
|
/* <?xml */
|
2013-01-30 19:26:05 +00:00
|
|
|
static const guint8 xml_magic[] = { '<', '?', 'x', 'm', 'l' };
|
2013-01-30 22:55:32 +00:00
|
|
|
static const guint8 png_magic[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' };
|
2013-01-30 19:26:05 +00:00
|
|
|
static const guint8 gif87a_magic[] = { 'G', 'I', 'F', '8', '7', 'a'};
|
|
|
|
static const guint8 gif89a_magic[] = { 'G', 'I', 'F', '8', '9', 'a'};
|
2013-10-23 01:36:49 +00:00
|
|
|
static const guint8 elf_magic[] = { 0x7F, 'E', 'L', 'F'};
|
2012-04-04 20:52:52 +00:00
|
|
|
|
2011-06-09 21:21:06 +00:00
|
|
|
static const mime_files_t magic_files[] = {
|
2012-04-04 20:52:52 +00:00
|
|
|
{ jpeg_jfif_magic, sizeof(jpeg_jfif_magic) },
|
2013-01-30 19:26:05 +00:00
|
|
|
{ xml_magic, sizeof(xml_magic) },
|
2013-01-30 22:55:32 +00:00
|
|
|
{ png_magic, sizeof(png_magic) },
|
|
|
|
{ gif87a_magic, sizeof(gif87a_magic) },
|
2013-10-23 01:36:49 +00:00
|
|
|
{ gif89a_magic, sizeof(gif89a_magic) },
|
|
|
|
{ elf_magic, sizeof(elf_magic) }
|
2011-06-09 21:21:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define N_MAGIC_TYPES (sizeof(magic_files) / sizeof(magic_files[0]))
|
|
|
|
|
2013-12-31 23:48:02 +00:00
|
|
|
/*
|
|
|
|
* Impose a not-too-large limit on the maximum file size, to avoid eating
|
|
|
|
* up 99% of the (address space, swap partition, disk space for swap/page
|
|
|
|
* files); if we were to return smaller chunks and let the dissector do
|
|
|
|
* reassembly, it would *still* have to allocate a buffer the size of
|
|
|
|
* the file, so it's not as if we'd neve try to allocate a buffer the
|
|
|
|
* size of the file.
|
|
|
|
*
|
|
|
|
* For now, go for 16MB.
|
|
|
|
*/
|
|
|
|
#define MAX_FILE_SIZE (16*1024*1024)
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
mime_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
|
2013-12-31 23:48:02 +00:00
|
|
|
Buffer *buf, int *err, gchar **err_info)
|
2013-05-18 03:02:04 +00:00
|
|
|
{
|
2013-12-31 23:48:02 +00:00
|
|
|
gint64 file_size;
|
|
|
|
int packet_size;
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if ((file_size = wtap_file_size(wth, err)) == -1)
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-12-31 23:48:02 +00:00
|
|
|
|
|
|
|
if (file_size > MAX_FILE_SIZE) {
|
|
|
|
/*
|
|
|
|
* Don't blow up trying to allocate space for an
|
|
|
|
* immensely-large file.
|
|
|
|
*/
|
|
|
|
*err = WTAP_ERR_BAD_FILE;
|
|
|
|
*err_info = g_strdup_printf("mime_file: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
|
|
|
|
file_size, MAX_FILE_SIZE);
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-12-31 23:48:02 +00:00
|
|
|
}
|
|
|
|
packet_size = (int)file_size;
|
|
|
|
|
2014-05-24 18:28:30 +00:00
|
|
|
phdr->rec_type = REC_TYPE_PACKET;
|
2013-12-31 23:48:02 +00:00
|
|
|
phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
|
2013-05-18 03:02:04 +00:00
|
|
|
|
|
|
|
phdr->caplen = packet_size;
|
|
|
|
phdr->len = packet_size;
|
2013-12-31 23:48:02 +00:00
|
|
|
|
|
|
|
phdr->ts.secs = 0;
|
|
|
|
phdr->ts.nsecs = 0;
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
|
2013-05-18 03:02:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
|
2011-06-09 21:21:06 +00:00
|
|
|
{
|
2013-12-31 23:48:02 +00:00
|
|
|
gint64 offset;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
2013-12-31 23:48:02 +00:00
|
|
|
*err = 0;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
offset = file_tell(wth->fh);
|
2011-06-09 21:21:06 +00:00
|
|
|
|
2013-12-31 23:48:02 +00:00
|
|
|
/* there is only ever one packet */
|
|
|
|
if (offset != 0)
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-05-18 03:02:04 +00:00
|
|
|
|
2013-12-31 23:48:02 +00:00
|
|
|
*data_offset = offset;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
return mime_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
|
2011-06-09 21:21:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
mime_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
|
2011-06-09 21:21:06 +00:00
|
|
|
{
|
2013-12-31 23:48:02 +00:00
|
|
|
/* there is only one packet */
|
|
|
|
if (seek_off > 0) {
|
|
|
|
*err = 0;
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2011-06-09 21:21:06 +00:00
|
|
|
}
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-05-18 03:02:04 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
return mime_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
|
2011-06-09 21:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-05-09 05:18:49 +00:00
|
|
|
mime_file_open(wtap *wth, int *err, gchar **err_info)
|
2011-06-09 21:21:06 +00:00
|
|
|
{
|
2011-06-09 22:01:57 +00:00
|
|
|
char magic_buf[128]; /* increase buffer size when needed */
|
2011-06-09 21:21:06 +00:00
|
|
|
int bytes_read;
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
gboolean found_file;
|
|
|
|
/* guint file_ok; */
|
2011-06-09 21:21:06 +00:00
|
|
|
guint i;
|
|
|
|
|
2011-06-09 22:01:57 +00:00
|
|
|
guint read_bytes = 0;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
|
|
|
for (i = 0; i < N_MAGIC_TYPES; i++)
|
|
|
|
read_bytes = MAX(read_bytes, magic_files[i].magic_len);
|
|
|
|
|
2012-12-21 03:32:16 +00:00
|
|
|
read_bytes = (guint)MIN(read_bytes, sizeof(magic_buf));
|
2014-05-09 05:18:49 +00:00
|
|
|
bytes_read = file_read(magic_buf, read_bytes, wth->fh);
|
2011-06-09 21:21:06 +00:00
|
|
|
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
if (bytes_read < 0) {
|
2014-05-09 05:18:49 +00:00
|
|
|
*err = file_error(wth->fh, err_info);
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bytes_read == 0)
|
|
|
|
return 0;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
found_file = FALSE;
|
|
|
|
for (i = 0; i < N_MAGIC_TYPES; i++) {
|
|
|
|
if ((guint) bytes_read >= magic_files[i].magic_len && !memcmp(magic_buf, magic_files[i].magic, MIN(magic_files[i].magic_len, (guint) bytes_read))) {
|
|
|
|
if (!found_file) {
|
|
|
|
found_file = TRUE;
|
|
|
|
/* file_ok = i; */
|
|
|
|
} else
|
|
|
|
return 0; /* many files matched, bad file */
|
2011-06-09 21:21:06 +00:00
|
|
|
}
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
}
|
2011-06-09 21:21:06 +00:00
|
|
|
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
if (!found_file)
|
|
|
|
return 0;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
return -1;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MIME;
|
|
|
|
wth->file_encap = WTAP_ENCAP_MIME;
|
|
|
|
wth->tsprecision = WTAP_FILE_TSPREC_SEC;
|
|
|
|
wth->subtype_read = mime_read;
|
|
|
|
wth->subtype_seek_read = mime_seek_read;
|
|
|
|
wth->snapshot_length = 0;
|
2011-06-09 21:21:06 +00:00
|
|
|
|
Do not call wtap_file_read_unknown_bytes() or
wtap_file_read_expected_bytes() from an open routine - open routines are
supposed to return -1 on error, 0 if the file doesn't appear to be a
file of the specified type, or 1 if the file does appear to be a file of
the specified type, but those macros will cause the caller to return
FALSE on errors (so that, even if there's an I/O error, it reports "the
file isn't a file of the specified type" rather than "we got an error
trying to read the file").
When doing reads in an open routine before we've concluded that the file
is probably of the right type, return 0, rather than -1, if we get
WTAP_ERR_SHORT_READ - if we don't have enough data to check whether a
file is of a given type, we should keep trying other types, not give up.
For reads done *after* we've concluded the file is probably of the right
type, if a read doesn't return the number of bytes we asked for, but
returns an error of 0, return WTAP_ERR_SHORT_READ - the file is
apparently cut short.
For NetMon and NetXRay/Windows Sniffer files, use a #define for the
magic number size, and use that for both magic numbers.
svn path=/trunk/; revision=46803
2012-12-27 12:19:25 +00:00
|
|
|
return 1;
|
2011-06-09 21:21:06 +00:00
|
|
|
}
|