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
393
394
395
396
397
398
399
400
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "logger.h"
#include "pldstr.h"
#include "bytedecoders.h"
#include "olestream-unwrap.h"
#include "localization.h"
#include "MALLOC.h"
#include "charEncoding.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif
#define DUW if (oleuw->debug)
struct OLE10_header
{
unsigned char data[6];
char *attach_name;
unsigned char data2[8];
char *fname_1;
char *fname_2;
size_t attach_size;
size_t attach_size_1;
size_t attach_start_offset;
};
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_init
Returns Type : int
----Parameter List
1. struct OLEUNWRAP_object *oleuw ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_init( struct OLEUNWRAP_object *oleuw )
{
oleuw->debug = 0;
oleuw->verbose = 0;
oleuw->filename_report_fn = NULL;
return OLEUW_OK;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_set_debug
Returns Type : int
----Parameter List
1. struct OLEUNWRAP_object *oleuw,
2. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_set_debug( struct OLEUNWRAP_object *oleuw, int level )
{
oleuw->debug = level;
return OLEUW_OK;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_set_verbose
Returns Type : int
----Parameter List
1. struct OLEUNWRAP_object *oleuw,
2. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_set_verbose( struct OLEUNWRAP_object *oleuw, int level )
{
oleuw->verbose = level;
return OLEUW_OK;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_set_save_unknown_streams
Returns Type : int
----Parameter List
1. struct OLEUNWRAP_object *oleuw,
2. int level ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_set_save_unknown_streams( struct OLEUNWRAP_object *oleuw, int level )
{
oleuw->save_unknown_streams = level;
return OLEUW_OK;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_save_stream
Returns Type : int
----Parameter List
1. char *fname,
2. char *stream,
3. size_t bytes ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, char *decode_path, char *stream, size_t bytes )
{
char *full_name;
FILE *f;
int result = 0;
DUW LOGGER_log("%s:%d:OLEUNWRAP_save_stream:DEBUG: fname=%s, decodepath=%s, size=%ld"
, FL
, fname
, decode_path
, bytes
);
full_name = PLD_dprintf("%s/%s", decode_path, fname );
if (full_name == NULL)
{
LOGGER_log(_("%s:%d:OLEUNWRAP_save_stream:ERROR: Unable to create filename string from '%s' and '%s'"), FL, fname, decode_path);
return -1;
}
wcfopen(f, full_name, "wb");
if (f != NULL)
{
size_t write_count;
write_count = fwrite( stream, 1, bytes, f );
if (write_count != bytes)
{
LOGGER_log(_("%s:%d:OLEUNWRAP_save_stream:WARNING: Only wrote %d of %d bytes to file %s\n"), FL, write_count, bytes, full_name );
}
fclose(f);
}
else
{
LOGGER_log(_("%s:%d:OLEUNWRAP_save_stream:ERROR: Unable to open %s for writing (%s)\n"), FL, full_name, strerror(errno));
result = -1;
}
if (full_name)
{
FREE(full_name);
}
DUW LOGGER_log("%s:%d:OLEUNWRAP_save_stream:DEBUG: Done saving '%s'", FL, fname);
return result;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_sanitize_filename
Returns Type : int
----Parameter List
1. char *fname ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_sanitize_filename( char *fname )
{
while (*fname)
{
if ( !isalnum((int)*fname) && (*fname != '.') )
{
*fname = '_';
}
if ( (*fname < ' ') || (*fname > '~') )
{
*fname = '_';
}
fname++;
}
return 0;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_decode_attachment
Returns Type : int
----Parameter List
1. char *stream ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, size_t stream_size, char *decode_path )
{
struct OLE10_header oh;
char *sp = stream;
char *data_start_point = stream;
int result = OLEUW_OK;
/* Get the data size*/
oh.attach_size_1 = (size_t)get_4byte_value( (unsigned char *) sp );
sp += 4;
DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: attachsize = %d, stream length = %d\n", FL, oh.attach_size_1, stream_size );
oh.attach_start_offset = (stream_size - oh.attach_size_1);
data_start_point = stream + oh.attach_start_offset;
/*if (oh.attach_start_offset == 4)*/
if (oh.attach_start_offset < 4)
{
/* If we only had the stream byte-length in our header*/
/* then we know we don't have a complex header.*/
oh.attach_name = PLD_dprintf("unknown-%ld", oh.attach_size_1);
oh.attach_size = oh.attach_size_1;
}
else
{
DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Decoding file information header", FL);
/* Unknown memory segment*/
memcpy( oh.data, sp, 2 );
sp += 2;
/* Full attachment string*/
oh.attach_name = strdup( sp );
sp = sp + strlen(oh.attach_name) + 1;
/* Attachment full path*/
oh.fname_1 = strdup( sp );
sp += strlen(oh.fname_1) + 1;
/* Unknown memory segment*/
memcpy( oh.data2, sp, 8 );
sp = sp + 8;
/* Attachment full path*/
oh.fname_2 = strdup( sp );
sp += strlen(oh.fname_2) + 1;
oh.attach_size = (size_t)get_4byte_value( (unsigned char*) sp );
sp += 4;
if (oh.attach_size > stream_size)
{
oh.attach_size = stream_size;
}
data_start_point = sp;
}
DUW LOGGER_log(_("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Attachment %s:%s:%s size = %d\n"), FL, oh.attach_name, oh.fname_1, oh.fname_2, oh.attach_size );
/** 20050119:2053:PLD - Added to sanitize 8-bit filenames **/
/** Sanitize the output filename **/
OLEUNWRAP_sanitize_filename(oh.attach_name);
OLEUNWRAP_sanitize_filename(oh.fname_1);
OLEUNWRAP_sanitize_filename(oh.fname_2);
result = OLEUNWRAP_save_stream( oleuw, oh.attach_name, decode_path, data_start_point, oh.attach_size );
if (result == OLEUW_OK)
{
if (oleuw->debug > 0)
{
LOGGER_log(_("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Calling reporter for the filename"), FL);
}
if ((oleuw->verbose > 0) && (oleuw->filename_report_fn != NULL))
{
oleuw->filename_report_fn(oh.attach_name);
}
/* Do call back to reporting function*/
}
/* Clean up our previously allocated data*/
if (oh.fname_1 != NULL)
{
FREE(oh.fname_1);
}
if (oh.attach_name != NULL)
{
FREE(oh.attach_name);
}
if (oh.fname_2 != NULL)
{
FREE(oh.fname_2);
}
return OLEUW_OK;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_decodestream
Returns Type : int
----Parameter List
1. char *element_string,
2. char *stream ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_decodestream( struct OLEUNWRAP_object *oleuw, char *element_string, char *stream, size_t stream_size, char *decode_path )
{
int result = OLEUW_OK;
if (strstr(element_string, OLEUW_ELEMENT_10NATIVE_STRING) != NULL)
{
OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, decode_path );
}
else
{
if (oleuw->debug)
{
LOGGER_log(_("Unable to decode stream with element string '%s'\n"), element_string);
}
result = OLEUW_STREAM_NOT_DECODED;
}
return result;
}
/*-----------------------------------------------------------------\
Function Name : OLEUNWRAP_set_filename_report_fn
Returns Type : int
----Parameter List
1. struct OLEUNWRAP_object *oleuw,
2. int (*ptr_to_fn)(char *) ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int OLEUNWRAP_set_filename_report_fn( struct OLEUNWRAP_object *oleuw, int (*ptr_to_fn)(char *) )
{
oleuw->filename_report_fn = ptr_to_fn;
return 0;
}
|