1MAGIC(5) BSD File Formats Manual MAGIC(5)
2
4 magic — file command's magic pattern file
5
7 This manual page documents the format of magic files as used by the
8 file(1) command, version 5.40. The file(1) command identifies the type
9 of a file using, among other tests, a test for whether the file contains
10 certain “magic patterns”. The database of these “magic patterns” is usu‐
11 ally located in a binary file in /usr/share/misc/magic.mgc or a directory
12 of source text magic pattern fragment files in /usr/share/misc/magic.
13 The database specifies what patterns are to be tested for, what message
14 or MIME type to print if a particular pattern is found, and additional
15 information to extract from the file.
16
17 The format of the source fragment files that are used to build this data‐
18 base is as follows: Each line of a fragment file specifies a test to be
19 performed. A test compares the data starting at a particular offset in
20 the file with a byte value, a string or a numeric value. If the test
21 succeeds, a message is printed. The line consists of the following
22 fields:
23
24 offset A number specifying the offset (in bytes) into the file of the
25 data which is to be tested. This offset can be a negative num‐
26 ber if it is:
27 • The first direct offset of the magic entry (at continuation
28 level 0), in which case it is interpreted an offset from end
29 end of the file going backwards. This works only when a
30 file descriptor to the file is available and it is a regular
31 file.
32 • A continuation offset relative to the end of the last up-
33 level field (&).
34
35 type The type of the data to be tested. The possible values are:
36
37 byte A one-byte value.
38
39 short A two-byte value in this machine's native byte or‐
40 der.
41
42 long A four-byte value in this machine's native byte or‐
43 der.
44
45 quad An eight-byte value in this machine's native byte
46 order.
47
48 float A 32-bit single precision IEEE floating point number
49 in this machine's native byte order.
50
51 double A 64-bit double precision IEEE floating point number
52 in this machine's native byte order.
53
54 string A string of bytes. The string type specification
55 can be optionally followed by /[WwcCtbT]*. The “W”
56 flag compacts whitespace in the target, which must
57 contain at least one whitespace character. If the
58 magic has n consecutive blanks, the target needs at
59 least n consecutive blanks to match. The “w” flag
60 treats every blank in the magic as an optional
61 blank. The “c” flag specifies case insensitive
62 matching: lower case characters in the magic match
63 both lower and upper case characters in the target,
64 whereas upper case characters in the magic only
65 match upper case characters in the target. The “C”
66 flag specifies case insensitive matching: upper case
67 characters in the magic match both lower and upper
68 case characters in the target, whereas lower case
69 characters in the magic only match upper case char‐
70 acters in the target. To do a complete case insen‐
71 sitive match, specify both “c” and “C”. The “t”
72 flag forces the test to be done for text files,
73 while the “b” flag forces the test to be done for
74 binary files. The “T” flag causes the string to be
75 trimmed, i.e. leading and trailing whitespace is
76 deleted before the string is printed.
77
78 pstring A Pascal-style string where the first byte/short/int
79 is interpreted as the unsigned length. The length
80 defaults to byte and can be specified as a modifier.
81 The following modifiers are supported:
82 B A byte length (default).
83 H A 2 byte big endian length.
84 h A 2 byte little endian length.
85 L A 4 byte big endian length.
86 l A 4 byte little endian length.
87 J The length includes itself in its count.
88 The string is not NUL terminated. “J” is used
89 rather than the more valuable “I” because this type
90 of length is a feature of the JPEG format.
91
92 date A four-byte value interpreted as a UNIX date.
93
94 qdate An eight-byte value interpreted as a UNIX date.
95
96 ldate A four-byte value interpreted as a UNIX-style date,
97 but interpreted as local time rather than UTC.
98
99 qldate An eight-byte value interpreted as a UNIX-style
100 date, but interpreted as local time rather than UTC.
101
102 qwdate An eight-byte value interpreted as a Windows-style
103 date.
104
105 beid3 A 32-bit ID3 length in big-endian byte order.
106
107 beshort A two-byte value in big-endian byte order.
108
109 belong A four-byte value in big-endian byte order.
110
111 bequad An eight-byte value in big-endian byte order.
112
113 befloat A 32-bit single precision IEEE floating point number
114 in big-endian byte order.
115
116 bedouble A 64-bit double precision IEEE floating point number
117 in big-endian byte order.
118
119 bedate A four-byte value in big-endian byte order, inter‐
120 preted as a Unix date.
121
122 beqdate An eight-byte value in big-endian byte order, inter‐
123 preted as a Unix date.
124
125 beldate A four-byte value in big-endian byte order, inter‐
126 preted as a UNIX-style date, but interpreted as lo‐
127 cal time rather than UTC.
128
129 beqldate An eight-byte value in big-endian byte order, inter‐
130 preted as a UNIX-style date, but interpreted as lo‐
131 cal time rather than UTC.
132
133 beqwdate An eight-byte value in big-endian byte order, inter‐
134 preted as a Windows-style date.
135
136 bestring16 A two-byte unicode (UCS16) string in big-endian byte
137 order.
138
139 leid3 A 32-bit ID3 length in little-endian byte order.
140
141 leshort A two-byte value in little-endian byte order.
142
143 lelong A four-byte value in little-endian byte order.
144
145 lequad An eight-byte value in little-endian byte order.
146
147 lefloat A 32-bit single precision IEEE floating point number
148 in little-endian byte order.
149
150 ledouble A 64-bit double precision IEEE floating point number
151 in little-endian byte order.
152
153 ledate A four-byte value in little-endian byte order, in‐
154 terpreted as a UNIX date.
155
156 leqdate An eight-byte value in little-endian byte order, in‐
157 terpreted as a UNIX date.
158
159 leldate A four-byte value in little-endian byte order, in‐
160 terpreted as a UNIX-style date, but interpreted as
161 local time rather than UTC.
162
163 leqldate An eight-byte value in little-endian byte order, in‐
164 terpreted as a UNIX-style date, but interpreted as
165 local time rather than UTC.
166
167 leqwdate An eight-byte value in little-endian byte order, in‐
168 terpreted as a Windows-style date.
169
170 lestring16 A two-byte unicode (UCS16) string in little-endian
171 byte order.
172
173 melong A four-byte value in middle-endian (PDP-11) byte or‐
174 der.
175
176 medate A four-byte value in middle-endian (PDP-11) byte or‐
177 der, interpreted as a UNIX date.
178
179 meldate A four-byte value in middle-endian (PDP-11) byte or‐
180 der, interpreted as a UNIX-style date, but inter‐
181 preted as local time rather than UTC.
182
183 indirect Starting at the given offset, consult the magic
184 database again. The offset of the indirect magic is
185 by default absolute in the file, but one can specify
186 /r to indicate that the offset is relative from the
187 beginning of the entry.
188
189 name Define a “named” magic instance that can be called
190 from another use magic entry, like a subroutine
191 call. Named instance direct magic offsets are rela‐
192 tive to the offset of the previous matched entry,
193 but indirect offsets are relative to the beginning
194 of the file as usual. Named magic entries always
195 match.
196
197 use Recursively call the named magic starting from the
198 current offset. If the name of the referenced be‐
199 gins with a ^ then the endianness of the magic is
200 switched; if the magic mentioned leshort for exam‐
201 ple, it is treated as beshort and vice versa. This
202 is useful to avoid duplicating the rules for differ‐
203 ent endianness.
204
205 regex A regular expression match in extended POSIX regular
206 expression syntax (like egrep). Regular expressions
207 can take exponential time to process, and their per‐
208 formance is hard to predict, so their use is dis‐
209 couraged. When used in production environments,
210 their performance should be carefully checked. The
211 size of the string to search should also be limited
212 by specifying /<length>, to avoid performance issues
213 scanning long files. The type specification can
214 also be optionally followed by /[c][s][l]. The “c”
215 flag makes the match case insensitive, while the “s”
216 flag update the offset to the start offset of the
217 match, rather than the end. The “l” modifier,
218 changes the limit of length to mean number of lines
219 instead of a byte count. Lines are delimited by the
220 platforms native line delimiter. When a line count
221 is specified, an implicit byte count also computed
222 assuming each line is 80 characters long. If nei‐
223 ther a byte or line count is specified, the search
224 is limited automatically to 8KiB. ^ and $ match the
225 beginning and end of individual lines, respectively,
226 not beginning and end of file.
227
228 search A literal string search starting at the given off‐
229 set. The same modifier flags can be used as for
230 string patterns. The search expression must contain
231 the range in the form /number, that is the number of
232 positions at which the match will be attempted,
233 starting from the start offset. This is suitable
234 for searching larger binary expressions with vari‐
235 able offsets, using \ escapes for special charac‐
236 ters. The order of modifier and number is not rele‐
237 vant.
238
239 default This is intended to be used with the test x (which
240 is always true) and it has no type. It matches when
241 no other test at that continuation level has matched
242 before. Clearing that matched tests for a continua‐
243 tion level, can be done using the clear test.
244
245 clear This test is always true and clears the match flag
246 for that continuation level. It is intended to be
247 used with the default test.
248
249 der Parse the file as a DER Certificate file. The test
250 field is used as a der type that needs to be
251 matched. The DER types are: eoc, bool, int,
252 bit_str, octet_str, null, obj_id, obj_desc, ext,
253 real, enum, embed, utf8_str, rel_oid, time, res2,
254 seq, set, num_str, prt_str, t61_str, vid_str,
255 ia5_str, utc_time, gen_time, gr_str, vis_str,
256 gen_str, univ_str, char_str, bmp_str, date, tod,
257 datetime, duration, oid-iri, rel-oid-iri. These
258 types can be followed by an optional numeric size,
259 which indicates the field width in bytes.
260
261 guid A Globally Unique Identifier, parsed and printed as
262 XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. It's format
263 is a string.
264
265 offset This is a quad value indicating the current offset
266 of the file. It can be used to determine the size
267 of the file or the magic buffer. For example the
268 magic entries:
269
270 -0 offset x this file is %lld bytes
271 -0 offset <=100 must be more than 100 \
272 bytes and is only %lld
273
274 For compatibility with the Single UNIX Standard, the type speci‐
275 fiers dC and d1 are equivalent to byte, the type specifiers uC
276 and u1 are equivalent to ubyte, the type specifiers dS and d2
277 are equivalent to short, the type specifiers uS and u2 are
278 equivalent to ushort, the type specifiers dI, dL, and d4 are
279 equivalent to long, the type specifiers uI, uL, and u4 are
280 equivalent to ulong, the type specifier d8 is equivalent to
281 quad, the type specifier u8 is equivalent to uquad, and the type
282 specifier s is equivalent to string. In addition, the type
283 specifier dQ is equivalent to quad and the type specifier uQ is
284 equivalent to uquad.
285
286 Each top-level magic pattern (see below for an explanation of
287 levels) is classified as text or binary according to the types
288 used. Types “regex” and “search” are classified as text tests,
289 unless non-printable characters are used in the pattern. All
290 other tests are classified as binary. A top-level pattern is
291 considered to be a test text when all its patterns are text pat‐
292 terns; otherwise, it is considered to be a binary pattern. When
293 matching a file, binary patterns are tried first; if no match is
294 found, and the file looks like text, then its encoding is deter‐
295 mined and the text patterns are tried.
296
297 The numeric types may optionally be followed by & and a numeric
298 value, to specify that the value is to be AND'ed with the nu‐
299 meric value before any comparisons are done. Prepending a u to
300 the type indicates that ordered comparisons should be unsigned.
301
302 test The value to be compared with the value from the file. If the
303 type is numeric, this value is specified in C form; if it is a
304 string, it is specified as a C string with the usual escapes
305 permitted (e.g. \n for new-line).
306
307 Numeric values may be preceded by a character indicating the op‐
308 eration to be performed. It may be =, to specify that the value
309 from the file must equal the specified value, <, to specify that
310 the value from the file must be less than the specified value,
311 >, to specify that the value from the file must be greater than
312 the specified value, &, to specify that the value from the file
313 must have set all of the bits that are set in the specified
314 value, ^, to specify that the value from the file must have
315 clear any of the bits that are set in the specified value, or ~,
316 the value specified after is negated before tested. x, to spec‐
317 ify that any value will match. If the character is omitted, it
318 is assumed to be =. Operators &, ^, and ~ don't work with
319 floats and doubles. The operator ! specifies that the line
320 matches if the test does not succeed.
321
322 Numeric values are specified in C form; e.g. 13 is decimal, 013
323 is octal, and 0x13 is hexadecimal.
324
325 Numeric operations are not performed on date types, instead the
326 numeric value is interpreted as an offset.
327
328 For string values, the string from the file must match the spec‐
329 ified string. The operators =, < and > (but not &) can be ap‐
330 plied to strings. The length used for matching is that of the
331 string argument in the magic file. This means that a line can
332 match any non-empty string (usually used to then print the
333 string), with >\0 (because all non-empty strings are greater
334 than the empty string).
335
336 Dates are treated as numerical values in the respective internal
337 representation.
338
339 The special test x always evaluates to true.
340
341 message The message to be printed if the comparison succeeds. If the
342 string contains a printf(3) format specification, the value from
343 the file (with any specified masking performed) is printed using
344 the message as the format string. If the string begins with
345 “\b”, the message printed is the remainder of the string with no
346 whitespace added before it: multiple matches are normally sepa‐
347 rated by a single space.
348
349 An APPLE 4+4 character APPLE creator and type can be specified as:
350
351 !:apple CREATYPE
352
353 A MIME type is given on a separate line, which must be the next non-blank
354 or comment line after the magic line that identifies the file type, and
355 has the following format:
356
357 !:mime MIMETYPE
358
359 i.e. the literal string “!:mime” followed by the MIME type.
360
361 An optional strength can be supplied on a separate line which refers to
362 the current magic description using the following format:
363
364 !:strength OP VALUE
365
366 The operand OP can be: +, -, *, or / and VALUE is a constant between 0
367 and 255. This constant is applied using the specified operand to the
368 currently computed default magic strength.
369
370 Some file formats contain additional information which is to be printed
371 along with the file type or need additional tests to determine the true
372 file type. These additional tests are introduced by one or more > char‐
373 acters preceding the offset. The number of > on the line indicates the
374 level of the test; a line with no > at the beginning is considered to be
375 at level 0. Tests are arranged in a tree-like hierarchy: if the test on
376 a line at level n succeeds, all following tests at level n+1 are per‐
377 formed, and the messages printed if the tests succeed, until a line with
378 level n (or less) appears. For more complex files, one can use empty
379 messages to get just the "if/then" effect, in the following way:
380
381 0 string MZ
382 >0x18 leshort <0x40 MS-DOS executable
383 >0x18 leshort >0x3f extended PC executable (e.g., MS Windows)
384
385 Offsets do not need to be constant, but can also be read from the file
386 being examined. If the first character following the last > is a ( then
387 the string after the parenthesis is interpreted as an indirect offset.
388 That means that the number after the parenthesis is used as an offset in
389 the file. The value at that offset is read, and is used again as an off‐
390 set in the file. Indirect offsets are of the form: (( x
391 [[.,][bBcCeEfFgGhHiIlmsSqQ]][+-][ y ]). The value of x is used as an
392 offset in the file. A byte, id3 length, short or long is read at that
393 offset depending on the [bBcCeEfFgGhHiIlmsSqQ] type specifier. The value
394 is treated as signed if “”, is specified or unsigned if “”. is speci‐
395 fied. The capitalized types interpret the number as a big endian value,
396 whereas the small letter versions interpret the number as a little endian
397 value; the m type interprets the number as a middle endian (PDP-11)
398 value. To that number the value of y is added and the result is used as
399 an offset in the file. The default type if one is not specified is long.
400 The following types are recognized:
401
402 Type Sy Mnemonic Sy Endian Sy Size
403 bcBc Byte/Char N/A 1
404 efg Double Little 8
405 EFG Double Big 8
406 hs Half/Short Little 2
407 HS Half/Short Big 2
408 i ID3 Little 4
409 I ID3 Big 4
410 m Middle Middle 4
411 q Quad Little 8
412 Q Quad Big 8
413
414 That way variable length structures can be examined:
415
416 # MS Windows executables are also valid MS-DOS executables
417 0 string MZ
418 >0x18 leshort <0x40 MZ executable (MS-DOS)
419 # skip the whole block below if it is not an extended executable
420 >0x18 leshort >0x3f
421 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
422 >>(0x3c.l) string LX\0\0 LX executable (OS/2)
423
424 This strategy of examining has a drawback: you must make sure that you
425 eventually print something, or users may get empty output (such as when
426 there is neither PE\0\0 nor LE\0\0 in the above example).
427
428 If this indirect offset cannot be used directly, simple calculations are
429 possible: appending [+-*/%&|^]number inside parentheses allows one to
430 modify the value read from the file before it is used as an offset:
431
432 # MS Windows executables are also valid MS-DOS executables
433 0 string MZ
434 # sometimes, the value at 0x18 is less that 0x40 but there's still an
435 # extended executable, simply appended to the file
436 >0x18 leshort <0x40
437 >>(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
438 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
439
440 Sometimes you do not know the exact offset as this depends on the length
441 or position (when indirection was used before) of preceding fields. You
442 can specify an offset relative to the end of the last up-level field us‐
443 ing ‘&’ as a prefix to the offset:
444
445 0 string MZ
446 >0x18 leshort >0x3f
447 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
448 # immediately following the PE signature is the CPU type
449 >>>&0 leshort 0x14c for Intel 80386
450 >>>&0 leshort 0x184 for DEC Alpha
451
452 Indirect and relative offsets can be combined:
453
454 0 string MZ
455 >0x18 leshort <0x40
456 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
457 # if it's not COFF, go back 512 bytes and add the offset taken
458 # from byte 2/3, which is yet another way of finding the start
459 # of the extended executable
460 >>>&(2.s-514) string LE LE executable (MS Windows VxD driver)
461
462 Or the other way around:
463
464 0 string MZ
465 >0x18 leshort >0x3f
466 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
467 # at offset 0x80 (-4, since relative offsets start at the end
468 # of the up-level match) inside the LE header, we find the absolute
469 # offset to the code area, where we look for a specific signature
470 >>>(&0x7c.l+0x26) string UPX \b, UPX compressed
471
472 Or even both!
473
474 0 string MZ
475 >0x18 leshort >0x3f
476 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
477 # at offset 0x58 inside the LE header, we find the relative offset
478 # to a data area where we look for a specific signature
479 >>>&(&0x54.l-3) string UNACE \b, ACE self-extracting archive
480
481 If you have to deal with offset/length pairs in your file, even the sec‐
482 ond value in a parenthesized expression can be taken from the file it‐
483 self, using another set of parentheses. Note that this additional indi‐
484 rect offset is always relative to the start of the main indirect offset.
485
486 0 string MZ
487 >0x18 leshort >0x3f
488 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
489 # search for the PE section called ".idata"...
490 >>>&0xf4 search/0x140 .idata
491 # ...and go to the end of it, calculated from start+length;
492 # these are located 14 and 10 bytes after the section name
493 >>>>(&0xe.l+(-4)) string PK\3\4 \b, ZIP self-extracting archive
494
495 If you have a list of known values at a particular continuation level,
496 and you want to provide a switch-like default case:
497
498 # clear that continuation level match
499 >18 clear
500 >18 lelong 1 one
501 >18 lelong 2 two
502 >18 default x
503 # print default match
504 >>18 lelong x unmatched 0x%x
505
507 file(1) - the command that reads this file.
508
510 The formats long, belong, lelong, melong, short, beshort, and leshort do
511 not depend on the length of the C data types short and long on the plat‐
512 form, even though the Single UNIX Specification implies that they do.
513 However, as OS X Mountain Lion has passed the Single UNIX Specification
514 validation suite, and supplies a version of file(1) in which they do not
515 depend on the sizes of the C data types and that is built for a 64-bit
516 environment in which long is 8 bytes rather than 4 bytes, presumably the
517 validation suite does not test whether, for example long refers to an
518 item with the same size as the C data type long. There should probably
519 be type names int8, uint8, int16, uint16, int32, uint32, int64, and
520 uint64, and specified-byte-order variants of them, to make it clearer
521 that those types have specified widths.
522
523BSD May 9, 2020 BSD