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.39. 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
40 order.
41
42 long A four-byte value in this machine's native byte
43 order.
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
127 local 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
131 local 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,
154 interpreted as a UNIX date.
155
156 leqdate An eight-byte value in little-endian byte order,
157 interpreted as a UNIX date.
158
159 leldate A four-byte value in little-endian byte order,
160 interpreted 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,
164 interpreted 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,
168 interpreted 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
174 order.
175
176 medate A four-byte value in middle-endian (PDP-11) byte
177 order, interpreted as a UNIX date.
178
179 meldate A four-byte value in middle-endian (PDP-11) byte
180 order, 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
199 begins 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
299 numeric value before any comparisons are done. Prepending a u
300 to the type indicates that ordered comparisons should be
301 unsigned.
302
303 test The value to be compared with the value from the file. If the
304 type is numeric, this value is specified in C form; if it is a
305 string, it is specified as a C string with the usual escapes
306 permitted (e.g. \n for new-line).
307
308 Numeric values may be preceded by a character indicating the
309 operation to be performed. It may be =, to specify that the
310 value from the file must equal the specified value, <, to spec‐
311 ify that the value from the file must be less than the specified
312 value, >, to specify that the value from the file must be
313 greater than the specified value, &, to specify that the value
314 from the file must have set all of the bits that are set in the
315 specified value, ^, to specify that the value from the file must
316 have clear any of the bits that are set in the specified value,
317 or ~, the value specified after is negated before tested. x, to
318 specify that any value will match. If the character is omitted,
319 it is assumed to be =. Operators &, ^, and ~ don't work with
320 floats and doubles. The operator ! specifies that the line
321 matches if the test does not succeed.
322
323 Numeric values are specified in C form; e.g. 13 is decimal, 013
324 is octal, and 0x13 is hexadecimal.
325
326 Numeric operations are not performed on date types, instead the
327 numeric value is interpreted as an offset.
328
329 For string values, the string from the file must match the spec‐
330 ified string. The operators =, < and > (but not &) can be
331 applied to strings. The length used for matching is that of the
332 string argument in the magic file. This means that a line can
333 match any non-empty string (usually used to then print the
334 string), with >\0 (because all non-empty strings are greater
335 than the empty string).
336
337 Dates are treated as numerical values in the respective internal
338 representation.
339
340 The special test x always evaluates to true.
341
342 message The message to be printed if the comparison succeeds. If the
343 string contains a printf(3) format specification, the value from
344 the file (with any specified masking performed) is printed using
345 the message as the format string. If the string begins with
346 “\b”, the message printed is the remainder of the string with no
347 whitespace added before it: multiple matches are normally sepa‐
348 rated by a single space.
349
350 An APPLE 4+4 character APPLE creator and type can be specified as:
351
352 !:apple CREATYPE
353
354 A MIME type is given on a separate line, which must be the next non-blank
355 or comment line after the magic line that identifies the file type, and
356 has the following format:
357
358 !:mime MIMETYPE
359
360 i.e. the literal string “!:mime” followed by the MIME type.
361
362 An optional strength can be supplied on a separate line which refers to
363 the current magic description using the following format:
364
365 !:strength OP VALUE
366
367 The operand OP can be: +, -, *, or / and VALUE is a constant between 0
368 and 255. This constant is applied using the specified operand to the
369 currently computed default magic strength.
370
371 Some file formats contain additional information which is to be printed
372 along with the file type or need additional tests to determine the true
373 file type. These additional tests are introduced by one or more > char‐
374 acters preceding the offset. The number of > on the line indicates the
375 level of the test; a line with no > at the beginning is considered to be
376 at level 0. Tests are arranged in a tree-like hierarchy: if the test on
377 a line at level n succeeds, all following tests at level n+1 are per‐
378 formed, and the messages printed if the tests succeed, until a line with
379 level n (or less) appears. For more complex files, one can use empty
380 messages to get just the "if/then" effect, in the following way:
381
382 0 string MZ
383 >0x18 leshort <0x40 MS-DOS executable
384 >0x18 leshort >0x3f extended PC executable (e.g., MS Windows)
385
386 Offsets do not need to be constant, but can also be read from the file
387 being examined. If the first character following the last > is a ( then
388 the string after the parenthesis is interpreted as an indirect offset.
389 That means that the number after the parenthesis is used as an offset in
390 the file. The value at that offset is read, and is used again as an off‐
391 set in the file. Indirect offsets are of the form: (( x
392 [[.,][bBcCeEfFgGhHiIlmsSqQ]][+-][ y ]). The value of x is used as an
393 offset in the file. A byte, id3 length, short or long is read at that
394 offset depending on the [bBcCeEfFgGhHiIlmsSqQ] type specifier. The value
395 is treated as signed if “”, is specified or unsigned if “”. is speci‐
396 fied. The capitalized types interpret the number as a big endian value,
397 whereas the small letter versions interpret the number as a little endian
398 value; the m type interprets the number as a middle endian (PDP-11)
399 value. To that number the value of y is added and the result is used as
400 an offset in the file. The default type if one is not specified is long.
401 The following types are recognized:
402
403 Type Sy Mnemonic Sy Endian Sy Size
404 bcBc Byte/Char N/A 1
405 efg Double Little 8
406 EFG Double Big 8
407 hs Half/Short Little 2
408 HS Half/Short Big 2
409 i ID3 Little 4
410 I ID3 Big 4
411 m Middle Middle 4
412 q Quad Little 8
413 Q Quad Big 8
414
415 That way variable length structures can be examined:
416
417 # MS Windows executables are also valid MS-DOS executables
418 0 string MZ
419 >0x18 leshort <0x40 MZ executable (MS-DOS)
420 # skip the whole block below if it is not an extended executable
421 >0x18 leshort >0x3f
422 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
423 >>(0x3c.l) string LX\0\0 LX executable (OS/2)
424
425 This strategy of examining has a drawback: you must make sure that you
426 eventually print something, or users may get empty output (such as when
427 there is neither PE\0\0 nor LE\0\0 in the above example).
428
429 If this indirect offset cannot be used directly, simple calculations are
430 possible: appending [+-*/%&|^]number inside parentheses allows one to
431 modify the value read from the file before it is used as an offset:
432
433 # MS Windows executables are also valid MS-DOS executables
434 0 string MZ
435 # sometimes, the value at 0x18 is less that 0x40 but there's still an
436 # extended executable, simply appended to the file
437 >0x18 leshort <0x40
438 >>(4.s*512) leshort 0x014c COFF executable (MS-DOS, DJGPP)
439 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
440
441 Sometimes you do not know the exact offset as this depends on the length
442 or position (when indirection was used before) of preceding fields. You
443 can specify an offset relative to the end of the last up-level field
444 using ‘&’ as a prefix to the offset:
445
446 0 string MZ
447 >0x18 leshort >0x3f
448 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
449 # immediately following the PE signature is the CPU type
450 >>>&0 leshort 0x14c for Intel 80386
451 >>>&0 leshort 0x184 for DEC Alpha
452
453 Indirect and relative offsets can be combined:
454
455 0 string MZ
456 >0x18 leshort <0x40
457 >>(4.s*512) leshort !0x014c MZ executable (MS-DOS)
458 # if it's not COFF, go back 512 bytes and add the offset taken
459 # from byte 2/3, which is yet another way of finding the start
460 # of the extended executable
461 >>>&(2.s-514) string LE LE executable (MS Windows VxD driver)
462
463 Or the other way around:
464
465 0 string MZ
466 >0x18 leshort >0x3f
467 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
468 # at offset 0x80 (-4, since relative offsets start at the end
469 # of the up-level match) inside the LE header, we find the absolute
470 # offset to the code area, where we look for a specific signature
471 >>>(&0x7c.l+0x26) string UPX \b, UPX compressed
472
473 Or even both!
474
475 0 string MZ
476 >0x18 leshort >0x3f
477 >>(0x3c.l) string LE\0\0 LE executable (MS-Windows)
478 # at offset 0x58 inside the LE header, we find the relative offset
479 # to a data area where we look for a specific signature
480 >>>&(&0x54.l-3) string UNACE \b, ACE self-extracting archive
481
482 If you have to deal with offset/length pairs in your file, even the sec‐
483 ond value in a parenthesized expression can be taken from the file
484 itself, using another set of parentheses. Note that this additional
485 indirect offset is always relative to the start of the main indirect off‐
486 set.
487
488 0 string MZ
489 >0x18 leshort >0x3f
490 >>(0x3c.l) string PE\0\0 PE executable (MS-Windows)
491 # search for the PE section called ".idata"...
492 >>>&0xf4 search/0x140 .idata
493 # ...and go to the end of it, calculated from start+length;
494 # these are located 14 and 10 bytes after the section name
495 >>>>(&0xe.l+(-4)) string PK\3\4 \b, ZIP self-extracting archive
496
497 If you have a list of known values at a particular continuation level,
498 and you want to provide a switch-like default case:
499
500 # clear that continuation level match
501 >18 clear
502 >18 lelong 1 one
503 >18 lelong 2 two
504 >18 default x
505 # print default match
506 >>18 lelong x unmatched 0x%x
507
509 file(1) - the command that reads this file.
510
512 The formats long, belong, lelong, melong, short, beshort, and leshort do
513 not depend on the length of the C data types short and long on the plat‐
514 form, even though the Single UNIX Specification implies that they do.
515 However, as OS X Mountain Lion has passed the Single UNIX Specification
516 validation suite, and supplies a version of file(1) in which they do not
517 depend on the sizes of the C data types and that is built for a 64-bit
518 environment in which long is 8 bytes rather than 4 bytes, presumably the
519 validation suite does not test whether, for example long refers to an
520 item with the same size as the C data type long. There should probably
521 be type names int8, uint8, int16, uint16, int32, uint32, int64, and
522 uint64, and specified-byte-order variants of them, to make it clearer
523 that those types have specified widths.
524
525BSD May 9, 2020 BSD