2 * Routines for tracking & saving objects (files) found in SMB streams
3 * See also: export_object.c / export_object.h for common code
4 * Initial file, prototypes and general structure initially copied
5 * from export_object_http.c
7 * Copyright 2010, David Perez & Jose Pico from TADDONG S.L.
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
37 #include <epan/packet.h>
38 #include <epan/dissectors/packet-smb.h>
41 #include "gtk/export_object.h"
44 /* These flags show what kind of data the object contains
45 (designed to be or'ed) */
46 #define SMB_EO_CONTAINS_NOTHING 0x00
47 #define SMB_EO_CONTAINS_READS 0x01
48 #define SMB_EO_CONTAINS_WRITES 0x02
49 #define SMB_EO_CONTAINS_READSANDWRITES 0x03
50 #define LEGAL_FILENAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ1234567890_."
52 static const value_string smb_eo_contains_string[]={
53 {SMB_EO_CONTAINS_NOTHING, ""},
54 {SMB_EO_CONTAINS_READS, "R"},
55 {SMB_EO_CONTAINS_WRITES, "W"},
56 {SMB_EO_CONTAINS_READSANDWRITES, "R&W"},
61 /* This struct contains the relationship between
62 the row# in the export_object window and the file being captured;
63 the row# in this GSList will match the row# in the entry list */
65 typedef struct _active_file {
67 guint64 file_length; /* The last free reported offset */
68 /* We treat it as the file length */
69 guint64 data_gathered; /* The actual total of data gathered */
70 guint8 flag_contains; /* What kind of data it contains */
71 GSList *free_chunk_list;
72 /* A list of virtual "holes" in the */
73 /* file stream stored in memory */
74 gboolean is_out_of_memory;
75 /* TRUE if we cannot allocate memory */
76 /* memory for this file */
79 /* This is the GSList that will contain all the files that we are tracking */
80 static GSList *GSL_active_files = NULL;
82 /* We define a free chunk in a file as an start offset and end offset
83 Consider a free chunk as a "hole" in a file that we are capturing */
84 typedef struct _free_chunk {
89 /* insert_chunk function will recalculate the free_chunk_list, the data_size,
90 the end_of_file, and the data_gathered as appropriate.
91 It will also insert the data chunk that is coming in the right
92 place of the file in memory.
94 file->data_gathered contains the real data gathered independently
96 file->file_length contains the length of the file in memory, i.e.,
97 the last offset captured. In most cases, the real
98 file length would be different.
101 insert_chunk(active_file *file, export_object_entry_t *entry, const smb_eo_t *eo_info)
103 guint nfreechunks = g_slist_length(file->free_chunk_list);
105 free_chunk *current_free_chunk;
106 free_chunk *new_free_chunk;
107 guint64 chunk_offset=eo_info->smb_file_offset;
108 guint64 chunk_length=eo_info->payload_len;
109 guint64 chunk_end_offset = chunk_offset+chunk_length-1;
110 /* Size of file in memory */
111 guint64 calculated_size = chunk_offset+chunk_length;
112 gpointer dest_memory_addr;
114 /* Let's recalculate the file length and data gathered */
115 if (file->data_gathered==0 && nfreechunks==0) {
116 /* If this is the first entry for this file, we first
117 create an initial free chunk */
118 new_free_chunk=g_malloc(sizeof(free_chunk));
119 new_free_chunk->start_offset=0;
120 new_free_chunk->end_offset=MAX(file->file_length,chunk_end_offset+1)-1;
121 file->free_chunk_list=NULL;
122 file->free_chunk_list=g_slist_append(file->free_chunk_list,new_free_chunk);
125 if (chunk_end_offset > file->file_length-1) {
126 new_free_chunk=g_malloc(sizeof(free_chunk));
127 new_free_chunk->start_offset=file->file_length;
128 new_free_chunk->end_offset=chunk_end_offset;
129 file->free_chunk_list=g_slist_append(file->free_chunk_list,new_free_chunk);
133 file->file_length = MAX(file->file_length,chunk_end_offset+1);
135 for (i=0;i<nfreechunks;i++) {
136 current_free_chunk = g_slist_nth_data(file->free_chunk_list,i);
137 if (chunk_offset <= current_free_chunk->start_offset ) {
138 if (chunk_end_offset >= current_free_chunk->start_offset) {
139 if (chunk_end_offset < current_free_chunk->end_offset) {
140 file->data_gathered+=
141 (chunk_end_offset-current_free_chunk->start_offset+1);
142 current_free_chunk->start_offset=chunk_end_offset+1;
144 file->data_gathered+=
145 (current_free_chunk->end_offset-current_free_chunk->start_offset+1);
146 file->free_chunk_list =
147 g_slist_remove(file->free_chunk_list,current_free_chunk);
149 if (nfreechunks==0) { /* The free chunk list is empty */
150 g_slist_free(file->free_chunk_list);
151 file->free_chunk_list=NULL;
159 if (chunk_offset <= current_free_chunk->end_offset) {
160 if (chunk_end_offset < current_free_chunk->end_offset) {
161 new_free_chunk=g_malloc(sizeof(free_chunk));
162 new_free_chunk->start_offset=chunk_end_offset+1;
163 new_free_chunk->end_offset=current_free_chunk->end_offset;
164 current_free_chunk->end_offset=chunk_offset-1;
165 file->free_chunk_list =
166 g_slist_insert(file->free_chunk_list,new_free_chunk,i+1);
167 file->data_gathered+=chunk_length;
169 file->data_gathered+=current_free_chunk->end_offset-chunk_offset+1;
170 current_free_chunk->end_offset=chunk_offset-1;
176 /* Now, let's insert the data chunk into memory
177 ...first, we shall be able to allocate the memory */
178 if (!entry->payload_data) {
179 /* This is a New file */
180 if (calculated_size > G_MAXSIZE) {
182 * The argument to g_try_malloc() is
183 * a gsize, the maximum value of which is
184 * G_MAXSIZE. If the calculated size is
185 * bigger than that, we just say the attempt
186 * to allocate memory failed.
188 entry->payload_data=NULL;
190 entry->payload_data = g_try_malloc((gsize)calculated_size);
191 entry->payload_len=calculated_size;
193 if (!entry->payload_data) {
195 file->is_out_of_memory=TRUE;
198 /* This is an existing file in memory */
199 if (calculated_size > (guint64) entry->payload_len &&
200 !file->is_out_of_memory) {
201 /* We need more memory */
202 if (calculated_size > G_MAXSIZE) {
204 * As for g_try_malloc(), so for
207 dest_memory_addr=NULL;
209 dest_memory_addr=g_try_realloc(
211 (gsize)calculated_size);
213 if(!dest_memory_addr) {
215 file->is_out_of_memory=TRUE;
216 /* We don't have memory for this file.
217 Free the current file content from memory */
218 g_free(entry->payload_data);
219 entry->payload_data=NULL;
220 entry->payload_len=0;
222 entry->payload_data=dest_memory_addr;
223 entry->payload_len=calculated_size;
227 /* ...then, put the chunk of the file in the right place */
228 if(!file->is_out_of_memory) {
229 dest_memory_addr=entry->payload_data+chunk_offset;
230 g_memmove(dest_memory_addr,eo_info->payload_data,eo_info->payload_len);
234 /* We use this function to obtain the index in the GSL of a given file */
236 find_incoming_file(GSList *GSL_active_files,active_file *incoming_file)
239 active_file *in_list_file;
242 last=g_slist_length(GSL_active_files)-1;
244 /* We lookup in reverse order because it is more likely that the file
245 is one of the latest */
246 for (i=last;i>=0;i--) {
247 in_list_file=g_slist_nth_data(GSL_active_files, i);
248 /* The best-working criteria of two identical files is that the file
249 that is the same of the file that we are analyzing is the last one
250 in the list that has the same tid and the same fid */
251 /* note that we have excluded in_list_file->uid == incoming_file->uid
252 from the comparison, because a file can be opened by different
253 SMB users and it is still the same file */
254 if (in_list_file->tid == incoming_file->tid &&
255 in_list_file->fid == incoming_file->fid) {
264 #if !GLIB_CHECK_VERSION(2,6,0)
267 * @str_array: a %NULL-terminated array of strings.
269 * Returns the length of the given %NULL-terminated
270 * string array @str_array.
272 * Return value: length of @str_array.
277 g_strv_length (gchar **str_array)
281 g_return_val_if_fail (str_array != NULL, 0);
290 /* This is the function answering to the registered tap listener call */
292 eo_smb_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
294 export_object_list_t *object_list = tapdata;
295 const smb_eo_t *eo_info = data;
297 export_object_entry_t *entry;
298 export_object_entry_t *current_entry;
299 active_file incoming_file;
301 active_file *new_file;
302 active_file *current_file;
304 gboolean is_supported_filetype;
307 gchar **aux_string_v;
309 /* Is this an eo_smb supported file_type? (right now we only support FILE */
310 is_supported_filetype = (eo_info->fid_type==SMB_FID_TYPE_FILE);
312 /* What kind of data this packet contains? */
313 switch(eo_info->cmd) {
314 case SMB_COM_READ_ANDX:
315 contains=SMB_EO_CONTAINS_READS;
317 case SMB_COM_WRITE_ANDX:
318 contains=SMB_EO_CONTAINS_WRITES;
321 contains=SMB_EO_CONTAINS_NOTHING;
325 /* Is this data from an already tracked file or not? */
326 incoming_file.tid=eo_info->tid;
327 incoming_file.uid=eo_info->uid;
328 incoming_file.fid=eo_info->fid;
329 active_row=find_incoming_file(GSL_active_files, &incoming_file);
331 if (active_row==-1) { /* This is a new-tracked file */
332 /* Construct the entry in the list of active files */
333 entry = g_malloc(sizeof(export_object_entry_t));
334 entry->payload_data=NULL;
335 entry->payload_len=0;
336 new_file = g_malloc(sizeof(active_file));
337 new_file->tid=incoming_file.tid;
338 new_file->uid=incoming_file.uid;
339 new_file->fid=incoming_file.fid;
340 new_file->file_length = eo_info->end_of_file;
341 new_file->flag_contains=contains;
342 new_file->free_chunk_list=NULL;
343 new_file->data_gathered=0;
344 new_file->is_out_of_memory=FALSE;
345 entry->pkt_num = pinfo->fd->num;
346 entry->hostname = g_strdup(eo_info->hostname);
347 if (g_str_has_prefix(eo_info->filename,"\\")) {
348 aux_string_v = g_strsplit(eo_info->filename, "\\", -1);
349 entry->filename = g_strdup(aux_string_v[g_strv_length(aux_string_v)-1]);
350 g_strfreev(aux_string_v);
352 entry->filename = g_strdup(eo_info->filename);
355 /* Insert the first chunk in the chunk list of this file */
356 if (is_supported_filetype) {
357 insert_chunk(new_file, entry, eo_info);
360 if(new_file->is_out_of_memory) {
361 entry->content_type =
362 g_strdup_printf("%s (%"G_GUINT64_FORMAT"?/%"G_GUINT64_FORMAT") %s [mem!!]",
363 match_strval(eo_info->fid_type, smb_fid_types),
364 new_file->data_gathered,
365 new_file->file_length,
366 match_strval(contains, smb_eo_contains_string));
368 if (new_file->file_length > 0) {
369 percent=(gfloat) 100*new_file->data_gathered/new_file->file_length;
374 entry->content_type =
375 g_strdup_printf("%s (%"G_GUINT64_FORMAT"/%"G_GUINT64_FORMAT") %s [%5.2f%%]",
376 match_strval(eo_info->fid_type, smb_fid_types),
377 new_file->data_gathered,
378 new_file->file_length,
379 match_strval(contains, smb_eo_contains_string),
383 object_list->entries =
384 g_slist_append(object_list->entries, entry);
386 g_slist_append(GSL_active_files, new_file);
388 else if (is_supported_filetype) {
389 current_file=g_slist_nth_data(GSL_active_files,active_row);
390 /* Recalculate the current file flags */
391 current_file->flag_contains=current_file->flag_contains|contains;
392 current_entry=g_slist_nth_data(object_list->entries,active_row);
394 insert_chunk(current_file, current_entry, eo_info);
396 /* Modify the current_entry object_type string */
397 if(current_file->is_out_of_memory) {
398 current_entry->content_type =
399 g_strdup_printf("%s (%"G_GUINT64_FORMAT"?/%"G_GUINT64_FORMAT") %s [mem!!]",
400 match_strval(eo_info->fid_type, smb_fid_types),
401 current_file->data_gathered,
402 current_file->file_length,
403 match_strval(current_file->flag_contains, smb_eo_contains_string));
405 percent=(gfloat) 100*current_file->data_gathered/current_file->file_length;
406 current_entry->content_type =
407 g_strdup_printf("%s (%"G_GUINT64_FORMAT"/%"G_GUINT64_FORMAT") %s [%5.2f%%]",
408 match_strval(eo_info->fid_type, smb_fid_types),
409 current_file->data_gathered,
410 current_file->file_length,
411 match_strval(current_file->flag_contains, smb_eo_contains_string),
416 return TRUE; /* State changed - window should be redrawn */
418 /* This is the eo_protocoldata_reset function that is used in the export_object module
419 to cleanup any previous private data of the export object functionality before perform
420 the eo_reset function or when the window closes */
425 active_file *in_list_file;
427 /* Free any previous data structures used in previous invocation to the
428 export_object_smb function */
429 last=g_slist_length(GSL_active_files);
430 if (GSL_active_files) {
431 for (i=last-1;i>=0;i--) {
432 in_list_file=g_slist_nth_data(GSL_active_files, i);
433 if (in_list_file->free_chunk_list) {
434 g_slist_free(in_list_file->free_chunk_list);
435 in_list_file->free_chunk_list=NULL;
437 g_free(in_list_file);
439 g_slist_free(GSL_active_files);
440 GSL_active_files=NULL;
445 eo_smb_cb(GtkWidget *widget _U_, gpointer data _U_)
447 /* Call the export_object window */
448 export_object_window("smb_eo", "SMB", eo_smb_packet, eo_smb_cleanup);