41 magic_t MagicInitContext(
void)
44 const char *filename = NULL;
49 SCLogError(
"magic_open failed: %s", magic_error(ctx));
53 (void)
ConfGet(
"magic-file", &filename);
56 if (filename != NULL) {
57 if (strlen(filename) == 0) {
66 if ( (fd = fopen(filename,
"r")) == NULL) {
67 SCLogWarning(
"Error opening file: \"%s\": %s", filename, strerror(errno));
74 if (magic_load(ctx, filename) != 0) {
75 SCLogError(
"magic_load failed: %s", magic_error(ctx));
89 void MagicDeinitContext(magic_t ctx)
103 char *MagicThreadLookup(magic_t *ctx,
const uint8_t *
buf, uint32_t buflen)
105 const char *result = NULL;
108 if (
buf != NULL && buflen > 0) {
109 result = magic_buffer(*ctx, (
void *)
buf, (
size_t)buflen);
110 if (result != NULL) {
123 #if defined OS_FREEBSD || defined OS_DARWIN
124 #define MICROSOFT_OFFICE_DOC "OLE 2 Compound Document"
126 #define MICROSOFT_OFFICE_DOC "Microsoft Office Document"
130 static int MagicInitTest01(
void)
135 magic_ctx = magic_open(0);
136 if (magic_ctx == NULL) {
137 printf(
"failure retrieving magic_ctx\n");
141 if (magic_load(magic_ctx, NULL) == -1) {
142 printf(
"failure magic_load\n");
148 magic_close(magic_ctx);
153 static int MagicDetectTest01(
void)
157 char buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
158 size_t buffer_len =
sizeof(buffer);
161 magic_ctx = magic_open(0);
162 if (magic_ctx == NULL) {
163 printf(
"failure retrieving magic_ctx\n");
167 if (magic_load(magic_ctx, NULL) == -1) {
168 printf(
"magic_load failure\n");
172 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
173 if (result == NULL || strncmp(result,
"PDF document", 12) != 0) {
174 printf(
"result %p:%s, not \"PDF document\": ", result,result?result:
"(null)");
180 magic_close(magic_ctx);
185 static int MagicDetectTest02(
void)
191 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
196 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
198 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 0x00, 0x10, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
201 0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
202 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00,
203 0x97, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
204 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
206 size_t buffer_len =
sizeof(buffer);
209 magic_ctx = magic_open(0);
210 if (magic_ctx == NULL) {
211 printf(
"failure retrieving magic_ctx\n");
215 if (magic_load(magic_ctx, NULL) == -1) {
216 printf(
"magic_load failure\n");
220 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
221 if (result == NULL || strcmp(result, MICROSOFT_OFFICE_DOC) != 0) {
222 printf(
"result %p:%s, not \"Microsoft Office Document\": ", result,result?result:
"(null)");
228 magic_close(magic_ctx);
233 static int MagicDetectTest03(
void)
236 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
237 0x00, 0x00, 0x0b, 0x55, 0x2a, 0x36, 0x5e, 0xc6,
238 0x32, 0x0c, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,
239 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
241 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
242 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
243 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x61,
244 0x73, 0x69, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e,
246 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74,
247 0x2e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x4b, 0x03,
248 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
249 0x55, 0x2a, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a,
252 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6e, 0x66, 0x69,
253 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
254 0x73, 0x32, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75,
256 0x73, 0x62, 0x61, 0x72, 0x2f, 0x50, 0x4b, 0x03,
257 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b,
259 size_t buffer_len =
sizeof(buffer);
261 magic_t magic_ctx = magic_open(0);
264 FAIL_IF(magic_load(magic_ctx, NULL) == -1);
266 char *result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
269 char *
str = strstr(result,
"OpenDocument Text");
272 magic_close(magic_ctx);
277 static int MagicDetectTest04(
void)
283 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x08,
284 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c, 0x8b, 0x70,
285 0x96, 0x08, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x00,
286 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
288 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
289 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
290 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75,
291 0x6e, 0x2e, 0x78, 0x6d, 0x6c, 0x2e, 0x62, 0x61,
293 0x73, 0x65, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,
294 0x00, 0x08, 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c,
295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
296 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
298 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
299 0x2f, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,
300 0x08, 0x08, 0x00, 0xa8, 0x42, 0x1d, 0x37, 0x5d,
301 0xa7, 0xb2, 0xc1, 0xde, 0x01, 0x00, 0x00, 0x7e,
303 0x04, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x63,
304 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x78,
305 0x6d, 0x6c, 0x95, 0x54, 0x4d, 0x6f, 0xdb, 0x30,
306 0x0c, 0xbd, 0xe7, 0x57, 0x18, 0x02, 0x06, 0x6c,
308 0x07, 0xc5, 0xe9, 0xb6, 0xc3, 0x22, 0xc4, 0x29,
309 0x86, 0x7d, 0x00, 0x05, 0x8a, 0x9d, 0xb2, 0x43,
310 0x8f, 0xb2, 0x24, 0xa7, 0xc2, 0x64, 0xc9, 0x15,
312 size_t buffer_len =
sizeof(buffer);
315 magic_ctx = magic_open(0);
316 if (magic_ctx == NULL) {
317 printf(
"failure retrieving magic_ctx\n");
321 if (magic_load(magic_ctx, NULL) == -1) {
322 printf(
"magic_load failure\n");
326 result = (
char *)magic_buffer(magic_ctx, (
void *)buffer, buffer_len);
327 if (result == NULL || strncmp(result,
"OpenOffice.org 1.x", 18) != 0) {
328 printf(
"result %p:%s, not \"OpenOffice.org 1.x\": ", result,result?result:
"(null)");
334 magic_close(magic_ctx);
340 static int MagicDetectTest05(
void)
342 const char *result = NULL;
344 uint8_t buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
345 size_t buffer_len =
sizeof(buffer);
349 ctx = MagicInitContext();
352 result = MagicThreadLookup(&ctx, buffer, buffer_len);
353 if (result == NULL || strncmp(result,
"PDF document", 12) != 0) {
354 printf(
"result %p:%s, not \"PDF document\": ", result,result?result:
"(null)");
360 MagicDeinitContext(ctx);
366 static int MagicDetectTest06(
void)
368 const char *result = NULL;
370 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
371 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
372 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
373 0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
375 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
377 0x96, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
378 0x00, 0x10, 0x00, 0x00, 0x98, 0x00, 0x00, 0x00,
380 0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
381 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00,
382 0x97, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
383 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
385 size_t buffer_len =
sizeof(buffer);
388 if (MagicInit() < 0) {
389 printf(
"MagicInit() failure\n");
393 result = MagicGlobalLookup(buffer, buffer_len);
394 if (result == NULL || strcmp(result, MICROSOFT_OFFICE_DOC) != 0) {
395 printf(
"result %p:%s, not \"Microsoft Office Document\": ", result,result?result:
"(null)");
407 static int MagicDetectTest07(
void)
409 const char *result = NULL;
412 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
413 0x00, 0x00, 0x0b, 0x55, 0x2a, 0x36, 0x5e, 0xc6,
414 0x32, 0x0c, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,
415 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
417 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
418 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
419 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x6f, 0x61,
420 0x73, 0x69, 0x73, 0x2e, 0x6f, 0x70, 0x65, 0x6e,
422 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74,
423 0x2e, 0x74, 0x65, 0x78, 0x74, 0x50, 0x4b, 0x03,
424 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
425 0x55, 0x2a, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
427 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a,
428 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6e, 0x66, 0x69,
429 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e,
430 0x73, 0x32, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x75,
432 0x73, 0x62, 0x61, 0x72, 0x2f, 0x50, 0x4b, 0x03,
433 0x04, 0x14, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0b,
435 size_t buffer_len =
sizeof(buffer);
437 ctx = MagicInitContext();
440 result = MagicThreadLookup(&ctx, buffer, buffer_len);
443 char *
str = strstr(result,
"OpenDocument Text");
446 MagicDeinitContext(ctx);
451 static int MagicDetectTest08(
void)
453 const char *result = NULL;
456 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x08,
457 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c, 0x8b, 0x70,
458 0x96, 0x08, 0x1c, 0x00, 0x00, 0x00, 0x1c, 0x00,
459 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x6d, 0x69,
461 0x6d, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70,
462 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
463 0x6e, 0x2f, 0x76, 0x6e, 0x64, 0x2e, 0x73, 0x75,
464 0x6e, 0x2e, 0x78, 0x6d, 0x6c, 0x2e, 0x62, 0x61,
466 0x73, 0x65, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,
467 0x00, 0x08, 0x00, 0x00, 0x52, 0x7b, 0x86, 0x3c,
468 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
471 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
472 0x2f, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,
473 0x08, 0x08, 0x00, 0xa8, 0x42, 0x1d, 0x37, 0x5d,
474 0xa7, 0xb2, 0xc1, 0xde, 0x01, 0x00, 0x00, 0x7e,
476 0x04, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x63,
477 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2e, 0x78,
478 0x6d, 0x6c, 0x95, 0x54, 0x4d, 0x6f, 0xdb, 0x30,
480 0x0c, 0xbd, 0xe7, 0x57, 0x18, 0x02, 0x06, 0x6c,
481 0x07, 0xc5, 0xe9, 0xb6, 0xc3, 0x22, 0xc4, 0x29,
482 0x86, 0x7d, 0x00, 0x05, 0x8a, 0x9d, 0xb2, 0x43,
483 0x8f, 0xb2, 0x24, 0xa7, 0xc2, 0x64, 0xc9, 0x15,
485 size_t buffer_len =
sizeof(buffer);
488 ctx = MagicInitContext();
491 result = MagicThreadLookup(&ctx, buffer, buffer_len);
492 if (result == NULL || strncmp(result,
"OpenOffice.org 1.x", 18) != 0) {
493 printf(
"result %p:%s, not \"OpenOffice.org 1.x\": ", result,result?result:
"(null)");
499 MagicDeinitContext(ctx);
504 static int MagicDetectTest09(
void)
506 const char *result1 = NULL;
507 const char *result2 = NULL;
508 uint8_t buffer[] = { 0x25,
'P',
'D',
'F',
'-',
'1',
'.',
'3', 0x0d, 0x0a};
509 size_t buffer_len =
sizeof(buffer);
512 if (MagicInit() < 0) {
513 printf(
"MagicInit() failure\n");
517 result1 = MagicGlobalLookup(buffer, buffer_len);
518 if (result1 == NULL || strncmp(result1,
"PDF document", 12) != 0) {
519 printf(
"result %p:%s, not \"PDF document\": ", result1,result1?result1:
"(null)");
523 result2 = MagicGlobalLookup(buffer, buffer_len);
524 if (result2 == NULL || strncmp(result2,
"PDF document", 12) != 0) {
525 printf(
"result %p:%s, not \"PDF document\": ", result2,result2?result2:
"(null)");
529 if (result1 != result2) {
530 printf(
"pointers not equal, weird... %p != %p: ", result1, result2);
542 static int MagicDetectTest10ValgrindError(
void)
544 const char *result = NULL;
547 0xFF,0xD8,0xFF,0xE0,0x00,0x10,0x4A,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2C,
548 0x01,0x2C,0x00,0x00,0xFF,0xFE,0x00,0x4C,0x53,0x69,0x67,0x6E,0x61,0x74,0x75,0x72,
549 0x65,0x3A,0x34,0x31,0x31,0x65,0x33,0x38,0x61,0x61,0x61,0x31,0x37,0x65,0x33,0x30,
550 0x66,0x30,0x32,0x38,0x62,0x61,0x30,0x31,0x36,0x32,0x36,0x37,0x66,0x66,0x30,0x31,
551 0x36,0x36,0x61,0x65,0x35,0x39,0x65,0x38,0x31,0x39,0x62,0x61,0x32,0x34,0x63,0x39,
552 0x62,0x31,0x33,0x37,0x33,0x62,0x31,0x61,0x35,0x61,0x38,0x65,0x64,0x63,0x36,0x30,
553 0x65,0x37,0xFF,0xE2,0x02,0x2C,0x49,0x43,0x43,0x5F,0x50,0x52,0x4F,0x46,0x49,0x4C,
554 0x45,0x00,0x01,0x01,0x00,0x00,0x02,0x1C,0x41,0x44,0x42,0x45,0x02,0x10,0x00,0x00,
555 0x6D,0x6E,0x74,0x72,0x52,0x47,0x42,0x20,0x58,0x59,0x5A,0x20,0x07,0xCF,0x00,0x05,
556 0x00,0x09,0x00,0x15,0x00,0x0B,0x00,0x21,0x61,0x63,0x73,0x70,0x41,0x50,0x50,0x4C,
557 0x00,0x00,0x00,0x00,0x6E,0x6F,0x6E,0x65,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
559 size_t buffer_len =
sizeof(buffer);
563 ctx = MagicInitContext();
566 result = MagicThreadLookup(&ctx, buffer, buffer_len);
567 if (result == NULL || strncmp(result,
"JPEG", 4) != 0) {
568 printf(
"result %p:%s, not \"JPEG\": ", result,result?result:
"(null)");
574 MagicDeinitContext(ctx);
598 MagicDetectTest10ValgrindError);